[K8s] 컨트롤러(Controller)

Aiden·2021년 6월 27일
0

K8S Study

목록 보기
7/9
post-thumbnail

1. 컨트롤러

1-1. Desired State & Controller

2. 레플리카셋 & 디플로이먼트

2-1. 레플리카셋 (Replica Set)

일정 갯수의 파드를 유지하는 컨트롤러

레플리카셋의 역할

  • 정해진 수의 동일한 Pod가 항상 실행되도록 관리
  • 노드 장애 등의 이유로 Pod를 사용할 수 없다면, 다른 노드에서 Pod를 다시 생성

YAML 템플릿

  • replicaset-cndk.yaml
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
  • spec.replicas : 동일한 포드를 몇 개 유지시킬 것인지 설정
  • spec.template : 포드를 생성 할 때 사용할 템플릿을 정의

생성 및 확인

  • Pod 생성
[root@master aiden (|kube:default)]# kubectl apply -f replicaset-cndk.yaml
replicaset.apps/replicaset-cndk created
  • 레플리카셋 확인
[root@master aiden (|kube:default)]# kubectl describe replicasets.apps replicaset-cndk
Name:         replicaset-cndk
Namespace:    default
Selector:     app=cndk-nginx-pods
Labels:       <none>
Annotations:  <none>
Replicas:     3 current / 3 desired
Pods Status:  3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=cndk-nginx-pods
  Containers:
   nginx:
    Image:        nginx:latest
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From                   Message
  ----    ------            ----  ----                   -------
  Normal  SuccessfulCreate  2m1s  replicaset-controller  Created pod: replicaset-cndk-wxwl8
  Normal  SuccessfulCreate  2m1s  replicaset-controller  Created pod: replicaset-cndk-h6nxz
  Normal  SuccessfulCreate  2m1s  replicaset-controller  Created pod: replicaset-cndk-x45pv
[root@master aiden (|kube:default)]# kubectl get replicasets.apps -o wide
NAME              DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES         SELECTOR
replicaset-cndk   3         3         3       2m31s   nginx        nginx:latest   app=cndk-nginx-pods
  • Pod 확인
[root@master aiden (|kube:default)]# kubectl get pod --show-labels
NAME                    READY   STATUS    RESTARTS   AGE     LABELS
replicaset-cndk-h6nxz   1/1     Running   0          3m20s   app=cndk-nginx-pods
replicaset-cndk-wxwl8   1/1     Running   0          3m20s   app=cndk-nginx-pods
replicaset-cndk-x45pv   1/1     Running   0          3m20s   app=cndk-nginx-pods
[root@master aiden (|kube:default)]# kubectl get pod -o wide
NAME                    READY   STATUS    RESTARTS   AGE     IP               NODE      NOMINATED NODE   READINESS GATES
replicaset-cndk-h6nxz   1/1     Running   0          3m25s   172.16.189.69    worker2   <none>           <none>
replicaset-cndk-wxwl8   1/1     Running   0          3m25s   172.16.235.152   worker1   <none>           <none>
replicaset-cndk-x45pv   1/1     Running   0          3m25s   172.16.182.3     worker3   <none>           <none>

Desired State 변경

  • replicas 변경(3 → 6)
[root@master aiden (|kube:default)]# kubectl edit replicasets.apps replicaset-cndk
... 생략 ...
spec:
  replicas: 6  # 기존의 3을 6으로 변경
... 생략 ...
:wq  # 저장
replicaset.apps/replicaset-cndk edited
  • 확인
[root@master aiden (|kube:default)]# kubectl get pods
NAME                    READY   STATUS    RESTARTS   AGE
replicaset-cndk-gd2d8   1/1     Running   0          90s
replicaset-cndk-h6nxz   1/1     Running   0          5m40s
replicaset-cndk-jfhqg   1/1     Running   0          90s
replicaset-cndk-tjv45   1/1     Running   0          90s
replicaset-cndk-wxwl8   1/1     Running   0          5m40s
replicaset-cndk-x45pv   1/1     Running   0          5m40s

삭제

  • 레플리카셋 삭제 및 확인
[root@master aiden (|kube:default)]# kubectl delete replicasets.apps replicaset-cndk
replicaset.apps "replicaset-cndk" deleted
[root@master aiden (|kube:default)]# kubectl get pods
No resources found in default namespace.

2-2. 레플리카셋 특징

selector.matchLabels 이 중복되는 파드를 생성 시 동작 확인

  • 레플리카셋 재생성 및 확인
[root@master aiden (|kube:default)]# kubectl apply -f replicaset-cndk.yaml
replicaset.apps/replicaset-cndk created
[root@master aiden (|kube:default)]# kubectl get replicasets.apps -o wide
NAME              DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES         SELECTOR
replicaset-cndk   3         3         3       30s   nginx        nginx:latest   app=cndk-nginx-pods
  • YAML 정의(위 파드와 Label이 동일)
cat << EOF > 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
EOF
  • 생성 및 확인
[root@master aiden (|kube:default)]# kubectl apply -f pod-rs.yaml && kubectl get pod -w
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-9ljwt   1/1     Running       0          2m48s
replicaset-cndk-cnnff   1/1     Running       0          2m48s
replicaset-cndk-fnltr   1/1     Running       0          2m48s
pod1                    0/1     Terminating   0          2s
pod1                    0/1     Terminating   0          2s
pod2                    0/1     Terminating   0          11s
pod2                    0/1     Terminating   0          11s

⇒ 생성 되자마자 삭제된다

Pod 삭제시 동작

  • Pod 삭제
[root@master aiden (|kube:default)]# kubectl delete pod -l app=cndk-nginx-pods
pod "replicaset-cndk-cnnff" deleted
pod "replicaset-cndk-fnltr" deleted
pod "replicaset-cndk-txgkj" deleted
  • 상태 확인
NAME                    READY   STATUS              RESTARTS   AGE
replicaset-cndk-cnnff   0/1     Terminating         0          8m37s
replicaset-cndk-fnltr   0/1     Terminating         0          8m37s
replicaset-cndk-mjtgz   1/1     Running             0          6s
replicaset-cndk-nzjst   0/1     ContainerCreating   0          6s
replicaset-cndk-txgkj   0/1     Terminating         0          3m10s
replicaset-cndk-z2n4f   1/1     Running             0          6s

⇒ 레플리카셋의 Desired state에 따라 3개의 Pod가 삭제되어도 바로 3개가 재생성된다

레플리카셋의 통제 범위에 벗어난 파드

  • 현재 Pod의 Label 재확인
[root@master aiden (|kube:default)]# kubectl get pod --show-labels
NAME                    READY   STATUS    RESTARTS   AGE     LABELS
replicaset-cndk-9ljwt   1/1     Running   0          4m46s   app=cndk-nginx-pods
replicaset-cndk-cnnff   1/1     Running   0          4m46s   app=cndk-nginx-pods
replicaset-cndk-fnltr   1/1     Running   0          4m46s   app=cndk-nginx-pods
  • 파드 1개 label 제거
[root@master aiden (|kube:default)]# kubectl label pod replicaset-cndk-9ljwt app-
pod/replicaset-cndk-9ljwt labeled
  • Pods 상태 확인
[root@master aiden (|kube:default)]# kubectl get pod --show-labels
NAME                    READY   STATUS    RESTARTS   AGE     LABELS
replicaset-cndk-9ljwt   1/1     Running   0          5m56s   <none>
replicaset-cndk-cnnff   1/1     Running   0          5m56s   app=cndk-nginx-pods
replicaset-cndk-fnltr   1/1     Running   0          5m56s   app=cndk-nginx-pods
replicaset-cndk-txgkj   1/1     Running   0          29s     app=cndk-nginx-pods

⇒ 레이블을 제거한 Pod는 레플리카셋의 통제에서 벗어난다. 장애시 분석을 위한 용도로 활용.

2-3. 디플로이먼트

레플리카셋의 상위 오브젝트이며, 애플리케이션 업데이트(롤링 업데이트) 및 배포 기능(롤백)을 제공

YAML 템플릿

  • deployment-cndk.yaml
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

생성 및 확인

  • 생성
[root@master aiden (|kube:default)]# kubectl apply -f deployment-cndk.yaml
deployment.apps/deployment-cndk created
  • 디플로이먼트, 레플리카셋, 파드 확인
[root@master aiden (|kube:default)]# kubectl get deploy,rs,pods
NAME                              READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/deployment-cndk   3/3     3            3           31s

NAME                                         DESIRED   CURRENT   READY   AGE
replicaset.apps/deployment-cndk-5f5d7bfcb8   3         3         3       31s

NAME                                   READY   STATUS    RESTARTS   AGE
pod/deployment-cndk-5f5d7bfcb8-dlbfc   1/1     Running   0          31s
pod/deployment-cndk-5f5d7bfcb8-fd54n   1/1     Running   0          31s
pod/deployment-cndk-5f5d7bfcb8-w9j4w   1/1     Running   0          31s
  • 디플로이먼트 확인
[root@master aiden (|kube:default)]# kubectl get deployments.apps -o wide
NAME              READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
deployment-cndk   3/3     3            3           60s   nginx        nginx:latest   app=cndk-nginx-pods

설정 변경

  • replicas 수 조정(3 → 6)
[root@master aiden (|kube:default)]# kubectl scale deployment --replicas=6 deployment-cndk
deployment.apps/deployment-cndk scaled
  • 확인
[root@master aiden (|kube:default)]# kubectl get deploy,rs,pods
NAME                              READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/deployment-cndk   6/6     6            6           4m12s

NAME                                         DESIRED   CURRENT   READY   AGE
replicaset.apps/deployment-cndk-5f5d7bfcb8   6         6         6       4m12s

NAME                                   READY   STATUS    RESTARTS   AGE
pod/deployment-cndk-5f5d7bfcb8-dlbfc   1/1     Running   0          4m12s
pod/deployment-cndk-5f5d7bfcb8-fd54n   1/1     Running   0          4m12s
pod/deployment-cndk-5f5d7bfcb8-mvn5j   1/1     Running   0          89s
pod/deployment-cndk-5f5d7bfcb8-pfw75   1/1     Running   0          89s
pod/deployment-cndk-5f5d7bfcb8-ss2dk   1/1     Running   0          89s
pod/deployment-cndk-5f5d7bfcb8-w9j4w   1/1     Running   0          4m12s

Pod 삭제

  • 삭제
[root@master aiden (|kube:default)]# kubectl delete pod --all
pod "deployment-cndk-5f5d7bfcb8-dlbfc" deleted
pod "deployment-cndk-5f5d7bfcb8-fd54n" deleted
pod "deployment-cndk-5f5d7bfcb8-mvn5j" deleted
pod "deployment-cndk-5f5d7bfcb8-pfw75" deleted
pod "deployment-cndk-5f5d7bfcb8-ss2dk" deleted
pod "deployment-cndk-5f5d7bfcb8-w9j4w" deleted
  • 확인
NAME                               READY   STATUS        RESTARTS  AGE
deployment-cndk-5f5d7bfcb8-4kj52   1/1     Running       0         12s
deployment-cndk-5f5d7bfcb8-cc5rx   1/1     Running       0         12s
deployment-cndk-5f5d7bfcb8-dlbfc   0/1     Terminating   0         4m59s
deployment-cndk-5f5d7bfcb8-mvn5j   0/1     Terminating   0         2m16s
deployment-cndk-5f5d7bfcb8-rnht9   1/1     Running       0         12s
deployment-cndk-5f5d7bfcb8-v8hsp   1/1     Running       0         12s
deployment-cndk-5f5d7bfcb8-vzbff   1/1     Running       0         12s
deployment-cndk-5f5d7bfcb8-x26bk   1/1     Running       0         12s

⇒ 6개 삭제 후 6개 재생성

레플리카셋 삭제

  • 삭제
[root@master aiden (|kube:default)]# kubectl delete replicasets.apps --all
replicaset.apps "deployment-cndk-5f5d7bfcb8" deleted
  • 확인
[root@master aiden (|kube:default)]# kubectl get deploy,rs,pods
NAME                              READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/deployment-cndk   6/6     6            6           6m32s

NAME                                         DESIRED   CURRENT   READY   AGE
replicaset.apps/deployment-cndk-5f5d7bfcb8   6         6         6       31s

NAME                                   READY   STATUS    RESTARTS   AGE
pod/deployment-cndk-5f5d7bfcb8-2cq54   1/1     Running   0          31s
pod/deployment-cndk-5f5d7bfcb8-j2flb   1/1     Running   0          31s
pod/deployment-cndk-5f5d7bfcb8-n5pjk   1/1     Running   0          31s
pod/deployment-cndk-5f5d7bfcb8-n68bj   1/1     Running   0          31s
pod/deployment-cndk-5f5d7bfcb8-p9mpr   1/1     Running   0          31s
pod/deployment-cndk-5f5d7bfcb8-phcvb   1/1     Running   0          31s

⇒ 레플리카셋 삭제에 따라 Pod들도 함께 삭제되지만, 디플로이먼트의 상태로 인해 레플리카셋 및 파드 재생성

디플로이먼트 삭제

  • 삭제
[root@master aiden (|kube:default)]# kubectl delete deployments.apps deployment-cndk
deployment.apps "deployment-cndk" deleted
  • 확인
[root@master aiden (|kube:default)]# kubectl get deploy,rs,pods
No resources found in default namespace.

3. 디플로이먼트 롤링 업데이트 및 롤백

3-1. 롤링 업데이트 / 롤백

  • 디플로이먼트는 레플리카셋 관리로 롤링 업데이트 가능 및 변경 사항을 저장(리비전 revision)을 통해 롤백 가능

YAML 템플릿

  • deployment-nginx-1.yaml : nginx v1.11 확인
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
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

생성 및 확인

  • 생성
[root@master aiden (|kube:default)]# kubectl apply -f deployment-nginx-1.yaml --record
deployment.apps/deployment-nginx created
  • 파드 정보 화인
[root@master aiden (|kube:default)]# kubectl get pod -o wide
NAME                                READY   STATUS    RESTARTS   AGE   IP               NODE      NOMINATED NODE   READINESS GATES
deployment-nginx-77b4cbf48d-576cr   1/1     Running   0          41s   172.16.189.84    worker2   <none>           <none>
deployment-nginx-77b4cbf48d-5c5hg   1/1     Running   0          41s   172.16.182.17    worker3   <none>           <none>
deployment-nginx-77b4cbf48d-lpp7g   1/1     Running   0          41s   172.16.235.169   worker1   <none>           <none>
  • Pod 이미지 확인
[root@master aiden (|kube:default)]# kubectl get pod -o yaml | grep "image: nginx"    - image: nginx:1.11
      image: nginx:1.11
    - image: nginx:1.11
      image: nginx:1.11
    - image: nginx:1.11
      image: nginx:1.11
  • —record 옵션 정보 확인
[root@master aiden (|kube:default)]# kubectl rollout history deployment deployment-nginx
deployment.apps/deployment-nginx 
REVISION  CHANGE-CAUSE
1         kubectl apply --filename=deployment-nginx-1.yaml --record=true

버전 업데이트 실행 및 확인

  • 터미널2 - 실시간 확인
while true; do kubectl rollout status deployment deployment-nginx && echo "--------------" && date; sleep 1; done
  • v1.12로 버전 업데이트 실행 및 확인
[root@master aiden (|kube:default)]# kubectl set image deployment deployment-nginx nginx=nginx:1.12 --record && kubectl get pod -w
deployment.apps/deployment-nginx image updated
NAME                                READY   STATUS              RESTARTS   AGE
deployment-nginx-77b4cbf48d-576cr   1/1     Running             0          3m20s
deployment-nginx-77b4cbf48d-5c5hg   1/1     Running             0          3m20s
deployment-nginx-77b4cbf48d-lpp7g   1/1     Running             0          3m20s
deployment-nginx-78bc6dfdf7-xmh8v   0/1     ContainerCreating   0          0s
deployment-nginx-78bc6dfdf7-xmh8v   0/1     ContainerCreating   0          0s
deployment-nginx-78bc6dfdf7-xmh8v   1/1     Running             0          10s
deployment-nginx-77b4cbf48d-lpp7g   1/1     Terminating         0          3m30s
deployment-nginx-78bc6dfdf7-wzdxj   0/1     Pending             0          0s
deployment-nginx-78bc6dfdf7-wzdxj   0/1     Pending             0          0s
deployment-nginx-78bc6dfdf7-wzdxj   0/1     ContainerCreating   0          0s
deployment-nginx-77b4cbf48d-lpp7g   1/1     Terminating         0          3m30s
deployment-nginx-78bc6dfdf7-wzdxj   0/1     ContainerCreating   0          1s
deployment-nginx-77b4cbf48d-lpp7g   0/1     Terminating         0          3m31s
deployment-nginx-77b4cbf48d-lpp7g   0/1     Terminating         0          3m34s
deployment-nginx-77b4cbf48d-lpp7g   0/1     Terminating         0          3m34s
deployment-nginx-78bc6dfdf7-wzdxj   1/1     Running             0          11s
deployment-nginx-77b4cbf48d-5c5hg   1/1     Terminating         0          3m41s
deployment-nginx-78bc6dfdf7-xpktr   0/1     Pending             0          0s
deployment-nginx-78bc6dfdf7-xpktr   0/1     Pending             0          0s
deployment-nginx-78bc6dfdf7-xpktr   0/1     ContainerCreating   0          0s
deployment-nginx-77b4cbf48d-5c5hg   1/1     Terminating         0          3m41s
deployment-nginx-78bc6dfdf7-xpktr   0/1     ContainerCreating   0          1s
deployment-nginx-77b4cbf48d-5c5hg   0/1     Terminating         0          3m42s
deployment-nginx-78bc6dfdf7-xpktr   1/1     Running             0          1s
deployment-nginx-77b4cbf48d-576cr   1/1     Terminating         0          3m42s
deployment-nginx-77b4cbf48d-576cr   1/1     Terminating         0          3m43s
deployment-nginx-77b4cbf48d-576cr   0/1     Terminating         0          3m43s
deployment-nginx-77b4cbf48d-5c5hg   0/1     Terminating         0          3m49s
deployment-nginx-77b4cbf48d-5c5hg   0/1     Terminating         0          3m49s
deployment-nginx-77b4cbf48d-576cr   0/1     Terminating         0          3m53s
deployment-nginx-77b4cbf48d-576cr   0/1     Terminating         0          3m53s
  • 확인(터미널2)
Sun 27 Jun 2021 01:10:05 PM UTC
Waiting for deployment "deployment-nginx" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "deployment-nginx" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "deployment-nginx" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "deployment-nginx" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "deployment-nginx" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "deployment-nginx" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "deployment-nginx" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "deployment-nginx" rollout to finish: 1 old replicas are pending termination...
deployment "deployment-nginx" successfully rolled out
  • 버전업데이트 리스토리 확인
[root@master aiden (|kube:default)]# kubectl rollout history deployment deployment-nginx
deployment.apps/deployment-nginx 
REVISION  CHANGE-CAUSE
1         kubectl apply --filename=deployment-nginx-1.yaml --record=true
2         kubectl set image deployment deployment-nginx nginx=nginx:1.12 --record=true

버전 롤백 및 확인

  • 리비전 1로 버전 롤백
[root@master aiden (|kube:default)]# kubectl rollout undo deployment deployment-nginx --to-revision=1
deployment.apps/deployment-nginx rolled back
  • 레플리카셋 정보 확인 - 버전별로 Label로 구분하여 기록
[root@master aiden (|kube:default)]# kubectl get replicasets.apps --show-labels
NAME                          DESIRED   CURRENT   READY   AGE     LABELS
deployment-nginx-77b4cbf48d   3         3         3       10m     app=cndk-nginx-pods,pod-template-hash=77b4cbf48d
deployment-nginx-78bc6dfdf7   0         0         0       6m46s   app=cndk-nginx-pods,pod-template-hash=78bc6dfdf7
  • 히스토리 확인
[root@master aiden (|kube:default)]# kubectl rollout history deployment deployment-nginxdeployment.apps/deployment-nginx 
REVISION  CHANGE-CAUSE
2         kubectl set image deployment deployment-nginx nginx=nginx:1.12 --record=true
3         kubectl apply --filename=deployment-nginx-1.yaml --record=true

삭제

  • 디플로이먼트 삭제
[root@master aiden (|kube:default)]# kubectl delete deployments.apps --all
deployment.apps "deployment-nginx" deleted

4. 데몬셋

데몬셋(DaemonSets)은 쿠버네티스의 모든 노드에 동일한 파드를 하나씩 생성하는 오브젝트이다

로깅, 모니터링, 네트워킹 등을 위한 에이전트를 각 노드에 생성해야 할 때 유용하게 사용할 수 있다

4-1. 데몬셋 생성 및 확인

디폴트 데몬셋 파드 확인

  • 확인
[root@master aiden (|kube:default)]# kubectl get daemonsets.apps -n kube-system
NAME          DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR            AGE
calico-node   4         4         4       4            4           kubernetes.io/os=linux   11d
kube-proxy    4         4         4       4            4           kubernetes.io/os=linux   11d
  • 상세 확인
kubectl describe daemonsets.apps -n kube-system calico-node
kubectl describe daemonsets.apps -n kube-system kube-proxy

YAML 템플릿

  • daemonset-1.yaml
cat << EOF > daemonset-1.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: daemonset-1
spec:
  selector:
    matchLabels:
      name: daemonset-cndk
  template:
    metadata:             
      labels:
        name: daemonset-cndk
    spec:
      containers:
      - name: daemonset-pod
        image: nginx
EOF

생성 및 확인

  • (옵션) 터미널2 - 실시간 확인
watch -d 'kubectl get pods,ds -o wide'
  • 생성
[root@master aiden (|kube:default)]# kubectl apply -f daemonset-1.yaml
daemonset.apps/daemonset-1 created
  • 확인
[root@master aiden (|kube:default)]# kubectl get ds -o wide
NAME          DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE   CONTAINERS      IMAGES   SELECTOR
daemonset-1   3         3         3       3            3           <none>          21s   daemonset-pod   nginx    name=daemonset-cndk
[root@master aiden (|kube:default)]# kubectl get pod --show-labels
NAME                READY   STATUS    RESTARTS   AGE   LABELS
daemonset-1-h7nln   1/1     Running   0          26s   controller-revision-hash=88d8d4645,name=daemonset-cndk,pod-template-generation=1
daemonset-1-lzpxw   1/1     Running   0          26s   controller-revision-hash=88d8d4645,name=daemonset-cndk,pod-template-generation=1
daemonset-1-qll8p   1/1     Running   0          26s   controller-revision-hash=88d8d4645,name=daemonset-cndk,pod-template-generation=1
[root@master aiden (|kube:default)]# kubectl describe ds daemonset-1
Name:           daemonset-1
Selector:       name=daemonset-cndk
... 생략 ...
Events:
  Type    Reason            Age   From                  Message
  ----    ------            ----  ----                  -------
  Normal  SuccessfulCreate  31s   daemonset-controller  Created pod: daemonset-1-lzpxw
  Normal  SuccessfulCreate  31s   daemonset-controller  Created pod: daemonset-1-qll8p
  Normal  SuccessfulCreate  31s   daemonset-controller  Created pod: daemonset-1-h7nln

파드 삭제

  • 삭제
[root@master aiden (|[root@master aiden (|kube:default)]# kubectl delete pod --allpod "daemonset-1-h7nln" deleted
pod "daemonset-1-lzpxw" deleted
  • 확인(터미널2) - 진행중
Every 2.0s: kubectl get pods,ds -o wide                                                                                    master: Sun Jun 27 13:30:14 2021

NAME                    READY   STATUS        RESTARTS   AGE     IP              NODE      NOMINATED NODE   READINESS GATES
pod/daemonset-1-9fzwx   1/1     Running       0          7s      172.16.189.88   worker2   <none>           <none>
pod/daemonset-1-lzpxw   0/1     Terminating   0          2m38s   <none>          worker1   <none>           <none>
pod/daemonset-1-qll8p   0/1     Terminating   0          2m38s   172.16.182.20   worker3   <none>           <none>

NAME                         DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE     CONTAINERS      IMAGES   SELECTOR
daemonset.apps/daemonset-1   3         3         1       3            1           <none>          2m38s   daemonset-pod   nginx    name=daemonset-cndk
  • 확인(터미널2) - 완료
Every 2.0s: kubectl get pods,ds -o wide                                                                                    master: Sun Jun 27 13:30:36 2021

NAME                    READY   STATUS    RESTARTS   AGE   IP               NODE      NOMINATED NODE   READINESS GATES
pod/daemonset-1-9fzwx   1/1     Running   0          29s   172.16.189.88    worker2   <none>           <none>
pod/daemonset-1-q8zs8   1/1     Running   0          16s   172.16.235.173   worker1   <none>           <none>
pod/daemonset-1-sbgxt   1/1     Running   0          21s   172.16.182.21    worker3   <none>           <none>

NAME                         DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE   CONTAINERS      IMAGES   SELECTOR  
daemonset.apps/daemonset-1   3         3         3       3            3           <none>          3m    daemonset-pod   nginx    name=daemonset-cndk
  • 삭제
[root@master aiden (|kube:default)]# kubectl delete ds daemonset-1
daemonset.apps "daemonset-1" deleted

4-2. tolerations

YAML 템플릿

  • daemonset-2.yaml
cat << EOF > daemonset-2.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: daemonset-2
spec:
  selector:
    matchLabels:
      name: daemonset-cndk-2
  template:
    metadata:             
      labels:
        name: daemonset-cndk-2
    spec:
      tolerations:
      - effect: NoSchedule
        operator: Exists
      containers:
      - name: daemonset-pod-2
        image: nginx
EOF

생성 및 확인

  • 생성
[root@master aiden (|kube:default)]# kubectl apply -f daemonset-2.yaml
daemonset.apps/daemonset-2 created
  • 확인
[root@master aiden (|kube:default)]# kubectl get ds -o wide
NAME          DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE   CONTAINERS        IMAGES   SELECTOR
daemonset-2   4         4         4       4            4           <none>          19s   daemonset-pod-2   nginx    name=daemonset-cndk-2
[root@master aiden (|kube:default)]# kubectl get pod --show-labels
NAME                READY   STATUS    RESTARTS   AGE   LABELS
daemonset-2-g4q6m   1/1     Running   0          25s   controller-revision-hash=8bc7c45d6,name=daemonset-cndk-2,pod-template-generation=1
daemonset-2-k2kw9   1/1     Running   0          25s   controller-revision-hash=8bc7c45d6,name=daemonset-cndk-2,pod-template-generation=1
daemonset-2-qcw52   1/1     Running   0          25s   controller-revision-hash=8bc7c45d6,name=daemonset-cndk-2,pod-template-generation=1
daemonset-2-zj4nf   1/1     Running   0          25s   controller-revision-hash=8bc7c45d6,name=daemonset-cndk-2,pod-template-generation=1

5. 잡(Jobs)

잡(Job)은 특정 동작을 수행하고 종료해야 하는 작업을 위한 오브젝트이다 (파드가 실행되어 정상적으로 종료되는 것)

5-1. 잡(Jobs) 실행 및 확인

YAML 템플릿

  • job.yaml : 파이(π)의 2000 자리까지 계산해서 출력
cat << EOF > job.yaml
apiVersion: batch/v1
kind: Job
metadata:
  name: pi
spec:
  template:
    spec:
      containers:
      - name: pi
        image: perl
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never
  backoffLimit: 4
EOF

생성 및 확인

  • (옵션) 터미널2 - 실시간 확인
watch -d 'kubectl get pods,jobs -o wide'
  • 생성 및 확인
[root@master aiden (|kube:default)]# kubectl apply -f job.yaml && kubectl get pods -w
job.batch/pi created
NAME       READY   STATUS              RESTARTS   AGE
pi-z2cxv   0/1     ContainerCreating   0          0s
pi-z2cxv   0/1     ContainerCreating   0          1s
pi-z2cxv   1/1     Running             0          4s
pi-z2cxv   0/1     Completed           0          9s
pi-z2cxv   0/1     Completed           0          9s
  • Jobs 확인
[root@master aiden (|kube:default)]# kubectl get jobs.batch pi -o wide
NAME   COMPLETIONS   DURATION   AGE   CONTAINERS   IMAGES   SELECTOR
pi     1/1           9s         31s   pi           perl     controller-uid=611b632c-4202-4503-af49-df6ddf780d25
[root@master aiden (|kube:default)]# kubectl describe jobs.batch pi
Name:           pi
Namespace:      default
Selector:       controller-uid=611b632c-4202-4503-af49-df6ddf780d25
Labels:         controller-uid=611b632c-4202-4503-af49-df6ddf780d25
                job-name=pi
... 생략 ...
Events:
  Type    Reason            Age   From            Message
  ----    ------            ----  ----            -------
  Normal  SuccessfulCreate  44s   job-controller  Created pod: pi-z2cxv
  Normal  Completed         35s   job-controller  Job completed

5-2. 크론잡 (CronJobs) 실행 및 확인

  • 크론잡은 잡을 주어진 일정에 따라 주기적으로 동작, kube-controller-manager 의 timezone 을 기준으로 함
  • 크론잡은 백업 실행 또는 이메일 전송과 같은 정기적이고 반복적인 작업을 만드는데 유용

YAML 템플릿

  • cronjob.yaml
cat << EOF > cronjob.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"            # Job 실행 주기
  successfulJobsHistoryLimit: 10     # 성공 Job 최대 기록
  jobTemplate:                       # 실행될 Job 설정 내용(spec)
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            imagePullPolicy: IfNotPresent
            command:
            - /bin/sh
            - -c
            - date; echo Hello from the DKOS Member!
          restartPolicy: OnFailure
EOF

실행 및 확인

  • 옵션(터미널2) - 실시간
watch -d 'kubectl get pods,cronjobs -o wide'
  • 생성 및 확인
[root@master aiden (|kube:default)]# kubectl apply -f cronjob.yaml && kubectl get jobs --watch
cronjob.batch/hello created
NAME             COMPLETIONS   DURATION   AGE
hello-27080027   0/1                      0s
hello-27080027   0/1           0s         0s
hello-27080027   1/1           2s         2s
hello-27080028   0/1                      0s
hello-27080028   0/1           0s         0s
hello-27080028   1/1           1s         1s
[root@master aiden (|kube:default)]# kubectl get pod
NAME                   READY   STATUS      RESTARTS   AGE
hello-27080027-rd65w   0/1     Completed   0          2m23s
hello-27080028-fqbs7   0/1     Completed   0          83s
[root@master aiden (|kube:default)]# kubectl get cronjobs.batch
NAME    SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
hello   */1 * * * *   False     0        59s             3m21s
[root@master aiden (|kube:default)]# kubectl get jobs.batch
NAME             COMPLETIONS   DURATION   AGE
hello-27080027   1/1           2s         3m3s
hello-27080028   1/1           1s         2m3s
  • 확인(터미널2)
NAME                       READY   STATUS      RESTARTS   AGE   IP               NODE      NOMINATED NODE   READINESS GATES
pod/hello-27080027-rd65w   0/1     Completed   0          82s   172.16.235.177   worker1   <none>           <none>
pod/hello-27080028-fqbs7   0/1     Completed   0          22s   172.16.235.178   worker1   <none>           <none>

NAME                       COMPLETIONS   DURATION   AGE   CONTAINERS   IMAGES    SELECTOR
job.batch/hello-27080027   1/1           2s         82s   hello        busybox   controller-uid=e7e6484f-f843-44ad-baf7-3b590e2dcfd0
job.batch/hello-27080028   1/1           1s         22s   hello        busybox   controller-uid=fa6f3fdf-ee67-497d-94a5-08e2cff9ed59

삭제

  • cronjob 삭제
[root@master aiden (|kube:default)]# kubectl delete -f cronjob.yaml
cronjob.batch "hello" deleted
profile
기억이 안되면 기록이라도🐳

0개의 댓글