现在我们容器内运行的进程有 php-fpm、nginx、php-fpm-exporter、nginx-prometheus-exporter,为了方便的管理和添加容器内的进程,我们使用supervisor进行进程管理。最后让supervisor在前台运行
我们的phpfpm镜像是基于debian系统的,安装supervisor命令为
apt install -y supervisor
supervisor前台运行的命令为
supervisord -n -c /etc/supervisor/supervisord.conf
supervisor每个进程项目的配置文件debian系统下为 /etc/supervisor/conf.d
nginx supervisor进程管理配置文件 supervisor-nginx.conf
- [program:nginx]
- command=nginx
- autostart=true
- autorestart=true
- priority=991
- stdout_events_enabled=true
- stderr_events_enabled=true
- stdout_logfile=/dev/stdout
- stdout_logfile_maxbytes=0
- stderr_logfile=/dev/stdout
- stderr_logfile_maxbytes=0
phpfpm supervisor进程管理配置文件supervisor-phpfpm.conf
- [program:php-fpm]
- command = php-fpm
- autostart=true
- autorestart=true
- priority=990
- stdout_logfile=/dev/stdout
- stdout_logfile_maxbytes=0
- stderr_logfile=/dev/stdout
- stderr_logfile_maxbytes=0
phpfpm-exporter supervisor进程管理配置文件supervisor-phpfpm-exporter.conf
- [program:php-fpm-exporter]
- command = /usr/local/bin/php-fpm-exporter --addr="0.0.0.0:9190" --fastcgi="tcp://127.0.0.1:9000/php_status"
- autostart=false
- autorestart=false
- priority=992
- stdout_logfile=/dev/stdout
- stdout_logfile_maxbytes=0
- stderr_logfile=/dev/stdout
- stderr_logfile_maxbytes=0
phpfpm exporter默认没有自动启动,只有在需要启动的时候,通过命令让他启动
启动命令为
supervisorctl start php-fpm-exporter
nginx-exporter supervisor进程管理配置文件supervisor-nginx-exporter.conf
- [program:nginx-exporter]
- command=/usr/local/bin/nginx-prometheus-exporter -nginx.scrape-uri=http://127.0.0.1/stub_status
- autostart=false
- autorestart=false
- priority=993
- stdout_events_enabled=true
- stderr_events_enabled=true
- stdout_logfile=/dev/stdout
- stdout_logfile_maxbytes=0
- stderr_logfile=/dev/stdout
- stderr_logfile_maxbytes=0
nginx exporter默认没有自动启动,只有在需要启动的时候,通过命令让他启动
启动命令为
supervisorctl start nginx-exporter
如果这样做,supervisor会报错 phpfpm 和 nginx 必须要设置成前台启动
nginx命令为
command=nginx -g 'daemon off;'
phpfpm命令为
command = php-fpm -F
基于以上内容,我们的dockerfile文件修改为
- # 安装supervisor
- RUN apt install -y supervisor
- ADD conf/supervisor-nginx.conf /etc/supervisor/conf.d/nginx.conf
- ADD conf/supervisor-phpfpm.conf /etc/supervisor/conf.d/phpfpm.conf
- ADD conf/supervisor-phpfpm-exporter.conf /etc/supervisor/conf.d/phpfpm-exporter.conf
- ADD conf/supervisor-nginx-exporter.conf /etc/supervisor/conf.d/nginx-exporter.conf
-
- # 执行容器启动后的命令
- CMD ["supervisord", "-n", "-c", "/etc/supervisor/supervisord.conf"]