1、介紹
Supervisor是一個客戶端/服務器系統,允許用戶在類UNIX操作系統上控制大量進程。 作用:為每個實例編寫啟動腳本通常是不方便的。 編寫和維護會很痛苦。此外,腳本不能自動重啟崩潰的進程,並且很多程序在崩潰時不能自行正常重啟。Supervisord作為其子進程啟動進程,並且可以配置為在崩潰時自動重啟進程。它也可以自動配置為在自己的調用中啟動進程。
2、安裝 Supervisor
yum install supervisor -y
更多安裝方法詳見官方手冊:http://www.supervisord.org/installing.html
3、配置文件詳解
Supervisor配置文件通常為 supervisord.conf。它由supervisord和supervisorctl使用。如果任一應用程序在沒有-c選項指定配置文件的情況下啟動,應用程序將按照指定的順序在以下位置查找名為supervisord.conf的文件。它將使用它找到的第一個文件。
$ CWD / supervisord.conf $ CWD的/ etc / supervisord.conf /etc/supervisord.conf /etc/supervisor/supervisord.conf
[unix_http_server] file = /tmp/supervisor.sock # 修改sock文件用戶和屬組,默認0700 chmod = 0777 # 修改sock文件用戶和屬組,默認為啟動supervisord的用戶和組 chown = sup:sup # web頁面驗證所需的用戶名。 username = admin password = 123 # 開啟在web界面配置管理 [inet_http_server] # web界面的訪問地址與端口。 port = 10.0.0.12:9001 # 監聽本機所有網卡地址 # port = *:9001 # port = :9001 # 默認不需要用戶名 user = admin # 默認不需要密碼,密碼為明文保存在配置文件中 password = 123 # supervisor服務參數配置 [supervisord] # 日志文件路徑,默認/var/log/supervisor/supervisord.log logfile = /var/log/supervisord.log # 日志切割大小,默認50M logfile_maxbytes = 1KB # 切割的日志保留份數,默認10 logfile_backups = 3 # 日志級別,默認info。critical, error, warn, info, debug, trace, blather loglevel = debug # pid文件,默認/run/supervisord.pid pidfile = /var/run/sup/sup.log # 是否后台開啟,默認false,true為前台開啟 nodaemon = false # 當supervisord守護進程時,切換到此目錄。 directory = /app # 程序部分 # 必須包含一個或多個程序部分,以便supervisord知道應該啟動和控制哪些程序。標題值是復合值。program ":" program_name [program:redis-6661] # 該程序啟動時將運行的命令。可以是絕對路徑 command = /app/redis/bin/redis-cli /app/redis/conf/redis-6661.conf # 程序名稱 process_name = redis-6661 # 是否自動啟動,默認是 autostart = true autorestart = true # 與autorestart同用,如果進程沒在root用戶停止或意外,其余狀態重啟program exitcodes = 0,2
更多參數詳見官網手冊:http://www.supervisord.org/configuration.html
4、使用supervisord管理redis集群
1、准備環境
6台redis
[root@redis conf]# ll total 336 -rw-r--r--. 1 root root 46723 Apr 25 20:23 redis -rw-r--r--. 1 root root 46725 Apr 25 21:01 redis-6661.conf -rw-r--r--. 1 root root 46725 Apr 25 21:01 redis-6662.conf -rw-r--r--. 1 root root 46725 Apr 25 21:01 redis-6663.conf -rw-r--r--. 1 root root 46725 Apr 25 21:01 redis-6664.conf -rw-r--r--. 1 root root 46725 Apr 25 21:01 redis-6665.conf -rw-r--r--. 1 root root 46725 Apr 25 21:02 redis-6666.conf
創建supervisord配置文件
[inet_http_server] port=:19001 username=admin password=1 [supervisorctl] ;serverurl=unix:///tmp/supervisor.sock serverurl=127.0.0.1:19001 username=super password=1 ;prompt=mysupervisor ;history_file=~/.sc_hist [supervisord] logfile = /var/log/supervisord.log logfile_maxbytes = 1KB logfile_backups = 3 loglevel = debug pidfile = /var/run/sup/sup.log nodaemon = false directory = /app [program:redis-6661] directory=/app/redis/conf command=/app/redis/bin/redis-server redis-6661.conf autostart=true autorestart=unexpected exitcodes=0,2 [program:redis-6662] directory=/app/redis/conf command=/app/redis/bin/redis-server redis-6662.conf autostart=true autorestart=unexpected exitcodes=0,2 [program:redis-6663] directory=/app/redis/conf command=/app/redis/bin/redis-server redis-6663.conf autostart=true autorestart=unexpected exitcodes=0,2 [program:redis-6664] directory=/app/redis/conf command=/app/redis/bin/redis-server redis-6664.conf autostart=true autorestart=unexpected exitcodes=0,2 [program:redis-6665] directory=/app/redis/conf command=/app/redis/bin/redis-server redis-6665.conf autostart=true autorestart=unexpected exitcodes=0,2 [program:redis-6666] directory=/app/redis/conf command=/app/redis/bin/redis-server redis-6666.conf autostart=true autorestart=unexpected exitcodes=0,2
啟動程序
systemctl start supervisord
報錯信息
BACKOFF Exited too quickly (process log may have details)
原因:supervisor適合監控業務應用,並且只能監控前台程序,redis daemon方式實現的daemon不能監控,提示退出太快,其實程序已經啟動。
Error: .ini file does not include supervisord section For help, use /usr/bin/supervisord -h
原因:配置文件中沒有加[supervisrod] 配置項
通過web界面進行管理
查看后台進程
[root@redis ~]# ps -ef|grep redis-server root 7175 6671 0 21:35 ? 00:00:00 /app/redis/bin/redis-server 10.0.0.12:6662 [cluster] root 7179 6671 0 21:35 ? 00:00:00 /app/redis/bin/redis-server 10.0.0.12:6663 [cluster] root 7183 6671 0 21:35 ? 00:00:00 /app/redis/bin/redis-server 10.0.0.12:6661 [cluster] root 7186 6671 0 21:35 ? 00:00:00 /app/redis/bin/redis-server 10.0.0.12:6666 [cluster] root 7190 6671 0 21:35 ? 00:00:00 /app/redis/bin/redis-server 10.0.0.12:6664 [cluster] root 7194 6671 0 21:35 ? 00:00:00 /app/redis/bin/redis-server 10.0.0.12:6665 [cluster]
supervisorctl使用
supervisor> help default commands (type help <topic>): ===================================== add clear fg open quit remove restart start stop update avail exit maintail pid reload reread shutdown status tail version supervisor> stop redis-6662 redis-6662: stopped supervisor> status redis-6661 RUNNING pid 9073, uptime 0:01:53 redis-6662 STOPPED Apr 25 10:04 PM redis-6663 RUNNING pid 9072, uptime 0:01:53 redis-6664 RUNNING pid 9075, uptime 0:01:53 redis-6665 RUNNING pid 9076, uptime 0:01:52 redis-6666 RUNNING pid 9074, uptime 0:01:53
注:shutdown為關閉supervisord服務
更多命令行使用見官方文檔:http://www.supervisord.org/running.html#supervisorctl-actions
錯誤1
http://localhost:9001 refused connection
解決方案:1、檢查監聽地址。默認為localhost:9001 2、檢查服務是否正常。
錯誤2
[root@redis ~]# supervisorctl Sorry, supervisord responded but did not recognize the supervisor namespace commands that supervisorctl uses to control it. Please check that the [rpcinterface:supervisor] section is enabled in the configuration file (see sample.conf).
解決方案:在配置文件中加入如下配置
[rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface