Skip to main content

Troubleshooting

How to Troubleshoot a Kubernetes Deployment and Service

The DevOpscube team is deploying an NGINX web application, but users can't access it through the Kubernetes Service.

A Deployment and Service manifest has been provided, but the configuration contains errors affecting how the Service routes traffic to the application.

nginx.yaml manifest:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: web
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx-deployment
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8080

Your task is to deploy the provided manifest and troubleshoot the configuration so that the NGINX application is correctly exposed through the Service named nginx-service.

The Deployment must run successfully, and the Service must correctly route traffic to the NGINX Pod on the appropriate port.

After correcting the configuration:

  • Verify that the Deployment and Pod are running.
  • Verify that the Service has the correct endpoints.
  • Confirm that the Service can successfully serve the NGINX homepage.
  • Use curl to access the Service.
  • Save the curl response to /tmp/index.html.
  • Verify that /tmp/index.html contains the NGINX homepage HTML content.
📚
For more details about Deployment and Service, refer to the official Kubernetes documentation.
🙅‍♀️
Do not create a new configuration from scratch.

Identify the Issues

The provided manifest has three main issues:

nginx.yaml before
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: web spec: replicas: 1 selector: matchLabels: app: web template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: nginx-service spec: selector: app: nginx-deployment ports: - protocol: TCP port: 80 targetPort: 8080
nginx.yaml after
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: web spec: replicas: 1 selector: matchLabels: app: web template: metadata: labels: app: web spec: containers: - name: nginx image: nginx ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: nginx-service spec: selector: app: web ports: - protocol: TCP port: 80 targetPort: 80

Line 15: Deployment Selector and Pod Labels Do Not Match

The Deployment selector expects app: web, but the Pod template uses app: nginx. These values must match so the Deployment can correctly identify and manage its Pods.

Line 29: Service Selector Does Not Match the Pod Labels

The Service uses app: nginx-deployment, but no Pods have this label. As a result, the Service has no matching backend Pods. Change the Service selector to app: web to match the Pod labels.

Line30: targetPort Is Incorrect

The NGINX container listens on port 80, but the Service currently targets port 8080. Update the Service's targetPort to 80 so traffic is forwarded to the correct container port.

Apply the Manifest

Save the corrected configuration as nginx.yaml and apply it:

$ kubectl apply -f nginx.yaml

deployment.apps/nginx-deployment created
service/nginx-service configured

Verify the Deployment and Service:

$ kubectl get deployment

NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   1/1     1            1           19s

$ kubectl get pods

NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-7b45cc55bc-8fcmv   1/1     Running   0          66s

$ kubectl get service nginx-service

NAME            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
nginx-service   ClusterIP   10.111.249.141   <none>        80/TCP     66s

You can also verify that the Service has selected the Nginx Pod:

$ kubectl get endpoints nginx-service

NAME            ENDPOINTS           AGE
nginx-service   10.244.140.110:80   66s

The Service should have an endpoint pointing to the Nginx Pod's IP on port 80.

Test the Service

Use an existing NGINX Pod to send a request to the Service and save the response:

$ kubectl exec nginx-deployment-79d9f568b9-ckxrx -- \
  curl http://10.99.226.164 > /tmp/index.html

Replace the Pod name and Service IP with the values from your cluster.

The curl command runs inside the Pod, but the > /tmp/index.html redirection happens on the machine where you run kubectl.

So, /tmp/index.html is created locally, not inside the Pod.

Verify the Nginx HTML

Check the file:

$ cat /tmp/index.html 

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<a href="https://f5.com/nginx">f5.com/nginx</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

You should see the Nginx welcome page HTML, confirming that the Service successfully routed the request to the Nginx Pod.

CKA Exam Tips

These tips will help you quickly identify key Kubernetes concepts and avoid common mistakes during the CKA exam's Deployment and Service Troubleshooting

1. Deployment Selector and Pod Labels Do Not Match

The Deployment selector expects app: web, but the Pod template uses app: nginx. These values must match so the Deployment can correctly identify and manage its Pods.

2. Kubernetes Service selector Does Not Match the Pod Labels

The Service uses app: nginx-deployment, but no Pods have this label. As a result, the Service has no matching backend Pods. Change the Kubernetes Service selector to app: web to match the Pod labels.

3. targetPort Is Incorrect

The NGINX container listens on port 80, but the Service currently targets port 8080. Update the Service's targetPort to 80 so traffic is forwarded to the correct container port.