Updated on 2024-11-01 GMT+08:00

Readiness Probe

After a pod is created, the Service can immediately select it and forward requests to it. However, it takes time to start a pod. If the pod is not ready (it takes time to load the configuration or data, or a preheating program may need to be executed), the pod cannot process requests, and the requests will fail.

Kubernetes solves this problem by adding a readiness probe to pods. The Service can forward requests to a pod only after the probe detects that the pod is ready.

The readiness probe periodically detects a pod and determines whether the pod is ready based on the response. Similar to Liveness Probe, CCI also supports two types of readiness probes.

  • HTTP GET: The probe sends an HTTP GET request to the container by using IP:port. If the probe receives a 2xx or 3xx status code, the container is ready.

    You need to configure the following annotation for the pod to make timeoutSeconds take effect:

    cci.io/httpget-probe-timeout-enable:"true"

    For details, see the example in Advanced Configuration of Liveness Probe.

  • Exec: The probe runs a command in the container and checks the exit status code. If the exit status code is 0, the container is ready.

Working Principles of the Readiness Probe

If you run the kubectl describe command to query the Service, information similar to the following is displayed:

$ kubectl describe svc nginx -n $namespace_name
Name:              nginx
......
Endpoints:         192.168.113.81:80,192.168.165.64:80,192.168.198.10:80
......

Endpoints is displayed, which is also a resource object in Kubernetes.

$ kubectl get endpoints -n $namespace_name
NAME         ENDPOINTS                                               AGE
nginx        192.168.113.81:80,192.168.165.64:80,192.168.198.10:80   14m

192.168.113.81:80 is the IP:port of the pod. You can run the following command to view the IP address of the pod, which is the same as the preceding IP address.

# kubectl get pods -o wide -n $namespace_name
NAME                     READY     STATUS    RESTARTS   AGE       IP                                                                             
nginx-55c54cc5c7-49chn   1/1       Running   0          1m        192.168.198.10
nginx-55c54cc5c7-x87lb   1/1       Running   0          1m        192.168.165.64
nginx-55c54cc5c7-xp4c5   1/1       Running   0          1m        192.168.113.81

Endpoints can be used as a readiness probe. When the pod is not ready, IP:port is deleted from the Endpoints and is added to the Endpoints after the pod is ready, as shown in the following figure.

Figure 1 Working principles of the readiness probe

Exec

The Exec mode is the same as the HTTP GET mode. As shown below, the probe runs the ls /ready command. If the file exists, 0 is returned, indicating that the pod is ready. Otherwise, another status code is returned.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:latest
        name: container-0
        resources:
          limits:
            cpu: 500m
            memory: 1024Mi
          requests:
            cpu: 500m
            memory: 1024Mi
        readinessProbe:      # Readiness Probe
          exec:              # Define the ls /ready command.
            command:
            - ls
            - /ready
      imagePullSecrets:
      - name: imagepull-secret

Save the definition of the Deployment to deploy-read.yaml, delete the previously created Deployment, and use deploy-read.yaml to recreate the Deployment.

# kubectl delete deploy nginx -n $namespace_name
deployment.apps "nginx" deleted

# kubectl create -f deploy-read.yaml -n $namespace_name
deployment.apps/nginx created

The nginx image does not contain the /ready file. Therefore, the container is not in Ready state after the creation, as shown below. Note that the value in the READY column is 0/1, indicating that the container is not ready.

# kubectl get po -n $namespace_name
NAME                     READY     STATUS    RESTARTS   AGE
nginx-7955fd7786-686hp   0/1       Running   0          7s
nginx-7955fd7786-9tgwq   0/1       Running   0          7s
nginx-7955fd7786-bqsbj   0/1       Running   0          7s

Check the Service again. If there are no values in the Endpoints line, no Endpoints are found.

$ kubectl describe svc nginx -n $namespace_name
Name:              nginx
......
Endpoints:         
......

If a /ready file is created in the container to make the readiness probe succeed, the container is in the Ready state. Check the pod and Endpoints. It is found that the container for which the /ready file is created is ready and an Endpoints record is added.

# kubectl exec -n $namespace_name nginx-7955fd7786-686hp -- touch /ready 

# kubectl get po -o wide -n $namespace_name
NAME                     READY     STATUS    RESTARTS   AGE       IP
nginx-7955fd7786-686hp   1/1       Running   0          10m       192.168.93.169 
nginx-7955fd7786-9tgwq   0/1       Running   0          10m       192.168.166.130
nginx-7955fd7786-bqsbj   0/1       Running   0          10m       192.168.252.160

# kubectl get endpoints -n $namespace_name
NAME       ENDPOINTS           AGE
nginx      192.168.93.169:80   14d

HTTP GET

The configuration of a readiness probe is the same as that of a liveness probe, which is also in the containers field of the pod description template. As shown below, the readiness probe sends an HTTP request to the pod. If the probe receives 2xx or 3xx, the pod is ready.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:latest
        name: container-0
        resources:
          limits:
            cpu: 500m
            memory: 1024Mi
          requests:
            cpu: 500m
            memory: 1024Mi
        readinessProbe:           # readinessProbe
          httpGet:                # HTTP GET definition
            path: /read
            port: 80
      imagePullSecrets:
      - name: imagepull-secret

Advanced Configuration of Readiness Probe

Similar to the liveness probe, the readiness probe also has the same advanced configuration items. The output of the describe command of the nginx pod is as follows:

Readiness: exec [ls /var/ready] delay=0s timeout=1s period=10s #success=1 #failure=3

This line indicates the parameter configuration of the readiness probe. The meanings of the parameters are as follows:

  • delay=0s indicates that the probe starts immediately after the container is started.
  • timeout=1s indicates that the container must respond to the probe within 1s. Otherwise, the detection fails.
  • period=10s indicates that the detection is performed every 10s.
  • #success=1 indicates that the detection is successful after succeeding once.
  • #failure=3 indicates that the container will be restarted after three consecutive detection failures.

These are set by default when the probe is created. You can also manually configure the parameters as follows:

        readinessProbe:      # Readiness Probe
          exec:              # Define the ls /readiness/ready command.
            command:
            - ls
            - /readiness/ready
          initialDelaySeconds: 10    # Readiness probes are initiated after the container has started for 10s.
          timeoutSeconds: 2          # The container must respond to the probe within 2s, or the detection fails.
          periodSeconds: 30          # The probe is performed every 30s.
          successThreshold: 1        # The container is considered ready as long as the probe succeeds once.
          failureThreshold: 3        # The probe is considered to be failed after three consecutive failures.