supervisor: supervisor安裝和gunicorn部署


安裝supervisor和gunicorn

  1. 如果用的是阿里雲的CentOS7會提示找不到supervisor,則yum install epel-release先安裝EPEL源
    yum install -y supervisor

  2. 安裝gunicorn
    /data/anaconda37/bin/pip install gunicorn
    模仿一個寫法其中Python37路徑是:
    /data/anaconda3/bin/python,
    /data/anaconda3/bin/pip install gunicorn
    Successfully installed gunicorn-20.0.4
    目錄應該在/data/anaconda3/bin/gunicorn, 后面配置文件會用到命令的目錄

一般使用配置文件目錄以及其解釋

  1. supervisord
    負責管理進程的server端,配置文件: /etc/supervisor/supervisord.conf或者在/etc/supervisord.conf
    修改/etc/supervisord.conf的最后一行.ini為.conf

  2. supervisorctl
    client端的命令行工具,管理子進程,配置文件: /etc/supervisor/supervisord.d/目錄下
    這目錄下又建立了supervisor.conf的配置文件, 把程序配置寫入

注意以上文件是 supervisord和supervisor的區別

設置開機自啟動

systemctl enable supervisord

系統啟動supervisor

  1. systemctl start supervisord
  2. supervisord -c /etc/supervisord.conf

查看啟動狀態

systemctl status supervisord
● supervisord.service - Process Monitoring and Control Daemon
   Loaded: loaded (/usr/lib/systemd/system/supervisord.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2020-05-07 18:22:50 CST; 14s ago
  Process: 49802 ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf (code=exited, status=0/SUCCESS)
 Main PID: 49805 (supervisord)
   CGroup: /system.slice/supervisord.service
           └─49805 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf

May 07 18:22:50 yzsjhl-evdc1-31.opi.com systemd[1]: Starting Process Monitoring and Control Daemon...
May 07 18:22:50 yzsjhl-evdc1-31.opi.com systemd[1]: Started Process Monitoring and Control Daemon.

修改配置文件開啟web界面訪問監控程序

vim /etc/supervisord.conf
把inet_http_server模塊的注釋去並修改IP、用戶名與密碼,如下

[inet_http_server]
port=0.0.0.0:9001
username=root 
password=root

重新載入配置文件supervisorctl reload

開啟Supervisor默認的9001端口

firewall-cmd --zone=public --add-port=9001/tcp --permanent
firewall-cmd --reload 

訪問web頁面(我的機器ip是10.4.1.31):http://10.4.1.31:9001, , 可以看到你部署東西的狀態

查看配置文件最后一個模塊

還是上面那個文件
cat /etc/supervisord.conf
配置需要管理的進程也可修改為files = supervisord.d/*.conf后綴,目錄在/etc/supervisord.d/下面

[include]
files = supervisord.d/*.conf

編輯supervisor.conf配置文件

gunicorn文件gunicorn_config.py

bind = '10.16.1.31:18888'
log_level = "debug"
worker_class = "sanic.worker.GunicornWorker"
workers = 4
worker_connections = 1000
spew = False
daemon = False

Python案例

[program:rrw_idfa]
command=/data/anaconda3/envs/sanic/bin/gunicorn -c config/gunicorn_config.py index:app
process_name=%(program_name)s-8888
directory=/home/tornado/rrw_idfa/src                  ; 運行前cd到此目錄
autostart=true                                          ; supervisord守護程序啟動時自動啟動sanic
autorestart=true                                        ; supervisord守護程序重啟時自動重啟sanic
user=tornado                                            ; 運行程序前su到此用戶
redirect_stderr=true                                    ; 將stderr重定向到stdout
stdout_logfile=/home/tornado/rrw_idfa/logs/sanic_tornado_stdout.log        ; 記錄控制台輸出的日志位置

Java案例

[program:xxx]
directory = /root/xxx                           ;啟動目錄
command =  java -jar xxx-4.0.jar       ;啟動命令
autostart = true                                               ;在supervisord啟動的時候也啟動
startsecs = 5                                                   ;啟動5秒后沒有異常退出,就當作已經正常啟動了
autorestart = true                                            ;程序異常退出后自動重啟
startretries = 3                                                ;啟動失敗自動重試次數,默認是3
user = root                                                      ;哪個用戶啟動
redirect_stderr = true                                      ;把stderr重定向到stdout,默認false
stdout_logfile_maxbytes = 20MB                    ;stdout日志文件大小,默認50MB
stdout_logfile_backups = 20                           ;stdout日志文件備份數
stdout_logfile = /root/xxx/logs/xxx.log
;stdout日志文件,需要手動創建/root/xxx/logs目錄
[program:rrw_operational_uid_api]
directory=/data/rrw_tmp/rrw_operational_uid_api/src                  ; 運行前cd到此目錄
command=/data/anaconda3/bin/gunicorn -c config/gunicorn_config.py index:app
process_name=%(program_name)s-8888
autostart=true                                          ; supervisord守護程序啟動時自動啟動sanic
autorestart=true                                        ; supervisord守護程序重啟時自動重啟sanic
user=tornado                                            ; 運行程序前su到此用戶
redirect_stderr=true                                    ; 將stderr重定向到stdout
stdout_logfile=/data/rrw_tmp/rrw_operational_uid_api/logs/supervisor_operational_stdout.log        ; 記錄控制台輸出的日志位置


單獨啟動rrw_operational_uid_api進程

supervisorctl start rrw_operational_uid_api

supervisorctl常用命令

> status           #查看程序狀態
> stop name    #關閉name程序
> start name    #啟動name程序
> restart name # 重啟name程序
> reread          #讀取有更新的配置文件,不會啟動新添加的程序
> update          #重啟配置文件修改過的程序
     

nginx配置

修改nginx配置文件把80端口映射到9001端口添加


# 文件位置
vim /usr/local/nginx/conf/nginx.conf
# 文件內容

location /supervisor/ {
        proxy_pass http://127.0.0.1:9001/;
}
# 改完以后執行命令
nginxsystemctl restart nginx

訪問http://192.168.1.108/supervisor/
或者繼續訪問帶端口的地址

查看supervisord.conf文件是否正常配置

supervisord -c /etc/supervisord.conf

出現報錯信息即錯誤要改掉

常見問題

問題1
Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord.
ps -ef | grep supervisord
kill -9 xxxx

問題2
Unlinking stale socket /var/run/supervisor/supervisor.sock
unlink /var/run/supervisor/supervisor.sock
stale是不新鮮的意思

問題3
unix:///var/run/supervisor/supervisor.sock no such file
sudo chmod 777 /run
sudo chmod 777 /var/log

查看所有狀態

查看所有狀態
supervisorctl -c /etc/supervisord.conf status
查看單個狀態:
supervisorctl -c /etc/supervisord.conf status xxx:xxx-17777
修改配置文件后重新啟動: 
supervisorctl -c /etc/supervisord.conf restart xxx:xxx-17777
更新代碼后一般重新啟動:
supervisorctl restart xxx:xxx-17777

出現問題: xxx:xxx-17777 FATAL Exited too quickly (process log may have details)
查看你部署服務的日志: tail -f -n 100 /data/www/ope_test/xxx/logs/xxx.log
日志顯示: Error: 'config/gunicorn_config_test.py' doesn't exist
mv gunicorn_confit_test.py gunicorn_config_test.py
原來文件名寫錯了

出現問題: Failed to find application object 'app' in 'index
將app初始化的拿到全局來, 不放在函數中

殺死進程等

yum install psmisc
pstree -ap|grep gunicorn
kill -9 端口殺不死
先停下supervisorctl stop all 再殺, 不然永遠殺不死進程


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM