Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the health check of docker container

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to realize the health check of docker containers". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

About container health check

Consider this situation: in the docker environment, the container of the springboot application is still there, but it is no longer able to provide services (such as various exceptions such as corrupted data or files, depletion of resources such as thread pools, etc.). You need a way to know this status quickly. At this point, the container health check (that is, HEALTHCHECK) will come in handy. As long as the container provides its own status information according to the rules of Docker, it can inform the outside world the container health information in a variety of ways.

Version requirement

According to the official documentation of docker, the HEALTHCHECK feature has been available since version 1.12. Here is a brief introduction to the version number of the docker community version:

Version 1.12 was released on July 28, 2016.

Version 1.13.1 was released on February 8, 2017. After this version, the version naming convention of docker has been changed to "YY.MM" format.

The 17.03.0-ce version was released on March 1, 2017, and the naming of the version in "YY.MM" format began.

Today's actual docker environment is version 19.03.2.

Actual combat environment information

Operating system: macOS Catalina 10.15

Docker:19.03.2

Begin to experience

Enter the following command on the console to create a container with health check information:

Docker run-- rm\-- name=healthcheck\-- p 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

There are four parameters related to health check in the above command, which are explained here:

The parameter name functions as health-cmd to specify the command to be executed within the container, which is used to check the health status of the container. The interval between each health check of health-interval is 30 seconds by default. Health-retries assumes that the value is 3, which means that if the result of three consecutive tests is unhealthy, the container is judged to be unhealthy. The default is 3health-timeout timeout. The default is 30 seconds.

With regard to the health-cmd parameter, the most commonly used command is the shell command. For example, in this case, it is curl-- silent-- fail localhost:8080/getstate | | exit 1, which means to initiate a http request to port 8080 of the container. If the code of the http response is 200, the return value of the entire shell is 0, which is judged as healthy by the docker. If the http response code is not 200 shell, the return value is 1, and the container is determined to be unhealthy by docker.

Open a console window, execute docker ps to check the container status, and pay attention to the STATUS field. It can be seen that the container was created in health: starting status, and will later change to healthy status:

(base) zhaoqindeMacBook-Pro:~ zhaoqin$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESd86c11321cef 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 psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESd86c11321cef bolingcavalry/dockerhealthcheck:0.0.1-SNAPSHOT "java-Xms1g-Xmx1g …" 17 seconds ago Up 16 seconds (healthy) 8080/tcp healthcheck

The actual combat image provides the http API localhost:8080/getstate, which is used to return the container status. Each call will print a line of information on the console. The container log is as follows:

2019-10-20 03 INFO 05V 02.350 INFO 1-[nio-8080-exec-1] o.s.web.servlet.DispatcherServlet: Initializing Servlet' dispatcherServlet'2019-10-203 V 05V 05V 02.364 INFO 1-[nio-8080-exec-1] o.s.web.servlet.DispatcherServlet: Completed initialization in 14 ms2019-10-20 03V 05V 05V 02.384 INFO 1-- [nio-8080-exec-1] c. B.d.DockerhealthcheckApplication: step probe return success2019-10-20 03 INFO 05V 17.584 INFO 1-[nio-8080-exec-2] c.b.d.DockerhealthcheckApplication: step probe return success2019-10-203Vol 05V 32.748 INFO 1-[nio-8080-exec-3] c.b.d.DockerhealthcheckApplication: step probe return success

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

Simulate an unhealthy state

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 is judged to be healthy.

To see what the unhealthy status looks like, as long as the return code of the http API localhost:8080/getstate is not 200, it is OK.

This image provides another interface to easily observe the unhealthy state. Assuming that the IP address of the host is 102.168.0.3, enter 192.168.0.3:8080/setstate?state=false in the browser. After the API is called, the return code of localhost:8080/getstate will be changed from 200 to 403.

Let's take a look at the console information of the container. This time, the content has changed from step probe return success to step probe return fail, and the error code of the getstate API is 403:

2019-10-20 3 INFO 38 INFO 1-[nio-8080-exec-3] c.b.d.DockerhealthcheckApplication: step probe return success2019-10-20 03 c.b.d.DockerhealthcheckApplication 39 c.b.d.DockerhealthcheckApplication 06.592 INFO 1-[nio-8080-exec-9] c.b.d.DockerhealthcheckApplication: step probe return fail2019-10-20 3 c.b.d.DockerhealthcheckApplication 39 step probe return success2019 21.757 INFO 1-[io-8080-exec-10] c.b.d.DockerhealthcheckApplication: Step probe return fail2019-10-20 03 c.b.d.DockerhealthcheckApplication 39 nio-8080-exec-3 36.912 INFO 1-[nio-8080-exec-3] c.b.d.DockerhealthcheckApplication: step probe return fail

Previously, the value of the health-retries parameter when creating the container is 10, which means that the localhost:8080/getstate will be judged unhealthy if the return code is less than 200 for 10 times in a row. Therefore, before the console outputs step probe return fail for 10 times in a row, execute the docker ps command to observe the container status, which should still be healthy. After more than 10 step probe return fail outputs, check the container status and become healthy:

(base) zhaoqindeMacBook-Pro:~ zhaoqin$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES070e56cc99f2 bolingcavalry/dockerhealthcheck:0.0.1-SNAPSHOT "java-Xms1g-Xmx1g..." 18 minutes ago Up 18 minutes (unhealthy) 0.0.0.0 unhealthy 8080-> 8080/tcp healthcheck

Restore the health status: enter 192.168.0.3:8080/setstate?state=true in the browser, so that the return code of the localhost:8080/getstate interface becomes 200. in the observation console, as long as you output "step probe return success" once, the container health status will be restored to healthy.

Observe container events

Enter docker events-- filter event=health_status in the console to observe all container health events on the host machine.

According to the above operation, type 192.168.0.3:8080/setstate?state=true or 192.168.0.3:8080/setstate?state=false in the browser to change the health status of the container several times, and the container event changes can be observed:

(base) zhaoqindeMacBook-Pro:~ zhaoqin$ docker events-- filter event=health_status2019-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) "how to implement the health check of docker containers" is introduced here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report