CodeBlanks Logo Icon CodeBlanks

Challenge - Intermediate project

Set up a project combining Deployment, Service and ConfigMap.

Challenge: the intermediate project

In the last challenge you'll repair a broken deployment. The api pods in the magaza namespace are in CrashLoopBackOff and the web service returns empty responses. Steps:

  1. Gather the symptoms (get, describe).
  2. Read the crashed container's log.
  3. Add the missing setting and restart the deployment.
  4. Compare the service selector with the pod labels and fix it.
$ kubectl logs -n magaza deploy/api --previous
Error: DB_HOST tanımlı değil
$ kubectl get svc web -n magaza -o jsonpath='{.spec.selector}'
{"app":"web-on"}       # the pods have app=web

Practice Diagnosis

Write three commands: list the pods in the magaza namespace; show the previous container log of deploy/api in magaza; print the selector of the web service in magaza with jsonpath='{.spec.selector}'.

Starter code

terminal
# buraya yaz

Sample solution

terminal
kubectl get pods -n magaza
kubectl logs deploy/api -n magaza --previous
kubectl get svc web -n magaza -o jsonpath='{.spec.selector}'

Expected output

NAME                   READY   STATUS             RESTARTS      AGE
api-7f8c9d6b5-abcde    0/1     CrashLoopBackOff   5 (40s ago)   6m
web-6d7e8f9a0-fghij    1/1     Running            0             6m
Error: DB_HOST tanımlı değil
{"app":"web-on"}

Practice Repair

Write three commands: apply the patch {"data":{"DB_HOST":"db"}} to the api-ayar ConfigMap in magaza; restart the api deployment in magaza; fix the selector of the web service in magaza with the patch {"spec":{"selector":{"app":"web"}}}.

Starter code

terminal
# buraya yaz

Sample solution

terminal
kubectl patch configmap api-ayar -n magaza -p '{"data":{"DB_HOST":"db"}}'
kubectl rollout restart deployment/api -n magaza
kubectl patch svc web -n magaza -p '{"spec":{"selector":{"app":"web"}}}'

Expected output

configmap/api-ayar patched
deployment.apps/api restarted
service/web patched

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 If the service selector is app=web-on and the pods have app=web, why are there "empty responses"?

  • The selector matches no pods; the service has no endpoints Correct answer
  • The pods are crashing
  • The NodePort is closed
  • DNS is broken

Explanation: The EndpointSlice appears empty.

2 Fix the service's selector.

Question
kubectl ____ svc web -n magaza -p '{"spec":{"selector":{"app":"web"}}}'
Answer
kubectl patch svc web -n magaza -p '{"spec":{"selector":{"app":"web"}}}'

Explanation: patch changes a field of the object.

3 Match.

Term Meaning
CrashLoopBackOff logs --previous
empty response Selector and EndpointSlice
missing setting Add to the ConfigMap
rollout restart Apply the new setting

All lessons in Intermediate