//雪花飘落特效 //右上角github跳转   

envoy 1.21 版本部署 + supervisor管理

#envoy 安装
sudo yum install yum-utils
sudo rpm --import 'https://rpm.dl.getenvoy.io/public/gpg.CF716AF503183491.key'
curl -sL 'https://rpm.dl.getenvoy.io/public/config.rpm.txt?distro=el&codename=7' > /tmp/tetrate-getenvoy-rpm-stable.repo
sudo yum-config-manager --add-repo '/tmp/tetrate-getenvoy-rpm-stable.repo'
sudo yum makecache --disablerepo='*' --enablerepo='tetrate-getenvoy-rpm-stable'

yum list getenvoy-envoy --showduplicates | sort -r

yum -y instll getenvoy-envoy.x86_64 1.18.2

]# yum -y install epel-release
]# yum -y install supervisor

]# cat envoy.ini 
[program:envoy]   #DeployLinux
command=python2.7 /usr/local/envoy/hot-restarter.py /usr/local/envoy/start.sh
directory=/usr/local/envoy
user=root
stopsignal=INT
autostart=true
autorestart=true
startsecs=3
stderr_logfile=/beta/logs/envoy/start.err.log
stdout_logfile=/beta/logs/envoy/start.out.log


#多版本切换
alternatives 
update-alternatives --install /usr/sbin/envoy envoy /usr/local/envoy/envoy 2
/usr/sbin/envoy  #必选项
envoy  #名称
/usr/local/envoy/envoy #新版本存放路径
2  #优先级

]# alternatives --config envoy

共有 2 个提供“envoy”的程序。

  选项    命令
-----------------------------------------------
*  1           /usr/bin/envoy
 + 2           /usr/local/envoy/envoy

Supervisor安装及使用

1. 简介

Supervisor( supervisord.org )是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。它可以很方便的监听、启动、停止、重启一个或多个进程。用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。

supervisor组件构成:

  • supervisord(server 部分):主要负责管理子进程,响应客户端命令以及日志的输出等;

  • supervisorctl(client 部分):命令行客户端,用户可以通过它与不同的 supervisord 进程联系,获取子进程的状态等。

  • Web Server主要可以在界面上管理进程,Web Server其实是通过XML_RPC来实现的,可以向supervisor请求数据,也可以控制supervisor及子进程。配置在[inet_http_server]块里面;

  • XML_RPC接口 : 提供了与webserver中相同的用于查询和控制进程的http接口;

    优点:

    • 可以将非后台运行程序后台运行
    • 自动监控,重启进程

    缺点:

    • 不能管理后台运行程序
    • 对多进程服务,不能使用kill关闭

2. 安装

2.1 安装软件

CentOS通过yum安装

]# yum -y install epel-release
]# yum install supervisor
 

Debian/Ubuntu可通过apt安装

apt-get install supervisor
 

pip安装

pip install supervisor
 

easy_install安装

easy_install supervisor
 

源码包离线安装

#下载页面地址
https://pypi.org/project/setuptools/#files
#官网部署地址
http://supervisord.org/installing.html
#下载包地址
https://files.pythonhosted.org/packages/af/e8/894c71e914dfbe01276a42dfad40025cd96119f2eefc39c554b6e8b9df86/setuptools-60.10.0.tar.gz


## 解压
unzip -o -d setuptools-46.2.0 setuptools-46.2.0.zip
# 把 setuptools-46.2.0.zip 文件解压到 setuptools-46.2.0
# -o :不提示的情况下覆盖文件
# -d:将文件解压缩到指定目录下
cd setuptools-46.2.0

## 安装
python setup.py install

离线从pip下载supervisor

#下载页面地址
https://pypi.org/project/supervisor/#files
#官网部署地址
http://supervisord.org/installing.html
#下载包地址
https://files.pythonhosted.org/packages/b3/41/2806c3c66b3e4a847843821bc0db447a58b7a9b0c39a49b354f287569130/supervisor-4.2.4.tar.gz

## 解压
tar -zxvf supervisor-4.2.0.tar.gz
cd supervisor-4.2.0

## 安装
python setup.py install

验证安装是否成功
supervisorctl --help

2.2 生成配置模板

生成配置文件:

#生成配置文件,且放在/etc目录下
echo_supervisord_conf > /etc/supervisord.conf

#为了不将所有新增配置信息全写在一个配置文件里,新建一个文件夹,每个程序设置一个配置文件,相互隔离;存放每个进程的配置文件
mkdir /etc/supervisord.conf.d/ 

有时会出错可以如下处理:

1.生成配置出错了
$echo_supervisord_conf > /etc/supervisord.conf
Traceback (most recent call last):
File "/usr/local/python-2.7/bin/echo_supervisord_conf", line 5, in <module>
from pkg_resources import load_entry_point
File "build/bdist.linux-x86_64/egg/pkg_resources.py", line 2603, in <module>
File "build/bdist.linux-x86_64/egg/pkg_resources.py", line 666, in require
File "build/bdist.linux-x86_64/egg/pkg_resources.py", line 565, in resolve
pkg_resources.DistributionNotFound: meld3>=0.6.5

2.处理方法:
$pip install setuptools
$pip install setuptools --upgrade
3、生成配置文件
$echo_supervisord_conf > /etc/supervisord.conf
$mkdir /etc/supervisord.conf.d/ ##存放每个进程的配置文件
复制代码

2.3 管理界面

可以配置 supervisrod 启动 web 管理界面,这个 web 后台使用 Basic Auth 的方式进行身份认证。 编辑配置文件/etc/supervisord.conf,添加inet_http_server节点,如下:

vim supervisord.conf
[inet_http_server] ; inet (TCP) server disabled by default
port=192.xx.xx.xx:9001 ; (ip_address:port specifier, *:port for all iface)
;username=user ; (default is no username (open server))
;password=123 ; (default is no password (open server))
其中,port是服务监听端口信息,username是管理员用户名,password是管理员密码。
复制代码

2.4 配置参考

最终配置参考:

cat supervisord.conf  |grep -v '^;' |grep -v '^$'
[unix_http_server]
file=/tmp/supervisor.sock   ; the path to the socket file

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

[supervisord]
logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
logfile_maxbytes=50MB        ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=3           ; # of main logfile backups; 0 means none, default 10
loglevel=info                ; log level; default info; others: debug,warn,trace
pidfile=/tmp/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:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket

[include]
files = /etc/supervisord.conf.d/*.conf

[include] 标签中添加 /etc/supervisord.d/*.conf

[include]
files = relative/directory/*.ini  /etc/supervisord.d/*.conf

长时间运行会报错 unix:///tmp/supervisor.sock no such file,参考

原因

unix:///tmp/supervisor.sock no such file

supervisor 默认配置会把 socket 文件和 pid 守护进程生成在/tmp/目录下,/tmp/目录是缓存目录,Linux 会根据不同情况自动删除其下面的文件。

修改配置

vi /etc/supervisord.conf

# 英文分号后的内容为注释
[unix_http_server]
;file=/tmp/supervisor.sock   ; (the path to the socket file)
file=/var/run/supervisor.sock   ; 修改为 /var/run 目录,避免被系统删除

[supervisord]
;logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile=/var/log/supervisord.log ; 修改为 /var/log 目录,避免被系统删除
pidfile=/var/run/supervisord.pid ; 修改为 /var/run 目录,避免被系统删除
...

[supervisorctl]
; 必须和'unix_http_server'里面的设定匹配
;serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket
serverurl=unix:///var/run/supervisor.sock ; 修改为 /var/run 目录,避免被系统删除

可选:

  • 修改权限
sudo chmod 777 /run
sudo chmod 777 /var/log
  • 创建supervisor.sock
sudo touch /var/run/supervisor.sock
sudo chmod 777 /var/run/supervisor.sock

更新配置文件

supervisorctl update

3. 启动及常用命令

3.1 启动/停止

#启动
service supervisord start
#停止
service supervisord stop

]# touch /usr/lib/systemd/system/supervisord.service

#https://github.com/Supervisor/initscripts/blob/master/centos-systemd-etcs
]# vim /usr/lib/systemd/system/supervisord.service
# supervisord service for systemd (CentOS 7.0+)
# by ET-CS (https://github.com/ET-CS)
[Unit]
Description=Supervisor daemon

[Service]
Type=forking
ExecStart=/usr/bin/supervisord
ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target


]# systemctl enable supervisord
Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.
]# systemctl is-enabled supervisord
enabled
]# systemctl daemon-reload
]# systemctl restart supervisord
]# systemctl status supervisord

3.2 常用命令

可以使用以下命令更新配置、更新进程、查看状态

# 读取有更新(增加)的配置文件,不会启动新添加的程序 
$supervisorctl reload
 # 重启配置文件修改过的程序 
$supervisorctl update
 # 查看程序状态 
$supervisorctl status
 # 启动程序 App_name 
$supervisorctl start App_name
 # 关闭程序 App_name 
$supervisorctl stop App_name
 # 重启程序 App_name 
supervisorctl -c /etc/supervisord.conf restart App_name:    
supervisorctl -c /etc/supervisord.conf start App_name:App_name_01
#supervisord **重载配置要用update ,不要用reload!

$supervisorctl restart App_name
以上命令也可以在supervisorctl Shell中执行:
$supervisorctl
supervisor>reread
supervisor> update
supervisor> status
supervisor> start App_name
supervisor> stop App_name
supervisor> restart App_name


supervisord -c /etc/supervisord.conf ## 启动
supervisorctl shutdown  ## 关闭
supervisord -c /etc/supervisord.conf  ## 通过配置文件启动supervisor
supervisorctl -c /etc/supervisord.conf status  ## 查看状态
supervisorctl -c /etc/supervisord.conf reload  ## 重新载入配置文件 
supervisorctl -c /etc/supervisord.conf start [all]|[x]  ## 启动所有/指定的程序进程 
supervisorctl -c /etc/supervisord.conf stop [all]|[x]  ## 关闭所有/指定的程序进程

4. supervisor管理应用

4.1 修改配置文件

把应用进程的配置文件都放在/etc/supervisord.conf.d目录。 1、修改supervisord的配置文件 修改/etc/supervisord.conf的内容如下:

vim /etc/supervisord.conf
[include]
;files = relative/directory/*.ini
files = /etc/supervisord.conf.d/*.conf


#(修改的配置文件生效,设置`autostart=true`的程序,会自动启动)
[root@4fff02d62bba ~]# supervisorctl update
Restarted supervisord

4.2 编辑应用配置文件

假定要对MySQL3307及Web服务进行管理。

  • MySQL应用配置: /etc/supervisord.conf.d/mysql3307.conf
  • Web应用配置: /etc/supervisord.conf.d/app.conf

1)MySQL应用配置:

$mkdir /etc/supervisord.conf.d/
$vim /etc/supervisord.conf.d/mysql3307.conf
[program:mysql3307]
directory = /data1/
command = /usr/local/mysql/bin/mysqld_safe --defaults-file=/data1/mysql3307/etc/my.cnf &
priority=1
numprocs=1
autostart=true
autorestart=true
stdout_logfile=/etc/supervisord.conf.d/mysql3307.log
startretries = 3
redirect_stderr = true
stopwaitsecs=2

2)Web应用配置:

配置Web的supervisor管理,配置文件名为/etc/supervisord.conf.d/app.conf

[program:App_name]
directory = /data1/workspace/App_name/main ; 程序的启动目录
command = /usr/local/python-2.7/bin/python ./api_main.py --port=78%(process_num)02d  ; 启动命令,可以看出与手动在命令行启动的命令是一样的
process_name=%(program_name)s_%(process_num)02d ; 
numprocs=4
numprocs_start=1
autostart = true     ; 在 supervisord 启动的时候也自动启动
startsecs = 5        ; 启动 5 秒后没有异常退出,就当作已经正常启动了
autorestart = true   ; 程序异常退出后自动重启
startretries = 3     ; 启动失败自动重试次数,默认是 3
redirect_stderr = true  ; 把 stderr 重定向到 stdout,默认 false
stdout_logfile_maxbytes = 20MB  ; stdout 日志文件大小,默认 50MB
stdout_logfile_backups = 5     ; stdout 日志文件备份数
; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
stdout_logfile = /etc/supervisord.conf.d/App_name.log
stopwaitsecs=2
复制代码

4.3 启动supervisord

$service supervisord start

#重载配置要用update ,不要用reload,否则原应用服务会中止。
supervisorctl update  (用此命令)
复制代码

说明: supervisord重载配置要用update ,不要用reload!

4.4 查看进程状态

查看进程状态,如下:

$supervisorctl status
App_name:App_name_01                   RUNNING   pid 11876, uptime 0:00:24
App_name:App_name_02                   RUNNING   pid 11878, uptime 0:00:24
App_name:App_name_03                   RUNNING   pid 11877, uptime 0:00:24
App_name:App_name_04                   RUNNING   pid 11879, uptime 0:00:24


复制代码

5. 开机自启动

1)创建/etc/init.d/supervisord文件

#创建/etc/init.d/supervisord文件
vim  /etc/init.d/supervisord
#!/bin/sh  
#  
# /etc/rc.d/init.d/supervisord  
#  
# Supervisor is a client/server system that  
# allows its users to monitor and control a  
# number of processes on UNIX-like operating  
# systems.  
#  
# chkconfig: - 64 36  
# description: Supervisor Server  
# processname: supervisord  
  
# Source init functions  
. /etc/init.d/functions  
  
RETVAL=0  
prog="supervisord"  
pidfile="/tmp/supervisord.pid"  
lockfile="/var/lock/subsys/supervisord"  
  
start()  
{  
        echo -n $"Starting $prog: "  
        daemon --pidfile $pidfile /usr/local/python-2.7/bin/supervisord -c /etc/supervisord.conf  
        RETVAL=$?  
        echo  
        [ $RETVAL -eq 0 ] && touch ${lockfile}  
}  
  
stop()  
{  
        echo -n $"Shutting down $prog: "  
        killproc -p ${pidfile} /usr/bin/supervisord  
        RETVAL=$?  
        echo  
        if [ $RETVAL -eq 0 ] ; then  
                rm -f ${lockfile} ${pidfile}  
        fi  
}  
  
case "$1" in  
  
  start)  
    start  
  ;;  
  
  stop)  
    stop  
  ;;  
  
  status)  
        status $prog  
  ;;  
  
  restart)  
    stop  
    start  
  ;;  
  
  *)  
    echo "Usage: $0 {start|stop|restart|status}"  
  ;;  
  
esac  
复制代码

2)设置为服务,如下的方法

chmod +x /etc/init.d/supervisord
chkconfig --add supervisord
chkconfig supervisord on
复制代码

3)执行命令

# Start the service
service supervisord start
# Stop the service
service supervisord stop
# Restart the service
service supervisord restart

6.配置详解

supervisord.conf是一些默认配置,可自行修改:

      [unix_http_server]
      file=/tmp/supervisor.sock   ;UNIX socket 文件,supervisorctl 会使用
      ;chmod=0700 ;socket文件的mode,默认是0700
      ;chown=nobody:nogroup ;socket文件的owner,格式:uid:gid
      ;[inet_http_server] ;HTTP服务器,提供web管理界面
      ;port=127.0.0.1:9001 ;Web管理后台运行的IP和端口,如果开放到公网,需要注意安全性
      ;username=user ;登录管理后台的用户名
      ;password=123 ;登录管理后台的密码
      [supervisord]
      logfile=/tmp/supervisord.log ;日志文件,默认是 $CWD/supervisord.log
      logfile_maxbytes=50MB ;日志文件大小,超出会rotate,默认 50MB,如果设成0,表示不限制大小
      logfile_backups=10 ;日志文件保留备份数量默认10,设为0表示不备份
      loglevel=info ;日志级别,默认info,其它: debug,warn,trace
      pidfile=/tmp/supervisord.pid ;pid 文件
      nodaemon=false ;是否在前台启动,默认是false,即以 daemon 的方式启动
      minfds=1024 ;可以打开的文件描述符的最小值,默认 1024
      minprocs=200 ;可以打开的进程数的最小值,默认 200
      [supervisorctl]
      serverurl=unix:///tmp/supervisor.sock ;通过UNIX socket连接supervisord,路径与unix_http_server部分的file一致
      ;serverurl=http://127.0.0.1:9001 ; 通过HTTP的方式连接supervisord
      ; [program:xx]是被管理的进程配置参数,xx是进程的名称
      [program:xx]
      command=/opt/apache-tomcat-8.0.35/bin/catalina.sh run  ; 程序启动命令
      autostart=true ; 在supervisord启动的时候也自动启动
      startsecs=10 ; 启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒
      autorestart=true ; 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
      startretries=3 ; 启动失败自动重试次数,默认是3
      user=tomcat ; 用哪个用户启动进程,默认是root
      priority=999 ; 进程启动优先级,默认999,值小的优先启动
      redirect_stderr=true ; 把stderr重定向到stdout,默认false
      stdout_logfile_maxbytes=20MB  ; stdout 日志文件大小,默认50MB
      stdout_logfile_backups = 20   ; stdout 日志文件备份数,默认是10
      ; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
      stdout_logfile=/opt/apache-tomcat-8.0.35/logs/catalina.out
      stopasgroup=false ;默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
      killasgroup=false ;默认为false,向进程组发送kill信号,包括子进程
      ;包含其它配置文件
      [include]
      files = relative/directory/*.ini ;可以指定一个或多个以.ini结束的配置文件
  
 

注意:[include]默认配置是制定.ini,习惯命名为.conf文件,因此修改配置如下:

      [include]
      files = relative/directory/*.conf
  
 

supervisord.d目录用来存放用户自定义的进程配置,参考实例一:

      [program:es]
      command=/opt/software/elasticsearch/bin/elasticsearch
      user=es
      stdout_logfile=/opt/supervisor_test/run.log
      autostart=true
      autorestart=true
      startsecs=60
      stopasgroup=true
      ikillasgroup=true
      startretries=1
      redirect_stderr=true
  
 

参考实例二:

vim /etc/supervisord.d/rdkit_postgres.conf

      [program:postgresql]
      command=[conda folder]/envs/my-rdkit-env/bin/postgres -D /folder/where/data/should/be/stored
      user=[your username]
      autorestart=true
  
 

服务段启动

supervisord -c /etc/supervisord.conf
 

注意: supervisor不能监控后台进程,command 不能为后台运行命令

常用命令介绍

supervisorctl 是 supervisord的命令行客户端工具

      supervisorctl status:查看所有进程的状态
      supervisorctl stop es:停止es
      supervisorctl start es:启动es
      supervisorctl restart es: 重启es
      supervisorctl update :配置文件修改后可以使用该命令加载新的配置
      supervisorctl reload: 重新启动配置中的所有程序
  
 

直接输入:supervisorctl 进入supervisorctl 的shell交互界面,上面的命令不带supervisorctl 可直接使用

posted @ 2022-03-23 15:35  农夫运维  阅读(254)  评论(0)    收藏  举报