Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A tutorial on how to manage Redis processes with Supervisor

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Preface

Supervisor is a process management tool implemented in Python, which can easily start, shut down, restart, view, and monitor the process. When the process crashes or is mistakenly killed for some reason, it can automatically restart and send event notifications. Supervisor is a sharp tool for operation and maintenance. Using Supervisor to manage processes can improve the high availability of the system.

As Redis becomes more and more popular, more and more companies use redis, so the process management of Redis has become a problem that many companies need to face. This paper introduces how to use Supervisor to manage Redis processes.

Introduction to Supervisor

Supervisor consists of the following four components.

1 、 supervisord

Server programs, the main function is to start the supervisord service and its management child processes, log, restart crashed child processes, and so on.

2 、 supervisorctl

A command-line client program that provides a shell-like interface that communicates with supervisord processes through UNIX domain sockets or TCP sockets and using the XML_RPC protocol. Its main function is to manage (start, shut down, restart, view status) child processes.

3 、 Web Server

To manage the process on the interface, you can also view the process log and clear the log.

4. XML-RPC interface

Supervisord can be managed remotely through XML_RPC protocol to achieve the same management functions as supervisorctl and Web Server.

After the process is managed by Supervisor, the transformation diagram of its running state is shown in figure 1:

Figure 1: child process state transition diagram

Let's pick a few important process states to illustrate.

Running: the process is running after receiving the startup request starting:Supervisor, the process is in the process of starting stopped: the process is in the shutdown state stopping:Supervisor after receiving the shutdown request, the process is in the process of shutting down backoff: after the process entered the starting state, it failed to enter the running state because it immediately exited fatal: the process did not start normally exited: the process exited from the running state

Friends who have not come into contact with Supervisor may feel a little abstract about the above description, don't worry, after the following practice, they will quickly understand these nouns involved in Supervisor.

The first experience of Supervisor

We take the CentOS platform as an example to illustrate how to use Supervisor, a powerful process management tool.

1. Installation

You can use easy_intall to install Supervisor:

Easy_install supervisor

You can also use pip to install Supervisor:

Pip install supervisor

The installation process is relatively simple, and we will not repeat it here.

After the installation is complete, you can use the following command to test whether the installation was successful:

Echo_supervisord_conf

Echo_supervisord_conf will output a sample Supervisor configuration on the terminal.

two。 Create a configuration directory and a master configuration file

To place the configuration of Supervisor in a separate directory, let's first create a directory:

Cd / etcmkdir supervisor

You can then continue to use the echo_supervisord_conf command to redirect the Supervisor sample configuration to a file file:

Echo_supervisord_conf > / etc/supervisor/supervisord.conf

In this way, we generate the main configuration file supervisord.conf for Supervisor.

To distinguish the process configuration managed by Supervisor from the main configuration file, we create a separate directory to hold the process configuration.

Cd / etc/supervisormkdir conf.d

Then, modify the main configuration file supervisord.conf, add the following configuration, and introduce the process configuration under the conf.d directory into Supervisor management:

[include] files =. / conf.d/*.ini

3. Create an administrative process

To test the functionality of Supervisor, we write the following python script and save it as hello.py.

Import timeimport syswhile True: print ("hello\ n") sys.stdout.flush () time.sleep (1)

The main function of hello.py is to continuously output "hello" strings to standard output.

4. Create process configuration

To take over the hello.py script by Supervisor, we create its configuration hello.ini in the / etc/supervisor/conf.d directory:

[program:hello] command=python / home/lihao/codes/python/hello.py stdout_logfile=/home/lihao/codes/python/hello.logstderr_logfile=/home/lihao/codes/python/hello_error.logcommand: command used to run the process stdout_logfile: specify standard output file stderr_logfile: standard error output file

It should be pointed out that processes managed by Supervisor cannot use daemon mode, but must be run in the foreground, otherwise an error will be reported.

5. Run supervisord

Since we need to use the Supervisor main configuration file in the specified directory, when running Supervisord, we need to use the-c parameter to specify the path to the main configuration file:

Supervisord-c / etc/supervisor/supervisord.conf

6. Use supervisorctl to manage processes

Use supervisorctl to view the status of monitored processes:

Supervisorctl-c / etc/supervisor/supervisord.conf

Output:

Hello RUNNING pid 8475, uptime 7:59:46supervisor >

As you can see, the script hello.py is already running (of course, using ps aux | grep hello can also see its process information). Open the file / home/lihao/codes/python/hello.log and you can see that there is constant "hello" output in the file.

Under the supervisorctl command line, you can also use start,stop,restart,status, and other commands to start, shut down, restart, view status monitoring processes, or enter help to view command help. Limited space, no longer expand here, detailed supervisorctl command can refer to: http://www.supervisord.org/running.html#running-supervisorctl.

Supervisor manages the Redis process

After talking about the basic use of Supervisor, let's take a look at how to use Supervisor to manage Redis processes.

Supervisor configuration of Redis

With the above foundation, we can easily write out the Supervisor configuration of the Redis service process:

[program:redis] command=/usr/local/bin/redis-serverautostart=trueautorestart=truestartsecs=3

After loading the new Redis configuration using supervisorctl reload, the Redis process causes Supervisor to start. If you need to specify the output log of Redis, you can specify it through the stdout_logfile configuration option. You can also refer to the hello example above.

Because the process managed by Supervisor cannot be set to daemon mode, if Redis does not start properly, you can check the configuration of Redis and set the daemonize option to no.

Daemonize no

Supervisord boot up

In order to deal with the situation of machine downtime and restart, the Redis service process needs to realize the function of automatic restart after machine restart. To do this, you need to configure the supervisord process to start as the machine starts. To do this, add the supervisord.conf file in the / etc/init directory:

Description "supervisord" start on runlevel [2345] stop on runlevel [! 2345] respawnexec supervisord-n-c / etc/supervisor/supervisord.conf

In this way, every time the machine is restarted, the supervisord process will start automatically, avoiding the need to manually start the supervisord process after each machine restart. After the Supervisord process starts, the processes it manages will then be started automatically. In this way, the process managed by Supervisor starts as the machine starts. Readers can test it on their own on the test machine.

Supervisor Web management interface

If you need to enable the Web management interface feature, you need to add the following configuration to the supervisord.conf configuration:

[inet_http_server] port=*:9001username=userpassword=123

Then, open the browser, enter the address http://127.0.0.1:9001, at this time, the input box will pop up, asking for a user name and password (user name: user, password: 123), you can enter the process management interface provided by Supervisor.

Figure 2: Supervisor Web management interface

In this interface, you can restart, shut down, view logs and other operations for a single process, as well as restart and shut down all processes.

Summary

The above is the whole content of this article, I hope that the content of this article can bring some help to your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.

references

Http://www.supervisord.org/Python Web development practice, by Dong Weiming Electronic Industry Press http://www.jianshu.com/p/9abffc905645http://www.supervisord.org/running.html#running-supervisorctlhttp://supervisord.org/configuration.htmlhttp://www.supervisord.org/subprocess.htmlhttps://lincolnloop.com/blog/automatically-running-supervisord-startup/https://serverfault.com/questions/96499/how-to-automatically-start-supervisord-on-linux-ubuntuhttps://segmentfault.com/a/1190000003955182

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report