Giới thiệu
Supervisor là một dịch vụ client/server được phát triển bằng Python, dùng để quản lý các tiến trình trên hệ điều hành Linux/Unix. Nó không hỗ trợ Windows. Supervisor giúp theo dõi, khởi động, dừng, và khởi động lại một hoặc nhiều tiến trình. Khi một tiến trình bị tắt bất ngờ, Supervisor sẽ tự động khởi động lại nó, giúp duy trì tính ổn định của ứng dụng mà không cần viết script shell.
Cài đặt Supervisor
Vì Supervisor được phát triển bằng Python 2, nên chúng ta sẽ sử dụng pip2 để cài đặt:
[root@k8s-node02 ~]# which pip2 # Kiểm tra xem có pip2 không
[root@k8s-node02 ~]# yum -y install python-pip # Cài đặt nếu chưa có
[root@k8s-node02 ~]# pip2 install supervisor -i https://pypi.tuna.tsinghua.edu.cn/simple
Cấu hình Supervisor
1. Tạo tệp cấu hình
Tạo tệp cấu hình mặc định:
echo_supervisord_conf > /etc/supervisord.conf
2. Cấu hình giao diện Supervisor
Sửa tệp cấu hình để bao gồm các tệp cấu hình khác:
vim /etc/supervisord.conf
[include]
files = /etc/supervisor/*.conf
3. Tạo dịch vụ Tomcat để kiểm tra
Tải về và cài đặt Tomcat:
wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.63/bin/apache-tomcat-8.5.63.tar.gz
yum install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64 -y
tar xf apache-tomcat-8.5.63.tar.gz -C /usr/local/
cd /usr/local/
ln -sv apache-tomcat-8.5.63/ tomcat
4. Tạo tệp cấu hình cho dịch vụ Tomcat
Tạo tệp cấu hình cho dịch vụ Tomcat:
vim /etc/supervisor/tomcat.conf
[program:tomcat]
directory=/usr/local/tomcat
command=/usr/local/tomcat/bin/catalina.sh run
autostart=true
startsecs=10
autorestart=true
startretries=3
user=root
priority=999
stopsignal=INT
redirect_stderr=true
stdout_logfile_maxbytes=200MB
stdout_logfile_backups = 100
stdout_logfile=/usr/local/tomcat/logs/catalina.out
stopasgroup=false
killasgroup=false
Khởi động Supervisor
Khởi động Supervisor và kiểm tra trạng thái của Tomcat:
supervisord -c /etc/supervisord.conf
ps -ef|grep java
Cấu hình giao diện Web của Supervisor
Sửa tệp cấu hình để kích hoạt giao diện Web:
vim /etc/supervisord.conf
[inet_http_server]
port=0.0.0.0:9001
supervisorctl reload
Tham số cấu hình tệp và lệnh thường dùng
Tham số cấu hình tệp
Một số tham số cấu hình thông dụng:
[unix_http_server]
file=/tmp/supervisor.sock
[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
[program:xx]
command=/opt/apache-tomcat-8.0.35/bin/catalina.sh run
autostart=true
startsecs=10
autorestart=true
startretries=3
user=tomcat
priority=999
redirect_stderr=true
stdout_logfile_maxbytes=20MB
stdout_logfile_backups = 20
stdout_logfile=/opt/apache-tomcat-8.0.35/logs/catalina.out
stopasgroup=false
killasgroup=false
[include]
files = relative/directory/*.ini
Lệnh thường dùng
Một số lệnh thường dùng:
supervisorctl status all
supervisorctl stop all
supervisorctl start all
supervisorctl restart all
supervisorctl reread
supervisorctl reload
supervisorctl update
supervisorctl status [tên tiến trình]
supervisorctl start [tên tiến trình]
supervisorctl restart [tên tiến trình]
supervisorctl stop [tên tiến trình]
supervisorctl add
supervisorctl remove