Skip to the page

Kubernetes: shipping a service · core

Deployment, Service and CronJob manifests

A Deployment runs pods, a Service gives them one stable address by matching labels, and a CronJob runs a task on a schedule. Most first bugs are labels and ports that do not match.

The notes

What you must know

  • API: a Deployment with replicas, labels (app: orders, component: api), the container with args, a containerPort, readiness and liveness probes, resource requests and a memory limit. A Service selects the pods by label, with port 80 and targetPort set to the port the app listens on.
  • Worker: a second Deployment from the same image with component: worker, a different command in args, no Service, its own resources, and a terminationGracePeriodSeconds long enough to finish the current job.
  • Nightly report: a CronJob with schedule "0 2 * * *" and timeZone: "Asia/Kolkata". Without timeZone the schedule follows the controller's zone, usually UTC, so 2 am runs at the wrong time.
  • Include the Service selector in labels carefully. If the selector says app: cart-api and the pods say app: cart, the Service has no endpoints. kubectl describe svc shows Endpoints: <none>.
  • targetPort must match the port the app listens on. The liveness probe port must match as well, or the kubelet restarts the container again and again.
  • containerPort is mostly documentation. It does not open, close or map a port. Give ports names (name: http) and refer to the name in Service and probes.
  • Selectors in a Deployment are immutable after creation. Use a rolling update (maxSurge, maxUnavailable) for a safe release.
  • Check: kubectl get pods, kubectl describe svc, kubectl get endpointslices -l kubernetes.io/service-name=<name>.

The red pen

Common traps: mistake → fix

  • Mistake: containerPort: 8080 makes the app listen on 8080

    Fix: The app decides its port. containerPort is a note.

  • Mistake: A Service with the right name always works

    Fix: It routes by label selector. A wrong label means no endpoints.

  • Mistake: A cron schedule uses my local time

    Fix: It follows the controller's zone, usually UTC, unless you set timeZone.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is the relationship between a Deployment, a ReplicaSet and a Pod?
  2. How does a Service find its pods?
  3. A Service has no endpoints. How do you debug it?
  4. Why would a CronJob at 2 am run at 7:30 am Indian time?
  5. Write the manifests for an API, a queue worker and a nightly job from one image.

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview