본문 바로가기

리눅스

[kubernetes] 쿠버네티스 디플로이먼트

728x90

쿠버네티스 디플로이먼트(kubernetes deployment)

디플로이먼트(Deployment) 는 파드와 레플리카셋(ReplicaSet)에 대한 선언적 업데이트를 제공한다.

디플로이먼트에서 의도하는 상태 를 설명하고, 디플로이먼트 컨트롤러(Controller)는 현재 상태에서 의도하는 상태로 비율을 조정하며 변경한다. 새 레플리카셋을 생성하는 디플로이먼트를 정의하거나 기존 디플로이먼트를 제거하고, 모든 리소스를 새 디플로이먼트에 적용할 수 있다.

 

이미지 출처-https://tachingchen.com/img/kubernetes-rolling-update-with-deployment/deployment-rs-pod.svg
이미지 출처-https://www.bluematador.com/hs-fs/hubfs/blog/new/Kubernetes%20Deployments%20-%20Rolling%20Update%20Configuration/Deployment%20Diagrams%20-%20static.png?width=600&name=Deployment%20Diagrams%20-%20static.png
이미지 출처-https://storage.googleapis.com/static.ianlewis.org/prod/img/765/deployments.png

 

디플로이먼트 생성(kubectl create deployment)

kubectl create deployment myapp --image=nginx
$ kubectl create deployment myapp --image=nginx
deployment.apps/myapp created

리소스 확인(kubectl get)

kubectl get deployments,replicasets,pods
$ kubectl get deployments,replicasets,pods
NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/myapp   1/1     1            1           2m48s

NAME                              DESIRED   CURRENT   READY   AGE
replicaset.apps/myapp-dc8dc9c59   1         1         1       2m48s

NAME                        READY   STATUS    RESTARTS   AGE
pod/myapp-dc8dc9c59-jvvwx   1/1     Running   0          2m48s
$ kubectl get deployments,replicasets,pods -o wide
NAME                    READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES   SELECTOR
deployment.apps/myapp   1/1     1            1           4m14s   nginx        nginx    app=myapp

NAME                              DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES   SELECTOR
replicaset.apps/myapp-dc8dc9c59   1         1         1       4m14s   nginx        nginx    app=myapp,pod-template-hash=dc8dc9c59

NAME                        READY   STATUS    RESTARTS   AGE     IP              NODE         NOMINATED NODE   READINESS GATES
pod/myapp-dc8dc9c59-jvvwx   1/1     Running   0          4m14s   10.233.118.67   kube-node3   <none>           <none>

curl 테스트(pods ip)

$ curl -I 10.233.118.67
HTTP/1.1 200 OK
Server: nginx/1.23.1
Date: Sat, 01 Oct 2022 09:18:17 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 19 Jul 2022 14:05:27 GMT
Connection: keep-alive
ETag: "62d6ba27-267"
Accept-Ranges: bytes

서비스 생성(kubectl expose deployment)

kubectl expose deployment myapp --port=80 --protocol TCP --target-port 80 --name myapp-svc --type LoadBalancer
$ kubectl expose deployment myapp --port=80 --protocol TCP --target-port 80 --name myapp-svc --type LoadBalancer
service/myapp-svc exposed

리소스 확인(kubectl get)

kubectl get deployments,replicasets,pods,service -o wide
$ kubectl get deployments,replicasets,pods,service -o wide
NAME                    READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES   SELECTOR
deployment.apps/myapp   1/1     1            1           9m31s   nginx        nginx    app=myapp

NAME                              DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES   SELECTOR
replicaset.apps/myapp-dc8dc9c59   1         1         1       9m31s   nginx        nginx    app=myapp,pod-template-hash=dc8dc9c59

NAME                        READY   STATUS    RESTARTS   AGE     IP              NODE         NOMINATED NODE   READINESS GATES
pod/myapp-dc8dc9c59-jvvwx   1/1     Running   0          9m31s   10.233.118.67   kube-node3   <none>           <none>

NAME                 TYPE           CLUSTER-IP     EXTERNAL-IP      PORT(S)        AGE    SELECTOR
service/kubernetes   ClusterIP      10.233.0.1     <none>           443/TCP        168m   <none>
service/myapp-svc    LoadBalancer   10.233.5.179   192.168.56.200   80:30811/TCP   5s     app=myapp
kubectl get service -o wide
$ kubectl get service myapp-svc -o wide
NAME        TYPE           CLUSTER-IP     EXTERNAL-IP      PORT(S)        AGE   SELECTOR
myapp-svc   LoadBalancer   10.233.5.179   192.168.56.200   80:30811/TCP   81s   app=myapp

curl 테스트(loadbalancer ip)

$ curl -I 192.168.56.200
HTTP/1.1 200 OK
Server: nginx/1.23.1
Date: Sat, 01 Oct 2022 09:23:21 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 19 Jul 2022 14:05:27 GMT
Connection: keep-alive
ETag: "62d6ba27-267"
Accept-Ranges: bytes

curl 테스트(nodeport)

$ kubectl get service myapp-svc -o wide
NAME        TYPE           CLUSTER-IP     EXTERNAL-IP      PORT(S)        AGE   SELECTOR
myapp-svc   LoadBalancer   10.233.5.179   192.168.56.200   80:30811/TCP   67m   app=myapp
$ kubectl get nodes -o wide
NAME            STATUS   ROLES           AGE     VERSION   INTERNAL-IP     EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION      CONTAINER-RUNTIME
kube-control1   Ready    control-plane   3h56m   v1.24.6   192.168.56.11   <none>        Ubuntu 20.04.5 LTS   5.4.0-126-generic   docker://20.10.17
kube-node1      Ready    <none>          3h54m   v1.24.6   192.168.56.21   <none>        Ubuntu 20.04.5 LTS   5.4.0-126-generic   docker://20.10.17
kube-node2      Ready    <none>          3h54m   v1.24.6   192.168.56.22   <none>        Ubuntu 20.04.5 LTS   5.4.0-126-generic   docker://20.10.17
kube-node3      Ready    <none>          3h54m   v1.24.6   192.168.56.23   <none>        Ubuntu 20.04.5 LTS   5.4.0-126-generic   docker://20.10.17
$ curl -I 192.168.56.21:30811
HTTP/1.1 200 OK
Server: nginx/1.23.1
Date: Sat, 01 Oct 2022 10:30:52 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 19 Jul 2022 14:05:27 GMT
Connection: keep-alive
ETag: "62d6ba27-267"
Accept-Ranges: bytes

파드 스케일링(kubectl scale deployment)

kubectl scale deployment myapp --replicas 3
$ kubectl scale deployment myapp --replicas 3
deployment.apps/myapp scaled

파드 스케일링 확인

kubectl get deployments,replicasets,pods
$ kubectl get deployments,replicasets,pods
NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/myapp   3/3     3            3           80m

NAME                              DESIRED   CURRENT   READY   AGE
replicaset.apps/myapp-dc8dc9c59   3         3         3       80m

NAME                        READY   STATUS    RESTARTS   AGE
pod/myapp-dc8dc9c59-jvvwx   1/1     Running   0          80m
pod/myapp-dc8dc9c59-mtn6w   1/1     Running   0          56s
pod/myapp-dc8dc9c59-shckl   1/1     Running   0          56s
kubectl get all
$ kubectl get all
NAME                        READY   STATUS    RESTARTS   AGE
pod/myapp-dc8dc9c59-jvvwx   1/1     Running   0          82m
pod/myapp-dc8dc9c59-mtn6w   1/1     Running   0          2m20s
pod/myapp-dc8dc9c59-shckl   1/1     Running   0          2m20s

NAME                 TYPE           CLUSTER-IP     EXTERNAL-IP      PORT(S)        AGE
service/kubernetes   ClusterIP      10.233.0.1     <none>           443/TCP        4h1m
service/myapp-svc    LoadBalancer   10.233.5.179   192.168.56.200   80:30811/TCP   72m

NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/myapp   3/3     3            3           82m

NAME                              DESIRED   CURRENT   READY   AGE
replicaset.apps/myapp-dc8dc9c59   3         3         3       82m

생성한 리소스 삭제

kubectl delete deployments,service --all
$ kubectl delete deployments,service --all
deployment.apps "myapp" deleted
service "kubernetes" deleted
service "myapp-svc" deleted
$ kubectl get all
NAME                        READY   STATUS        RESTARTS   AGE
pod/myapp-dc8dc9c59-jvvwx   1/1     Terminating   0          83m
pod/myapp-dc8dc9c59-mtn6w   1/1     Terminating   0          3m52s
pod/myapp-dc8dc9c59-shckl   1/1     Terminating   0          3m52s
$ kubectl get all
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.233.0.1   <none>        443/TCP   34s

 

참고URL

- 쿠버네티스 문서 / 개념 / 워크로드 / 워크로드 리소스 / 디플로이먼트 : https://kubernetes.io/ko/docs/concepts/workloads/controllers/deployment/

 

728x90