liveBook · Manning

Kubernetes Deployment

As explained in Learn Docker in a Month of Lunches, Second Edition

A Deployment is a Kubernetes API resource that manages a set of Pods through a declarative Pod template and desired replica count.

A Pod can run an application, but what replaces it if it disappears? A Kubernetes Deployment manages Pods from a template and keeps the specified number running. If a Pod is deleted or its node goes offline, the Deployment creates a replacement. The Deployment selects its Pods by labels, so its selector must match labels in the Pod template. section 15.3

Listing 15.4 Deployment spec that includes the templated Pod spec
apiVersion: v1 
kind: Deployment
metadata:
  name: numbers-api
spec:                   # this is the Deployment spec
  replicas: 1           # the number of Pods to create
  selector:             # selector to find Pods
    matchLabels:
      app: numbers
      component: api
  template:             # this is the template for creating Pods
    metadata:
      labels:           # labels the Deployment selector uses
        app: numbers
        component: api
    spec:               # and this is the Pod spec
      containers:
        - name: api     # full container spec snipped

See how `replicas`, `selector`, and the nested Pod template fit into one Deployment specification. section 15.3

Try the desired-state workflow:

  • Apply the Deployment and Service YAML with `kubectl apply -f ch15/exercises/numbers-api-v2/`. Check the result with `kubectl get deploy` and `kubectl get po -l app=numbers --show-labels`. section 15.3
  • Delete the selected Pods with `kubectl delete pod -l app=numbers`, then list them again. New Pods appear because the Deployment still requires them. section 15.3
  • Run `kubectl scale deploy/numbers-api --replicas 3`. Inspect the Pods with `kubectl get po -l app=numbers -o wide` and the Service endpoints with `kubectl get endpoints numbers-api`: the additional Pods share the template and join the Service. section 15.3

The template can also specify container readiness and liveness probes. To try an updated specification, apply `ch15/exercises/numbers-api-v3/` and watch the Pods with `kubectl get po -l app=numbers -o wide -w`. A Deployment can reference ConfigMaps through its Pod specification, loading settings as environment variables or mounting them as files. In the demonstrated file-configuration update, applying the changed Deployment specification replaces its Pod. section 15.3 section 15.4

Practice: change logging configuration

Deploy the to-do application from `ch15/exercises/todo-list`. Its web component initially logs only errors. Inject `ch15/lab/logging.json` into the web container beside `config.json`, then check its logs. Determine whether the existing Pods use the new file or whether the Deployment needs to create new Pods. section 15.6

How Learn Docker in a Month of Lunches, Second Edition covers it

How deep it goes
Works through YAML structure, label matching, replacement, scaling, probes, and configuration updates without detailing controller internals.
Examples
Uses a numbers API scaling exercise, ConfigMap-backed container settings, and a logging-configuration lab.
What it assumes
Introduces Deployments through existing Pod specifications and YAML, rather than assuming familiarity with Pod controllers.
What you can do after
Operate/configure it with kubectl to deploy, inspect, scale, update, and check replacement Pods.
How it connects
Connects Deployments to Pods, label selectors, Services, container probes, and ConfigMaps.

Also explained in

Compare how 10 books explain Kubernetes Deployment →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Docker in a Month of Lunches, Second Edition. The section links point to the parts of the book it draws on.