Containerprobe in Kubernetes

KubernetesKubernetesIntermediate
Practice Now

Introduction

In Kubernetes, a probe is a diagnostic tool used to determine whether a container is alive and ready to accept traffic. There are two types of probes, liveness probes and readiness probes. In this lab, we will focus on ContainerProbe, which is a tool used to detect when a container is ready to accept traffic.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/TroubleshootingandDebuggingCommandsGroup(["`Troubleshooting and Debugging Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedCommandsGroup(["`Advanced Commands`"]) kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/describe("`Describe`") kubernetes/BasicCommandsGroup -.-> kubernetes/get("`Get`") kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") subgraph Lab Skills kubernetes/describe -.-> lab-12263{{"`Containerprobe in Kubernetes`"}} kubernetes/get -.-> lab-12263{{"`Containerprobe in Kubernetes`"}} kubernetes/apply -.-> lab-12263{{"`Containerprobe in Kubernetes`"}} end

Create a Deployment

The first step is to create a deployment in Kubernetes. We will use this deployment to test the ContainerProbe.

  1. Create a new file named deployment.yaml in the /home/labex/project directory.
  2. Copy and paste the following code into the file:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: containerprobe-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: containerprobe
  template:
    metadata:
      labels:
        app: containerprobe
    spec:
      containers:
        - name: containerprobe
          image: nginx
          ports:
            - containerPort: 80

This code creates a deployment with one replica, a selector with the label app: containerprobe, and a container running the nginx image.

  1. Apply the deployment to your cluster:
kubectl apply -f deployment.yaml

Add a Liveness Probe

The next step is to add a liveness probe to the nginx container. A liveness probe is used to determine if the container is alive. If the probe fails, Kubernetes will restart the container.

  1. Update the deployment.yaml in the /home/labex/project directory with the follow content:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: containerprobe-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: containerprobe
  template:
    metadata:
      labels:
        app: containerprobe
    spec:
      containers:
        - name: containerprobe
          image: nginx
          ports:
            - containerPort: 80
          livenessProbe:
            httpGet:
              path: /
              port: 80

This code specifies that the liveness probe should send an HTTP GET request to the root path on port 80.

  1. Update the deployment:
kubectl apply -f deployment.yaml

Test the Liveness Probe

Now that we have added a liveness probe, we can test it to see if it is working correctly.

  1. Get the pod name:
kubectl get pods -l app=containerprobe -o jsonpath='{.items[0].metadata.name}'

This command gets the name of the pod created by the deployment.

  1. Get the status of the liveness probe:
kubectl describe pod <pod-name>

Replace <pod-name> with the name of the pod from the previous step.

You should see output that includes the following:

Liveness: http-get http://:80/ delay=0s timeout=1s period=10s #success=1 #failure=3

This indicates that the liveness probe is configured correctly.

Add a Readiness Probe

The next step is to add a readiness probe to the nginx container. A readiness probe is used to determine if the container is ready to accept traffic. If the probe fails, Kubernetes will not send traffic to the container.

  1. Add the following code to the container definition in deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: containerprobe-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: containerprobe
  template:
    metadata:
      labels:
        app: containerprobe
    spec:
      containers:
        - name: containerprobe
          image: nginx
          ports:
            - containerPort: 80
          livenessProbe:
            httpGet:
              path: /
              port: 80
          readinessProbe:
            httpGet:
              path: /
              port: 80

This code specifies that the readiness probe should send an HTTP GET request to the root path on port 80.

  1. Update the deployment:
kubectl apply -f deployment.yaml

Test the Readiness Probe

Now that we have added a readiness probe, we can test it to see if it is working correctly.

  1. Get the pod name:
kubectl get pods -l app=containerprobe -o jsonpath='{.items[0].metadata.name}'

This command gets the name of the pod created by the deployment.

  1. Get the status of the readiness probe:
kubectl describe pod <pod-name>

Replace <pod-name> with the name of the pod from the previous step.

You should see output that includes the following:

Readiness: http-get http://:80/ delay=0s timeout=1s period=10s #success=1 #failure=3

This indicates that the readiness probe is configured correctly.

Summary

In this lab, we learned how to use ContainerProbe in Kubernetes. We created a deployment, added a liveness probe, tested the liveness probe, added a readiness probe, and tested the readiness probe. By using ContainerProbe, we can ensure that our containers are ready to accept traffic and are functioning correctly.

Other Kubernetes Tutorials you may like