DEV Community

Yusuf Adeyemo
Yusuf Adeyemo

Posted on • Originally published at blog.yusadolat.me on

Observability in Kubernetes: Understanding Readiness Probes with Examples

Readiness Probes with Examples

Kubernetes is an open-source platform that automates the deployment, scaling, and management of containerized applications. In a production environment, it's crucial to ensure that your applications are running smoothly and are available to handle incoming requests. That's where the concept of observability comes into play.

Readiness probes are one of the key components of observability in Kubernetes, providing valuable information about the health and availability of your applications. They help to solve the problem of ensuring that only healthy containers receive traffic, and provide several advantages for your applications and infrastructure.

The Problem: Is my container ready to start accepting traffic?

In a large-scale production environment, it's critical to ensure that containers are healthy and ready to handle incoming requests. Without the proper tools and processes in place, it's difficult to determine the health of individual containers, leading to potential downtime and a negative impact on your users.

The Solution: Readiness Probes

Readiness probes in Kubernetes provide a solution to the problem of determining container health. They allow you to monitor the health of your containers in real time and determine if they're ready to handle incoming requests. Readiness probes are executed at regular intervals to ensure that containers are healthy and available, and if they fail, the Kubernetes scheduler will automatically stop sending traffic to that container.

What you get: Improved Reliability and Performance

By implementing readiness probes in your Kubernetes infrastructure, you can enjoy several advantages, including improved reliability and performance. With the ability to monitor the health of your containers in real-time, you can quickly identify and resolve any issues that may arise, reducing downtime and improving the overall availability of your applications. Additionally, by only sending traffic to healthy containers, you can improve the performance of your applications and provide a better experience for your users.

Setting up Readiness Probes in Kubernetes

To set up readiness probes in Kubernetes, you'll need to define a readiness probe as part of your container spec in the deployment configuration file. There are three types of probes you can use: HTTP, TCP, and Command.

Here's an example of how to set up an HTTP readiness probe in a Kubernetes deployment file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app-container
        image: my-app-image
        ports:
        - containerPort: 3000
        readinessProbe:
          httpGet:
            path: /ping
            port: 8080
          initialDelaySeconds: 5
          periodSeconds: 10

Enter fullscreen mode Exit fullscreen mode

In this example, the readiness probe is an HTTP GET request to the /ping endpoint on port 3000. The probe will be executed every 10 seconds, with an initial delay of 5 seconds.

Here's an example of how to set up a TCP readiness probe in a Kubernetes deployment file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app-container
        image: my-app-image
        ports:
        - containerPort: 3000
        readinessProbe:
          tcpSocket:
            port: 3000
          initialDelaySeconds: 5
          periodSeconds: 10

Enter fullscreen mode Exit fullscreen mode

In this example, the readiness probe is a TCP connection to port 3000. The probe will be executed every 10 seconds, with an initial delay of 5 seconds.

Here's an example of how to set up a Command readiness probe in a Kubernetes deployment file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app-container
        image: my-app-image
        ports:
        - containerPort: 3000
        readinessProbe:
          exec:
            command:
            - /bin/sh
            - -c
            - "curl -f http://localhost:3000/ping || exit 1"
          initialDelaySeconds: 5
          periodSeconds: 10

Enter fullscreen mode Exit fullscreen mode

In this example, the readiness probe is a shell script that uses curl to make an HTTP request to the /ping endpoint on port 3000. The probe will be executed every 10 seconds, with an initial delay of 5 seconds.

If the curl command returns a non-zero exit code, it means the probe failed and the container is considered not ready.

Conclusion

Readiness probes are a crucial component of observability in Kubernetes, providing valuable information about the health and availability of your containers. By implementing readiness probes, you can improve the reliability and performance of your applications, ensuring that they are always available to handle incoming requests.

Top comments (0)