brunch

You can make anything
by writing

C.S.Lewis

by Master Seo Jun 29. 2021

쿠버1탄-10. 쿠버네티스 -컨트롤러

다음은 쿠버네티스 스터디 자료를 참고해  정리한 내용입니다.


모니터링하며, 원하는 상태가 되도록 만들어 주는것이 컨트롤러 이다.


<1> 컨트롤러?

<2> ReplicaSet ?

<3> 리플리카셋 실습

<4> 죽지 않는 pod - 리플리카셋이 관리하고 있음

<5> 디플로이 먼트 실습

<6> 롤링 업데이트와 롤백

<7> 정리

<8> 다음 과정



<1> 컨트롤러?


정해진 작업을 수행하는 주체

원하는 상태를 만들어 준다.


먼저 볼 컨트롤러 동영상

https://youtu.be/SNA1sSNlmy0


https://youtu.be/3M_9sgcvTts




<2>  Replica Set ?


Pod의 수를 일정 갯수로 유지 되도록 해주는것

아래 예제에서 3개의 Pod를 유지해보자~



1

node 1 다운 ?

pod의 레이블 참고한다. 

레플리카셋의 셀렉터을 참고한다.



2

clear


master node의 api 서버 접속

ssh -i ~/.ssh/id_rsa ubuntu@api.k8s.serverchk.com

kubectl delete pod --all


3

터미널 하나 더 띠워서 모니터링

watch -d 'kubectl get pods,rs,deploy -o wide'



4

pod 3개

selector  

label 확인

spec 용량 확인



cat << EOF > replicaset-cndk.yaml

apiVersion: apps/v1

kind: ReplicaSet

metadata:

  name: replicaset-cndk

spec:

  replicas: 3

  selector:

    matchLabels:

      app: cndk-nginx-pods

  template:

    metadata:

      name: cndk-nginx-pod

      labels:

        app: cndk-nginx-pods

    spec:

      containers:

      - name: nginx

        image: nginx:latest

EOF


5

kubectl apply -f replicaset-cndk.yaml

replicaset.apps/replicaset-cndk created



6

모니터링에 떠 있는것

rs 3개



7

리플리카 세트 확인 ?

kubectl get replicasets.apps -o wide


NAME              DESIRED   CURRENT   READY   AGE    CONTAINERS   IMAGES         SELECTOR

replicaset-cndk   3         3         3       4m2s   nginx        nginx:latest   app=cndk-nginx-pods

// Selecor가 있다. app=cndk-nginx-pods



8

상세 확인

kubectl describe replicasets.apps replicaset-cndk




9

pod 확인 ?

kubectl get pod --show-labels

NAME                    READY   STATUS    RESTARTS   AGE   LABELS

replicaset-cndk-dxvvw   1/1     Running   0          42s   app=cndk-nginx-pods

replicaset-cndk-hl4b8   1/1     Running   0          42s   app=cndk-nginx-pods

replicaset-cndk-jr6s4   1/1     Running   0          42s   app=cndk-nginx-pods

// pod이름은 replicaset-cndk-xxxxxxxxx로 생성됨

// LABLES 도 확인 바람



8

replicas: 6  으로 수정 ?

kubectl edit replicasets.apps replicaset-cndk


spec:

  replicas: 6



9

kubectl get pods

NAME                    READY   STATUS    RESTARTS   AGE

replicaset-cndk-46wgx   1/1     Running   0          10s

replicaset-cndk-4pfgr   1/1     Running   0          10s

replicaset-cndk-6dtrx   1/1     Running   0          10s

replicaset-cndk-dxvvw   1/1     Running   0          9m28s

replicaset-cndk-hl4b8   1/1     Running   0          9m28s

replicaset-cndk-jr6s4   1/1     Running   0          9m28s

// 3개가 더 생성되었다!



10

리플리카 세트 삭제 


kubectl delete replicasets.apps replicaset-cndk

replicaset.apps "replicaset-cndk" deleted


 k delete replicaset  replicaset-cndk




<3> 리플리카셋  실습


1

다시 생성 

kubectl apply -f replicaset-cndk.yaml



2

확인

kubectl get replicasets.apps -o wide

NAME              DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES         SELECTOR

replicaset-cndk   3         3         3       15s   nginx        nginx:latest   app=cndk-nginx-pods



3

중복되는 Label의  Pod 생성하면 ?

생성되자 마자 죽는다!!

ubuntu@ip-172-20-41-255:~$ more pod-rs.yaml


apiVersion: v1

kind: Pod

metadata:

  name: pod1

  labels:

    app: cndk-nginx-pods

spec:

  containers:

  - name: hello1

    image: gcr.io/google-samples/hello-app:1.0

---

apiVersion: v1

kind: Pod

metadata:

  name: pod2

  labels:

    app: cndk-nginx-pods

spec:

  containers:

  - name: hello2

    image: gcr.io/google-samples/hello-app:2.0



4

적용 ?

kubectl apply -f pod-rs.yaml && kubectl get pod -w


중복되는 Pod 생성시 ?

생성되자 마자 종료 시킨다.!!

이미 Relicaset 이  라벨 3개인것을 관리하고 있다.  

더 들어 오더라도 3개로 관리하고 있어서 종료 시킨다.


pod/pod1 created

pod/pod2 created

NAME                    READY   STATUS        RESTARTS   AGE

pod1                    0/1     Terminating   0          0s

pod2                    0/1     Terminating   0          0s

replicaset-cndk-4jf6c   1/1     Running       0          2m50s

replicaset-cndk-dx7vt   1/1     Running       0          2m50s

replicaset-cndk-sghdl   1/1     Running       0          2m50s

pod1                    0/1     Terminating   0          1s

pod2                    0/1     Terminating   0          1s

pod2                    0/1     Terminating   0          2s



<4> 죽지 않는 pod - 리플리카셋이 관리하고 있음


1

kubectl get pod -w


ubuntu@ip-172-20-41-255:~$ kubectl get pod -w

NAME                    READY   STATUS    RESTARTS   AGE

replicaset-cndk-4jf6c   1/1     Running   0          4m38s

replicaset-cndk-dx7vt   1/1     Running   0          4m38s

replicaset-cndk-sghdl   1/1     Running   0          4m38s


2

pod 삭제 ?

kubectl delete pod -l app=cndk-nginx-pods


// 안죽는다.

kubectl get pod -w


3

전체 pod 삭제 ?

kubectl delete pod --all

안죽는다

Pod는 레플리카 세트가 3개로 유지하락고 관리하고 있다.


4

라벨 보기 ?

kubectl get pod --show-labels

NAME                    READY   STATUS    RESTARTS   AGE   LABELS

replicaset-cndk-fx6dn   1/1     Running   0          15s   app=cndk-nginx-pods

replicaset-cndk-gj8zv   1/1     Running   0          15s   app=cndk-nginx-pods

replicaset-cndk-xvhbl   1/1     Running   0          15s   app=cndk-nginx-pods


kubectl get pod -w



5

pod에 라벨 하나 제거 ?

kubectl label pod  <pod name> app-


라벨 제거

// app-   - 붙이면 라벨을 제거 하는 것이다.

kubectl label pod  replicaset-cndk-fx6dn app-

pod/replicaset-cndk-fx6dn labeled


라벨 보기

kubectl get pod --show-labels

NAME                    READY   STATUS    RESTARTS   AGE     LABELS

replicaset-cndk-5fv9m   1/1     Running   0          15s     app=cndk-nginx-pods

replicaset-cndk-fx6dn   1/1     Running   0          2m22s   <none>

replicaset-cndk-gj8zv   1/1     Running   0          2m22s   app=cndk-nginx-pods

replicaset-cndk-xvhbl   1/1     Running   0          2m22s   app=cndk-nginx-pods



6

kubectl delete rs replicaset-cndk

replicaset.apps "replicaset-cndk" deleted



7

레플리카 세트를 삭제하면  라벨 없는 pod만 살아 있다.

라벨이 있는것만 레플리카 세트가 관리한다.


ubuntu@ip-172-20-41-255:~$ kubectl get pod --show-labels

NAME                    READY   STATUS    RESTARTS   AGE     LABELS

replicaset-cndk-fx6dn   1/1     Running   0          3m41s   <none>



8

pod 모두 삭제

kubectl delete pod --all

pod "replicaset-cndk-fx6dn" deleted


// 장애 처리시 ?

라벨을 삭제하여 새로운 pod를 올리고, 살아 있는 pod에 접근해서 원인을 확인 


RS삭제 ?

kubectl delete rs --all




<5> 디플로이먼트 ?


레플리카 세트를 관리한다.

롤링 업데이트와 배포,롤백 제공한다.


1

구성?

디플로이먼트 ---- 레플리카셋 1 , 레플리카셋 2 로 구성

레플리카셋 1  아래에 pod 들

레플리카셋 2  아래에 pod 들



2

터미널2에서 모니터링

마스터 ?

watch -d 'kubectl get pods,rs,deploy -o wide'


3

clear


4

cat << EOF > deployment-cndk.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

  name: deployment-cndk

spec:

  replicas: 3

  selector:

    matchLabels:

      app: cndk-nginx-pods

  template:

    metadata:

      name: cndk-nginx-pod

      labels:

        app: cndk-nginx-pods

    spec:

      containers:

      - name: nginx

        image: nginx:latest

EOF



5

kubectl apply -f deployment-cndk.yaml

deployment.apps/deployment-cndk created



6

확인?

kubectl get deploy,rs,pods

NAME                              READY   UP-TO-DATE   AVAILABLE   AGE

deployment.apps/deployment-cndk   3/3     3            3           29s


NAME                                         DESIRED   CURRENT   READY   AGE

replicaset.apps/deployment-cndk-5f5d7bfcb8   3         3         3       29s


NAME                                   READY   STATUS    RESTARTS   AGE

pod/deployment-cndk-5f5d7bfcb8-4vttz   1/1     Running   0          29s

pod/deployment-cndk-5f5d7bfcb8-9fhrm   1/1     Running   0          29s

pod/deployment-cndk-5f5d7bfcb8-rntrh   1/1     Running   0          29s

// deploy,rs,pods 가 생성 되어 있다.


7

deployments 확인 ?

kubectl get deployments.apps -o wide

NAME              READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR

deployment-cndk   3/3     3            3           88s   nginx        nginx:latest   app=cndk-nginx-pods


8

deployments 상세 확인 ?

kubectl describe deployments.apps deployment-cndk


9

리플리카 셋 확인?

kubectl get replicasets.apps -o wide

NAME                         DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES         SELECTOR

deployment-cndk-5f5d7bfcb8   3         3         3       2m55s   nginx        nginx:latest   app=cndk-nginx-pods,pod-template-hash=5f5d7bfcb8


10

파드 , 파드 라벨 확인?

kubectl get pod --show-labels

NAME                               READY   STATUS    RESTARTS   AGE     LABELS

deployment-cndk-5f5d7bfcb8-4vttz   1/1     Running   0          3m20s   app=cndk-nginx-pods,pod-template-hash=5f5d7bfcb8

deployment-cndk-5f5d7bfcb8-9fhrm   1/1     Running   0          3m20s   app=cndk-nginx-pods,pod-template-hash=5f5d7bfcb8

deployment-cndk-5f5d7bfcb8-rntrh   1/1     Running   0          3m20s   app=cndk-nginx-pods,pod-template-hash=5f5d7bfcb8



11

파드 갯수 증가 ?

kubectl scale deployment --replicas=6 deployment-cndk

deployment.apps/deployment-cndk scaled


kubectl get pod --show-labels

NAME                               READY   STATUS    RESTARTS   AGE     LABELS

deployment-cndk-5f5d7bfcb8-4vttz   1/1     Running   0          4m22s   app=cndk-nginx-pods,pod-

deployment-cndk-5f5d7bfcb8-9fhrm   1/1     Running   0          4m22s   app=cndk-nginx-

deployment-cndk-5f5d7bfcb8-rntrh   1/1     Running   0          4m22s   app=cndk-nginx-pods,pod-

deployment-cndk-5f5d7bfcb8-svv4b   1/1     Running   0          6s      app=cndk-nginx-pods,pod-

deployment-cndk-5f5d7bfcb8-vz5ml   1/1     Running   0          6s      app=cndk-nginx-pods,pod-

deployment-cndk-5f5d7bfcb8-whh6l   1/1     Running   0          6s      app=cndk-nginx-pods,pod-

// 6개로 증가됨



12

pod 삭제

kubectl delete pod --all

//  pod 삭제가 안된다.

// pod 다시 6개 됨


13

rs 삭제

kubectl delete replicasets.apps --all

// replicasets 삭제가 안된다.

// 디플로이 먼트가 replicasets를 다시 생성한다.


14

디플로이 먼트 삭제

kubectl delete deployments.apps deployment-cndk

deployment.apps "deployment-cndk" deleted

// 모두 삭제됨

// pod, rs ,deployment 모두 삭제됨.

// 디플로이 먼트 삭제를 해야 모두 삭제 된다!



15

확인 ?

kubectl get deploy,rs,pod

No resources found in default namespace.





<6> 롤링 업데이트 와 롤백


롤링 업데이트  = 순차적으로 하나씩 Pod 를 업데이트 하는것.


1

clear


2

deployment-nginx-1.yaml 

nginx v1.11 확인


3

cat << EOF > deployment-nginx-1.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

  name: deployment-nginx

spec:

  replicas: 3

  selector:

    matchLabels:

      app: cndk-nginx-pods

  template:

    metadata:

      name: cndk-nginx-pod

      labels:

        app: cndk-nginx-pods

    spec:

      containers:

      - name: nginx

        image: nginx:1.11

EOF



4

// 레코드를 추가해 생성 한다 !!

kubectl apply -f deployment-nginx-1.yaml --record

deployment.apps/deployment-nginx configured


5

pod 정보 확인?

kubectl get pod -o yaml | grep "image: nginx"

    - image: nginx:1.11

    - image: nginx:1.11

    - image: nginx:1.11


배포 히스토리 확인 명령어

kubectl rollout history deployment deployment-nginx

ectl rollout history deployment deployment-nginx

deployment.apps/deployment-nginx

REVISION  CHANGE-CAUSE

1         kubectl apply --filename=deployment-nginx-1.yaml --record=true



6

업데이트 실행 ?

set image  로  이미지를 set image로  변경해 보자.  


kubectl set image deployment deployment-nginx nginx=nginx:1.12 --record && kubectl get pod -w

kubectl get pod -o yaml | grep "image: nginx"


NAME                                READY   STATUS              RESTARTS   AGE

deployment-nginx-77b4cbf48d-2mtj8   1/1     Running             0          90s

deployment-nginx-77b4cbf48d-bb2g2   1/1     Running             0          90s

deployment-nginx-77b4cbf48d-bc598   1/1     Running             0          90s

deployment-nginx-78bc6dfdf7-cx6lw   0/1     ContainerCreating   0          0s

deployment-nginx-78bc6dfdf7-cx6lw   0/1     ContainerCreating   0          0s

deployment-nginx-78bc6dfdf7-cx6lw   1/1     Running             0          1s

deployment-nginx-77b4cbf48d-bc598   1/1     Terminating         0          91s

deployment-nginx-78bc6dfdf7-96wqb   0/1     Pending             0          0s

deployment-nginx-78bc6dfdf7-96wqb   0/1     Pending             0          0s

deployment-nginx-78bc6dfdf7-96wqb   0/1     ContainerCreating   0          0s

deployment-nginx-77b4cbf48d-bc598   1/1     Terminating         0          91s

deployment-nginx-78bc6dfdf7-96wqb   0/1     ContainerCreating   0          0s

deployment-nginx-77b4cbf48d-bc598   0/1     Terminating         0          92s

deployment-nginx-78bc6dfdf7-96wqb   1/1     Running             0          2s

deployment-nginx-77b4cbf48d-2mtj8   1/1     Terminating         0          93s

deployment-nginx-78bc6dfdf7-6xr7w   0/1     Pending             0          0s

deployment-nginx-78bc6dfdf7-6xr7w   0/1     Pending             0          0s

deployment-nginx-78bc6dfdf7-6xr7w   0/1     ContainerCreating   0          0s

deployment-nginx-77b4cbf48d-2mtj8   1/1     Terminating         0          93s

deployment-nginx-78bc6dfdf7-6xr7w   0/1     ContainerCreating   0          0s

deployment-nginx-78bc6dfdf7-6xr7w   1/1     Running             0          1s

deployment-nginx-77b4cbf48d-2mtj8   0/1     Terminating         0          94s

deployment-nginx-77b4cbf48d-bb2g2   1/1     Terminating         0          94s

deployment-nginx-77b4cbf48d-bb2g2   1/1     Terminating         0          94s

deployment-nginx-77b4cbf48d-bb2g2   0/1     Terminating         0          95s

deployment-nginx-77b4cbf48d-2mtj8   0/1     Terminating         0          103s

deployment-nginx-77b4cbf48d-2mtj8   0/1     Terminating         0          103s

deployment-nginx-77b4cbf48d-bc598   0/1     Terminating         0          103s

deployment-nginx-77b4cbf48d-bc598   0/1     Terminating         0          103s

deployment-nginx-77b4cbf48d-bb2g2   0/1     Terminating         0          107s

deployment-nginx-77b4cbf48d-bb2g2   0/1     Terminating         0          107s



업데이트 확인

kubectget pod -o yaml | grep "image: nginx"-w

    - image: nginx:1.12

    - image: nginx:1.12

    - image: nginx:1.12



7

1.13으로 업데이트?

kubectl set image deployment deployment-nginx nginx=nginx:1.13 --record && kubectl get pod -w

kubectl get pod -o yaml | grep "image: nginx"


8

1.14 버전으로 업데이트 ?

kubectl set image deployment deployment-nginx nginx=nginx:1.14 --record && kubectl get pod -w

kubectl get pod -o yaml | grep "image: nginx"


9

히스토리 확인 ?

kubectl rollout history deployment deployment-nginx

EVISION  CHANGE-CAUSE

6         kubectl apply --filename=deployment-nginx-1.yaml --record=true

7         kubectl set image deployment deployment-nginx nginx=nginx:1.12 --record=true

8         kubectl set image deployment deployment-nginx nginx=nginx:1.13 --record=true

9         kubectl set image deployment deployment-nginx nginx=nginx:1.14 --record=true



10

롤백 ?

kubectl rollout undo deployment deployment-nginx --to-revision=7

deployment.apps/deployment-nginx rolled back


11

kubectl get pod -o yaml | grep "image: nginx"

    - image: nginx:1.12

    - image: nginx:1.12

    - image: nginx:1.12



12

라벨로 구분 되어 있어 롤백이 가능하다 ?

 kubectl get replicasets.apps --show-labels

NAME                          DESIRED   CURRENT   READY   AGE   LABELS

deployment-nginx-5cbcc59b4c   0         0         0       45h   app=cndk-nginx-pods,pod-template-deployment-nginx-67cc8cd4f4   0         0         0       45h   app=cndk-nginx-pods,pod-template-

deployment-nginx-77b4cbf48d   0         0         0       45h   app=cndk-nginx-pods,pod-template-

deployment-nginx-78bc6dfdf7   3         3         3       45h   app=cndk-nginx-pods,pod-template-



13

상세 확인? 

kubectl describe deployments.apps deployment-nginx


14

watch -d 'kubectl get pods,rs,deploy -o wide'


NAME                               READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES       SELECTOR

deployment.apps/deployment-nginx   3/3     3            3           47s   nginx        nginx:1.11   app=cndk-nginx-pods



15

디프롤이먼트 삭제해야 다 삭제된다.


kubectl delete deployments.apps --all

deployment.apps "deployment-nginx" deleted




<7>  정리


1

쿠버네티스는   컨트롤러라는 놈이 있다.

원하는 상태로 만들어 준다.


2

 ReplicaSet이 있다.

파드 수를 관리한다. 


3

디플로이 먼트가 있다.

리플리케 세트를 관리하는 상위 오브젝트이다.

리플리카세트와 파드를 조정하며, 이중화 되도록 한다.

롤링 업데이트와 롤백도 지원한다.



<8> 다음 과정


https://brunch.co.kr/@topasvga/1675



감사합니다.

매거진의 이전글 쿠버1탄-9. 쿠버네티스 -ConfigMap

작품 선택

키워드 선택 0 / 3 0

댓글여부

afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari