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









