当前位置:网站首页>Supervisor usage document

Supervisor usage document

2022-07-06 07:10:00 afterlife_ union

1、supervisor What problem to solve

Each of our servers may need to run several processes , For example, a server often exists at the same time nginx、php-fpm Multiple processes , Machines that perform scheduled tasks often run multiple at the same time cli Process task , If you use the command line mode to start and stop manually one by one, it is obviously very troublesome 、 Inefficient , And if the task exits abnormally 、 It is unrealistic to restart manually every time , In addition, it is inconvenient to check the status of each process .Supervisord Tools are used to manage multiple processes on each machine , And Supervisord Similar tools include monit, daemontools and runit.

2、supervisor Introduce

In conclusion ,Supervisor Yes, it is Python A common set of process managers developed , Can turn a normal command line process into a background daemon, And monitor the process status , Automatically restart the process that exited abnormally , Command line program and web The interface is used to view 、 Management process .

Supervisor In most classes Unix The system can work normally , Include Linux,Mac OS X and Solaris(10 for Intel) and FreeBSD 6.1, But not supported Windows platform .

Some information on the Internet shows Supervisor Only in Python 2 Run under , But in fact, the latest version Supervisor Has supported Python 3, requirement >= Python 3.4.

Official website
http://supervisord.org/

2、supervisor install

mac The installation command is as follows

brew install supervisor

start-up supervisor

brew services restart supervisor

Starting from supervisor The output of can be seen , The configuration file used is as follows

/opt/homebrew/etc/supervisord.conf 

The profile shows , From /opt/homebrew/etc/supervisor.d/ Read from .ini The configuration file , Manage related process tasks

Use -c Parameters can specify the main configuration file

 /opt/homebrew/bin/supervisord  -c /opt/homebrew/etc/supervisord.conf

close 、 Restart command

 close supervisor
brew services stop supervisor

3、supervisor To configure

Configurations can be divided into two categories supervisord.conf To configure and Subprocess configuration

3.1 supervisord.conf To configure

[unix_http_server]
file=/tmp/supervisor.sock   #UNIX socket  file ,supervisorctl  Will use 
chmod=0700                  #socket Of documents mode, The default is 0700
chown=nobody:nogroup        #socket Of documents owner, Format :uid:gid
username=user              #  Certified accounts 
password=123               #  The authentication code 

[inet_http_server]         #HTTP The server , Provide web Management interface , Default not on , After enabling, you can use web Interface view 、 Manage subprocesses 
port=127.0.0.1:9001        #Web Manage the backstage IP And port , If open to the public network , Need to pay attention to safety 
username=user              # Login the user name in the administrative background 
password=123               # Login management background password 
 
[supervisord]
logfile=/tmp/supervisord.log # Log files , The default is  $CWD/supervisord.log
logfile_maxbytes=50MB        # Log file size , Beyond the will rotate, Default  50MB, If set to 0, It means unlimited size 
logfile_backups=10           # Log files retain the number of backups by default 10, Set to 0 Indicates no backup 
loglevel=info                # The level of logging , Default info, Other : debug,warn,trace
pidfile=/tmp/supervisord.pid #pid  file 
nodaemon=false               # Whether to start in the foreground , The default is false, That is to  daemon  Way to start 
minfds=1024                  # The minimum value of a file descriptor that can be opened , Default  1024
minprocs=200                 # The minimum number of processes that can be opened , Default  200
 
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock # adopt UNIX socket Connect supervisord, Path and unix_http_server Part of the file Agreement 
serverurl=http://127.0.0.1:9001       # adopt HTTP Mode of connection supervisord
 
# Include other configuration files 
[include]
files = relative/directory/*.ini      # You can specify one or more to .ini End of profile 

3.2 program To configure

That is, the sub process configuration that needs to be monitored ,

#  The name of the child process , That is to say web The process name displayed in the interface 
[program:blog]
# The scripts directory 
directory=/opt/bin
# Script execution command 
command=/usr/bin/python /opt/bin/test.py

#supervisor Whether to start at the same time when starting , Default True
autostart=true
# When the program exit When , This program No automatic restart , Default unexpected, Set the automatic restart of the child process after hanging up , There are three options ,false,unexpected and true. If false When , In any case , Will not be restarted , If unexpected, Only if the exit code of the process is not below exitcodes Internally defined 
autorestart=false
# This option is the number of seconds after the child process starts , At this point, if the state is running, Then we think the launch was successful . The default value is 1
startsecs=1

# The user who the script runs as  
user = test

# Log output  
stderr_logfile=/tmp/blog_stderr.log 
stdout_logfile=/tmp/blog_stdout.log 
# hold stderr Redirect to stdout, Default  false
redirect_stderr = true
#stdout Log file size , Default  50MB
stdout_logfile_maxbytes = 20MB
#stdout Number of log file backups 
stdout_logfile_backups = 20

4、supervisorctl command

An example command is as follows

supervisorctl status        // View the status of all processes 
supervisorctl stop es       // stop it es
supervisorctl start es      // start-up es
supervisorctl restart       // restart es
supervisorctl update        // After modifying the configuration file, use this command to load the new configuration 
supervisorctl reload        // Restart all programs in the configuration 

notes : hold es Switch to all You can manage all processes in the configuration . Direct input supervisorctl Get into supervisorctl Of shell Interactive interface , At this point, the above command does not carry supervisorctl Can be used directly .

5、 Data aggregation

If there are a lot of services to manage , How to put these machines supervisor The managed services are managed uniformly ?

6、 problem

6.1 supervisor couldn’t setuid to 0 Can’t drop privilege as nonroot user

The users of the main configuration file and the configuration file of the child process are the same root user , When starting with the following command

/opt/homebrew/bin/supervisord  -c /opt/homebrew/etc/supervisord.conf

Keep reporting the following error

Error: Can't drop privilege as nonroot user
For help, use /opt/homebrew/bin/supervisord -h

Find information tips , Say so root User start supervisor, But not root User start
https://stackoverflow.com/questions/67344737/error-cant-drop-privilege-as-nonroot-user-container-keeps-restarting-on-googl
So I thought of root The user executes the startup command , The order is as follows , Finally, the program can run normally .

sudo /opt/homebrew/bin/supervisord  -c /opt/homebrew/etc/supervisord.conf

6.2 Start command

supervisor The configuration files of both the main configuration and the sub process support setting the execution user , But it seems that if the users are different , It can't start up , Not sure if this is a necessary requirement

Reference documents

https://zhuanlan.zhihu.com/p/424346764
https://www.jianshu.com/p/00fbff2c6ee1
https://blog.csdn.net/cyt0906/article/details/107344361

原网站

版权声明
本文为[afterlife_ union]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060704365278.html