業務場景: win10 64位下開發go程序,部署在linux環境下發布.
supervisor和go的相關介紹,在此不再贅述,觀看本文的讀者,最好有一定的go基礎,linux命令基礎。
目錄
一.helloworld創建與編譯
二.linux安裝supervisor
三.上傳和發佈
四.常見錯誤
一.HelloWorld創建與編譯
ide環境:
1. goland
2. git for windows
3. go 1.10
代碼
package main
import "github.com/gin-gonic/gin"
func main() {
router:= gin.Default()
router.GET("/sayhello",sayHello)
router.Run(":8899")
}
func sayHello(c *gin.Context){
c.JSON(200,"Hello")
return
}
測試,開啓服務,在本地瀏覽器上輸入和返回:
交叉編譯
cd 到該項目路徑下,git bash here
GOOS表示目標系統,GOARCH是編譯的go版本,-o是是指定交叉編譯之後的名字
windows下的準備工作基本做完了,沒有完全做完,還要有ssh-key需要配,這裏先假定已經配好了,有運維的讓運維幫你配,自己配請百度
二.linux安裝supervisor(已經裝配好的可以跳過該步驟)
在git中輸入
ssh xxx@xxx.xxx.xxx.xx //服務端/虛擬機地址,比如ft@110.110.110.110
//請一步一步執行
wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
easy_install supervisor
初始化配置文件:
mkdir /etc/supervisor
echo_supervisord_conf > /etc/supervisor/supervisord.conf
mkdir /etc/supervisor/config.d
vim /etc/supervisor/supervisord.conf
//在最後一行,添加(修改)成:
[include]
files = /etc/supervisor/config.d/*.ini
supervisord.conf裏分號後面的都是註釋內容,不用管。到這一步,原生的supervisor已經裝好了,但投入使用還是不方便的,設置一下開機自動啓動
cd /lib/systemd/system
vim supervisor.service
//內容修改(添加)成:
[Unit]
Description=supervisor
After=network.target
[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
//回到bash命令上
systemctl enable supervisor.service
systemctl daemon-reload
chmod 766 supervisor.service
vim /etc/rc.d/init.d/supervisor
//修改(添加)內容爲:
#!/bin/bash
#
# supervisord This scripts turns supervisord on
#
# Author: Mike McGrath <[email protected]> (based off yumupdatesd)
#
# chkconfig: - 95 04
#
# description: supervisor is a process control utility. It has a web based
# xmlrpc interface as well as a few other nifty features.
# processname: supervisord
# config: /etc/supervisor/supervisord.conf
# pidfile: /var/run/supervisord.pid
#
# source function library
. /etc/rc.d/init.d/functions
RETVAL=0
start() {
echo -n $"Starting supervisord: "
daemon "supervisord -c /etc/supervisor/supervisord.conf "
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/supervisord
}
stop() {
echo -n $"Stopping supervisord: "
killproc supervisord
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/supervisord
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart|force-reload|reload)
restart
;;
condrestart)
[ -f /var/lock/subsys/supervisord ] && restart
;;
status)
status supervisord
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
exit 1
esac
exit $RETVAL
//回到bash上
chmod 755 /etc/rc.d/init.d/supervisor
chkconfig supervisor on
supervisor基本的準備完了,可以使用以下命令開啓看看效果,supervisorctl查看管理的客戶端,quit退出
supervisord -c /etc/supervisor/supervisord.conf
下面來進行helloworld的適配,以及對使用中遇到的常見問題的解決方案
1. 首先,linux對/tmp/下面的文件會定期清理,關機也會清理,如果比較重要的日誌,還是修改一下日誌路徑比較好
vim /etc/supervisor/supervisor.conf
權限不足的使用sudo vim /etc/supervisor/supervisor.conf ,或者-u 切換到root
內容修改爲:
; 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=/etc/supervisor/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=/etc/supervisor/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=/etc/supervisor/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:///etc/supervisor/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=/etc/supervisor/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=/etc/supervisor/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.
; add.
[include]
files = /etc/supervisor/config.d/*.ini
主要改動:
1. 末尾一定要添加,這是不同業務,比如helloworld1,hellworld2的配置路徑
2. 各個原本路徑是/tmp的全部改到了/etc/supervisor/tmp
如果找不到tmp目錄,記得mkdir /etc/supervisor/tmp先
來適配helloworld吧
cd /etc/supervisor/config.d
ll
vim helloworld.ini
//內容修改(添加)爲:
[program:helloworld]
directory = /home/tonnn/helloworld/
command = /home/tonnn/helloworld/helloworld
autorestart = true
redirect_stderr = true
stdout_logfile = /data/log/helloworld.log
簡約介紹一下,[program:helloworld]是文件頭,勿缺,會報錯,文件頭錯誤,directory是交叉編譯的文件所在路徑,command是supervisor管理時執行的命令文件本體,autorestart自動重啓,重定向錯誤,輸出日誌路徑
directory和stdout_logfile 請預先使用mkdir和vim創建好,不然會報錯誤哦
mkdir /home/tonnn/helloworld
vim /data/log/helloworld.log
//輸入一點註釋
[log for helloworld]
然後就是最重要的supervisor重啓了!
supervisord -c /etc/supervisor/supervisord.conf
supervisorctl -c /etc/supervisor/supervisord.conf
三.上傳和發佈
在交叉編譯的helloworld項目路徑下,git bash here
scp -P 22 helloworld xx@xxx.xxx.xxx.xxx:/home/tonnn/helloworld
新起一個git bash here
ssh xx@xxx.xxx.xxx.xxx
cd /home/tonnn/helloworld
ll
chmod 777 helloworld
supervisorctl start helloworld
結束!
從本機,訪問xxxx.xxxx.xxx.xxx:8899/sayhello
部署成功!!!!!!!!
以後如果有hello2,hello3項目,只需要執行幾步:
GOOS=linux GOARCH=amd64 go build -o hellox
scp -P 22 hellox [email protected]:/home/tonnn/projects
ssh [email protected]
cd /home/tonnn/projects
supervisorctl restart hellox
PS
1. 對已有項目修改上傳時,報無法上傳,或xxx佔用,建議先上傳到/tmp/projects,然後執行,即先上傳,再複製覆蓋原本的,並修改執行權限
sudo cp -f /tmp/projects/hello /home/tonnn/projects/hello
sudo chmod 777 /home/tonnn/projects/hello
四.常見錯誤
- 執行supervisorctl update 和supervisorctl reload時, 出現
Connection refused: file: /usr/lib64/python2.7/socket.py line: 224
這是因爲supervisor還沒啓動,使用下行啓動
supervisord -c /etc/supervisor/supervisord.conf
2. 執行supervisord -c /etc/supervisor/supervisord.conf時,出現
Error: File contains no section headers.
file: /etc/supervisor/config.d/helloworld.ini, line: 1
'directory = /home/tonnn/helloworld/\n'
For help, use /usr/bin/supervisord -h
這是因爲,helloworld.ini的頭沒帶上
- 出現文件路徑找不到,比如執行supervisord -c /etc/supervisor/supervisord.conf,出現
Error: The directory named as part of the path /data/log/helloworld.log does not exist in section ‘program:helloworld’ (file: ‘/etc/supervisor/config.d/helloworld.ini’)
For help, use /usr/bin/supervisord -h
這是因爲helloworld.ini或者helloworld.log找不到該文件,我們在conf.d裏的helloworld.ini內容特地強調了log路徑和它本身要建好。
4.執行supervisorctl start hello 出現 no process error
這是因爲,supervisord還沒有啓動,依次執行
supervisord -c /etc/supervisor/supervisord.conf
supervisorctl -c /etc/supervisor/supervisord.conf
5.出現各種未知錯誤,請保持修改之後執行
supervisorctl reload和supervisorctl update的良好習慣。
下一節講如何將複雜的部署一鍵化
https://blog.csdn.net/fwhezfwhez/article/details/80857017