netcore使用 jenkins + supervisor 實現standalone下多副本自動化釋出

一線碼農發表於2019-01-22

 

      上一篇我們用jenkins做了一個簡單的自動化釋出,在shell中採用的是 BUILD_ID=dontKillMe nohup dotnet xxx.dll &  這種簡單的後臺承載,如果你的業務

量相對比較小,可以用這個方法玩一玩,但存在二個問題:1. 無法對程式進行批量或者視覺化管理。 2. 單機模式下的多副本部署比較麻煩,比如你在一臺機

器上開啟多個同樣的程式來提高佇列的處理能力,解決這兩個問題你可以使用netcore官方推薦的supervisor 程式管理工具。

 

一: supervisor

       具體這玩意是幹嘛的,我就不說了,大家自己看官網: http://www.supervisord.org/  接下來快速部署一下。

1.  pip 

    pip是python的一個包管理器,類似於nuget,如果你的centos上沒有安裝,那麼請執行下面命令。

1 yum -y install epel-release
2 yum -y install python-pip

 

2. supervisor

    因為supervisor是python寫的,所以直接通過pip進行安裝即可。

 1 [root@k8s-node1 ~]# pip install supervisor
 2 Collecting supervisor
 3   Downloading https://files.pythonhosted.org/packages/ba/65/92575a8757ed576beaee59251f64a3287bde82bdc03964b89df9e1d29e1b/supervisor-3.3.5.tar.gz (421kB)
 4     100% |████████████████████████████████| 430kB 47kB/s 
 5 Collecting meld3>=0.6.5 (from supervisor)
 6   Downloading https://files.pythonhosted.org/packages/b6/ae/e6d731e4b9661642c1b20591d8054855bb5b8281cbfa18f561c2edd783f7/meld3-1.0.2-py2.py3-none-any.whl
 7 Installing collected packages: meld3, supervisor
 8   Running setup.py install for supervisor ... done
 9 Successfully installed meld3-1.0.2 supervisor-3.3.5
10 You are using pip version 10.0.1, however version 18.1 is available.
11 You should consider upgrading via the 'pip install --upgrade pip' command.
12 [root@k8s-node1 ~]# pip install --upgrade pip
13 Collecting pip
14   Downloading https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl (1.3MB)
15     100% |████████████████████████████████| 1.3MB 49kB/s 
16 Installing collected packages: pip
17   Found existing installation: pip 10.0.1
18     Uninstalling pip-10.0.1:
19       Successfully uninstalled pip-10.0.1
20 Successfully installed pip-18.1

 

3. supervisord

    這個就是supervisor的server端,啟動之前生成一個預設的配置檔案,放在 /data/supervisor/目錄下。

1 [root@k8s-node1 supervisor]# pwd
2 /data/supervisor
3 [root@k8s-node1 supervisor]# echo_supervisord_conf > ./supervisord.conf
4 [root@k8s-node1 supervisor]# ls
5 supervisord.conf

 

關於配置檔案的詳細配置,你可以參考官方的:http://www.supervisord.org/configuration.html ,     

; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Notes:
;  - Shell expansion ("~" or "$HOME") is not supported.  Environment
;    variables can be expanded using this syntax: "%(ENV_HOME)s".
;  - Quotes around values are not supported, except in the case of
;    the environment= options as shown below.
;  - Comments must have a leading space: "a=b ;comment" not "a=b;comment".
;  - Command will be truncated if it looks like a config file comment, e.g.
;    "command=bash -c 'foo ; bar'" will truncate to "command=bash -c 'foo ".

[unix_http_server]
file=/tmp/supervisor.sock   ; the path to the socket file
;chmod=0700                 ; socket file mode (default 0700)
;chown=nobody:nogroup       ; socket file uid:gid owner
;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=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)

[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
;umask=022                   ; process file creation umask; default 022
;user=chrism                 ; default is current user, required if root
;identifier=supervisor       ; supervisord identifier, default is 'supervisor'
;directory=/tmp              ; default is not to cd during start
;nocleanup=true              ; don't clean up tempfiles at start; default false
;childlogdir=/tmp            ; 'AUTO' child log dir, default $TEMP
;environment=KEY="value"     ; key value pairs to add to environment
;strip_ansi=false            ; strip ansi escape codes in logs; def. false

; 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
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris              ; should be same as in [*_http_server] if set
;password=123                ; should be same as in [*_http_server] if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history  ; use readline history if available

; The sample program section below shows all possible program subsection values.
; Create one or more 'real' program: sections to be able to control them under
; supervisor.

;[program:theprogramname]
;command=/bin/cat              ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1                    ; number of processes copies to start (def 1)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=999                  ; the relative start priority (default 999)
;autostart=true                ; start at supervisord start (default: true)
;startsecs=1                   ; # of secs prog must stay up to be running (def. 1)
;startretries=3                ; max # of serial start failures when starting (default 3)
;autorestart=unexpected        ; when to restart if exited after running (def: unexpected)
;exitcodes=0,2                 ; 'expected' exit codes used with autorestart (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false             ; send stop signal to the UNIX process group (default false)
;killasgroup=false             ; SIGKILL the UNIX process group (def false)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=true          ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10     ; # of stdout logfile backups (0 means none, default 10)
;stdout_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10     ; # of stderr logfile backups (0 means none, default 10)
;stderr_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A="1",B="2"       ; process environment additions (def no adds)
;serverurl=AUTO                ; override serverurl computation (childutils)

; The sample eventlistener section below shows all possible eventlistener
; subsection values.  Create one or more 'real' eventlistener: sections to be
; able to handle event notifications sent by supervisord.

;[eventlistener:theeventlistenername]
;command=/bin/eventlistener    ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1                    ; number of processes copies to start (def 1)
;events=EVENT                  ; event notif. types to subscribe to (req'd)
;buffer_size=10                ; event buffer queue size (default 10)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=-1                   ; the relative start priority (default -1)
;autostart=true                ; start at supervisord start (default: true)
;startsecs=1                   ; # of secs prog must stay up to be running (def. 1)
;startretries=3                ; max # of serial start failures when starting (default 3)
;autorestart=unexpected        ; autorestart if exited after running (def: unexpected)
;exitcodes=0,2                 ; 'expected' exit codes used with autorestart (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false             ; send stop signal to the UNIX process group (default false)
;killasgroup=false             ; SIGKILL the UNIX process group (def false)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=false         ; redirect_stderr=true is not allowed for eventlisteners
;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10     ; # of stdout logfile backups (0 means none, default 10)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10     ; # of stderr logfile backups (0 means none, default 10)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A="1",B="2"       ; process environment additions
;serverurl=AUTO                ; override serverurl computation (childutils)

; The sample group section below shows all possible group values.  Create one
; or more 'real' group: sections to create "heterogeneous" process groups.

;[group:thegroupname]
;programs=progname1,progname2  ; each refers to 'x' in [program:x] definitions
;priority=999                  ; the relative start priority (default 999)

; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.

;[include]
;files = relative/directory/*.ini
View Code

 

上面的檔案主要改兩個地方:

《1》 開啟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)

 

《2》 下面這個語法表示可以載入指定目錄的所有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. supervisorctl 

     supervisotctl 是一個客戶端工具,通過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~

 

相關文章