ruk·si

☸️ Kubernetes
Deployment

Updated at 2018-12-31 02:51

Deployment is a high-level controller for stateless applications. It manages ReplicaSets which manage pods.

Deployment offers a lot out-of-the box:

  • Rolling updates
  • Rollback
  • Pausing updates

YAML:

  • .metadata.name unique name for the deployment
  • .metadata.labels same as labels in pod template
  • .spec.replicas number of pod replicas to maintain
  • .spec.minReadySeconds wait for the duration before consider pod to be truly Ready
  • .spec.selector which pods to manage, should not be changed later
  • .spec.strategy.type defaults to RollingUpdate
  • .spec.strategy.rollingUpdate.maxUnavailable  number or % of instances that can update at once, defaults to 25%
  • .spec.strategy.rollingUpdate.maxSurge how much can we go above the desired number of pods if required, defaults to 25%
  • .spec.template pod template to run
  • .spec.template.meta.labels must match the selector above
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.7.9
          ports:
            - containerPort: 80
kubectl create -f  nginx-deployment.yaml
kubectl get deployments
kubectl rollout status deployment/nginx-deployment
kubectl get pods --show-labels

Status:

  • .spec.replicas / DESIRED = desired state
  • .status.replicas / CURRENT = current state
  • .status.updatedReplicas / UP-TO-DATE = how many are using the latest pod template
  • .status.availableReplicas / AVAILABLE = how many of the pod replicas have been Ready alive long enough to be considered active

Updating:

kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1

# or edit .spec.template.spec.containers[0].image
kubectl edit deployment/nginx-deployment

Rollback:

kubectl rollout history deployment/nginx-deployment
kubectl rollout history deployment/nginx-deployment --revision=2

kubectl rollout undo deployment/nginx-deployment
kubectl rollout undo deployment/nginx-deployment --to-revision=2

Scaling:

kubectl scale deployment nginx-deployment --replicas=10

Auto-scaling (uses HPA and requires Metrics Server):

kubectl autoscale deployment nginx-deployment --min=10 --max=15 --cpu-percent=80

Pausing allows to make multiple changes with one rollout:

kubectl rollout pause deployment/nginx-deployment
kubectl set image deploy/nginx-deployment nginx=nginx:1.9.1
kubectl rollout history deploy/nginx-deployment
kubectl set resources deployment nginx-deployment -c=nginx --limits=cpu=200m,memory=512Mi
kubectl rollout resume deploy/nginx-deployment

.spec.progressDeadlineSeconds tells how long wait for updates to finish. Then Deployment .status.conditions[type=Progressing] is marked as False while the Reason gives more details. Failed deployments are not automatically retried so you need to handle this at the higher-level.

kubectl patch deployment/nginx-deployment -p '{"spec":{"progressDeadlineSeconds":600}}'

.spec.revisionHistoryLimit specifies how many old ReplicaSets to retain. The default is 10; if you set this to 0, you won't be able to rollback.

Sources