-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b8d1aa7
commit 91f0e52
Showing
4 changed files
with
322 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
186 changes: 186 additions & 0 deletions
186
docs/dotnetcore/examples/ASPNETCore-Supervisord-Ubuntu.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
# ASP.NET Core+Supervisord部署至Ubuntu | ||
|
||
# ASP.NET Core+Supervisord部署至Ubuntu | ||
|
||
asp.netcore中内置了kestrel,如果不通过代理工具,如IIS,Nginx,Apache,HTTP请求内容如下 | ||
|
||
## 前提条件 | ||
|
||
- ubuntu服务器 | ||
- 安装nginx | ||
- 服务器已安装.net sdk 6 | ||
- 安装守护进程 supervisor | ||
|
||
## supervisor 守护进程 | ||
|
||
- 官网 [http://supervisord.org/](http://supervisord.org/) | ||
|
||
什么是守护进程 | ||
|
||
守护进程(Daemon)是一个运行在后台的特殊进程,它独立于终端,并且以周期性地执行某些任务,在linux中,由于在linux中,每个系统与用户进行交流的界面称为终端,每一个从此终端开始运行的进程都会依附于这个终端,这个终端被称为这些进程的控制终端,当控制终端被关闭的时候,相应的进程都会自动关闭。但是守护进程却能突破这种限制,它脱离于终端并且在后台运行,并且它脱离终端的目的是为了避免进程在运行的过程中的信息在任何终端中显示并且进程也不会被任何终端所产生的终端信息所打断。它从被执行的时候开始运转,直到整个系统关闭才退出。在 Linux 上有很多可以管理进程的工具,我们使用 Supervisor 来做这个事情。 | ||
|
||
为什么部署asp.netcore项目需要一个守护进程 | ||
|
||
因为asp.netcore项目部署后,服务器可能存在关机,断开终端,无法保证web程序自启动。 | ||
|
||
## Ubuntu安装supervisor | ||
|
||
```bash | ||
sudo apt-get install supervisor # 安装 守护进程 supervisor | ||
cd /etc/supervisor/conf.d/ # 进入配置目录 | ||
vim devops_api.conf # 新建 一个配置文件 ,只要以 .conf结尾即可。 | ||
``` | ||
|
||
## 配置devops_api.conf | ||
|
||
```bash | ||
[program:devops_api] | ||
command=dotnet DevOps.Api.dll --urls="http://*:7000" | ||
directory=/home/igeekfan/code/devops_api | ||
environment=ASPNETCORE__ENVIRONMENT=Production | ||
user=www-data | ||
stopsignal=INT | ||
autostart=true | ||
autorestart=true | ||
startsecs=1 | ||
stderr_logfile=/home/igeekfan/code/devops_api/DevOps_Api.err.log | ||
stdout_logfile=/home/igeekfan/code/devops_api/DevOps_Api.out.log | ||
``` | ||
|
||
把linux的发布包放到了/home/igeekfan/code/devops_api目录中 | ||
|
||
有相应注释的,conf不能有注释,虽然没有任何异常,但无法启动服务。 | ||
|
||
```bash | ||
[program:devops_api]##程序名称,终端控制时需要的标识 | ||
command=dotnet DevOps.Api.dll --urls="http://*:7000" #要执行的命令 | ||
directory=/home/igeekfan/code/devops_api #命令执行的目录 | ||
environment=ASPNETCORE__ENVIRONMENT=Production #环境变量 | ||
user=www-data #进程执行的用户身份 | ||
stopsignal=INT | ||
autostart=true #是否自动启动 | ||
autorestart=true #是否自动重启 | ||
startsecs=1 #自动重启间隔 | ||
stderr_logfile=/home/igeekfan/code/devops_api/DevOps_Api.err.log #标准错误日志 | ||
stdout_logfile=/home/igeekfan/code/devops_api/DevOps_Api.out.log #标准输出日志 | ||
``` | ||
|
||
### 重启守护进程 | ||
|
||
(重新发布后,或服务崩溃后) | ||
|
||
```bash | ||
#重启守护进程 | ||
sudo /etc/init.d/supervisor restart | ||
sudo service supervisor restart | ||
# 暂停服务supervisor | ||
sudo service supervisor stop | ||
# 启动服务supervisor | ||
sudo service supervisor start | ||
``` | ||
|
||
## supervisorctl 常用的相关命令 | ||
|
||
```bash | ||
# 查看所有任务状态 | ||
supervisorctl status | ||
# 关闭所有任务 | ||
supervisorctl shutdown | ||
#重启指定应用 | ||
supervisorctl restart <application name> | ||
#停止指定应用 | ||
supervisorctl stop <application name> | ||
#启动指定应用 | ||
supervisorctl start <application name> | ||
#重启所有应用 | ||
supervisorctl restart all | ||
#停止所有应用 | ||
supervisorctl stop all | ||
#启动所有应用 | ||
supervisorctl start all | ||
# 修改配置文件可用 | ||
sudo supervisorctl reload | ||
``` | ||
|
||
查看日志 | ||
|
||
```bash | ||
# 查看日志 | ||
tail -f /home/igeekfan/code/devops_api/DevOps_Api.err.log | ||
``` | ||
|
||
查看devops_api进程 | ||
|
||
```bash | ||
ps -ef | grep devops_api | ||
www-data 38 31 0 02:57 ? 00:00:00 dotnet /home/igeekfan/code/devops_api/DevOps.Api.dll --urls=http://*:7000 | ||
igeekfan 124 9 0 03:00 pts/0 00:00:00 grep --color=auto devops_api | ||
``` | ||
|
||
## supervisor设置开机 | ||
|
||
设置`ubuntu`下的supervisor开机 自启动 | ||
|
||
```bash | ||
sudo vim /etc/rc.local | ||
``` | ||
|
||
在exit 0 之前加入以下命令 | ||
|
||
```bash | ||
/usr/local/bin/supervisord | ||
``` | ||
|
||
## Supervisor UI 管理台 | ||
|
||
Supervisor 默认给我们提供了一个图形界面来供我们管理进程和任务,linux下需要我们手动开启 | ||
|
||
```bash | ||
sudo vim /etc/supervisor/supervisord.conf | ||
``` | ||
|
||
增加配置,我们就能直接访问localhost:7002 | ||
|
||
```bash | ||
[inet_http_server]port=0.0.0.0:7002username=userpassword=123 | ||
``` | ||
|
||
如果有防火墙 | ||
|
||
```bash | ||
firewall-cmd --zone=public --add-port=7002/tcp --permanent | ||
firewall-cmd --reload | ||
``` | ||
|
||
## Nginx反向代理 | ||
|
||
一般情况下,我们建议使用nginx进行代理 | ||
|
||
![356361-5afcd9e65421b9e5.webp](./356361-5afcd9e65421b9e5.webp) | ||
|
||
```bash | ||
sudo apt-get install nginx | ||
``` | ||
|
||
安装好以后,定位到 /etc/nginx/sites-available/default 文件。更改server 节点如下 | ||
|
||
```bash | ||
server { | ||
listen 80; | ||
location / { | ||
proxy_pass http://localhost:7000; | ||
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; | ||
} | ||
} | ||
``` | ||
|
||
然后重新启动 Nginx | ||
|
||
```bash | ||
sudo service nginx restart | ||
sudo nginx -s reload #也可以使用这条命令重新加载配置项 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# ASP.NET Core+Systemd部署至Ubuntu | ||
|
||
# Systemd部署至Ubuntu | ||
|
||
本指南介绍如何在 Ubuntu 16.04 服务器上设置生产就绪 [ASP.NET](http://asp.net/) Core 环境。 这些说明可能适用于较新版本的 Ubunt | ||
|
||
> 对于 Ubuntu 14.04,建议使用 supervisord 作为监视 Kestrel 进程的解决方案。 systemd 不适用于 Ubuntu 14.04。 | ||
> | ||
有关 Ubuntu 14.04 的说明,请参阅[本主题的以前版本](https://github.com/dotnet/AspNetCore.Docs/blob/e9c1419175c4dd7e152df3746ba1df5935aaafd5/aspnetcore/publishing/linuxproduction.md)。 | ||
|
||
## 下载源码 | ||
|
||
部署devops/Devops.Api.csproj,打包,将所有文件上传至linux的 `/var/www/devops_api` 目录中 | ||
|
||
[https://github.com/luoyunchong/devops](https://github.com/luoyunchong/devops) | ||
|
||
```bash | ||
cd e:/code:/devops/DevOps.Api | ||
dotnet publish --configuration Release | ||
``` | ||
|
||
## Systemd | ||
|
||
服务器设置为将对 `http://<serveraddress>:80` 发起的请求转发到在 `http://127.0.0.1:5000` 中的 Kestrel 上运行的 [ASP.NET](http://asp.net/) Core 应用。 但是,未将 Nginx 设置为管理 Kestrel 进程。 `systemd` 可用于创建服务文件以启动和监视基础 Web 应用。 `systemd` 是一个初始系统,可以提供启动、停止和管理进程的许多强大的功能。 | ||
|
||
创建服务定义文件: | ||
|
||
```bash | ||
sudo vim /etc/systemd/system/kestrel-devops-api.service | ||
``` | ||
|
||
下示例是应用的一个 `.ini` 服务文件: | ||
|
||
```bash | ||
[Unit] | ||
Description=Example .NET Web API App running on Ubuntu | ||
After=network.target | ||
|
||
[Service] | ||
WorkingDirectory=/var/www/devops_api | ||
ExecStart=/usr/bin/dotnet /var/www/devops_api/DevOps.Api.dll --urls="http://*:7000" | ||
Restart=always | ||
# Restart service after 10 seconds if the dotnet service crashes: | ||
RestartSec=10 | ||
KillSignal=SIGINT | ||
SyslogIdentifier=devops-api | ||
User=www-data | ||
Environment=ASPNETCORE_ENVIRONMENT=Production | ||
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false | ||
|
||
[Install] | ||
WantedBy=multi-user.target | ||
``` | ||
|
||
在前面的示例中,管理服务的用户由 `User` 选项指定。 用户 (`www-data`) 必须存在并且拥有正确应用文件的所有权。 | ||
|
||
使用 `TimeoutStopSec` 配置在收到初始中断信号后等待应用程序关闭的持续时间。 如果应用程序在此时间段内未关闭,则将发出 SIGKILL 以终止该应用程序。 提供作为无单位秒数的值(例如,`150`)、时间跨度值(例如,`2min 30s`)或 `infinity` 以禁用超时。 `TimeoutStopSec` 默认为管理器配置文件 (`systemd-system.conf`, `system.conf.d`, `systemd-user.conf`, `user.conf.d`) 中 `DefaultTimeoutStopSec` 的值。 大多数分发版的默认超时时间为 90 秒。 | ||
|
||
```bash | ||
# The default value is 90 seconds for most distributions. | ||
TimeoutStopSec=90 | ||
``` | ||
|
||
Linux 具有区分大小写的文件系统。 将 `ASPNETCORE_ENVIRONMENT` 设置为 `Production` 时,将搜索配置文件 `appsettings.Production.json`,而不搜索 `appsettings.production.json`。 | ||
|
||
必须转义某些值(例如,SQL 连接字符串)以供配置提供程序读取环境变量。 使用以下命令生成适当的转义值以供在配置文件中使用: | ||
|
||
```bash | ||
systemd-escape "<value-to-escape>" | ||
``` | ||
|
||
环境变量名不支持冒号 (`:`) 分隔符。 使用双下划线 (`__`) 代替冒号。 环境变量读入配置时,[环境变量配置提供程序](https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/configuration/?view=aspnetcore-6.0#environment-variables)将双下划线转换为冒号。 以下示例中,连接字符串密钥 `ConnectionStrings:DefaultConnection` 以 `ConnectionStrings__DefaultConnection` 形式设置到服务定义文件中: | ||
|
||
```bash | ||
Environment=ConnectionStrings__DefaultConnection={Connection String} | ||
``` | ||
|
||
`After=network.target` 是systemd服务单元文件中的一个设置,它指定了服务单元应该在哪个系统目标(target)之后启动。在这种情况下,`network.target` 指的是网络服务的启动。这个设置确保了在网络服务启动之后,才会启动你的服务单元。这是因为在某些情况下,你的服务可能需要依赖于网络服务。这个设置可以确保你的服务在网络服务可用时启动,避免了启动服务时出现问题的可能性。 | ||
|
||
保存该文件并启用该服务。 | ||
|
||
```bash | ||
sudo systemctl enable kestrel-devops-api.service | ||
``` | ||
|
||
启用该服务,并确认它正在运行 | ||
|
||
```bash | ||
sudo systemctl start kestrel-devops-api.service | ||
sudo systemctl status kestrel-devops-api.service | ||
sudo systemctl stop kestrel-devops-api.service | ||
``` | ||
|
||
```bash | ||
● kestrel-devops-api.service - Example .NET Web API App running on Ubuntu | ||
Loaded: loaded (/etc/systemd/system/kestrel-devops-api.service; enabled; vendor preset: enabled) | ||
Active: active (running) since Sun 2022-06-12 01:41:00 CST; 2min 2s ago | ||
Main PID: 2395006 (dotnet) | ||
Tasks: 14 (limit: 4612) | ||
Memory: 26.8M | ||
CGroup: /system.slice/kestrel-devops-api.service | ||
└─2395006 /usr/bin/dotnet /var/www/devops_api/DevOps.Api.dll --urls=http://*:7000 | ||
|
||
Jun 12 01:41:00 hecs-x-large-2-linux-20200616150407 dotnet-example[2395006]: info: Microsoft.Hosting.Lifetime[14] | ||
Jun 12 01:41:00 hecs-x-large-2-linux-20200616150407 dotnet-example[2395006]: Now listening on: http://[::]:7000 | ||
Jun 12 01:41:00 hecs-x-large-2-linux-20200616150407 dotnet-example[2395006]: info: Microsoft.Hosting.Lifetime[0] | ||
Jun 12 01:41:00 hecs-x-large-2-linux-20200616150407 dotnet-example[2395006]: Application started. Press Ctrl+C to shut down. | ||
Jun 12 01:41:00 hecs-x-large-2-linux-20200616150407 dotnet-example[2395006]: info: Microsoft.Hosting.Lifetime[0] | ||
Jun 12 01:41:00 hecs-x-large-2-linux-20200616150407 dotnet-example[2395006]: Hosting environment: Production | ||
Jun 12 01:41:00 hecs-x-large-2-linux-20200616150407 dotnet-example[2395006]: info: Microsoft.Hosting.Lifetime[0] | ||
Jun 12 01:41:00 hecs-x-large-2-linux-20200616150407 dotnet-example[2395006]: Content root path: /var/www/devops_api/ | ||
Jun 12 01:41:12 hecs-x-large-2-linux-20200616150407 dotnet-example[2395006]: warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3] | ||
Jun 12 01:41:12 hecs-x-large-2-linux-20200616150407 dotnet-example[2395006]: Failed to determine the https port for redirect. | ||
``` | ||
|
||
## 查看日志 | ||
|
||
使用 Kestrel 的 Web 应用是通过 `systemd` 进行管理的,因此所有事件和进程都被记录到集中日志。 但是,此日志包含由 `systemd` 管理的所有服务和进程的全部条目。 若要查看特定于 `kestrel-helloapp.service` 的项,请使用以下命令: | ||
|
||
```bash | ||
sudo journalctl -fu kestrel-devops-api.service | ||
``` | ||
|
||
有关进一步筛选,使用时间选项(如 `--since today`、`--until 1 hour ago`)或这些选项的组合可以减少返回的条目数。 | ||
|
||
```bash | ||
sudo journalctl -fu kestrel-devops-api.service --since "2016-10-18" --until "2016-10-18 04:00" | ||
``` | ||
|
||
[使用 Nginx 在 Linux 上托管 ASP.NET Core | Microsoft Docs](https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-6.0) | ||
|
||
[https://github.com/luoyunchong/devops](https://github.com/luoyunchong/devops) |
Binary file not shown.