溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

利用Supervisor管理Redis進(jìn)程的方法教程

發(fā)布時(shí)間:2020-09-30 17:58:03 來源:腳本之家 閱讀:173 作者:haozlee 欄目:數(shù)據(jù)庫

前言

Supervisor 是一個(gè)用 Python 實(shí)現(xiàn)的進(jìn)程管理工具,可以很方便地啟動(dòng),關(guān)閉,重啟,查看,以及監(jiān)控進(jìn)程,當(dāng)進(jìn)程由于某種原因崩潰或者被誤殺后,可以自動(dòng)重啟并發(fā)送事件通知。Supervisor 可謂運(yùn)維利器,使用 Supervisor 來管理進(jìn)程,可以提高系統(tǒng)的高可用特性。

隨著 Redis 越來越流行,越來越多的公司都使用上了 redis,因此 Redis 的進(jìn)程管理就成了很多公司都需要面臨的問題,本文介紹如何使用 Supervisor 來管理 Redis 進(jìn)程。

Supervisor 簡介

Supervisor 包括以下四個(gè)組件。

1、supervisord

服務(wù)端程序,主要功能是啟動(dòng) supervisord 服務(wù)及其管理的子進(jìn)程,記錄日志,重啟崩潰的子進(jìn)程,等。

2、supervisorctl

命令行客戶端程序,它提供一個(gè)類似 shell 的接口,通過 UNIX 域套接字或者 TCP 套接字并使用 XML_RPC 協(xié)議與 supervisord 進(jìn)程進(jìn)行數(shù)據(jù)通信。它的主要功能是管理(啟動(dòng),關(guān)閉,重啟,查看狀態(tài))子進(jìn)程。

3、Web Server

實(shí)現(xiàn)在界面上管理進(jìn)程,還能查看進(jìn)程日志和清除日志。

4、XML-RPC 接口

可以通過 XML_RPC 協(xié)議對(duì) supervisord 進(jìn)行遠(yuǎn)程管理,達(dá)到和 supervisorctl 以及 Web Server 一樣的管理功能。

進(jìn)程被 Supervisor 管理后,其運(yùn)行狀態(tài)的轉(zhuǎn)化圖如下圖 1 所示:

利用Supervisor管理Redis進(jìn)程的方法教程

圖 1 :子進(jìn)程狀態(tài)轉(zhuǎn)移圖

我們挑幾個(gè)重要的進(jìn)程狀態(tài)來說明。

  • running:進(jìn)程處于運(yùn)行狀態(tài)
  • starting:Supervisor 收到啟動(dòng)請(qǐng)求后,進(jìn)程處于正在啟動(dòng)過程中
  • stopped:進(jìn)程處于關(guān)閉狀態(tài)
  • stopping:Supervisor 收到關(guān)閉請(qǐng)求后,進(jìn)程處于正在關(guān)閉過程中
  • backoff:進(jìn)程進(jìn)入 starting 狀態(tài)后,由于馬上就退出導(dǎo)致沒能進(jìn)入 running 狀態(tài)
  • fatal:進(jìn)程沒有正常啟動(dòng)
  • exited:進(jìn)程從 running 狀態(tài)退出

沒有接觸過 Supervisor 的朋友可能對(duì)上面的描述感到有些抽象,不用擔(dān)心,經(jīng)過下面的實(shí)踐后,會(huì)快速理解 Supervisor 涉及的這些名詞的。

Supervisor 初體驗(yàn)

我們以 CentOS 平臺(tái)下為例,說明如何使用 Supervisor 這一強(qiáng)大的進(jìn)程管理工具。

1. 安裝

可以使用easy_intall來安裝 Supervisor:

easy_install supervisor

也可以使用pip來安裝 Supervisor:

pip install supervisor

安裝過程比較簡單,此處我們不再贅述。

安裝完畢后,可以使用以下命令來測試安裝是否成功:

echo_supervisord_conf

echo_supervisord_conf將會(huì)在終端輸出 Supervisor 配置的樣例。

2. 創(chuàng)建配置目錄以及主配置文件

為了將 Supervisor 的配置放置到獨(dú)立的目錄中,我們先創(chuàng)建目錄:

cd /etc
mkdir supervisor

接著,可以繼續(xù)使用echo_supervisord_conf命令,將 Supervisor 樣例配置重定向輸出到文件文件中:

echo_supervisord_conf > /etc/supervisor/supervisord.conf

這樣,我們便生成了 Supervisor 的主配置文件supervisord.conf。

為了將 Supervisor 管理的進(jìn)程配置與主配置文件區(qū)分開來,我們創(chuàng)建獨(dú)立的目錄來存放進(jìn)程配置。

cd /etc/supervisor
mkdir conf.d

然后,修改主配置文件 supervisord.conf,添加以下配置,將 conf.d 目錄下的進(jìn)程配置引入 Supervisor 管理:

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

3. 創(chuàng)建管理進(jìn)程

為方便測試 Supervisor 的功能,我們編寫以下 python 腳本,并保存為 hello.py。

import time
import sys
while True:
  print("hello\n")
  sys.stdout.flush()
  time.sleep(1)

hello.py 的主要功能是往標(biāo)準(zhǔn)輸出中不斷地輸出 “hello” 字符串。

4. 創(chuàng)建進(jìn)程配置

為將 hello.py 腳本被 Supervisor 接管,我們?cè)?/etc/supervisor/conf.d 目錄創(chuàng)建其配置 hello.ini:

[program:hello]
command=python /home/lihao/codes/python/hello.py    
stdout_logfile=/home/lihao/codes/python/hello.log
stderr_logfile=/home/lihao/codes/python/hello_error.log
  • command:運(yùn)行進(jìn)程使用的命令
  • stdout_logfile:指定標(biāo)準(zhǔn)輸出文件
  • stderr_logfile:標(biāo)準(zhǔn)錯(cuò)誤輸出文件

需要指出的是,被 Supervisor 管理的進(jìn)程,不能使用 daemon 模式,而必須在前臺(tái)運(yùn)行,否則會(huì)報(bào)錯(cuò)。

5. 運(yùn)行 supervisord

由于我們需要使用指定目錄下的 Supervisor 主配置文件,在運(yùn)行 Supervisord 時(shí),需要使用-c參數(shù)來指定主配置文件的路徑:

supervisord -c /etc/supervisor/supervisord.conf

6. 使用 supervisorctl 管理進(jìn)程

使用 supervisorctl 可以查看監(jiān)控的進(jìn)程狀態(tài):

supervisorctl -c /etc/supervisor/supervisord.conf

輸出:

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

可以看到,腳本 hello.py 已經(jīng)運(yùn)行了起來(當(dāng)然使用 ps aux | grep hello也可以看到其進(jìn)程信息)。打開文件 /home/lihao/codes/python/hello.log,可以看到文件中不斷有”hello”輸出。

在 supervisorctl 命令行下,也可以使用start,stop,restart,status,等命令來啟動(dòng),關(guān)閉,重啟,查看狀態(tài)監(jiān)控的進(jìn)程,也可以輸入help來查看命令幫助。限于篇幅,此處不再展開,詳細(xì)的 supervisorctl 命令可以參考:http://www.supervisord.org/running.html#running-supervisorctl。

Supervisor 管理 Redis 進(jìn)程

在說完 Supervisor 的基本使用后,我們來看下如何使用 Supervisor 來管理 Redis 進(jìn)程。

Redis 的 Supervisor 配置

有了上面的基礎(chǔ),我們很容易寫出 Redis 服務(wù)進(jìn)程的 Supervisor 配置:

[program:redis]
command=/usr/local/bin/redis-server
autostart=true
autorestart=true
startsecs=3

使用 supervisorctl reload 載入新的 Redis 配置后,Redis 進(jìn)程便讓 Supervisor 啟動(dòng)了起來。如果需要指定 Redis 的輸出日志,可以通過stdout_logfile配置選項(xiàng)指定,具體也可以參考上述的 hello 例子。

由于 Supervisor 管理的進(jìn)程不能設(shè)置為 daemon 模式,故如果 Redis 無法正常啟動(dòng),可以查看一下 Redis 的配置,并將daemonize選項(xiàng)設(shè)置為 no。

daemonize no

Supervisord 開機(jī)啟動(dòng)

為了處理機(jī)器宕機(jī)重啟的情況,Redis 服務(wù)進(jìn)程需要實(shí)現(xiàn)機(jī)器重啟后自動(dòng)重啟的功能。 為此,需要配置 supervisord 進(jìn)程隨著機(jī)器啟動(dòng)而啟動(dòng)。要實(shí)現(xiàn)這一目的 ,可以在 /etc/init 目錄下添加 supervisord.conf 文件:

description  "supervisord"
start on runlevel [2345]
stop on runlevel [!2345]

respawn

exec supervisord -n -c /etc/supervisor/supervisord.conf

這樣,每當(dāng)機(jī)器重啟后,supervisord 進(jìn)程都會(huì)自動(dòng)啟動(dòng)起來,避免機(jī)器每次重啟后都需要手工啟動(dòng) supervisord 進(jìn)程的操作。Supervisord 進(jìn)程啟動(dòng)后,接下來會(huì)將其管理的進(jìn)程自動(dòng)地啟動(dòng)起來。這樣,便實(shí)現(xiàn)了被 Supervisor 管理的進(jìn)程隨著機(jī)器啟動(dòng)而啟動(dòng)的效果。讀者可以自行在測試機(jī)器上測試一下。

Supervisor Web 管理界面

如果需要開啟 Web 管理界面功能,需要在supervisord.conf配置中添加以下配置:

[inet_http_server]
port=*:9001
username=user
password=123

然后,打開瀏覽器,輸入地址 http://127.0.0.1:9001,這時(shí),會(huì)彈出輸入框,要求輸入用戶名和密碼(用戶名:user,密碼:123),便可以進(jìn)入 Supervisor 提供的進(jìn)程管理界面。

利用Supervisor管理Redis進(jìn)程的方法教程

圖 2 :Supervisor Web 管理界面

在此界面下,可以對(duì)單個(gè)進(jìn)程進(jìn)行重啟,關(guān)閉,查看日志等操作,也可以對(duì)所有的進(jìn)程進(jìn)行重啟,關(guān)閉等操作。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)億速云的支持。

參考資料

  • http://www.supervisord.org/
  • Python Web 開發(fā)實(shí)戰(zhàn),董偉明著,電子工業(yè)出版社
  • http://www.jianshu.com/p/9abffc905645
  • http://www.supervisord.org/running.html#running-supervisorctl
  • http://supervisord.org/configuration.html
  • http://www.supervisord.org/subprocess.html
  • https://lincolnloop.com/blog/automatically-running-supervisord-startup/
  • https://serverfault.com/questions/96499/how-to-automatically-start-supervisord-on-linux-ubuntu
  • https://segmentfault.com/a/1190000003955182
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI