Your First Kubernetes Deployment and Service

Kubernetes’ first hour is confusing because almost every tutorial jumps straight to Helm. This one stays concrete: a container image, a Deployment, a Service, and enough kubectl to see what is actually happening.

The reason people bounce off Kubernetes early is that they are handed a chart before they understand the objects the chart is templating. You end up copying YAML you cannot read. So we will do the opposite. We will write two small manifests by hand, apply them to a throwaway local cluster, reach the app, update it, watch the rollout, and roll it back. By the end you will understand the moving parts, not just the incantation.

A local cluster you can throw away

You want a cluster you can break without consequence. kind (Kubernetes IN Docker) runs a full cluster inside Docker containers on your laptop, which is ideal for learning. You need Docker running, plus the kind and kubectl binaries on your PATH. On most systems these are single downloaded binaries rather than a package-managed install — the same tidy-your-tooling instinct that applies to installing Python CLI tools sanely applies here too.

Free · 4 minutes

Is your engineering team shipping safely, or quietly accumulating risk?

Fourteen questions on how work gets from idea to production — cadence, testing, rollback, and the key-person risk in your delivery. Banded finding on screen, full sheet by email.

# Create a single-node cluster called 'demo'
kind create cluster --name demo

# Confirm kubectl is talking to it
kubectl cluster-info --context kind-demo
kubectl get nodes

You should see one node in Ready state. That is your cluster. When you are finished, kind delete cluster --name demo removes every trace of it.

The Deployment: what runs, and how many

A Deployment describes a desired state: which container image to run, how many identical copies (replicas), and how to label them. Kubernetes then works continuously to make reality match that description. If a pod dies, the Deployment’s controller starts another. That self-healing loop is the whole point — you declare the end state, not the steps.

Save this as deployment.yaml. It runs two replicas of a stock nginx image.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
  labels:
    app: web
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: web
          image: nginx:1.27
          ports:
            - containerPort: 80

Three things earn their keep here. The selector.matchLabels tells the Deployment which pods it owns — it must match the labels under template.metadata.labels, or the manifest is rejected. The template is the pod blueprint; every replica is stamped from it. The containerPort is documentation of what the process listens on inside the pod; it does not publish anything by itself. Apply it:

kubectl apply -f deployment.yaml
kubectl get deployment web
kubectl get pods -l app=web

You should see two pods moving to Running. If one is stuck in Pending or ImagePullBackOff, do not guess — ask the object what is wrong: kubectl describe pod <name> prints an events log at the bottom, and kubectl logs <name> shows the container’s own output. Reading status is the skill; the manifests are the easy part.

The Service: a stable address for moving pods

Pods are cattle. They get replaced, and each new one gets a new IP address. Nothing should ever talk to a pod IP directly. A Service is a stable virtual address and load balancer that sits in front of whichever pods currently match its selector. Save this as service.yaml.

apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 80

The selector is the join: this Service routes to any pod labelled app: web, which is exactly what our Deployment produces. port is what the Service exposes; targetPort is the port on the pod it forwards to. With no type set, you get the default, ClusterIP — reachable from inside the cluster only. That is the right default for an internal API. Apply and inspect:

kubectl apply -f service.yaml
kubectl get service web
kubectl get endpoints web   # should list two pod IPs

Those endpoints are the proof the selector matched. An empty endpoints list means the Service and the pods disagree on labels — the single most common beginner failure.

Reaching the app

Because the Service is ClusterIP, you need a tunnel from your laptop into the cluster. kubectl port-forward gives you one without changing the Service type or exposing anything publicly.

# Map localhost:8080 to the Service's port 80
kubectl port-forward service/web 8080:80

# In another terminal
curl http://localhost:8080

You should get the nginx welcome HTML. That is a container image, running under a Deployment, fronted by a Service, reachable from your machine — the full path, with nothing hidden in a chart.

Rolling out an update, and rolling it back

Now change the running version and watch Kubernetes do the work. A Deployment performs a rolling update by default: it brings up new pods and drains old ones gradually, so the Service never has zero healthy endpoints.

# Change the image to a newer nginx and watch the rollout
kubectl set image deployment/web web=nginx:1.28
kubectl rollout status deployment/web

# See the revision history
kubectl rollout history deployment/web

The rollout status command blocks until the new pods are ready, then returns success — which makes it a natural gate in a deployment script. If the new version were broken, you would reverse it in one command:

# Undo the last rollout
kubectl rollout undo deployment/web
kubectl rollout status deployment/web

Kubernetes keeps prior ReplicaSets, so a rollback is just re-pointing at the previous one. Note that the older --record flag for capturing the change cause is deprecated; if you want a readable history, set the kubernetes.io/change-cause annotation on the Deployment instead.

What you have actually learned

Four objects and one loop. A Deployment declares desired state and heals toward it. A Service gives that fleet a stable address and load-balances across it. Labels and selectors are the glue that binds the two, and reading pod status is how you debug when the glue slips. Everything more advanced — Ingress, autoscaling, and yes, Helm — is built on these primitives, not a replacement for understanding them.

The next questions follow naturally once you have more than one Service. How do they authenticate to each other, given that a ClusterIP is reachable by anything in the cluster? That is where mutual TLS between internal services earns its place. And before either service ships, the contract between them is worth pinning down — spec-first API development keeps that discipline honest. Learn the objects first, and the rest of Kubernetes stops looking like magic and starts looking like plumbing.

Free interactive tool

Website compliance checklist

What your site has to do, based on what it actually does

Answer as much or as little as you like — the list builds as you go. Nothing is stored against your name and no email is required.

Most technology problems are not technology problems. They are control problems.

The systems exist. The investment has been made. The question is whether leadership can understand, direct, evidence, and sustain what those systems produce. Find out where control exists — and where it only appears to.