Liveness and readiness probes are mechanisms that check a container's status. A liveness probe determines if the container is running properly, while a readiness probe determines if it is ready to serve requests. This topic describes how to configure liveness and readiness probes for containers in Elastic Container Instance (ECI). These probes help ECI monitor and manage your containers' state, ensuring high service availability and stability.
Overview
ECI supports liveness probes and readiness probes to check the health and status of containers.
|
Probe |
Description |
Use cases |
|
liveness probe |
Checks if a container is running properly.
|
|
|
readiness probe |
Checks if a container is ready to accept and serve requests.
|
Use a readiness probe if an application is temporarily unable to serve traffic, for example, while loading large amounts of data or configuration files during startup. In such cases, you do not want to terminate the application, but you do want to temporarily stop sending it requests. |
API configuration
When creating an ECI instance with the CreateContainerGroup operation, you can configure probes using the LivenessProbe and ReadinessProbe parameters. For more information, see CreateContainerGroup.
LivenessProbe parameters
When you configure LivenessProbe parameters, you can specify only one check method: HttpGet, Exec, or TcpSocket.
|
Parameter |
Type |
Example |
Description |
|
Container.N.LivenessProbe.HttpGet.Path |
string |
/healthyz |
The path for the HTTP GET health check request. |
|
Container.N.LivenessProbe.HttpGet.Port |
integer |
8888 |
The port for the HTTP GET health check request. |
|
Container.N.LivenessProbe.HttpGet.Scheme |
string |
HTTP |
The protocol of the HTTP GET request. Valid values:
|
|
Container.N.LivenessProbe.Exec.Command.N |
array |
cat /tmp/healthy |
The command to run inside the container for the health check. |
|
Container.N.LivenessProbe.TcpSocket.Port |
integer |
8000 |
The port to check for the TCP socket health check. |
|
Container.N.LivenessProbe.InitialDelaySeconds |
integer |
5 |
The delay in seconds after the container starts before the first probe runs. |
|
Container.N.LivenessProbe.PeriodSeconds |
integer |
1 |
The interval at which the probe runs. Default value: 10. Minimum value: 1. Unit: seconds. |
|
Container.N.LivenessProbe.SuccessThreshold |
integer |
1 |
The number of consecutive successes required for the probe to be considered successful after a failure. This value must be 1 for liveness probes. Default: 1. |
|
Container.N.LivenessProbe.FailureThreshold |
integer |
3 |
The number of consecutive failures required for the probe to be considered failed. Default value: 3. |
|
Container.N.LivenessProbe.TimeoutSeconds |
integer |
1 |
The number of seconds after which the probe times out. Default value: 1. Minimum value: 1. Unit: seconds. |
ReadinessProbe parameters
When you configure ReadinessProbe parameters, you can specify only one check method: HttpGet, Exec, or TcpSocket.
|
Parameter |
Type |
Example |
Description |
|
Container.N.ReadinessProbe.HttpGet.Path |
string |
/healthyz |
The path for the HTTP GET health check request. |
|
Container.N.ReadinessProbe.HttpGet.Port |
integer |
8888 |
The port for the HTTP GET health check request. |
|
Container.N.ReadinessProbe.HttpGet.Scheme |
string |
HTTP |
The protocol of the HTTP GET request. Valid values:
|
|
Container.N.ReadinessProbe.Exec.Command.N |
array |
cat /tmp/healthy |
The command to run inside the container for the health check. |
|
Container.N.ReadinessProbe.TcpSocket.Port |
integer |
8000 |
The port to check for the TCP socket health check. |
|
Container.N.ReadinessProbe.InitialDelaySeconds |
integer |
5 |
The delay in seconds after the container starts before the first probe runs. |
|
Container.N.ReadinessProbe.PeriodSeconds |
integer |
1 |
The interval at which the probe runs. Default value: 10. Minimum value: 1. Unit: seconds. |
|
Container.N.ReadinessProbe.SuccessThreshold |
integer |
1 |
The minimum number of consecutive successes required for the probe to be considered successful after a failure. Default: 1. Minimum: 1. |
|
Container.N.ReadinessProbe.FailureThreshold |
integer |
3 |
The number of consecutive failures required for the probe to be considered failed. Default value: 3. |
|
Container.N.ReadinessProbe.TimeoutSeconds |
integer |
1 |
The number of seconds after which the probe times out. Default value: 1. Minimum value: 1. Unit: seconds. |
Console configuration
When you create an ECI instance in the Elastic Container Instance console, you can select a container in the Container Configurations section, expand the container's advanced settings, enable health checks, and then configure the probes.
When you configure probes in the console, only the command line and HTTP request methods are supported. The TCP socket method is not supported.
|
Parameter |
Description |
|
Timeout Period |
|
|
Method |
Select a check method.
|
|
Script |
If you select Script as the check method, you must specify the command to run in the container. |
|
HTTP Request |
If you select HTTP Request as the check method, you must specify the path, port, and protocol for the HTTP GET request. |
Example
This example shows how to create an ECI instance with a liveness probe and a readiness probe, and then simulate a service failure to see the probes in action.
-
Call the CreateContainerGroup operation to create an ECI instance.
The following example uses the TCP socket method for health checks:
ContainerGroupName=test-probe # Use the nginx image and automatically create and associate an EIP. Container.1.Name=container-1 Container.1.Image=nginx AutoCreateEip=true # Liveness probe: checks port 80 every 3s, starting 5s after launch. Success threshold: 1, failure threshold: 3, timeout: 10s. Container.1.LivenessProbe.TcpSocket.Port=80 Container.1.LivenessProbe.InitialDelaySeconds=5 Container.1.LivenessProbe.PeriodSeconds=3 Container.1.LivenessProbe.SuccessThreshold=1 Container.1.LivenessProbe.FailureThreshold=3 Container.1.LivenessProbe.TimeoutSeconds=10 # Readiness probe: checks port 80 every 3s, starting 5s after launch. Success threshold: 1, failure threshold: 3, timeout: 10s. Container.1.ReadinessProbe.TcpSocket.Port=80 Container.1.ReadinessProbe.InitialDelaySeconds=5 Container.1.ReadinessProbe.PeriodSeconds=3 Container.1.ReadinessProbe.SuccessThreshold=1 Container.1.ReadinessProbe.FailureThreshold=3 Container.1.ReadinessProbe.TimeoutSeconds=10 -
View the ECI instance events in the Elastic Container Instance console.
After the ECI instance is created, click the instance ID to open its details page. On the Events tab, you can see that the container started normally.
The event list shows four Normal events in order: Pulling image "nginx", Successfully pulled image "nginx", Created container container-1, and Started container container-1.
-
Connect to the container. For more information, see Connect to an ECI instance with Workbench.
-
Change the Nginx listening port to simulate a service failure.
-
Modify the Nginx listening port.
NoteThe Nginx image used in this example does not have
viminstalled. Before you run thevicommand, runapt-get updateandapt-get install vimto install vim.vi /etc/nginx/conf.d/default.confChange the listening port from 80 to 8080, as shown in the following example:
server { listen 8080; listen [::]:8080; server_name localhost; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } -
Reload the Nginx configuration.
nginx -s reload
-
-
Check the effect of the probes.
A few seconds after you reload Nginx, the container automatically restarts. You can view the instance events to see the container restart after the liveness and readiness probes fail.
The event list shows that the probes failed with the message
dial tcp 172.x.x.x:80: connect: connection refused, which indicates that the connection to the container port was refused. As a result, ECI automatically restarted thecontainer-1container.