前面的文章聊过用 jenkins 做了一个简单的自动化发布,在shell中采用的是 BUILD_ID=dontKillMe nohup dotnet xxx.dll & 这种简单的后台承载,如果你的业务量相对比较小,可以用这个方法玩一玩,但存在二个问题:1. 无法对进程进行批量或者可视化管理。2. 单机模式下的多副本部署比较麻烦,比如你在一台机器上开启多个同样的程序来提高队列的处理能力,解决这两个问题你可以使用netcore官方推荐的 supervisor 进程管理工具。
一:supervisor具体这玩意是干嘛的,我就不说了,大家自己看官网:http://www.supervisord.org/ 接下来快速部署一下。
1. pippip是python的一个包管理器,类似于nuget,如果你的centos上没有安装,那么请执行下面命令。
yum -y install epel-release
yum -y install python-pip
2. supervisor因为supervisor是python写的,所以直接通过pip进行安装即可。
[root@k8s-node1 ~]# pip install supervisor
Collecting supervisor
Downloading https://files.pythonhosted.org/packages/ba/65/92575a8757ed576beaee59251f64a3287bde82bdc03964b89df9e1d29e1b/supervisor-3.3.5.tar.gz (421kB)
100% |████████████████████████████████| 430kB 47kB/s
Collecting meld3>=0.6.5 (from supervisor)
Downloading https://files.pythonhosted.org/packages/b6/ae/e6d731e4b9661642c1b20591d8054855bb5b8281cbfa18f561c2edd783f7/meld3-1.0.2-py2.py3-none-any.whl
Installing collected packages: meld3, supervisor
Running setup.py install for supervisor ... done
Successfully installed meld3-1.0.2 supervisor-3.3.5
You are using pip version 10.0.1, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[root@k8s-node1 ~]# pip install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl (1.3MB)
100% |████████████████████████████████| 1.3MB 49kB/s
Installing collected packages: pip
Found existing installation: pip 10.0.1
Uninstalling pip-10.0.1:
Successfully uninstalled pip-10.0.1
Successfully installed pip-18.1
3. supervisord这个就是supervisor的server端,启动之前生成一个默认的配置文件,放在 /data/supervisor/目录下。
[root@k8s-node1 supervisor]# pwd
/data/supervisor
[root@k8s-node1 supervisor]# echo_supervisord_conf > ./supervisord.conf
[root@k8s-node1 supervisor]# ls
supervisord.conf
关于配置文件的详细配置,你可以参考官方的:http://www.supervisord.org/configuration.html ,
[unix_http_server]
file=/tmp/supervisor.sock
[supervisord]
logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
logfile_maxbytes=50MB ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=10 ; # of main logfile backups; 0 means none, default 10
loglevel=info ; log level; default info; others: debug,warn,trace
pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false ; start in foreground if true; default false
minfds=1024 ; min. avail startup file descriptors; default 1024
minprocs=200 ; min. avail process descriptors;default 200
; The rpcinterface:supervisor section must remain in the config file for
; RPC (supervisorctl/web interface) to work. Additional interfaces may be
; added by defining them in separate [rpcinterface:x] sections.
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
; The supervisorctl section configures how supervisorctl will connect to
; supervisord. configure it match the settings in either the unix_http_server
; or inet_http_server section.
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
上面的文件主要改两个地方:
- 开启supervisor的可视化界面
;[inet_http_server] ; inet (TCP) server disabled by default
;port=127.0.0.1:9001 ; ip_address:port specifier, *:port for all iface
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server)
改成:
[inet_http_server] ; inet (TCP) server disabled by default
port=0.0.0.0:9001 ; ip_address:port specifier, *:port for all iface
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server)
- 下面这个语法表示可以加载指定目录的所有ini文件,这个口子就方便我们给每个应用程序配一个xx.ini文件,这样当你update的时候,supervisor会自动 将conf目录下*.ini文件和supervisor.conf进行合并。
;[include]
;files = relative/directory/*.ini
改成
[include]
files = ./conf/*.ini
最后启动一下,更多的你可以通过help命令查看。
[root@k8s-node1 supervisor]# supervisord -c ./supervisord.conf
4. supervisorctlsupervisotctl 是一个客户端工具,通过9001端口与server进行交互,可处理如下18个命令。
[root@k8s-master supervisord]# supervisorctl help
default commands (type help <topic>):
=====================================
add exit open reload restart start tail
avail fg pid remove shutdown status update
clear maintail quit reread signal stop version
接下来可以在conf目录下生成一个 consoleapp3.ini 文件,程序名为:consoleapp3。
[root@k8s-master conf]# tail consoleapp3.ini
[program:consoleapp3]
command=/usr/bin/dotnet /data/supervisord/ConsoleApp3/ConsoleApp3.dll
autostart=true
autorestart=true
stdout_logfile=/data/supervisord/ConsoleApp3/1.log
执行以下update命令让supervisor重新加载配置文件。
[root@k8s-master supervisord]# supervisorctl update
回到文章开头的问题,如果生成多个副本,你可以采用group模式,比如下面这样的 mytest.ini 文件放置到conf目录下。
[group:mytest]
programs=mytest1,mytest2,mytest3
[program:mytest1]
command=/usr/bin/dotnet /data/supervisord/mytest/ConsoleApp1.dll
autostart=true
autorestart=true
stdout_logfile=/data/supervisord/mytest/4.log
[program:mytest2]
command=/usr/bin/dotnet /data/supervisord/mytest/ConsoleApp1.dll
autostart=true
autorestart=true
stdout_logfile=/data/supervisord/mytest/5.log
[program:mytest3]
command=/usr/bin/dotnet /data/supervisord/mytest/ConsoleApp1.dll
autostart=true
autorestart=true
stdout_logfile=/data/supervisord/mytest/6.log
然后通过 supervisorctl update 让supervisor重新加载配置文件,然后可观察可视化界面。
[root@k8s-master conf]# supervisorctl update
mytest: added process group
有了这个基础之后,和jenkins集成就很简单了,因为9001端口被占用,就重新指定了一个19001端口,把应用程序的*.ini文件放在解决方案里,这样自动化的时候就可以找到*.ini文件,然后copy到supervisor的conf目录下,因为是强制copy,所以用管道模式 yes | .....
1)普通模式
supervisorctl -s http://localhost:19001 stop memsql-test \
&& cd ./MemSql.NetCore/MemSql.Test \
&& yes | cp ./doc/memsql-test.ini /data/supervisor/conf/ \
&& dotnet publish -o /data/output/MemSql.Test -c Release \
&& supervisorctl -s http://localhost:19001 update \
&& supervisorctl -s http://localhost:19001 start memsql-test
2)组模式
supervisorctl -s http://localhost:19001 stop memsql-automationdeploy:* \
&& cd ./MemSql.NetCore/MemSql.AutomationDeploy \
&& yes | cp ./doc/memsql-automationdeploy.ini /data/supervisor/conf/ \
&& dotnet publish -o /data/output/MemSql.AutomationDeploy -c Release \
&& supervisorctl -s http://localhost:19001 update \
&& supervisorctl -s http://localhost:19001 start memsql-automationdeploy:*
好了,本篇就说到这里,如果有人问多机器怎么玩,下篇再说ansible~