Readiness probes allow k8s to probe the application running inside the container to check if it’s ready yet or not. Only after the application is ready, k8s sets the Ready condition of the container to True.

If multiple replicas of the same pods are serving traffic for an application and a new replica pod is added, if the readiness probes are set correctly, the service will wait for the application inside the new replica container to start before sending traffic to the pod.

By default, k8s sets the Ready condition on the container to True as soon as the container starts. This means that the pod will become ready to accept requests from the service as soon as the pod’s Ready condition becomes True. If the application running inside the container takes longer to start, this would cause the service to start sending requests even before the application has started, because the state of the pod (or container) is ready.

Readiness check is done at the container level in one of the following ways:

HTTP based Readiness Check

This is commonly used for containers hosting web applications. The application exposes an HTTP health check endpoint. Only if the endpoint returns a 200 status code, the container will be considered ready.

apiVersion: v1
kind: Pod
metadata:
  labels:
    name: frontend
spec:
  containers:
	  - name: webapp
	    image: webapp
			ports:
				- containerPort: 8080
			readinessProbe:
				httpGet:
					path: /api/ready
					port: 8080

TCP based Readiness Check

This is commonly used for containers hosting databases. The container’s TCP port on which the DB is exposed is checked for readiness.

apiVersion: v1
kind: Pod
metadata:
  labels:
    name: database
spec:
  containers:
	  - name: database
	    image: database
			ports:
				- containerPort: 3306
			readinessProbe:
				tcpSocket:
					port: 3306

Shell Script based Readiness Check

Run a shell script inside the container to check the readiness of the application. The return code of the shell script is used to determine the readiness of the container.

apiVersion: v1
kind: Pod
metadata:
  labels:
    name: app
spec:
  containers:
	  - name: app
	    image: app
			readinessProbe:
				exec:
					command: 
						- cat
						- /app/ready

Configuration

readinessProbe:
	httpGet:
		path: /api/ready
		port: 8080

	initialDelaySeconds: 10
	periodSeconds: 5
	failureThreshold: 5

initialDelaySeconds - start checking for readiness after some delay (when we know the application takes some time to start)

periodSeconds - readiness check interval

failureThreshold - how many times to check for readiness before declaring the status of container to failed and restart the container (default 3)