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.
Start the Minikube Cluster
Before creating resources, you need a running Kubernetes cluster. Minikube is a lightweight Kubernetes environment that runs on your local machine.
Navigate to your working directory:
Open the terminal and navigate to the default project folder:
cd /home/labex/projectStart Minikube:
Start Minikube to initialize a Kubernetes cluster:
minikube start- This command sets up a single-node Kubernetes cluster on your local machine.
- Minikube may take a few minutes to start depending on your system's performance.
Verify Minikube is running:
Check the status of the Minikube cluster:
minikube status- Look for components like
kubeletandapiserverlisted asRunning. - If the cluster is not running, rerun
minikube start.
- Look for components like
If you encounter issues starting Minikube. Use minikube delete to reset the environment if needed.
Create a Deployment
The first step is to create a deployment in Kubernetes. We will use this deployment to test the ContainerProbe.
- Create a new file named
deployment.yamlin the/home/labex/projectdirectory. - 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.
- 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.
- Update the
deployment.yamlin the/home/labex/projectdirectory 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.


