如何将Supervisor 4.0.4从Python 2切换至Python 3?
Supervisor 从 Python 2 切换到 Python 3 的低停机方案
以下是几种可行的切换方案,按停机时间从短到长排序:
方案一:并行部署+平滑迁移(几乎零停机)
适合对服务可用性要求极高的场景,全程仅单个进程重启,无全局停机:
- 用 Python 3 环境安装同版本 Supervisor,避免版本兼容问题:
pip3 install supervisor==4.0.4 - 复制现有配置文件并修改冲突路径(避免与 Python 2 实例互斥):
cp /etc/supervisor/supervisord.conf /etc/supervisord_py3.conf # 编辑新配置,修改以下字段: # pidfile=/var/run/supervisord_py3.pid # logfile=/var/log/supervisord_py3.log # [unix_http_server] socket=/var/run/supervisor_py3.sock - 启动 Python 3 版本的 Supervisor 实例:
python3 -m supervisord -c /etc/supervisord_py3.conf - 逐个迁移管理的进程:
- 在旧实例停止目标进程:
supervisorctl stop <process_name> - 复制进程配置到新实例的 conf.d 目录:
cp /etc/supervisor/conf.d/<process>.conf /etc/supervisor_py3/conf.d/(目录不存在则创建) - 新实例加载配置并启动进程:
python3 -m supervisorctl -c /etc/supervisord_py3.conf reread python3 -m supervisorctl -c /etc/supervisord_py3.conf start <process_name> - 验证进程正常后,继续迁移下一个
- 在旧实例停止目标进程:
- 所有进程迁移完成后,停止旧实例并清理:
supervisorctl shutdown pip uninstall supervisor - 可选:将新配置路径替换为原路径,更新系统服务文件(如 systemd)的
ExecStart为python3 -m supervisord -c /etc/supervisor/supervisord.conf,确保开机启动指向 Python 3 版本
方案二:原地替换(短停机)
适合对停机时间容忍度较高(仅需所有进程重启的时间)的场景,操作更简洁:
- 备份配置与进程状态:
cp -r /etc/supervisor /etc/supervisor_backup supervisorctl status > supervisor_status_backup.txt - 停止 Python 2 版本的 Supervisor:
supervisorctl shutdown - 卸载旧版本:
pip uninstall supervisor - 用 Python 3 安装同版本 Supervisor:
pip3 install supervisor==4.0.4 - 检查进程配置文件,若进程依赖
python命令,需改为python3(确保进程运行在 Python 3 环境) - 启动新实例并验证:
supervisord -c /etc/supervisor/supervisord.conf supervisorctl status
方案三:虚拟环境隔离(干净切换)
适合希望隔离 Supervisor 运行环境,避免污染系统 Python 的场景:
- 创建 Python 3 虚拟环境:
python3 -m venv /opt/supervisor_py3 - 激活虚拟环境并安装 Supervisor:
source /opt/supervisor_py3/bin/activate pip install supervisor==4.0.4 deactivate - 复制并修改配置文件(同方案一,修改 pid、log、socket 路径)
- 启动虚拟环境中的 Supervisor:
/opt/supervisor_py3/bin/supervisord -c /etc/supervisord_py3.conf - 按方案一的步骤迁移所有进程,完成后停止旧实例并清理
- 更新系统服务文件,将
ExecStart指向虚拟环境的 supervisord 路径(/opt/supervisor_py3/bin/supervisord -c /etc/supervisor/supervisord.conf)
内容的提问来源于stack exchange,提问作者MW_DEVEL
相关产品推荐
相关产品推荐

