The purpose of this article is to experience the health check function of Docker container, which is mainly experience and does not involve development. Development-related content will be detailed in the following article.

About container health check

Consider a docker environment where the springBoot application container is still there but no longer provides services (for example, data or files are corrupted, resources such as thread pools are exhausted, etc.) and you need a way to quickly know this status. This is where the container HEALTHCHECK comes in. As long as the container provides its own status information according to Docker rules, the container health information can be disclosed to the outside world in various ways.

Version for

Docker’s HEALTHCHECK feature is available as of Version 1.12. Here is a brief introduction to the docker community edition:

  1. Version 1.12 was released on July 28, 2016;
  2. Version 1.13.1 Docker was released on February 8, 2017. After this version, the version naming rule has changed to “YY.mm” format.
  3. 17.03.0-CE version was released on March 1, 2017, and the version naming format of “YY.mm” began from then on.
  4. Today’s docker environment is version 19.03.2;

Actual combat environment information

  1. Operating system: macOS Catalina 10.15
  2. Docker: 19.03.2

Start to experience

  1. To create a container with health check information, enter the following command on the console:
docker run --rm \ --name=healthcheck \ -p 8080:8080 \ --health-cmd="curl --silent --fail localhost:8080/getstate || exit 1 \ "- health - interval = 15 s \ - health - retries = 10 \ - health - timeout = 5 s \ bolingcavalry/dockerhealthcheck: 0.0.1 - the SNAPSHOTCopy the code
  1. The above command has four parameters related to health check, which are explained here:
Parameter names role
health-cmd Specifies a command to be executed in a container to check the container health status
health-interval Interval for each health check. Default value: 30 seconds
health-retries If the value is 3, the container is considered unhealthy if the returned result of three consecutive checks is unhealthy. The default value is 3
health-timeout Timeout period, 30 seconds by default
  1. About the health of -cmd parameters, the most commonly used is the shell command, such as this case is the curl, silent – fail localhost: 8080 / getstate | | exit 1, mean launched an HTTP request, the container port 8080 If the HTTP response code is 200, the return value of the whole shell is 0, and docker determines that the container is healthy. If the HTTP response code is not 200, the return value of the shell is 1, and docker determines that the container is unhealthy.
  2. Open a console window again, execute docker ps view your container STATUS, pay attention to the STATUS field, first create the container is visible health: starting state, later will become a healthy state:
(base) zhaoqindeMacBook-Pro:~ zhaoqin$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d86c11321cef Bolingcavalry/dockerhealthcheck: 0.0.1 - the SNAPSHOT "Java - Xms1g - Xmx1g..." 13 seconds ago Up 12 seconds (health: starting) 8080/tcp healthcheck (base) zhaoqindeMacBook-Pro:~ zhaoqin$ docker ps CONTAINER ID IMAGE COMMAND CREATED The STATUS PORTS NAMES d86c11321cef bolingcavalry/dockerhealthcheck: 0.0.1 - the SNAPSHOT "Java - Xms1g - Xmx1g..." 17 seconds ago Up 16 seconds (healthy) 8080/tcp healthcheckCopy the code
  1. The HTTP interface localhost:8080/ getState is used to return the container status. Each call will print a line of information on the console. The container log is as follows:
The 2019-10-20 03:05:02. 350 INFO 1 - [nio - 8080 - exec - 1] O.S.W eb. Servlet. DispatcherServlet: Initializing Servlet' dispatcherServlet' 2019-10-20 03:05:02.364 INFO 1 -- [NIO-8080-exec-1] o.s.web.servlet.DispatcherServlet : 14 ms 2019-10-20 03:05:02.384 INFO 1 -- [NIO-8080-exec-1] c.b.d.DockerhealthcheckApplication : Step the probe return success 03:05:17 2019-10-20. 584 INFO 1 - [nio - 8080 - exec - 2] C.B.D.D ockerhealthcheckApplication: Step the probe return success 03:05:32 2019-10-20. 748 INFO 1 - [nio - 8080 - exec - 3] C.B.D.D ockerhealthcheckApplication: step probe return successCopy the code

You can see that this interface is invoked every 15 seconds since the container started.

Simulate an unhealthy state

  1. As we learned from the previous operation, the container is considered healthy as long as the return code of the container’s HTTP interface localhost:8080/ getState is 200;
  2. To see what an unhealthy state looks like, the return code of the HTTP interface localhost:8080/ getState is not 200;
  3. This mirror provides another convenient interface to observe an unhealthy state, assuming that the IP address of the host machine is 102.168.0.3, the browser input 192.168.0.3:8080 / setstate? State =false, localhost:8080/ getState return code changed from 200 to 403;
  4. This time, the content has changed from Step Probe return success to Step Probe Return fail. At this time, the return code of getState interface is 403:
The 2019-10-20 03:38:51. 428 INFO 1 - [nio - 8080 - exec - 3] C.B.D.D ockerhealthcheckApplication: Step the probe return success 03:39:06 2019-10-20. 592 INFO 1 - [nio - 8080 - exec - 9] C.B.D.D ockerhealthcheckApplication: Step the probe return fail the 2019-10-20 03:39:21. 757 INFO 1 - [IO - 8080 - exec - 10] C.B.D.D ockerhealthcheckApplication: Step the probe return fail the 2019-10-20 03:39:36. 912 INFO 1 - [nio - 8080 - exec - 3] C.B.D.D ockerhealthcheckApplication: step probe return failCopy the code
  1. The value of the health-retries parameter before creating the container is 10, which means that localhost:8080/ getState is considered unhealthy only if the return code is not 200 for 10 consecutive times. Therefore, before the console sends step Probe return fail for 10 consecutive times, Execute docker ps command to observe the container state, it should still be healthy, after more than ten step probe return fail output, and then look at the container state, it will become healthy:
(base) zhaoqindeMacBook-Pro:~ zhaoqin$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 070e56cc99f2 Bolingcavalry/dockerhealthcheck: 0.0.1 - the SNAPSHOT "Java - Xms1g - Xmx1g..." 18 minutes ago Up 18 minutes (unhealthy) 0.0.0.0:8080->8080/ TCP HealthCheckCopy the code
  1. Restore health status: the browser input 192.168.0.3:8080 / setstate? State =true, so the return code of localhost:8080/ getState interface becomes 200 again. Observe the console, as long as “Step Probe return success” output once, the container health state will restore to healthy.

Observing container events

  1. Enter docker events –filter event=health_status on the console to observe all container health status events on the host.
  2. According to the operation of the above, in the browser input 192.168.0.3:8080 / setstate? The state = true or 192.168.0.3:8080 / setstate? State =false, change the health state of the container several times, observe the container event change:
(base) zhaoqindeMacBook-Pro:~ ZHAOqin $docker events --filter event=health_status 2019-10-20T12:19:18.349588676+08:00 container health_status: unhealthy 2d538f8752ae1e94ce23f34b7fb71c8f2ea3a075df82943ffdbe62c49ad4d6c8 (image = bolingcavalry/dockerhealthcheck: 0.0.1 - the SNAPSHOT, Name = healthcheck) 2019-10-20 T12: thou. 030857534 + 08:00 container health_status: healthy 2d538f8752ae1e94ce23f34b7fb71c8f2ea3a075df82943ffdbe62c49ad4d6c8 (image = bolingcavalry/dockerhealthcheck: 0.0.1 - the SNAPSHOT, name = healthcheck)Copy the code

At this point, the Docker container health experience is completed, we have a basic understanding of this function, next practice, we will try to make our application container support health check function;

Welcome to pay attention to the public number: programmer Xin Chen