Kubernetes

[Cloud] 5. Kubernetes Deployment deploy

트리스탄1234 2023. 1. 30. 19:52
728x90
반응형

Let's take a look at Deployment, which is the parent object in Kubernetes. A Deployment object is a higher level object than a Replicaset or Pod. In other words, all desired objects can be created through Deployment. And it enables application update or rollback through Deployment object. ​ Then, let's create a Deployment object and deploy it.

As shown in the figure, Deployment contains Replicaset and Pod objects. In the above figure, Replicaset is maintained with 4 Pods, the Level value is my-nginx, and the target Pod is defined, and detailed Spec is defined in the Container part.

 

반응형

Rather than creating and deploying each object individually in Kubernetes. It is recommended to deploy using this Deployment object, and the advantages of using Deployment are as follows.

  • Defines the criteria to scale in/out of Pods.
  • Track all deployed and updated versions of a Pod
  • Able to perform rollback on deployed Pods

Now, let's create a Deployment directory under the test directory, create deployment.yaml, and save the following contents to a file before deploying.

root@master-VirtualBox:~/test# mkdir deployment
root@master-VirtualBox:~/test# cd deployment/
root@master-VirtualBox:~/test/deployment# vi deployment.yaml
apiVersion: apps/v1
kind: Deployment ==> define deployed object
metadata:
name: my-nginx-deployment ==> define name of object
spec:
selector:
matchLabels:
run: my-nginx ==> Label vaule of Deployment
replicas: 2 ==> quantity of Pod
template:
metadata:
labels:
run: my-nginx
spec: ==> Spec of Pod
containers:
- name: my-nginx-container
image: nginx
imagePullPolicy: Always
ports:
- containerPort: 80

After inputting and saving as above, we will now deploy the Deployment object through the kubectl command.

root@master-VirtualBox:~/test/deployment# kubectl apply -f deployment.yaml
deployment.apps/my-nginx-deployment created
root@master-VirtualBox:~/test/deployment# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
my-nginx-deployment 0/2 2 0 26s
root@master-VirtualBox:~/test/deployment# kubectl get replicasets
NAME DESIRED CURRENT READY AGE
my-nginx-deployment-5f85fc96f7 2 2 2 36s
root@master-VirtualBox:~/test/deployment# kubectl get pod
NAME READY STATUS RESTARTS AGE
my-nginx-deployment-5f85fc96f7-s9glk 1/1 Running 0 44s
my-nginx-deployment-5f85fc96f7-xv75s 1/1 Running 0 44s

Through the deployment object, both replicaset and pod have been deployed and confirmed that they are in a normal state. ​ Now, let's check whether the replicaset automatically recovers by deleting the Pods arbitrarily.

root@master-VirtualBox:~/test/deployment# kubectl delete pod my-nginx-deployment-5f85fc96f7-s9glk
pod "my-nginx-deployment-5f85fc96f7-s9glk" deleted
root@master-VirtualBox:~/test/deployment# kubectl get pod
NAME READY STATUS RESTARTS AGE
my-nginx-deployment-5f85fc96f7-xv75s 1/1 Running 0 6m55s
my-nginx-deployment-5f85fc96f7-z7lgs 0/1 ContainerCreating 0 2s

As you can see from the above log, as soon as one Pod is deleted, one is automatically created by Replicast. The reason for the new creation is that 2 was specified in the replica parameter of the Deployment object, so it was newly created to maintain 2 Pods.

Now, let's use the kubectl command to change the value of the replica of the deployment and query it.

root@master-VirtualBox:~/test/deployment# kubectl edit deployments my-nginx-deployment
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"name":"my-nginx-deployment","namespace":"default"},"spec":{"replicas":2,"selector":{"matchLabels":{"run":"my-nginx"}},"template":{"metadata":{"labels":{"run":"my-nginx"}},"spec":{"containers":[{"image":"nginx","imagePullPolicy":"Always","name":"my-nginx-container","ports":[{"containerPort":80}]}]}}}}
creationTimestamp: "2022-08-01T04:14:17Z"
generation: 2
name: my-nginx-deployment
namespace: default
resourceVersion: "59837"
uid: 448df212-8dc8-42ec-aa90-a7f484bdb974
spec:
progressDeadlineSeconds: 600
replicas: 4
revisionHistoryLimit: 10
selector:
matchLabels:
run: my-nginx
strategy:

 

After completing the modification, let's check the information of Pod, Deployment, and Replicaset as shown in the figure below.

root@master-VirtualBox:~/test/deployment# kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
my-nginx-deployment 4/4 4 4 13m
root@master-VirtualBox:~/test/deployment#
root@master-VirtualBox:~/test/deployment# kubectl get replicasets.apps
NAME DESIRED CURRENT READY AGE
my-nginx-deployment-5f85fc96f7 4 4 4 13m
root@master-VirtualBox:~/test/deployment# kubectl get pod
NAME READY STATUS RESTARTS AGE
my-nginx-deployment-5f85fc96f7-bw48j 1/1 Running 0 113s
my-nginx-deployment-5f85fc96f7-mvrvn 1/1 Running 0 113s
my-nginx-deployment-5f85fc96f7-xv75s 1/1 Running 0 13m
my-nginx-deployment-5f85fc96f7-z7lgs 1/1 Running 0 6m46s

You can see that 2 Pods are being operated while increasing from 2 Pods to 4 Pods. ​ Now, after changing the versions of the deployed Pods, let's go back to the previous version. Change the image of the pod from nginx:latest to nginx:1.9.1, and add change-cause to annotations to record the history of the change. This is in order to inquire with the changed contents in the case of history inquiry later. ​ Open the deployment.yaml file as shown below, modify it as shown in the red part, and save it.

root@master-VirtualBox:~/test/deployment# vi deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx-deployment
annotations:
kubernetes.io/change-cause: "set image nginx 1.9.1"
spec:
selector:
matchLabels:
run: my-nginx
replicas: 4
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- name: my-nginx-container
image: nginx:1.9.1
imagePullPolicy: Always
ports:
- containerPort: 80

Now let's deploy our modified deployment and see the changes.

root@master-VirtualBox:~/test/deployment# kubectl apply -f deployment.yaml
root@master-VirtualBox:~/test/deployment# kubectl get deployments ==> deployment의 정보 조회
NAME READY UP-TO-DATE AVAILABLE AGE
my-nginx-deployment 4/4 4 4 44m
root@master-VirtualBox:~/test/deployment# kubectl get replicasets ==> replicaset의 정보 조회
NAME DESIRED CURRENT READY AGE
my-nginx-deployment-5f85fc96f7 0 0 0 44m
my-nginx-deployment-d6b6494f6 4 4 4 2m46s
root@master-VirtualBox:~/test/deployment# kubectl get pods ==> pod의 정보 조회
NAME READY STATUS RESTARTS AGE
my-nginx-deployment-d6b6494f6-5v4xk 1/1 Running 0 26s
my-nginx-deployment-d6b6494f6-6nmxk 1/1 Running 0 2m33s
my-nginx-deployment-d6b6494f6-dk29d 1/1 Running 0 26s
my-nginx-deployment-d6b6494f6-n2f4b 1/1 Running 0 2m53s
root@master-VirtualBox:~/test/deployment# kubectl describe pods my-nginx-deployment-d6b6494f6-5v4xk
Name: my-nginx-deployment-d6b6494f6-5v4xk
Namespace: default
Priority: 0
Node: worknode2-virtualbox/192.168.1.6
Start Time: Mon, 01 Aug 2022 13:58:24 +0900
Labels: pod-template-hash=d6b6494f6
run=my-nginx
Annotations: cni.projectcalico.org/podIP: 172.16.7.137/32
cni.projectcalico.org/podIPs: 172.16.7.137/32
Status: Running
IP: 172.16.7.137
IPs:
IP: 172.16.7.137
Controlled By: ReplicaSet/my-nginx-deployment-d6b6494f6
Containers:
my-nginx-container:
Container ID: containerd://1f041353efe8f49e7de4522032d61bf0ba0b88fe444a6b148ffa34962a85db85
Image: nginx:1.9.1
Image ID: sha256:ee609d78a6476f051fcbf5f3be370c2bb2c5c3b72d05538df0451f9de1604816
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Mon, 01 Aug 2022 13:58:30 +0900
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-5kkl2 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-5kkl2:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 3m58s default-scheduler Successfully assigned default/my-nginx-deployment-d6b6494f6-5v4xk to worknode2-virtualbox
Normal Pulling 3m57s kubelet Pulling image "nginx:1.9.1"
Normal Pulled 3m53s kubelet Successfully pulled image "nginx:1.9.1" in 4.602785633s
Normal Created 3m52s kubelet Created container my-nginx-container
Normal Started 3m52s kubelet Started container my-nginx-container

As shown in the picture above, you can see that the image has been changed to 1.9.1 in the red part. ​ Now, let's use the command below to query the revision history of deployment.

root@master-VirtualBox:~/test/deployment# kubectl rollout history deployment
deployment.apps/my-nginx-deployment
REVISION CHANGE-CAUSE
1 <none>
2 change image nginx 1.9.1 ==> revision 2의 세부 내역을 아래 명령어로 조회
root@master-VirtualBox:~/test/deployment# kubectl rollout history deployment --revision=2
deployment.apps/my-nginx-deployment with revision #2
Pod Template:
Labels: pod-template-hash=d6b6494f6
run=my-nginx
Annotations: kubernetes.io/change-cause: change image nginx 1.9.1
Containers:
my-nginx-container:
Image: nginx:1.9.1
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>

Now, let's rollback to revision 1, the first version among the results of 'kubectl rollout history deployment'.

root@master-VirtualBox:~/test/deployment# kubectl rollout undo deployment --to-revision=1
deployment.apps/my-nginx-deployment rolled back
root@master-VirtualBox:~/test/deployment# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
my-nginx-deployment 4/4 4 4 56m
root@master-VirtualBox:~/test/deployment# kubectl get replicasets
NAME DESIRED CURRENT READY AGE
my-nginx-deployment-5f85fc96f7 4 4 4 57m
my-nginx-deployment-d6b6494f6 0 0 0 15m
root@master-VirtualBox:~/test/deployment# kubectl get pod
NAME READY STATUS RESTARTS AGE
my-nginx-deployment-5f85fc96f7-2pd8t 1/1 Running 0 69s
my-nginx-deployment-5f85fc96f7-5wj4s 1/1 Running 0 66s
my-nginx-deployment-5f85fc96f7-t9s9t 1/1 Running 0 69s
my-nginx-deployment-5f85fc96f7-z8wrs 1/1 Running 0 6
root@master-VirtualBox:~/test/deployment# kubectl describe pod my-nginx-deployment-5f85fc96f7-2pd8t
Name: my-nginx-deployment-5f85fc96f7-2pd8t
Namespace: default
Priority: 0
Node: worknode2-virtualbox/192.168.1.6
Start Time: Mon, 01 Aug 2022 14:10:26 +0900
Labels: pod-template-hash=5f85fc96f7
run=my-nginx
Annotations: cni.projectcalico.org/podIP: 172.16.7.138/32
cni.projectcalico.org/podIPs: 172.16.7.138/32
Status: Running
IP: 172.16.7.138
IPs:
IP: 172.16.7.138
Controlled By: ReplicaSet/my-nginx-deployment-5f85fc96f7
Containers:
my-nginx-container:
Container ID: containerd://8204770b9456cda5faf12aad5438d8893225459c7391a50814b9964c38c7196b
Image: nginx
Image ID: docker.io/library/nginx@sha256:bd06dfe1f8f7758debd49d3876023992d41842fd8921565aed315a678a309982
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Mon, 01 Aug 2022 14:10:29 +0900
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-9jctd (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-9jctd:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 88s default-scheduler Successfully assigned default/my-nginx-deployment-5f85fc96f7-2pd8t to worknode2-virtualbox
Normal Pulling 87s kubelet Pulling image "nginx"
Normal Pulled 85s kubelet Successfully pulled image "nginx" in 1.927570903s
Normal Created 85s kubelet Created container my-nginx-container
Normal Started 85s kubelet Started container my-nginx-container
root@master-VirtualBox:~/test/deployment#

As shown in the picture above, you can confirm that the image has been rolled back to the previous version.

728x90
반응형

'Kubernetes' 카테고리의 다른 글

[Cloud] 7. Deployment strategies  (23) 2023.02.05
[Cloud] 6. Kubernetes Service deploy  (24) 2023.02.01
[Cloud] 4. Kubernetes Replicaset deployment  (38) 2023.01.24
[Cloud] 3.Kuberdnetes Pod deploy  (14) 2022.12.25
[Cloud] 2. Kubernetes namespace  (11) 2022.12.25