supervisor manages the process of the server -- daemon

Posted by xenooreo on Thu, 09 Dec 2021 07:04:52 +0100

preface

Recently, the kafka process crashed because of the company's production line server. After checking, the official website told me it was a bug. Just upgrade the version. Sometimes it just collapses. So the daemon is on the agenda. Colleagues have studied the supervisor. Now we can use it for process status query, guard and startup. This allows me to see the management of server processes. Before I studied writing shell scripts, it was a headache until this time I saw this thing and I saw light. Then I realized that maybe my o & M had not even entered the door before. What shall I do? Learn. Learn from brother Flathead. You are either learning or looking for learning goals.

install

It's strange that the official website can't find the download address. I found the download address of the official website in Baidu. Put it here in case I can't find it in the future: https://pypi.org/project/supervisor/
Let's just take the tar bag.
Get into / opt
In addition, my system is CentOS 8

tar zxvf supervisor-4.1.0.tar.gz
mv supervisor-4.1.0 supervisor
cd supervisor
#Then I found that I didn't install python
sudo dnf install python2
python2 setup.py install

It's worth noting that I need to use python2 here, because I've tried python3. No, well, that's it.
Then, we enter this command: echo_supervisord_conf can see a big bang of information, which means that we have succeeded.

to configure

Create initialization profile

mkdir /usr/supervisor #Create profile directory
echo_supervisord_conf > /usr/supervisor/supervisord.conf   #Create initial profile
mkdir /usr/supervisor/supervisord.d/  #In order not to write all the new configuration information in one configuration file, a new folder is created here

Import the configuration file in / usr / Supervisor / Supervisor. D / in / usr / Supervisor / supervisor.conf. The include comment at the end of the open configuration is changed to:

[include]
files = /usr/supervisor/supervisord.d/*.conf

Remove the comment of [inet_http_server] and use the web to view the process

[inet_http_server]         ; inet (TCP) server disabled by default
port=*:9001        ; ip_address:port specifier, *:port for all iface
username=root              ; default is no username (open server)
password=123456              ; default is no password (open server)

Change all / tmp directories to a directory you specify. The files in / tmp will be deleted, which will cause the operation to fail when using the command to operate the process, because the corresponding sock file cannot be found.

start-up

supervisord -c /usr/supervisor/supervisord.conf

Power on

vim /lib/systemd/system/supervisor.service

[Unit]
Description=supervisor
After=network.target

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /usr/supervisor/supervisord.conf
PrivateTmp=true

[Install]
WantedBy=multi-user.target


systemctl enable supervisor.service
systemctl start supervisor.service

Configuration process

##kafka
Kafka configuration file / usr / Supervisor / supervisor.d/kafka.conf

[program:kafka-service] ;Program name, identification required for terminal control
command=/opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/server.properties
directory=/opt/kafka/bin ; Directory of command execution
numprocs=1 ; Start several processes
autorestart=true ; The program exited unexpectedly. Do you want to restart automatically
user=root ; User identity of process execution
priority=2 ;
stderr_logfile=/home/supervisorlog/kafka-service.log ;log file

zookeeper

Zookeeper configuration file / usr / Supervisor / supervisor.d/zookeeper.conf

[program:zookeeper-service] ;Program name, identification required for terminal control
command=/opt/zookeeper/bin/zkServer.sh start-foreground
directory=/opt/zookeeper/bin ; Directory of command execution
numprocs=1 ; Start several processes
autorestart=true ; The program exited unexpectedly. Do you want to restart automatically
user=root ; User identity of process execution
priority=1 ;
stderr_logfile=/home/supervisorlog/zookeeper-service.log ;log file

redis

Redis configuration file / usr / Supervisor / supervisor.d/redis-6379.conf

[program:redis-6379] ;Program name, identification required for terminal control
command=/usr/local/bin/redis-server /etc/redis/6379.conf
directory=/opt/redis/src ; Directory of command execution
numprocs=1 ; Start several processes
autorestart=true ; The program exited unexpectedly. Do you want to restart automatically
user=root ; User identity of process execution
stderr_logfile=/home/supervisorlog/redis-6379.log ;log file

Here, redis needs to change the daemon in the configuration file to no. If the background starts, the supervisor will think that the process is suspended, and then start it repeatedly. In fact, redis is started, and then the repeated starts will fail because the port is occupied.

After restarting the process, visit the web side and you can see that the process has started. In the future, you can see what processes the server has through the web page and click restart manually.

tomcat

[program:jenkins] ;Program name, identification required for terminal control
command=/opt/jenkins/apache-tomcat-9.0.30/bin/catalina.sh run
directory=/opt/jenkins/apache-tomcat-9.0.30/bin ; Directory of command execution
numprocs=1 ; Start several processes
autorestart=true ; The program exited unexpectedly. Do you want to restart automatically
user=root ; User identity of process execution
priority=2 ;
stderr_logfile=/home/supervisorlog/jenkins.log ;log file

In order to configure jenkins' startup, I used tomcat to start instead of directly starting the war package. Too lazy to go back. Similarly, the background startup of tomcat also leads to repeated startup and final failure. The solution is to replace start.sh with catalina.sh run.

Common operation

  • update updates the new configuration to supervisor (the previously running program will not be restarted)
  • reload, load all configuration files, and start and manage all processes according to the new configuration (the original running program will be restarted)
  • start xxx: start a process
  • restart xxx: restart a process
  • stop xxx: stop a process (xxx). XXX is the value configured in [program: theprogrammname]
  • stop groupworker: restart all processes belonging to the group named groupworker (start and restart are the same)
  • Stop all, stop all processes. Note: start, restart and stop will not load the latest configuration file
  • reread. When a service is changed from automatic startup to manual startup, execute it once

remarks

I mainly refer to this blog about the use of Supervisor: https://www.cnblogs.com/toutou/p/supervisor.html Thank you.

Troubleshooting

No module named setuptools

yum install python-setuptools

Restart error

Supervisor has commands to reload configuration files and restart related processes. However, I found that the error of unable to find the sock file was reported when executing these commands. After searching, it is found that the sock, pid and log files are placed in tmp by default. Just move them to the directory we have prepared. Specifically, modify your supervisor.conf configuration file

Topics: Operation & Maintenance kafka server