【docker初级实战】8 - 容器健康检查机制、使用Compose配置容器的健康检查

--health-cmd:  检查的命令

--health-interval两次健康检查的间隔

--health-timeout健康检查命令运行超时时间,超过代表失败

--health-retries当连续失败指定次数后,则将容器状态视为 unhealthy

--health-start-period:  容器启动的初始化时间此时健康检查失效不会计入次数

 

docker run 的方式运行一个新容器:

docker run  --name web3 -d -p 8080:80 --privileged -v /home/sugarppig/nginx/web1/:/var/www/html/ \

--health-cmd="curl --silent --fail http://localhost:80/ || exit 1" --health-interval=3s --health-retries=3 \

--health-timeout=5s \

 centos:jdk

我们接下来可以尝试把 web3 容器里面的网站关掉。可以看到变成了 unhealthy

docker inspect web3 可以看到结果

只看 Health 部分:

docker inspect --format='{{json .State.Health}}' web3

使用 docker compose 的方式运行容器,修改 docker-compose.yaml

# 加入 
healthcheck:
      test: ["CMD", "curl","-s","-f", "http://localhost:80"]
      interval: 5s
      timeout: 5s
      retries: 3
services: 
  nginx: 
    container_name: mynginx
    image: "centos:nginx"
    healthcheck:
      test: ["CMD","curl","-s","-f","http://localhost:80"]
      interval: 5s
      timeout: 5s
      retries: 3
    ports: 
      - "9090:80"
    privileged: true
    volumes: 
      - "/home/sugarppig/nginx/conf/nginx.conf:/etc/nginx/nginx.conf"
    networks:
      mynginx-net:
        ipv4_address: ${nginx_addr}
  web1: 
    container_name: web1
    image: "centos:jdk"
    healthcheck:
      test: ["CMD","curl","-s","-f","http://localhost:80"]
      interval: 5s
      timeout: 5s
      retries: 3
    ports: 
      - "8080:80"
    privileged: true
    volumes: 
      - "/home/sugarppig/nginx/web1/:/var/www/html/"
    networks:
      mynginx-net:
        ipv4_address: ${web1_addr}
  web2: 
    container_name: web2
    image: "centos:jdk"
    healthcheck:
      test: ["CMD","curl","-s","-f","http://localhost:80"]
      interval: 5s
      timeout: 5s
      retries: 3
    ports: 
      - "8081:80"
    privileged: true
    volumes: 
      - "/home/sugarppig/nginx/web2/:/var/www/html/"
    networks:
      mynginx-net:
        ipv4_address: ${web2_addr}
networks:
  mynginx-net:
    driver: bridge
    ipam:
      config:
        - subnet: 192.156.0.0/16
version: "3"

重新构建容器

docker-compose up -d

进入 web1 容器,并停掉网站,重新查看,已经检查到 web1 unhealthy

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值