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

来源:https://www.cnblogs.com/huangxincheng/archive/2019/01/22/10305387.html
-Advertisement-
Play Games

上一篇我們用jenkins做了一個簡單的自動化發佈,在shell中採用的是 BUILD_ID=dontKillMe nohup dotnet xxx.dll & 這種簡單的後臺承載,如果你的業務 量相對比較小,可以用這個方法玩一玩,但存在二個問題:1. 無法對進程進行批量或者可視化管理。 2. 單機 ...


 

      上一篇我們用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~

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1 # -*- conding=utf-8 -*- 2 3 import requests 4 from bs4 import BeautifulSoup 5 import io 6 7 url = "https://www.mzitu.com/164871" 8 #Referer = ? 是模擬電 ...
  • 一.概述 本文介紹處理 ASP.NET Core 應用中常見錯誤的一些方法。主要是關於:開發環境異常頁;非開發環境配置自定義異常處理頁;配置狀態代碼頁(沒有正文響應,http狀態400~599的)。 1.1 開發環境異常頁 要將應用配置為顯示有關異常的詳細信息的頁面,請使用開發環境異常頁。要環境設置 ...
  • 由於谷歌翻譯官方API是付費版本,本著免費和開源的精神。分享一下用 Net Core 實現谷歌翻譯API的代碼。 項目引用的Nuget 包: ChakraCore.NET Newtonsoft.Json JavaScriptEngineSwitcher.ChakraCore.Native.win-x ...
  • 一.編寫併發布WebService服務 1.新建空web應用程式 2.右鍵項目解決方案-添加-新建項-選擇web服務 添加完成如下: 3.可以看到實例代碼里有這一行註釋,請取消註釋,因為我們要使用ajax來調用webservice // [System.Web.Script.Services.Scr ...
  • 公司項目最近出現獲取訪問功能變數名稱、埠、IP錯誤現象,通過排查發現, 之前項目一直通過Nginx自定義Headers信息來獲取,但最近運維人員失誤操作造成自定義Header信息丟失,造成項目拿不到對應的數據。思前想後,想找找官方有沒有關於此類問題通用標準化的解決方案。 一、Nginx配置如下: 二、.N ...
  • 登錄界面的搭建還是比較簡單的,雖然有點簡陋,但能用的姑且當做好的吧,如下圖: 這裡使用的是DevExpress控制項,其中值得一看的就是使用LayoutControl控制項來進行TextEdit控制項的佈局。對於一般幾個TextEdit併排的佈局而言,使用這個控制項的效果還是不錯的。 既然涉及到了系統的登錄 ...
  • FileUpload在HTML中是個常用的基礎控制項,在涉及到上傳各種格式的文件時候都會用到;筆者前段時間正好用到它做上傳功能,記錄下來做一些累積, 前端到後臺用的是的Jquery中的Ajax進行數據傳輸,在後臺的邏輯處理中以HttpPostedFileBase的對象調用SaveAs(ServerSa ...
  • C# 實體類轉json數據過濾掉欄位為null的欄位 語法如下: var jsonSetting = new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore}; var json = JsonConvert.S ...
一周排行
    -Advertisement-
    Play Games
  • 前言 在我們開發過程中基本上不可或缺的用到一些敏感機密數據,比如SQL伺服器的連接串或者是OAuth2的Secret等,這些敏感數據在代碼中是不太安全的,我們不應該在源代碼中存儲密碼和其他的敏感數據,一種推薦的方式是通過Asp.Net Core的機密管理器。 機密管理器 在 ASP.NET Core ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 順序棧的介面程式 目錄順序棧的介面程式頭文件創建順序棧入棧出棧利用棧將10進位轉16進位數驗證 頭文件 #include <stdio.h> #include <stdbool.h> #include <stdlib.h> 創建順序棧 // 指的是順序棧中的元素的數據類型,用戶可以根據需要進行修改 ...
  • 前言 整理這個官方翻譯的系列,原因是網上大部分的 tomcat 版本比較舊,此版本為 v11 最新的版本。 開源項目 從零手寫實現 tomcat minicat 別稱【嗅虎】心有猛虎,輕嗅薔薇。 系列文章 web server apache tomcat11-01-官方文檔入門介紹 web serv ...
  • C總結與剖析:關鍵字篇 -- <<C語言深度解剖>> 目錄C總結與剖析:關鍵字篇 -- <<C語言深度解剖>>程式的本質:二進位文件變數1.變數:記憶體上的某個位置開闢的空間2.變數的初始化3.為什麼要有變數4.局部變數與全局變數5.變數的大小由類型決定6.任何一個變數,記憶體賦值都是從低地址開始往高地 ...
  • 如果讓你來做一個有狀態流式應用的故障恢復,你會如何來做呢? 單機和多機會遇到什麼不同的問題? Flink Checkpoint 是做什麼用的?原理是什麼? ...
  • C++ 多級繼承 多級繼承是一種面向對象編程(OOP)特性,允許一個類從多個基類繼承屬性和方法。它使代碼更易於組織和維護,並促進代碼重用。 多級繼承的語法 在 C++ 中,使用 : 符號來指定繼承關係。多級繼承的語法如下: class DerivedClass : public BaseClass1 ...
  • 前言 什麼是SpringCloud? Spring Cloud 是一系列框架的有序集合,它利用 Spring Boot 的開發便利性簡化了分散式系統的開發,比如服務註冊、服務發現、網關、路由、鏈路追蹤等。Spring Cloud 並不是重覆造輪子,而是將市面上開發得比較好的模塊集成進去,進行封裝,從 ...
  • class_template 類模板和函數模板的定義和使用類似,我們已經進行了介紹。有時,有兩個或多個類,其功能是相同的,僅僅是數據類型不同。類模板用於實現類所需數據的類型參數化 template<class NameType, class AgeType> class Person { publi ...
  • 目錄system v IPC簡介共用記憶體需要用到的函數介面shmget函數--獲取對象IDshmat函數--獲得映射空間shmctl函數--釋放資源共用記憶體實現思路註意 system v IPC簡介 消息隊列、共用記憶體和信號量統稱為system v IPC(進程間通信機制),V是羅馬數字5,是UNI ...