使用普通用户运行 supervisor
安装 supervisor
安装 supervisor
1
yum install -y supervisor
查看 supervisor 相关的文件
1
rpm -ql supervisor
修改文件权限
1
2
3
4
5
6
7chown -R fabric:fabric /var/run/supervisor \
/var/log/supervisor \
/usr/bin/supervisord \
/usr/bin/supervisorctl \
/etc/logrotate.d/supervisor \
/etc/supervisord.conf \
/etc/supervisord.d配置文件 /etc/supervisord.conf 的默认配置如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18$ grep -v '^;\|^$' /etc/supervisord.conf
[unix_http_server]
file=/var/run/supervisor/supervisor.sock ; (the path to the socket file)
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/var/run/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)
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run/supervisor/supervisor.sock ; use a unix:// URL for a unix socket
[include]
files = supervisord.d/*.ini启动 supervisord 守护进程
1
systemctl enable --now supervisord.service
普通用户管理 supervisord
修改 supervisor 服务文件,添加 User,Group 配置参数,如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14$ cat /usr/lib/systemd/system/supervisord.service
[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
User=fabric
Group=fabric
[Install]
WantedBy=multi-user.target修改 supervisord 配置文件,修改 sock 以及 pid 文件存放位置,如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23$ cat /etc/supervisord.conf
[unix_http_server]
file=/home/fabric/supervisor/supervisor.sock ; (the path to the socket file)
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/home/fabric/supervisor/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)
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///home/fabric/supervisor/supervisor.sock ; use a unix:// URL for a unix socket
[include]
files = supervisord.d/*.ini创建 /home/fabric/supervisor 目录并修改属组
1
2mkdir /home/fabric/supervisor
chown -R fabric:fabric /home/fabric/supervisor重启 supervisord 服务
1
2systemctl daemon-reload
systemctl restart supervisord.service切换到普通用户 fabric,测试使用 supervisorctl 命令
1
2
3
4
5
6
7
8
9
10
11
12
13$ supervisorctl help
default commands (type help <topic>):
=====================================
add exit open reload restart start tail
avail fg pid remove shutdown status update
clear maintail quit reread signal stop version
$ supervisorctl reload
Restarted supervisord
$ supervisorctl version
3.4.0
使用 supervisor 管理 Redis 服务
安装 Redis
下载 Redis 源码
1
wget https://download.redis.io/releases/redis-6.2.6.tar.gz
安装 Redis
1
2
3
4
5
6tar xf redis-6.2.6.tar.gz
cd redis-6.2.6
make
# 将 Redis 二进制文件安装到 /usr/local/bin 目录下
make install创建 redis 配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63mkdir -p redis-6.2.6/{conf,logs}
cat > conf/redis-7381.conf <<EOF
bind 0.0.0.0
protected-mode yes
port 7381
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no # 如果想使用 supervisord 管理 redis 进程,此设置必须设置为 no
supervised no
pidfile /home/fabric/redis-6.2.6/redis_7381.pid
loglevel notice
logfile "/home/fabric/redis-6.2.6/logs/redis_7381.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump_7381.rdb
dir ./
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass 7381_123!QAZ
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly yes
appendfilename "7381appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
EOF测试启动 Redis
1
redis-server redis-6.2.6/conf/redis-7381.conf
验证服务是否启动
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20$ tailf redis-6.2.6/logs/redis_7379.log
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
7856:M 18 Apr 2022 10:47:10.823 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
7856:M 18 Apr 2022 10:47:10.823 # Server initialized
7856:M 18 Apr 2022 10:47:10.823 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
7856:M 18 Apr 2022 10:47:10.823 * Ready to accept connections
$ ps aux |grep redis
fabric 7856 0.2 0.4 162512 9864 ? Ssl 10:47 0:00 redis-server 0.0.0.0:7381
$ netstat -anptl |grep 7381
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 0.0.0.0:7381 0.0.0.0:* LISTEN 7856/redis-server 0
配置 supervisor 管理 Redis 进程
创建 /etc/supervisord.d/redis.ini 文件,内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14[program:redis-7381]
command=redis-server /home/fabric/redis-6.2.6/conf/redis-7381.conf
autostart=true
startsecs=10
autorestart=true
startretries=3
user=fabric
priority=999
redirect_stderr=true
stdout_logfile_maxbytes=20MB
stdout_logfile_backups = 5
stdout_logfile=/home/fabric/redis-6.2.6/logs/redis-7381.log
stopasgroup=false
killasgroup=false配置说明:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15[program:redis-7381]
command=redis-server /home/fabric/redis-6.2.6/conf/redis-7381.conf ; 程序启动命令
autostart=true ; 在supervisord启动的时候也自动启动
startsecs=10 ; 启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒
autorestart=true ; 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
startretries=3 ; 启动失败自动重试次数,默认是3
user=fabric ; 用哪个用户启动进程,默认是root
priority=999 ; 进程启动优先级,默认999,值小的优先启动
redirect_stderr=true ; 把stderr重定向到stdout,默认false
stdout_logfile_maxbytes=20MB ; stdout 日志文件大小,默认50MB
stdout_logfile_backups = 5 ; stdout 日志文件备份数,默认是10
; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
stdout_logfile=/home/fabric/redis-6.2.6/logs/redis-7381.log
stopasgroup=false ; 默认为false, 进程被杀死时,是否向这个进程组发送 stop 信号,包括子进程
killasgroup=false ; 默认为false,向进程组发送 kill 信号,包括子进程执行以下命令,重载配置
1
supervisorctl reload
查看服务状态
1
2$ supervisorctl status
redis-7381 RUNNING pid 9921, uptime 0:00:25查看服务日志
1
2
3
4
5
6
7
8
9
10
11
12
13
14$ supervisorctl tail -f redis-7381
==> Press Ctrl-C to exit <==
'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
9921:M 18 Apr 2022 11:31:11.921 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
9921:M 18 Apr 2022 11:31:11.921 # Server initialized
9921:M 18 Apr 2022 11:31:11.921 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
9921:M 18 Apr 2022 11:31:11.921 * Ready to accept connections重启服务器,验证服务是否会自动重启