Use Docker to quickly create. Net Core2.0 Nginx Load Balancing nodes and dockernginx
This article is copyrighted by the blog site and the author Wu Shuang himself. For reposted and crawlers, please enter the original address www.cnblogs.com/tdws.
I. Self-Host Kestrel
1. Create the dotnet core2.0 webapi project ApiService in vs2017
2. Refer to official documents, https://docs.microsoft.com/en-us/aspnet/core/publishing/linuxproduction? Tabs = aspnetcore2x added in Startup
app.UseForwardedHeaders(new ForwardedHeadersOptions{ ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto});
Configure the running Url in Program. cs.
3. Publish the project file and upload it to the linux server through FTP. A new core2.0 webapi project is only several hundred kb after it is released
4. Switch the directory, dotnet ApiService. dll
5. The server port is opened successfully, but the current status is selfhost running on Kestrel.
2. A proxy is required.
ASP. the running environment of NET Core is the responsibility of the newly developed Kestrel Server. IIS is returned to the HTTP listener role. Microsoft has specially developed IIS Platform Handler for this demand, to handle information forwarding between HTTP and the running environment, Microsoft officially recommends using Nginx, Haproxy, and other agents on Linux servers as Kestrel servers.
The most important thing to understand dotnet core host is that it runs independently. It does not run in IIS or IIS. It has an independent self-host Web Server and uses the self-host server internally to process requests.
However, you can still put IIS in front of the self-host server as a front-end proxy, because Kestrel is a web server with only the original functions, it does not provide the complete web server functions like iis. For example, Kestrel does not support the binding of port 80 to multiple applications on a single ip address. IIS can also provide static file services, gzip compression, for other advanced functions such as static file caching, IIS has a very high request validity rate, so it is necessary to use this to enable iis to process the tasks that it really specializes in, and pass the dynamic task to the core application. Therefore, on windows, iis continues to play a very important role.
In the traditional classic asp.netapplication, all content is included in the iiswork process (w3wp.exe). This is what we often call the application pool. In addition, the application is loaded and instantiated by the built-in hosting function of IIS, And the aspnet_isapi.dll is loaded by the worker process. the. Net Runtime is loaded using aspnet_isapi. The application pool in the IIS worker process loads the application domain. After a series of tasks, the ISAPIRuntime object calls the PR method, encapsulates the HttpWorkerRequest object, passes it to HttpRuntime to create an HttpApplication instance, and then executes a series of HttpApplication initialization and pipeline events. Of course, when loading and running, application domains and so on are only the first thing to do after the arrival of the request.
In dotnet core, the core does not run in the iis worker process, but in its own Kestrel component. Execute an external application through a native IIS module called AspNetCoreModule. Kestrel is a dotnet web server that has greatly optimized throughput performance. It quickly delivers network requests to your application, but it is just a raw web server, there is no comprehensive Web management service like IIS.
Although the IIS Site still needs an application pool, it should be set to unmanaged code. Because the application pool serves only as the proxy for forwarding requests, it does not need to be instantiated during. net runtime. So in linux, we also need a front-end proxy of self-host. For more information, see nginx.
Iii. Use nginx as a proxy
Find/etc/nginx to configure nginx. conf
server { listen 80; location / { proxy_pass http://localhost:5000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection keep-alive; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; }}
I changed the nginx user to root 5000 to my own 10000.
Create service file
Nano/etc/systemd/system/apiservice. service
Service file content, official example:
1 [Unit] 2 Description = Example. NET Web API Application running on Ubuntu 3 4 [Service] 5 WorkingDirectory =/var/aspnetcore/hellomvc 6 ExecStart =/usr/bin/dotnet/var/aspnetcore/hellomvc. dll 7 Restart = always 8 RestartSec = 10 # Restart service after 10 seconds if dotnet service crashes 9 SyslogIdentifier = dotnet-example10 User = www-data11 Environment = ASPNETCORE_ENVIRONMENT = Production 12 13 [Install] 14 then getView Code
The User is changed to root. The working directory is the directory uploaded by my project file ftp. do not modify the dll Directory in ExecStart dotnet and change it to the directory of the target dll to be executed.
Then enable service
Execute systemctl enable kestrel-hellomvc.service
Start and verify the service status
Systemctl start kestrel-hellomvc.service
Systemctl status kestrel-hellomvc.service
The access to port 80 in the listener proves that the service is successful.
4. Load Balancing
Deploy another 10001 in the same way, modify nginx, and configure Server Load balancer.
The access proves that the configuration is successful.
5. Create a Docker Image
The official dotnet core image is microsoft/dotnet. The basic docker commands are not mentioned. At the beginning, you can learn and remember them. The following code creates an image based on microsoft/dotnet image. To quickly run multiple docker images and configure more Server Load balancer instances, you do not need to manually copy them to each server and configure the environment. That is to say, no matter how many or even hundreds of images are created, if we have our own docker hub, it will be quickly created, and it will not be available on this server. Other problems may occur on another server.
The following is just an example of yourself in the learning process. It is still far from the best practice method. I hope it will be helpful to anyone who can read the article.
Because nginx is also placed in front of each image's apiService, core application runs on Kestrel in the form of self-host in each container. Proxy the port number exposed by docker through nginx at the front end.
Create a Dockerfile under the released website directory.
Save and run docker to create an image using the Dockerfile in the current directory. Docker build-t image/apiservice-v3. Note that there is a. (use the current directory)
View images using docker images
We can find that the newly created docker image is larger than the size of microsoft/dotnet FROM.
Run the following command to check whether the four images we just created have been run.
Docker run-d-p: 81: 20000 image/apiservice-v3
Docker ps-a: view the running image Process
The following describes how to configure nginx load balancing and then service nginx reload. The experiment is complete.
The following uses docker kill to stop docker container one by one and then access it to confirm that the Server Load balancer is successful. When all four containers are stopped, nginx returns 502 error.
References
Https://weblog.west-wind.com/posts/2016/Jun/06/Publishing-and-Running-ASPNET-Core-Applications-with-IIS
Http://www.cnblogs.com/shanyou/p/Jexus_Kestrel.html
Https://docs.microsoft.com/en-us/aspnet/core/publishing/linuxproduction? Tabs = aspnetcore2x
If you believe that reading this blog has some benefits, click "add 【Recommendation] Button.
If you want to discover my new blog more easily, click follow below.
My passion for sharing is inseparable from your support.
Thank you for reading this article. I will continue to share it with you. I am a snail bait, keep learning, and remember to be modest. Improper installation, fun and dream.