Monday, March 15, 2021

Kubernetes - Deployment

Deployment


How do you upgrade your application/pod

rolling update
----------------

rollback

pods are deployed using replication controller or replicaset

Deployment allow upgrade/undo changes.

how do create deployment
- it is similar to replicaset

> cat deployment-def.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deployment
  labels:
    app: myapp
    type: front-end
spec:
  template:
    metadata:
      name: myapp-pod
      labels:
        app: myapp
        type: front-end
    spec:
      containers:
      - name: nginx-container
        image: nginx
  replicas: 3
  selector:
    metchLabels:
      type: front-end

> kc create -f deployment-def.yaml
display newly created deployment
> kc get deployment

deployment automatically creates replicaSets in the name of deployment
> kc get rs

replica sets creates pod
> kc get pods

you will see name of the pod with name of deployment and replicaset.

deployment creates new object called deployment whch allow you to update, upgrade, and manage replica set and undo the change.

summary
------------
> kc get all
you will see deployment, replica set and pods it created.


Certification Tip!

Creating yaml file manually is a bit of challenging. the best option to use kc run command followed by name of the pod and name of the image.
for eg, to create a pod with httpd image

> kc run http --image=httpd
> kc run nginx --image=nginx

to create a manifest (yaml) file, use the option --dry-run. it will not create pod but generates yaml file

> kc run nginx --image=nginx --dry-run=client -o yaml

Create a deployment
> kc create deployment --image=nginx nginx

generate deployment yaml file. As we know --dry-run will not create deployment
> kc create deployment nginx  --image=nginx --dry-run=client =o yaml

add 4 replicas  (--replicas=4) on above command
> kc create deployment nginx --image=nginx --dry-run=client -o yaml > nginx.deploy.yaml
edit the output file and change the replicas to 4.
once file is modified, create the deployment.


https://kubernetes.io/docs/reference/kubectl/conventions/


LAB
-----------

1. How many PODs exist on the system in the current(default) namespace?
controlplane $ alias kc=kubectl
controlplane $ kc get po
No resources found in default namespace.
controlplane $

2. How many ReplicaSets exist on the system in the current(default) namespace?
controlplane $ kc get rs
No resources found in default namespace.
controlplane $ kc get replicasets
No resources found in default namespace.
controlplane $

hint: Run the command 'kubectl get replicaset' and count the number of ReplicaSets.

3. How many Deployments exist on the system in the current(default) namespace?

controlplane $ kc get deployment
No resources found in default namespace.

controlplane $ kc get ns
NAME              STATUS   AGE
default           Active   4m2s
kube-node-lease   Active   4m4s
kube-public       Active   4m4s
kube-system       Active   4m4s

hints: Run the command 'kubectl get deployment' and count the number of Deployments.

4. How many Deployments exist on the system now?
We just created a Deployment! Check again!

controlplane $ kc get deployment
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
frontend-deployment   0/4     4            0           18s
controlplane $


Hint: Run the command 'kubectl get deployment' and count the number of deployments.

5. How many ReplicaSets exist on the system now?

controlplane $ kc get rs
NAME                             DESIRED   CURRENT   READY   AGE
frontend-deployment-56d8ff5458   4         4         0       77s
controlplane $


Hint: Run the command 'kubectl get replicaset' and count the number of ReplicaSets.

6. How many PODs exist on the system now?
controlplane $ kc get rs
NAME                             DESIRED   CURRENT   READY   AGE
frontend-deployment-56d8ff5458   4         4         0       77s
controlplane $ kc get po
NAME                                   READY   STATUS             RESTARTS   AGE
frontend-deployment-56d8ff5458-7mhgl   0/1     ImagePullBackOff   0          2m12s
frontend-deployment-56d8ff5458-t829q   0/1     ImagePullBackOff   0          2m12s
frontend-deployment-56d8ff5458-wt8bg   0/1     ErrImagePull       0          2m12s
frontend-deployment-56d8ff5458-zkjq9   0/1     ImagePullBackOff   0          2m12s
controlplane $

7. Out of all the existing PODs, how many are ready?

Looking at the output above, ready is = 0

hint: Run the command 'kubectl get deployment' and count the number of PODs.

8. What is the image used to create the pods in the new deployment?
> kc describe deployment frontend-deployment | grep -i image
image:      busybox888

controlplane $ kc get rs
NAME                             DESIRED   CURRENT   READY   AGE
frontend-deployment-56d8ff5458   4         4         0       4m1s
controlplane $ kc describe rs frontend-deployment-56d8ff5458
Name:           frontend-deployment-56d8ff5458
Namespace:      default
Selector:       name=busybox-pod,pod-template-hash=56d8ff5458
Labels:         name=busybox-pod
                pod-template-hash=56d8ff5458
Annotations:    deployment.kubernetes.io/desired-replicas: 4
                deployment.kubernetes.io/max-replicas: 5
                deployment.kubernetes.io/revision: 1
Controlled By:  Deployment/frontend-deployment
Replicas:       4 current / 4 desired
Pods Status:    0 Running / 4 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  name=busybox-pod
           pod-template-hash=56d8ff5458
  Containers:
   busybox-container:
    Image:      busybox888
    Port:       <none>
    Host Port:  <none>
    Command:
      sh
      -c
      echo Hello Kubernetes! && sleep 3600
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age    From                   Message
  ----    ------            ----   ----                   -------
  Normal  SuccessfulCreate  4m32s  replicaset-controller  Created pod: frontend-deployment-56d8ff5458-7mhgl
  Normal  SuccessfulCreate  4m32s  replicaset-controller  Created pod: frontend-deployment-56d8ff5458-wt8bg
  Normal  SuccessfulCreate  4m32s  replicaset-controller  Created pod: frontend-deployment-56d8ff5458-t829q
  Normal  SuccessfulCreate  4m32s  replicaset-controller  Created pod: frontend-deployment-56d8ff5458-zkjq9
controlplane $

hint: Run the command 'kubectl describe deployment' and look under the containers section.

9. Why do you think the deployment is not ready?
> kc get pods
> kc describe pod <nameof the pod>

looking at the output above,
- image busybox88 does not exist
failed to pull the image. rpc error so repo may not be available.

10. Create a new Deployment using the 'deployment-definition-1.yaml' file located at /root/
There is an issue with the file, so try to fix it.
    Name: deployment-1


cat deployment-def.yaml
---
apiVersion: apps/v1
kind: deployment
metadata:
  name: deployment-1
spec:
  replicas: 2
  selector:
    matchLabels:
      name: busybox-pod
  template:
    metadata:
      labels:
        name: busybox-pod
    spec:
      containers:
      - name: busybox-container
        image: busybox888
        command:
        - sh
        - "-c"
        - echo Hello Kubernetes! && sleep 3600

controlplane $ kc create -f deployment-definition-1.yaml
Error from server (BadRequest): error when creating "deployment-definition-1.yaml": deployment in version "v1" cannot be handled as a Deployment: no kind "deployment" is registered for version "apps/v1" in scheme "k8s.io/kubernetes/pkg/api/legacyscheme/scheme.go:30"
controlplane $

change Kind: deployment to Deployment - first letter uppercase

controlplane $ kc create -f deployment-definition-1.yaml
deployment.apps/deployment-1 created
controlplane $


11. Create a new Deployment with the below attributes using your own deployment definition file

Name: httpd-frontend; Replicas: 3; Image: httpd:2.4-alpine

    Name: httpd-frontend
    Replicas: 3
    Image: httpd:2.4-alpine

> kc create deploymetn http-frontend; replicas=3 --image=httpd:2.4-alpine

or
> kc create deployment httpd-frontend --image=httpd:2.4-alpine
> kc scale deployment --replicas=3 httpd-frontend
> kc get deployment httpd-frontend

or
do it manually
apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd-frontend-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      name: httpd-frontend
  template:
    metadata:
      labels:
        name: httpd-frontend
    spec:
      containers:
      - name: httpd-frontend
        image: httpd:2.4-alpine


controlplane $ kc create -f mydep.yaml
deployment.apps/httpd-frontend created
controlplane $ kc get deploy
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
deployment-1          0/2     2            0           9m31s
frontend-deployment   0/4     4            0           21m
httpd-frontend        3/3     3            3           8s
controlplane $ cat mydep.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd-frontend
spec:
  replicas: 3
  selector:
    matchLabels:
      name: httpd-frontend
  template:
    metadata:
      labels:
        name: httpd-frontend
    spec:
      containers:
      - name: httpd-frontend
        image: httpd:2.4-alpine
controlplane $





No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...