Browse topics

Deployments

Version
1.37
Review
Draft

Summary#

A Deployment keeps a requested number of identical Pods running. It manages ReplicaSets, which create the Pods. A change to the Pod template starts a rollout to a new ReplicaSet, while a change to the replica count only resizes.

Syntax#

sh
kubectl create deployment NAME --image=IMAGE --replicas=3
kubectl scale deployment/NAME --replicas=5
kubectl rollout status deployment/NAME
yaml
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web

Example#

sh
(
  set -eu
  ns="ref-deployments-$$"
  kubectl create namespace "$ns" >/dev/null
  trap 'kubectl delete namespace "$ns" >/dev/null' EXIT

  kubectl apply -n "$ns" -f - <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: app
          image: registry.k8s.io/pause:3.10
EOF
  kubectl rollout status -n "$ns" deployment/web --timeout=120s >/dev/null
  kubectl get -n "$ns" deployment web -o jsonpath='ready={.status.readyReplicas}/{.spec.replicas}{"\n"}'

  kubectl scale -n "$ns" deployment/web --replicas=3
  kubectl rollout status -n "$ns" deployment/web --timeout=120s >/dev/null
  kubectl get -n "$ns" deployment web -o jsonpath='ready={.status.readyReplicas}/{.spec.replicas}{"\n"}'
  kubectl get -n "$ns" replicasets -l app=web \
    -o jsonpath='{range .items[*]}owner={.metadata.ownerReferences[0].kind}/{.metadata.ownerReferences[0].name} replicas={.spec.replicas}{"\n"}{end}'
)

Expected output#

Text
deployment.apps/web created
ready=2/2
deployment.apps/web scaled
ready=3/3
owner=Deployment/web replicas=3

Common mistakes#

spec.selector must match the labels in spec.template.metadata.labels, and it cannot be changed after the Deployment is created. Choose a selector that no other controller in the namespace also matches; overlapping selectors make controllers interfere with each other.

Version applicability#

The example targets Kubernetes 1.37 and uses apps/v1, which is available since Kubernetes 1.9. Older beta versions of the Deployment API are no longer served since 1.16.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0