Cài đặt và sử dụng Supervisor

1. Giới thiệu

Supervisor là một hệ thống quản lý quy trình (process) cho các ứng dụng chạy trên Unix. Nó bao gồm hai thành phần chính: supervisord, một dịch vụ chạy nền, và supervisorctl, một giao diện dòng lệnh để tương tác với supervisord.

Supervisord chịu trách nhiệm khởi động, giám sát, và kiểm soát các quy trình con. Supervisorctl cung cấp các lệnh để quản lý các quy trình này thông qua một giao diện shell.

Supervisor yêu cầu Python 2.4 trở lên, nhưng không hỗ trợ Python 3. Nên cài đặt phiên bản 3.0 trở lên.

2. Cài đặt ngoại tuyến

cd /usr/local/src
wget https://pypi.python.org/packages/7b/17/88adf8cb25f80e2bc0d18e094fcd7ab300632ea00b601cbbbb84c2419eae/supervisor-3.3.2.tar.gz
tar -zxvf supervisor-3.3.2.tar.gz
cd supervisor-3.3.2
python setup.py install
  

3. Cài đặt trực tuyến

yum install epel-release -y && yum clean all && yum makecache
yum install -y supervisor
  

Khởi động Supervisor:

/usr/bin/python2 /usr/bin/supervisord -c /etc/supervisord.conf
supervisorctl
  

Đăng ký dịch vụ hệ thống:

cat > /usr/lib/systemd/system/supervisord.service << EOF
[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service nss-user-lookup.target

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf

[Install]
WantedBy=multi-user.target
EOF
ps aux | grep supervisord | head -n 1 | awk '{print $2}' | xargs kill -9
systemctl daemon-reload && systemctl restart supervisord && systemctl enable supervisord
systemctl status supervisord
  

4. Tệp cấu hình Supervisor

Tệp cấu hình mặc định của Supervisor: `/etc/supervisord.conf`

[unix_http_server]
file=/tmp/supervisor.sock
chmod=0700
chown=nobody:nogroup

[inet_http_server]
port=127.0.0.1:9001
username=user
password=123

[supervisord]
logfile=/tmp/supervisord.log
logfile_maxbytes=50MB
logfile_backups=10
loglevel=info
pidfile=/tmp/supervisord.pid
nodaemon=false
minfds=1024
minprocs=200

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock
serverurl=http://127.0.0.1:9001

[include]
files = relative/directory/*.ini
  

5. Tệp cấu hình quy trình con

Tệp cấu hình quy trình con được lưu trong thư mục `/etc/supervisord.d/` với phần mở rộng `.ini`.

[program:zcm-node-proxy]
command=/bin/bash -c \
    '. /zcm/supervisor/zcm-env.profile; \
    [ `docker ps -a -f "name=zcm-node-proxy"| wc -l` -gt 1 ] && \
    exec docker start -a zcm-node-proxy || \
    docker run --name zcm-node-proxy \
        -v /zcm/node-proxy/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf \
        -v /run/containerd:/run/containerd \
        --restart=on-failure:5 \
        --memory=512M \
        --net=host \
        -e CLOUD_APP_NAME=paas_zcm-node-proxy \
        -v /etc/localtime:/etc/localtime:ro \
        -v /etc/timezone:/etc/timezone:ro \
        -v /tmp/zlogs:/tmp/zlogs \
        -v /tmp/zcore:/tmp/zcore \
        -d \
        ${ZCM_HARBOR}/zcm9/zcm-openresty:${ZCM_NODE_PROXY_TAG}; \
    exec docker start -a zcm-node-proxy'
process_name= zcm-node-proxy
numprocs=1
autostart=true
autorestart=true
startretries = 6
user=root
stdout_logfile=/var/log/zcm/zcm-node-proxy.log
stdout_logfile_maxbytes=10MB
stderr_logfile=/var/log/zcm/zcm-node-proxy.log
stderr_logfile_maxbytes=10MB
stopwaitsecs=20
  

6. Các lệnh Supervisor

supervisord                 // Khởi động supervisord
supervisorctl shutdown      // Dừng supervisord
supervisorctl status        // Xem trạng thái tất cả các quy trình
supervisorctl stop es       // Dừng quy trình es
supervisorctl start es      // Khởi động quy trình es
supervisorctl restart es    // Khởi động lại quy trình es
supervisorctl update        // Cập nhật cấu hình mới
supervisorctl reload        // Khởi động lại tất cả các quy trình
supervisorctl start all     // Khởi động tất cả các quy trình
supervisorctl stop all      // Dừng tất cả các quy trình
supervisorctl restart all   // Khởi động lại tất cả các quy trình
  

Ví dụ:

supervisorctl status 
supervisorctl stop nginx-gateway
supervisorctl remove nginx-gateway
supervisorctl update nginx-gateway
supervisorctl status 
docker logs -f nginx-gateway --tail=200
  

Thẻ: Supervisor systemd Unix python docker

Đăng vào ngày 14 tháng 6 lúc 21:40