Post

Kubernetes - Deployments and ReplicaSets

Lab assignment for creating namespaces using kubectl

Kubernetes - Deployments and ReplicaSets

Prerequisites

  • Kubernetes
  • kubectl

Assignment

  • Lets start by creating a deployment and inspect the repliacesets.

1️⃣ Create a simple deployment using nginx using imperative command and also create a manifest file.

1
kubectl create deployment nginx --image=nginx --dry-run=client -o yaml | tee nginx-deployment.yaml | kubectl apply -f -

2️⃣ List all the deployments and replicasets.

1
2
kubectl get deployment
kubectl get replicaset

3️⃣ Query the pods and check that these pods will be named with the name of the deployment, replicaSet and a unique identifier.

1
kubectl get pods -o wide

4️⃣ Check the initial rollout history and annote this to make use of this record.

1
2
3
kubectl rollout history deployment/nginx
kubectl annotate deployment/nginx kubernetes.io/change-cause="initial deployment"
kubectl rollout history deployment/nginx

5️⃣ Scale the number of replicas and monitor the change.

1
2
kubectl scale deployment/nginx --replicas=10; watch kubectl get pods -o wide
kubectl get deployment -o wide

6️⃣ Modify the nginx-deployment.yaml file to increase the replica count to 12.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
cat <<EOF > nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 12
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}
EOF
1
2
kubectl apply -f nginx-deployment.yaml; watch kubectl get pods -o wide
kubectl get deployments -o wide

7️⃣ Check if the rollout history has changed.

1
kubectl rollout history deployment/nginx

8️⃣ Modify the deployment to use a different image nginx:stable and check the rollout history.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
cat <<EOF > nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 12
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:stable
        name: nginx
        resources: {}
status: {}
EOF
1
kubectl apply -f nginx-deployment.yaml && kubectl rollout status deployment/nginx
1
kubectl get deployments -o wide

9️⃣ Annotate the newly added entry in the rollout history.

1
kubectl annotate deployment/nginx kubernetes.io/change-cause="change image to nginx:stable"
1
kubectl rollout history deployment/nginx

🔟 Change the image using CLI and watch the rollout status.

1
kubectl set image deployment/nginx nginx=nginx:alpine && kubectl rollout status deployment/nginx
1
kubectl annotate deployment/nginx kubernetes.io/change-cause="change image to nginx:alpine"
1
kubectl rollout history deployment/nginx

1️⃣1️⃣. Describe the deployment to check the reference to the new and old replicasets.

1
kubectl describe deployment/nginx
1
kubectl get replicaset -o wide

1️⃣2️⃣. Change the image name to an invalid value and verify how it safeguards the deployment.

1
kubectl set image deployment/nginx nginx=nginx:bananas && watch kubectl get pods -o wide
1
kubectl annotate deployment/nginx kubernetes.io/change-cause="change image to nginx:bananas - failed"
1
kubectl describe deployment/nginx

1️⃣3️⃣. Undo the change using rollout option.

1
kubectl rollout undo deployment/nginx && kubectl rollout status deployment/nginx
1

1️⃣4️⃣. Undo Rollout to a specific version and check the rollout history.

1
kubectl rollout undo deployment/nginx --to-revision=1 && kubectl rollout status deployment/nginx
1
kubectl get replicaset -o wide
1
kubectl rollout history deployment/nginx

CleanUp

1
kubectl delete deployment/nginx --now
This post is licensed under CC BY 4.0 by the author.