Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Kubernetes: Deployment

Back


Deployment


Deployment status


Additional Labels


Deployment vs ReplicaSet


Deployment Deletion


Imperative Command

Command Description
kubectl explain deploy show deployment documentation
kubectl get deploy List all Deployments in the current namespace.
kubectl describe deploy deploy_name Show detailed information about a specific Deployment.
kubectl create deploy deploy_name --image=img_name Create a deployment using image
kubectl create deploy nginx --image=nginx --dry-run=client --replicas=4 -o yaml Show the deployemnt in yaml file
kubectl set image deploy_name nginx=nginx:1.25 Update the container image in a deployment
kubectl delete deploy deploy_name Delete a Deployment by name.
kubectl scale deploy deploy_name --replicas=count-num Scale the number of replicas for a Deployment.

Declarative Manifest

Key fields


Scaling Deployment


Good Practice: Omitting replicas field


Lab: omitting replicas field

Problem

# demo-deploy-with-replica.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo-deploy-with-replica
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - image: nginx
          name: nginx
kubectl apply -f demo-deploy-with-replica.yaml
# deployment.apps/demo-deploy-with-replica created

kubectl get deploy
# NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
# demo-deploy-with-replica   3/3     3            3           25s
kubectl scale deploy demo-deploy-with-replica --replicas=5
# deployment.apps/demo-deploy-with-replica scaled

# confirm
kubectl get deploy
# NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
# demo-deploy-with-replica   5/5     5            5           2m56s
kubectl apply -f demo-deploy-with-replica.yaml
# deployment.apps/demo-deploy-with-replica configured

# confirm: replica back to 3
kubectl get deploy
# NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
# demo-deploy-with-replica   3/3     3            3           3m53s

the replica number change back to 3


Solution

# demo-deploy-without-replica.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo-deploy-without-replica
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - image: nginx
          name: nginx
kubectl apply -f demo-deploy-without-replica.yaml
# deployment.apps/demo-deploy-without-replica created

kubectl get deploy
# NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
# demo-deploy-without-replica   1/1     1            1           22s
kubectl scale deploy demo-deploy-without-replica --replicas=5
# deployment.apps/demo-deploy-without-replica scaled

kubectl get deploy
# NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
# demo-deploy-without-replica   5/5     5            5           2m33s
kubectl apply -f demo-deploy-without-replica.yaml
# deployment.apps/demo-deploy-without-replica configured

kubectl get deploy demo-deploy-without-replica
# NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
# demo-deploy-without-replica   5/5     5            5           5m29s

the replica remain 5.


Lab: Default Replica and strategy

# demo-deploy-default-replica.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo-deploy-default-replica
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
        type: front-end
    spec:
      containers:
        - name: nginx
          image: nginx
# create deployment
kubectl create -f demo-deploy-default-replica.yaml
# deployment.apps/demo-deploy-default-replica created

kubectl get deploy
# NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
# demo-deploy-default-replica   1/1     1            1           96s

# also create rs
kubectl get rs
# NAME                                     DESIRED   CURRENT   READY   AGE
# demo-deploy-default-replica-65cf588b89   1         1         1       106s

kubectl get pods
# NAME                                           READY   STATUS    RESTARTS   AGE
# demo-deploy-default-replica-65cf588b89-v4kpd   1/1     Running   0          2m20s

# confirm deploy:
#   default replica=1; default StrategyType=RollingUpdate; RollingUpdateStrategy:25% max unavailable, 25% max surge
kubectl describe deploy demo-deploy-default-replica
# Selector:               app=nginx
# Replicas:               1 desired | 1 updated | 1 total | 1 available | 0 unavailable
# StrategyType:           RollingUpdate
# RollingUpdateStrategy:  25% max unavailable, 25% max surge

# confirm rs:
#   Controlled by: deloy
#   additional label added: pod-template-hash + selector labels
kubectl describe rs demo-deploy-default-replica
# Labels:         app=nginx
#                 pod-template-hash=65cf588b89
#                 type=front-end
# Controlled By:  Deployment/demo-deploy-default-replica
# Replicas:       1 current / 1 desired

# confirm pod:
#   Controlled by rs
#   additional label added: pod-template-hash + selector labels
kubectl describe pod demo-deploy-default-replica
# Controlled By:  ReplicaSet/demo-deploy-default-replica-65cf588b89
# Labels:           app=nginx
#                   pod-template-hash=65cf588b89
#                   type=front-end