In the first two:
.net Core deployed to Linux (CentOS) most complete solution, general chapter
.net Core Deployment for Linux (CentOS) Supervisor+Nginx
With a good understanding of how.net Core can be deployed to Linux, this article continues to take you through deploying.net Core applications using Doker+ Jexus or Nginx.
1. Use Jexus as the Web server
Jexus is a high-performance WEB server and load balancing gateway server on the Linux platform. It features ASP.NET, ASP.NET CORE, PHP, reverse proxy, intrusion detection and other important functions. It’s safe to say that Jexus is. NET,.net CORE is the best cross-platform hosting server, and we would be right to think of it as IIS for Linux, because Jexus is fast and has a high level of security that IIS and other Web servers don’t have. At the same time, Jexus Web Server is a domestic software completely independently developed by Chinese people, which is truly “safe, reliable and controllable” and has the key qualities required by the informatization construction of Party and government organs and important enterprises and public institutions in China.
Jexus official website: www.jexus.org/
1.1 installation of Jexus
Jexus installation is very simple, execute the following command:
curl https://jexus.org/release/x64/install.sh|sudo sh
Copy the code
Note: The operator must have root permission to run the installation command.
1.2 configuration and use of Jexus
After the installation is complete, switch to the /usr/jexus directory and change the default in the /usr/jexus/siteconf directory as follows:
Note: Because I used port 80 with nginx in the previous section, I use port 8099 here.
Port =8099 # host =* # domain AppHost={CMD =dotnet core50test.dll; root=/root/app_data/core50test/publish; port=5000}Copy the code
Next, we restart Jexus and execute the following command:
/usr/jexus/jws restart
Copy the code
When we say Ok, our application is up.
You can run the following command to view the result:
ps -aux
Copy the code
You can see that our application is up, go to the browser and use port 8099 to see the effect.
Please refer to the official documentation for more detailed instructions on how to use Jexus.
1.3 Jexus operation command
Jexus includes the following commands: start: sudo./ JWS start Stop: sudo./ JWS stop Restart: sudo
Docker+Nginx or Jexus deployment
In the introduction of the previous article, both Supervisor + Nginx and Jexus need to install the SDK or runtime of.NET Core on the server. If we have multiple applications, each of which uses different VERSIONS of.NET Core, The above approach requires us to install the corresponding SDK or runtime respectively, which is quite troublesome for maintenance. The emergence of Docker can perfectly solve the above problems.
2.1,DockerWhat is?
- Docker is an open source software deployment solution;
- Docker is also a lightweight application container framework;
- Docker can package, publish, and run any application.
Docker has the following advantages:
Deliver your applications quickly and consistently
Docker simplifies the development lifecycle by allowing developers to work in a standardized environment using native containers of your applications or services.
Containers are well suited for continuous integration and continuous delivery (CI/CD) workflows, consider the following example scenario:
- Your developers write code locally and use Docker containers to share their work with colleagues.
- They use Docker to push their applications into a test environment and perform automated or manual testing.
- When developers find bugs, they can fix them in the development environment and then redeploy them to the test environment for testing and verification.
- When the test is complete, the patch is pushed to production as easily as an updated image.
Responsive deployment and scaling
Docker is a container-based platform that allows for highly portable workloads. Docker containers can run on a developer’s machine, on a physical or virtual machine in a data center, on a cloud service, or in a hybrid environment.
Docker’s portability and lightweight nature also allows you to easily complete dynamically managed workloads and extend or dismantle applications and services in real time as indicated by business requirements.
3. Run more workloads on the same hardware
Docker is light and fast. It provides a viable, economical, and efficient alternative to hypervisor-based virtual machines, so you can leverage more computing power to achieve business goals. Docker is ideal for high-density environments and small to medium sized deployments, where you can do more with less.
2.2 Docker installation
Run the following command:
sudo yum install docker
Copy the code
PS:
Another app is currentlty holding the yum lock, waiting for it to exit..
Solution: rm -r /var/run/yum.pid Command is used to unlock the IP address
After the installation is complete, run the following command to set up docker startup:
systemctl enable docker
Copy the code
Run the following command to start docker:
systemctl start docker
Copy the code
Check docker startup status:
Ps: If there is a problem with the installation process, you may run the command installation directly.
yum install docker-ce-cli
Docker version command:
2.3..NET Core Docker support
After installing Docker, we need to add dockerFile to our code.
Visual Studio can help us generate it automatically. In the solution, right-click the project name and choose [Add] -> [Docker support], as shown below:
The generated dockerfile looks like this:
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["Core50Test.csproj".""]
RUN dotnet restore "./Core50Test.csproj"
COPY.
WORKDIR "/src/."
RUN dotnet build "Core50Test.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Core50Test.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet"."Core50Test.dll"]
Copy the code
Push the code to the Git repository, and then pull the latest code from the Linux server.
Switch to the /root/app_data/source/core50test directory and run the following command to pull the latest code:
git pull
Copy the code
Then execute the following command to package the code as an image:
docker build -f Dockerfile -t core50test .
Copy the code
The first time packaging an image involves pulling. The official image of NET Core. The pull speed may be slow. Please wait patiently.
After the package is complete, run the following command to view existing images on the server:
docker images
Copy the code
After the image is packaged, we can run the docker run command to enable our application:
docker run -p 81:80 -dit --restart=always --name core50test core50test
Copy the code
The result is as follows:
Visit http://ip:81 through your browser. If nothing else, we can see that the application is running successfully.
** Docker run: ** Creates a new container and runs a command
grammar
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Copy the code
The OPTIONS:
- -a stdin: specifies the standard input and output types. The options are stdin, STDOUT, or STDERR.
- -d: Runs the container in the background and returns the container ID.
- -i: Runs the container in interactive mode, usually with -t.
- -p: indicates random port mapping. Ports inside a container are randomly mapped to ports on a host
- -p: specifies the port mapping. The format is host (host) port: container port
- -t: reassigns a pseudo-input terminal to the container. It is usually used together with -i.
- –name=”nginx-lb”: specify a name for the container;
- — DNS 8.8.8.8: Specifies the DNS server used by the container. The default DNS server is the same as the host server.
- –dns-search example.com: specify the container DNS search domain name, which is the same as the host by default.
- -h “Mars “: specifies the hostname of the container.
- -e username=” Ritchie “: Set the environment variable.
- –env-file=[]: reads environment variables from specified files;
- –cpuset=”0-2″ or –cpuset=”0,1,2″: bind the container to the specified CPU;
- **-m :** Sets the maximum memory used by the container.
- – net = “bridge” : specify the container’s network connection type, support bridge/host/none/container: four types;
- –link=[]: Add link to another container;
- –expose=[]: Open a port or group of ports;
- –volume, -v: Binds a volume
2.4. Realize docker one-click update application through shell script
You can write shell scripts to update applications with one click. In the /root directory, create the dockerbuild.sh file as follows:
#! /bin/bash
cd /root/app_data/source/core50test
git pull
imtag=$(uuidgen |sed 's/-//g')
docker build -f Dockerfile -t core50test:${imtag} .
docker stop core50test
docker rm core50test
docker run -dit --restart=always --name core50test -p 81:80 core50test:${imtag}
Copy the code
Set execute permission to the dockerbuild.sh file as follows:
chmod 777 dockerbuild.sh
Copy the code
When new code is pushed to the Git repository, go to the server and execute dockerbuild.sh to quickly update the application.
./dockerbuild.sh
The introduction to.net Core deployment using Docker mode is complete.
2.5 Common docker commands
Docker common commands are generally divided into the following types:
- Docker Environment Information –
docker [info|version]
- Container Lifecycle Management –
docker [create|exec|run|start|stop|restart|kill|rm|pause|unpause]
- Container operation and maintenance —
docker [ps|inspect|top|attach|wait|export|port|rename|stat]
- Container rootfs command —
docker [commit|cp|diff]
- Mirror warehouse –
docker [login|pull|push|search]
- Local Image Management –
docker [build|images|rmi|tag|save|import|load]
- Container Resource Management –
docker [volume|network]
- System Log information –
docker [events|history|logs]
Starting from the use of docker command, the following command structure diagram is sorted out:
For more detailed commands, see the official website or command help
docker --help
Copy the code
3, summarize
Through the introduction of the previous three articles, we have gained a very thorough understanding of.net Core multi-solution deployment to Linux, which is sufficient for small teams. If you’re paying attention, you’ll notice that while the current solution allows one-click deployment updates, you still need to log in to the Linux server to execute the commands.
It shows that this is not the optimal solution, so is there a better solution? For example: when we commit to git repository, will it be more convenient to automatically deploy the command?
The answer is yes, we can do that with Jenkins. If you are interested, you can do your own research, or stay tuned for the following explanation. And DEVOps based on K8S in large projects. In fact, the principle of implementing DevOPS in K8S is similar to that described above. It is based on monitoring the status of git repository with the help of GitLab Runner or Jenkins. When changes are found in the specified branch, the image is packaged and then automatically updated by replacing the image in k8S Deployment. At the same time, K8S achieved elastic expansion, rolling update and other functions.
Over the years, thanks to supporters and users of the RDIFramework.NET framework, you can find out more at the following address.
RDIFramework.NET official website: www.rdiframework.net/
RDIFramework.NET official blog: blog.rdiframework.net/
Special note, the framework related technical articles please refer to the official website prevail, welcome everyone to collect!
RDIFramework.NET is built by the professional team of Hainan Guosi Software Technology Co., LTD., which has been updated for a long time. Please feel free to use it!
Please follow the official wechat account of RDIFramework.NET (wechat id: Guosisoft) to keep abreast of the latest developments.
Use wechat to scan the QR code for immediate attention