Kubernetes/따라하면서 배우는 쿠버네티스

[쿠버네티스] 6-4. DaemonSet! + RollingUpdate

맨날화남 2023. 8. 2. 15:00

 

https://youtu.be/wJeb561CMOg

 

DaemonSet

  • 전체 노드에서 pod가 한 개씩 실행되도록 보장
  • 로그 수집기, 모니터링 에이전트와 같은 프로그램 실행 시 적용

daemonset으로 실행 시 node가 2개 있으니 각 1개씩해서 총 2개 실행
노드가 추가됐을 시 추가된 노드에도 pod가 1개 실행된다.

  • 만약 pod에 문제가 생겼을 시 문제 생긴 pod가 삭제된 후 다시 생성한다.
  • 이렇게 모든 pod에서의 동작을 보장하기에 로그수집기나 모니터링 같이 모든 pod에 있어야 하는 거에 많이 쓰임

 

 

 

DaemonSet definition

ReplicaSet definition DaemonSet definition
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: rs-nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      name: nginx-pod
      labels:
        app: webui
    spec:
      containers:
      - name: nginx-container
        image: nginx:1.14
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: daemonset-nginx
spec:
  
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      name: nginx-pod
      labels:
        app: webui
    spec:
      containers:
      - name: nginx-container
        image: nginx:1.14

 

DaemonSet example

$cat daemonset-exam.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: daemonset-nginx
spec:
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      name: nginx-pod
      labels:
        app: webui
    spec:
      containers:
      - name: nginx-container
        image: nginx:1.14

 

TEST

현재 node는 2개인 상태에서 테스트 시작
daemonset yaml을 실행하면 node가 2개이므로 pod가 각 노드마다 1개씩, 총 2개 올라온다. 이제 node를 추가해보자

 

Node 추가

  • 우선 node를 하나 생성해두고 control-plane 접속해서 node를 추가해보자
  • node에서 control-plane에 join 할라면 token이 필요한데 token의 유효기간은 24시간이기에 처음에 생성한 token은 쓸 수 없다.
  • 그러니 control-plane에서 token을 새로 생성하여 join 하도록 하자
  • kubeadm token list : 토큰 목록 확인
  • kubeadm token create --ttl [시간] : 토큰 생상
  • kubectl reset : 해당 node가 control-plane에 연결했던 이력이 있었다면 node에서 reset으로 node 정보를 초기화한다.

첫 구축 이후 24시간이 지났다면 아무것도 나오지 않는다
1시간짜리 token 생성
맨 처음 구축때 저장해놨던 join에서 token 부분만 바꿔주면 끝이다.
node를 1개 추가하여 node가 총 3개가 됐다.
node 추가 후 아무것도 안해도 추가된 node에 pod가 자동적으로 올라왔다.

 

 

 

Rolling Update

  • DaemonSet도 Deploy처럼 Rolling Update 기능을 가지고 있다.
  • kubectl edit daemonset daemonset-nginx 로 확인해보자
apiVersion: apps/v1
kind: DaemonSet
metadata:
  annotations:
    deprecated.daemonset.template.generation: "1"
  creationTimestamp: "2023-08-02T04:52:46Z"
  generation: 1
  name: daemonset-nginx
  namespace: default
  resourceVersion: "580163"
  uid: da7b47b3-c3d9-4c2b-a71a-d934eefdbdf5
spec:
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: webui
      name: nginx-pod
    spec:
      containers:
      - image: nginx:1.14
        imagePullPolicy: IfNotPresent
        name: nginx-container
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
  updateStrategy:
    rollingUpdate:
      maxSurge: 0
      maxUnavailable: 1
    type: RollingUpdate
status:
  currentNumberScheduled: 3
  desiredNumberScheduled: 3
  numberAvailable: 3
  numberMisscheduled: 0
  numberReady: 3
  observedGeneration: 1
  updatedNumberScheduled: 3
  • revisionHistoryLimit, maxSurge, maxUnavailable 등 Rolling Update 옵션이 보인다.
  • Daemonset에서는 여기서 이미지의 버전만 바꾸면 update가 된다.

이미지를 1.15로 바꾸고 저장해보자
하나씩 업데이트가 진행된다.
kubectl describe pod <pod-name>으로 확인
kubectl rollout history로 확인했는데 버전 정보가 안나와있다.
Rolling update시 kubernetes.io/change-cause: version x.xx를 추가해주자
버전 정보가 등록된 것을 확인
1.18인 상황에서 전 버전으로 롤백 수행
1.17로 돌아가졌다.
history도 변경된 것을 확인