In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use Docker to quickly create. Net Core2.0 Nginx load balancing node, the article introduces in great detail, has a certain reference value, interested friends must read it!
I. Self-Host Kestrel
1. Create a new dotnet core2.0 webapi project ApiService in vs2017
two。 Referring to the official documentation, https://docs.microsoft.com/en-us/aspnet/core/publishing/linuxproduction?tabs=aspnetcore2x is added to Startup.
App.UseForwardedHeaders (new ForwardedHeadersOptions {ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto})
Configure to run Url, in Program.cs
3. Publish the project file and upload it to the linux server via FTP. There are only a few hundred kb after a new core2.0 webapi project is released.
4. Switch directories, dotnet ApiService.dll
5. Running successfully, the server port is open, but it is currently running in the selfhost state of Kestrel.
two。 Need an agent
The newly developed Kestrel Server is responsible for the running environment of ASP.NET Core, and IIS returns to the role of HTTP listener. Microsoft has also developed IIS Platform Handler for this requirement to handle the information forwarding between HTTP and the running environment. Microsoft officially recommends using proxy Kestrel Server such as Nginx,Haproxy on Linux servers.
The most important thing about understanding dotnet core host is that it runs independently. Does not run in IIS, nor does it require IIS to run. It has a separate self-hosting Web Server and internally uses self-host server to process requests.
However, you can still put IIS in front of self-host server as a front-end proxy, because Kestrel is a web server with only original functions, and it does not have complete web server functions like iis. For example, Kestrel does not support multiple application binding ports 80 on a single ip. IIS can also provide other advanced functions such as static file services, gzip compression, static file caching and so on. IIS is very efficient in processing requests. So it's necessary to take advantage of this by letting iis handle the tasks it's really good at and passing dynamic tasks to the core application. So iis continues to play a very important role on windows.
In traditional classic Asp.Net applications, everything is hosted in the iis working process (w3wp.exe), which is often referred to as application pool. And the application is instantiated by the built-in hosting function of IIS, the aspnet_isapi.dll is loaded through the worker process, and the .net runtime is loaded with aspnet_isapi. The application pool in the IIS worker process loads the application domain. After a series of work, the ISAPIRuntime object calls the PR method, encapsulates the HttpWorkerRequest object, passes it to HttpRuntime to create a HttpApplication instance, and then a series of HttpApplication initialization and pipeline events are executed. Of course, when loading the runtime, the application domain and so on are all just done after the arrival of * requests.
What is very different in dotnet core is that core does not run in the iis worker process, but in its own Kestrel component. External applications are executed through a native IIS module called AspNetCoreModule. Kestrel is an implementation of dotnet web server that does a lot of optimization for throughput performance, it quickly passes network requests to your application, but it is only a raw web server, not as comprehensive Web management services as IIS.
Although the IIS site still requires an application pool, it should be set to unmanaged code, and since the application pool acts only as a proxy for forwarding requests, you do not need to instantiate the. net runtime So the same is true on linux, where we need a front-end agent for self-host, which uses nginx in the reference documentation here.
3. Nginx acts as an agent
Find / etc/nginx configuration 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 nginx's user to root 5000 to my own 10000.
Create service file
Nano / etc/systemd/system/apiservice.service
Content of service file, 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/hellomvc.dll 7 Restart=always 8 RestartSec=10 # Restart service after 10 seconds if dotnet service crashes 9 SyslogIdentifier=dotnet-example 10 User=www-data 11 Environment=ASPNETCORE_ENVIRONMENT=Production 12 13 [Install] 14 WantedBy=multi-user.target
Changed User to root. I also modified the working directory, which is the directory after the upload of my project file ftp. The dotnet directory of ExecStart does not modify the dll directory. Change it to the directory of the target dll to be executed.
And then enable service
Execute systemctl enable kestrel-hellomvc.service
Start and verify the status of service
Systemctl start kestrel-hellomvc.service
Systemctl status kestrel-hellomvc.service
Access port 80 in monitoring to prove that the service is successful.
four。 Do load balancing
In the same way, we deploy another 10001, modify the nginx, and configure the load balancer.
The visit proves that our configuration is successful.
five。 Create Docker Image
The official dotnet core mirror bit microsoft/dotnet. Docker basic commands will not be mentioned, at the beginning of the use is also to learn at the same time. Let's create your own image based on microsoft/dotnet image. In order to run multiple docker image quickly and configure more load balancers, there is no need to manually copy to each server and then configure the environment. That is to say, no matter we create dozens or even hundreds of docker hub, it will be very fast to create, and it will not cause any other problems on this server because it is available on this server.
The following is just an example of yourself in the process of learning, which is still a long way from practice. I hope it can be helpful to friends who read essays.
Since nginx is also placed in front of the apiService of each image, core application runs on Kestrel in the form of self-host in each container. The port number exposed by docker is proxied through nginx at the front end.
Create a Dockerfile in the directory of the published Web site.
After saving, perform a docker build to create an image using the Dockerfile of the current directory. Docker build-t image/apiservice-v3. Notice that there is one at the end. (use current directory)
Docker images View Mirror
We can find that the docker image we just created is a little larger than the microsoft/dotnet size of our FROM.
Let's take a look at the four lines of commands that run the four image we just created.
Docker run-d-p: 81 20000 image/apiservice-v3
Docker ps-a view the running image process
Next, configure nginx load balancer and then service nginx reload. The experiment is complete.
Use docker kill to stop the docker container one by one, and then access it to confirm the success of load balancing. When all four container stops, nginx returns 502 error.
The above is all the contents of the article "how to use Docker to quickly create. Net Core2.0 Nginx load balancing nodes". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.