We will create a Pod named web-server using the nginx image and expose port 80. Use the kubectl run command in dry-run mode to generate the YAML file:
kubectl run web-server --image=nginx --port=80 --restart=Never \
-o yaml --dry-run=client > probed-pod.yamlEdit the probed-pod.yaml file to include a startup probe that performs an HTTP GET request on the root (/) path of the container:
apiVersion: v1
kind: Pod
metadata:
labels:
run: web-server
name: web-server
spec:
containers:
- image: nginx
name: web-server
ports:
- containerPort: 80
name: nginx-port
startupProbe:
httpGet:
path: /
port: nginx-port
dnsPolicy: ClusterFirst
restartPolicy: NeverModify the probed-pod.yaml to include a readiness probe. This probe ensures that the container is ready to accept traffic. It should:
- Perform an HTTP GET request on
/. - Wait 5 seconds before the first check.
readinessProbe:
httpGet:
path: /
port: nginx-port
initialDelaySeconds: 5Updated probed-pod.yaml:
apiVersion: v1
kind: Pod
metadata:
labels:
run: web-server
name: web-server
spec:
containers:
- image: nginx
name: web-server
ports:
- containerPort: 80
name: nginx-port
startupProbe:
httpGet:
path: /
port: nginx-port
readinessProbe:
httpGet:
path: /
port: nginx-port
initialDelaySeconds: 5
dnsPolicy: ClusterFirst
restartPolicy: NeverModify probed-pod.yaml to include a liveness probe. This probe ensures that the container is still running properly. It should:
- Perform an HTTP GET request on
/. - Wait 10 seconds before the first check.
- Check every 30 seconds.
livenessProbe:
httpGet:
path: /
port: nginx-port
initialDelaySeconds: 10
periodSeconds: 30Final probed-pod.yaml:
apiVersion: v1
kind: Pod
metadata:
labels:
run: web-server
name: web-server
spec:
containers:
- image: nginx
name: web-server
ports:
- containerPort: 80
name: nginx-port
startupProbe:
httpGet:
path: /
port: nginx-port
readinessProbe:
httpGet:
path: /
port: nginx-port
initialDelaySeconds: 5
livenessProbe:
httpGet:
path: /
port: nginx-port
initialDelaySeconds: 10
periodSeconds: 30
dnsPolicy: ClusterFirst
restartPolicy: Neverkubectl create -f probed-pod.yamlkubectl get pod web-serverThe output should show the container transitioning from ContainerCreating to Running.
kubectl describe pod web-serverUse the Kubernetes Metrics Server to check CPU and memory usage:
kubectl top pod web-serverWe will create a Pod named custom-cmd using the busybox image. The container will attempt to run top-analyzer --all, which does not exist in the image:
kubectl run custom-cmd --image=busybox --restart=Never \
-- /bin/sh -c "top-analyzer --all"kubectl get pod custom-cmdThe output will show the Pod in Error state.
Check the logs for failure messages:
kubectl logs custom-cmdThe output will indicate that top-analyzer is not available in the busybox image.
- We created a Pod with probes to ensure proper startup, readiness, and liveness.
- We monitored the Pod’s lifecycle and resource usage.
- We troubleshot a failed Pod due to a missing command.
This lab helps in understanding Kubernetes probes, monitoring, and debugging in real-world scenarios. 🚀