Docker 的健康检测

通常我们为了防止容器断电或异常关闭后不能自动开机,我们可以加上

--restart=always 

例如

[root@aliyun ~]# docker run --restart=always -d --name blog -d -v /www:/www -v /wwwlogs:/var/log/wwwlogs -p 65423:65422 -p 80:80 -p 443:443 677
7714a84063ee6d405c80b891254bba0e5930f5d271c5ad76cfd6e2f0058d8056

这样容器就可以自动重启,但是有时候程序进入死锁状态,或者死循环状态,应用进程并不退出,但是该容器已经无法提供服务了。在 1.12 以前,Docker 不会检测到容器的这种状态,从而不会重新调度,导致可能会有部分容器已经无法提供服务了却还在接受用户请求。

而自 1.12 之后,Docker 提供了 HEALTHCHECK 指令,通过该指令指定一行命令,用这行命令来判断容器主进程的服务状态是否还正常,从而比较真实的反应容器实际状态。

当在一个镜像指定了 HEALTHCHECK 指令后,用其启动容器,初始状态会为 starting,在 HEALTHCHECK 指令检查成功后变为 healthy,如果连续一定次数失败,则会变为 unhealthy。

HEALTHCHECK 支持下列选项:

  • –interval=<间隔>:两次健康检查的间隔,默认为 30 秒;
  • –timeout=<时长>:健康检查命令运行超时时间,如果超过这个时间,本次健康检查就被视为失败,默认 30 秒;
  • –retries=<次数>:当连续失败指定次数后,则将容器状态视为 unhealthy,默认 3 次。
    和 CMD, ENTRYPOINT 一样,HEALTHCHECK 只可以出现一次,如果写了多个,只有最后一个生效。

在 HEALTHCHECK [选项] CMD 后面的命令,格式和 ENTRYPOINT 一样,分为 shell 格式,和 exec 格式。命令的返回值决定了该次健康检查的成功与否:0:成功;1:失败;2:保留,不要使用这个值。

下面我们看下这个 dockerfile 文件

FROM centos
LABEL maintainer "awen Email: <hi@awen.me>"
WORKDIR /opt/

COPY CentOS7-Base-163.repo /etc/yum.repos.d/CentOS-Base.repo
COPY nginx /etc/init.d/nginx

ENV NGINX_V=1.13.5 \
    OPENSSL_V=1.0.2l \
    PCRE_V=8.41 \
    ZLIB_V=1.2.11 

RUN yum -y update  \
    &&  yum -y install openssh-server openssl gcc gcc-c++ pcre-devel openssl-devel zlib-devel wget make perl tar net-tools \
    &&  wget -c -4 https://nginx.org/download/nginx-$NGINX_V.tar.gz \
    &&  wget -c -4 https://www.openssl.org/source/openssl-$OPENSSL_V.tar.gz \
    &&  wget -c -4 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-$PCRE_V.tar.gz \
    &&  wget -c -4 http://zlib.net/zlib-$ZLIB_V.tar.gz \
    &&  groupadd -r www && useradd -r -g www www \
    &&  tar zxvf zlib-$ZLIB_V.tar.gz \
    &&  cd zlib-$ZLIB_V \
    &&  ./configure \
    &&  make \
    &&  make install \
    &&  cd /opt \
    &&  tar  zxvf pcre-$PCRE_V.tar.gz \
    &&  cd pcre-$PCRE_V \
    &&  ./configure \
    &&  make \
    &&  make install \
    &&  cd /opt \
    &&  tar zxvf openssl-$OPENSSL_V.tar.gz \
    &&  tar zxvf nginx-$NGINX_V.tar.gz \
    &&  cd nginx-$NGINX_V \
    &&  ./configure --prefix=/usr/local/nginx --user=www --group=www --with-pcre=/opt/pcre-$PCRE_V --with-http_ssl_module --with-zlib=/opt/zlib-$ZLIB_V --with-openssl=/opt/openssl-$OPENSSL_V --with-http_v2_module --with-http_ssl_module \
    &&  make \
    &&  make install \
    &&  rm -rf /opt/* \
    &&  mkdir -p /usr/local/nginx/ssl \
    &&  mkdir -p /usr/local/nginx/conf/vhost \
    &&  mkdir -p /var/log/wwwlogs/ \
    &&  mkdir -p /www/ \
    &&  ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N '' \
    &&  ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -N '' \
    &&  ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N '' \
    &&  ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N '' \
    &&  echo "RSAAuthentication yes" >> /etc/ssh/sshd_config \
    &&  echo "PubkeyAuthentication yes" >> /etc/ssh/sshd_config   \
    &&  sed -i "s/PasswordAuthentication yes/PasswordAuthentication no/g" /etc/ssh/sshd_config \
    &&  sed -i "s/UsePAM yes/UsePAM no/g" /etc/ssh/sshd_config \
    &&  sed -i "s/#Port 22/Port 65422/g" /etc/ssh/sshd_config \
    &&  yum clean all \
    &&  mkdir /var/run/sshd \
    &&  chmod +x /etc/init.d/nginx \
    &&  rm -rf /root/*.cfg \
    &&  echo "Asia/Shanghai" > /etc/localtime


COPY ssl/* /usr/local/nginx/ssl/ 
COPY vhost/* /usr/local/nginx/conf/vhost/
COPY nginx.conf /usr/local/nginx/conf/
COPY ssh/* /root/.ssh/


VOLUME ["/www","/var/log/wwwlogs","/usr/local/nginx/ssl","/usr/local/nginx/conf/vhost"]

EXPOSE 65422 80 443

HEALTHCHECK CMD curl -fs http://localhost/ || exit 1
ENTRYPOINT /etc/init.d/nginx start && chown -R www:www /var/log/wwwlogs/ && /usr/sbin/sshd -D

其中

HEALTHCHECK CMD curl -fs http://localhost/ || exit 1

就是增加的健康监测配置,然后编译后启动,查看进程会发现其状态是 starting

[root@aliyun ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                            PORTS                                                                NAMES
7714a84063ee        677                 "/bin/sh -c '/etc/ini"   3 seconds ago       Up 2 seconds (health: starting)   0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:65423->65422/tcp   blog

稍等查看,会发现其状态为 healthy

[root@aliyun ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                        PORTS                                                                NAMES
7714a84063ee        677                 "/bin/sh -c '/etc/ini"   About a minute ago   Up About a minute (healthy)   0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:65423->65422/tcp   blog

我们可以通过 inspect 查看 最近3次的状态

[root@aliyun ~]# docker inspect --format '{{json .State.Health}}' blog | python -m json.tool
{
    "FailingStreak": 0,
    "Log": [
        {
            "End": "2017-10-11T11:15:27.516562686+08:00",
            "ExitCode": 0,
            "Output": "<html>\r\n<head><title>301 Moved Permanently</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>301 Moved Permanently</h1></center>\r\n<hr><center>nginx</center>\r\n</body>\r\n</html>\r\n",
            "Start": "2017-10-11T11:15:27.470554485+08:00"
        },
        {
            "End": "2017-10-11T11:15:57.563377729+08:00",
            "ExitCode": 0,
            "Output": "<html>\r\n<head><title>301 Moved Permanently</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>301 Moved Permanently</h1></center>\r\n<hr><center>nginx</center>\r\n</body>\r\n</html>\r\n",
            "Start": "2017-10-11T11:15:57.516690754+08:00"
        },
        {
            "End": "2017-10-11T11:16:27.609685416+08:00",
            "ExitCode": 0,
            "Output": "<html>\r\n<head><title>301 Moved Permanently</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>301 Moved Permanently</h1></center>\r\n<hr><center>nginx</center>\r\n</body>\r\n</html>\r\n",
            "Start": "2017-10-11T11:16:27.563533362+08:00"
        },
        {
            "End": "2017-10-11T11:16:57.654441173+08:00",
            "ExitCode": 0,
            "Output": "<html>\r\n<head><title>301 Moved Permanently</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>301 Moved Permanently</h1></center>\r\n<hr><center>nginx</center>\r\n</body>\r\n</html>\r\n",
            "Start": "2017-10-11T11:16:57.609810588+08:00"
        },
        {
            "End": "2017-10-11T11:17:27.701113019+08:00",
            "ExitCode": 0,
            "Output": "<html>\r\n<head><title>301 Moved Permanently</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>301 Moved Permanently</h1></center>\r\n<hr><center>nginx</center>\r\n</body>\r\n</html>\r\n",
            "Start": "2017-10-11T11:17:27.654580727+08:00"
        }
    ],
    "Status": "healthy"
}

如果健康检查连续失败超过了重试次数,状态就会变为 (unhealthy)。