注:本文基于CentOS 6.6
上一篇我们介绍了使用supervisor来管理容器内的多进程,但是我们注意到supervisor只能管理到前台进程,对于一般的服务,没有终端的进程supervisor无法管理。这就需要请出我们的monit了,相对于supervisor而言,monit的功能更为强大,不仅可以管理前台、后台进程,而且还能监控文件系统,网络的资源。接下来我们就来走进monit。
直接使用yum方式安装,但是要先安装epel yum 源。
yum install -y epel-release && yum install -y monit
monit安装后,其配置文件路径为:/etc/monit.conf。我们剔除注释部分,选择最精简的配置大概了解一下monit的配置。
set daemon 30 # check services at 30 seconds intervals
set log syslog
set pidfile /var/run/monit.pid
set idfile /var/.monit.id
set statefile /var/.monit.state
set httpd port 2812 and
use address localhost # only accept connection from localhost
allow localhost # allow localhost to connect to the server and
allow admin:monit # require user 'admin' with password 'monit'
include /etc/monit.d/*
比较重要的是set daemon 这个值,它是配置monit轮训的间隔时间,这里设置为30s检查一次进程状态。然后设置日志记录,pid文件等存放路径。往后set httpd字段是用于monit WEB UI的,我们暂不使用,先不介绍。最后一个也是很重要的,将我们管理的其他进程的配置文件包含进来,和supervisor一样,可以每个进程一个单独的配置文件,但是记得千万不要重复包含monit自身的配置文件。
我们的需求还是让sshd和crond进程能够随着容器启动而拉起。
#使用CentOS 6.6官方镜像
FROM centos:6.6
#安装epel源及monit组件
RUN yum install -y epel-release && yum install -y monit
#安装ssh和cron
RUN yum install -y openssh-server openssh-clients openssh cronie
#暴露sshd使用的22号端口
EXPOSE 22
#设置密码
RUN echo "root:root" | chpasswd
#将配置文件拷贝至对应目录
COPY monit.conf /etc/monit.conf
RUN chmod 600 /etc/monit.conf
COPY sshd.conf /etc/monit.d/
COPY cron.conf /etc/monit.d/
#设置容器启动时执行的命令
ENTRYPOINT ["/usr/bin/monit", "-I"]
可见,docke镜像的打包也是很简单的,不过有几个点还是需要注意一下的,
而sshd和crond进程的配置更为简单,只要设置启动和停止命令参数即可。
sshd进程配置:
check process sshd with pidfile /var/run/sshd.pid
start program = "/etc/init.d/sshd start"
stop program = "/etc/init.d/sshd stop"
crond进程配置:
check process crond with pidfile /var/run/crond.pid
start program = "/etc/init.d/crond start"
stop program = "/etc/init.d/crond stop"
所以说,monit监控的服务无需前台运行,使用一般的服务文件启动即可。但是有一点限制就是,这个服务必须要有pid文件,如果某个服务没有pid文件,那么monit也就没办法操作了。
当然,以上配置是最简单的配置,还可以增加其他配置项,比如CPU负载,内存使用等资源监控,具体的详细配置可以参考官网手册:[https://mmonit.com/monit/documentation/monit.html#DESCRIPTION]。
使用docker build构建镜像后,就可以运行起来了,使用本地8000号端口映射容器的22号端口。
docker run -d --name ccc -h ccc -p 8000:22 aeb71970de6e
运行后查看容器状态,以及容器内进程信息
[root@localhost 6.6-monit]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
debd1f80a334 aeb71970de6e "/usr/bin/monit -I" 3 seconds ago Up 2 seconds 0.0.0.0:8000->22/tcp ccc
[root@localhost 6.6-monit]# docker top ccc
UID PID PPID C STIME TTY TIME CMD
root 25726 1529 0 10:22 ? 00:00:00 /usr/bin/monit -I
root 26048 25726 0 10:22 ? 00:00:00 /usr/sbin/sshd
root 26114 25726 0 10:22 ? 00:00:00 crond
此时容器内sshd和crond进程已经在位,最后我们看下远程登录的情况,
[root@localhost 6.6-monit]# ssh [email protected] -p 8000
The authenticity of host '[192.168.3.60]:8000 ([192.168.0.6]:8000)' can't be established.
RSA key fingerprint is d1:55:de:94:1f:4e:ed:20:7c:60:**:**:**:**:**:**.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.0.6]:8000' (RSA) to the list of known hosts.
[email protected]'s password:
[root@ccc ~]# exit
logout
Connection to 192.168.0.6 closed.
远程登录也OK,good。
参考资料:
1、https://mmonit.com/monit/documentation/monit.html#DESCRIPTION