Experience docker container health quickly

Keywords: Java Docker snapshot shell

The purpose of this article is to experience the health check function of docker container. Experience is the main function, and development is not involved. The content related to development will be detailed in the following article.

About container health check

Consider this situation: in the docker environment, the container of spring boot application is still there, but it can no longer provide services (for example, data or files are damaged, resources such as thread pool are exhausted and other exceptions). At this time, a way to quickly know this state is needed.
At this point, the container health check (i.e. HEALTHCHECK) comes into use. As long as the container provides its own status information according to the rules of Docker, it can inform the outside world of the container health information in various ways.

Version requirements

The official document of docker indicates that the HEALTHCHECK function is available from version 1.12. Here is a brief introduction to the version number of docker community version:

  1. Version 1.12 was released on July 28, 2016;
  2. Version 1.13.1 was released on February 8, 2017. After this version, the naming rules of docker version changed to "YY.MM" format;
  3. 17.03.0-ce version was released on March 1, 2017, and the version naming of "YY.MM" format has been started since then;
  4. Today's actual docker environment is version 19.03.2.

    Actual combat environment information

  5. Operating system: macOS Catalina 10.15
  6. Docker: 19.03.2

    Start experiencing

  7. Create a container with health check information by entering the following command in the console:
docker run --rm \
--name=healthcheck \
-p 8080:8080 \
--health-cmd="curl --silent --fail localhost:8080/getstate || exit 1" \
--health-interval=15s \
--health-retries=10 \
--health-timeout=5s \
bolingcavalry/dockerhealthcheck:0.0.1-SNAPSHOT
  1. There are four health check related parameters in the above command, which are explained here:
Parameter name Effect
health-cmd Specifies that the command is executed within the container to check the health status of the container
health-interval Interval time of each health check, 30 seconds by default
health-retries Assuming that the value is 3, it means that if the returned results of three consecutive tests are unhealthy, the container is determined to be unhealthy. The default value is 3.
health-timeout Timeout, 30 seconds by default
  1. For the health CMD parameter, the most commonly used is the shell command. For example, in this example, curl -- silent -- fail localhost: 8080 / getstate | Exit 1, which means to send an http request to port 8080 of the container. If the http response code is 200, the return value of the whole shell is 0. At this time, the docker determines that the container is healthy. If the http response code is not 200, the return value of the shell is 1. It is judged as unhealthy by docker.
  2. Open another console window, execute docker ps to view the container STATUS, and pay attention to the STATUS field. It can be seen that when the container is first created, it is in health: starting STATUS, which will change to health STATUS later:
(base) zhaoqindeMacBook-Pro:~ zhaoqin$ docker ps
CONTAINER ID        IMAGE                                            COMMAND                  CREATED             STATUS                             PORTS               NAMES
d86c11321cef        bolingcavalry/dockerhealthcheck:0.0.1-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             STATUS                    PORTS               NAMES
d86c11321cef        bolingcavalry/dockerhealthcheck:0.0.1-SNAPSHOT   "java -Xms1g -Xmx1g ..."   17 seconds ago      Up 16 seconds (healthy)   8080/tcp            healthcheck
  1. The image of this actual combat provides the http interface localhost:8080/getstate, which is used to return the container state. Each time it is called, a line of information will be printed on the console. The container log is as follows:
2019-10-20 03:05:02.350  INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2019-10-20 03:05:02.364  INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 14 ms
2019-10-20 03:05:02.384  INFO 1 --- [nio-8080-exec-1] c.b.d.DockerhealthcheckApplication       : step probe return success
2019-10-20 03:05:17.584  INFO 1 --- [nio-8080-exec-2] c.b.d.DockerhealthcheckApplication       : step probe return success
2019-10-20 03:05:32.748  INFO 1 --- [nio-8080-exec-3] c.b.d.DockerhealthcheckApplication       : step probe return success

It can be seen that after the container is started, the interface will be called once every 15 seconds.

Simulate unhealthy state

  1. In the previous operation, we know that as long as the return code of the container's http interface localhost:8080/getstate is 200, the container will be judged as healthy;
  2. Want to see the unhealthy state, as long as the return code of http interface localhost:8080/getstate is not 200;
  3. This image provides another interface to facilitate the observation of unhealthy status. Suppose the IP address of the host is 102.168.0.3, and enter 192.168.0.3:8080/setstate?state=false in the browser. After the interface is called, the return code of localhost:8080/getstate changes from 200 to 403.
  4. Go to the console information of the container again. This time, the content has changed from step probe return success to step probe return fail. At this time, the return code of the getstate interface is 403:
2019-10-20 03:38:51.428  INFO 1 --- [nio-8080-exec-3] c.b.d.DockerhealthcheckApplication       : step probe return success
2019-10-20 03:39:06.592  INFO 1 --- [nio-8080-exec-9] c.b.d.DockerhealthcheckApplication       : step probe return fail
2019-10-20 03:39:21.757  INFO 1 --- [io-8080-exec-10] c.b.d.DockerhealthcheckApplication       : step probe return fail
2019-10-20 03:39:36.912  INFO 1 --- [nio-8080-exec-3] c.b.d.DockerhealthcheckApplication       : step probe return fail
  1. The previous value of the health returns parameter when creating a container is 10, which means that the localhost:8080/getstate will be judged as unhealthy only if the return code is not 200 for 10 consecutive times. Therefore, before the console outputs the step probe return failure for 10 consecutive times, execute the docker ps command to observe the container state. It should be healthy. After the step probe return failure is output for more than 10 times, go to see the container. The state becomes health:
(base) zhaoqindeMacBook-Pro:~ zhaoqin$ docker ps
CONTAINER ID        IMAGE                                            COMMAND                  CREATED             STATUS                      PORTS                    NAMES
070e56cc99f2        bolingcavalry/dockerhealthcheck:0.0.1-SNAPSHOT   "java -Xms1g -Xmx1g ..."   18 minutes ago      Up 18 minutes (unhealthy)   0.0.0.0:8080->8080/tcp   healthcheck
  1. Restore health status: enter 192.168.0.3:8080/setstate?state=true in the browser, so the return code of localhost:8080/getstate interface becomes 200 again. Observe the console, as long as "step probe return success" is output once, the health status of the container will be restored to health;

    Observe container events

  2. Enter docker events -- filter event = health_statusin the console to observe all container health events on the host;
  3. According to the above operation, enter 192.168.0.3:8080/setstate?state=true or 192.168.0.3:8080/setstate?state=false in the browser to change the health state of the container several times, and you can observe the change of the container event:
(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-SNAPSHOT, name=healthcheck)
2019-10-20T12:20:19.030857534+08:00 container health_status: healthy 2d538f8752ae1e94ce23f34b7fb71c8f2ea3a075df82943ffdbe62c49ad4d6c8 (image=bolingcavalry/dockerhealthcheck:0.0.1-SNAPSHOT, name=healthcheck)

At this point, the docker container health experience is completed, and we have a basic understanding of this function. Next, in the actual combat, we will try to make our application container support the health check function;

This article is based on the platform of blog one article multiple sending OpenWrite Release!

Posted by NFClimber on Sat, 19 Oct 2019 22:44:16 -0700