CKA

Cluster Maintenance

백셀건전지 2021. 9. 17. 17:19
  • Worker node가 5분 이상 문제가 발생하면, 그 worker node에 있는 pod들은 terminated되고 다른 worker node에 생김
  • kubectl drain node-1 명령어로 문제가 있는 worker node의 pod들을 다른 worker node에 띄움. 
  • node-1은 수동으로 제한을 풀지 않는 이상 cordon되고 unschedulable로 표시됨
  • kubectl uncordon node-1 : 제한 풀어서 pod 가 node-1에 schedule되도록 함
  • kubectl cordon node-1: node-1 에 pod를 unschedule하도록 함
  • pod가 node에 있을 때 drain하면 pod는 삭제되어 버리므로 주의

K8S Versions

  • v1.22.1  = major.minor.patch
  • minor version은 몇달마다 한번씩 new feature & functionalities 와 함께 업데이트
  • 첫 번째 v1.0 은 2015년 7월
  • alpha version은 feature이 disable되어있고, beta는 enabled 되어있음
  • ETCD와 CoreDNS는 자체 version이 있음

Cluster Upgrade

  • Core components들은 버전을 다르게 가져갈 수 있지만, kube-apiserver보다는 낮은 버전을 사용해야 함
  • kubectl은 상관없음
  • 현재 버전의 -2까지 지원
  • 버전 upgrade시 한 번에 최신으로 올리지 않고 한 minor version씩 차례차례 올리는 것이 좋음
  • 방법은 크게 master node upgrade / worker node upgrade로 나뉨
  • master node가 업그레이드를 위해 잠시 내려가도 worker node에 당장의 영향은 없음 but all management functions are down
  • master node가 upgrade 된 후에 worker node를 upgrade. 한 번에 upgrade하면 pod 다 내려가서 서비스 X
  • 한 번에 한 node씩 upgrade를 진행하면 pod를 실행중인 node로 옮겨가며 서비스 진행하면서 업그레이드 가능

kubeadm upgrade

  • kubeadm upgrade plan : 여러 정보를 볼 수 있음.
  • kubeadm 명령어 자체가 worker node를 업그레이드하지 않고, kubelet이 진행함.
  • k8s upgrade하려면 kubeadm이 먼저 업그레이드 되어있어야.

upgrade 순서

  1. apt-get upgrade -y kubeadm=1.12.0-00
  2. kubeadm upgrade apply v1.12.0 : 특정 버전으로 upgrade
  3. apt-get upgrade -y kubelet=1.12.0-00
  4. systemctl restart kubelet
  5. kubectl drain node-1
  6. apt-get upgrade -y kubeadm=1.12.0-00
  7. apt-get upgrade -y kubelet=1.12.0-00
  8. kubeadm upgrade node config --kubelet-version v1.12.0
  9. systemctl restart kubelet
  10. kubectl uncordon node-1

Control plane 업그레이드

On the controlplane node, run the command run the following commands:
  1. apt update
    This will update the package lists from the software repository.

  2. apt install kubeadm=1.20.0-00
    This will install the kubeadm version 1.20

  3. kubeadm upgrade apply v1.20.0
    This will upgrade kubernetes controlplane. Note that this can take a few minutes.

  4. apt install kubelet=1.20.0-00 This will update the kubelet with the version 1.20.

  5. You may need to restart kubelet after it has been upgraded.
    Run: systemctl restart kubelet

 

Worker node upgrade

On the node01 node, run the command run the following commands:

  1. If you are on the master node, run ssh node01 to go to node01

  2. apt update
    This will update the package lists from the software repository.

  3. apt install kubeadm=1.20.0-00
    This will install the kubeadm version 1.20

  4. kubeadm upgrade node
    This will upgrade the node01 configuration.

  5. apt install kubelet=1.20.0-00 This will update the kubelet with the version 1.20.

  6. You may need to restart kubelet after it has been upgraded.
    Run: systemctl restart kubelet

  7. Type exit or enter CTL + d to go back to the controlplane node.

 

Backup & Restore

  • kubectl get all --all-namespaces -o yaml > all-deploy-services.yaml :클러스터 내의 모든 리소스 yaml파일로 생성
  • Velero 와 같은 tool 사용하여 backup 가능
  • etcd cluster를 backup할 수도 있음

etcd 백업 순서

  1. etcd.service 파일의 --data-dir에 모든 데이터가 저장되어 있음.
  2. service kube-apiserver stop
  3. etcdctl snapshot save snapshot.db : etcd 스냅샷 찍는 명령어
  4. etcdctl snapshot status snapshot.db : snapshot status 확인
  5. etcdctl snapshot restore snapshot.db --data-dir /var/lib/etcd-from-backup
  6. systemctl daemon-reload
  7. service etcd restart
  8. service kube-apiserver start

 

1. Get etcdctl utility if it's not already present.
go get github.com/coreos/etcd/etcdctl

2. Backup
ETCDCTL_API=3 etcdctl --endpoints=https://[127.0.0.1]:2379 --cacert=/etc/kubernetes/pki/etcd/ca.crt \
     --cert=/etc/kubernetes/pki/etcd/server.crt --key=/etc/kubernetes/pki/etcd/server.key \
          snapshot save /opt/snapshot-pre-boot.db

          -----------------------------

          Disaster Happens

          -----------------------------
3. Restore ETCD Snapshot to a new folder
ETCDCTL_API=3 etcdctl --endpoints=https://[127.0.0.1]:2379 --cacert=/etc/kubernetes/pki/etcd/ca.crt \
     --name=master \
     --cert=/etc/kubernetes/pki/etcd/server.crt --key=/etc/kubernetes/pki/etcd/server.key \
     --data-dir /var/lib/etcd-from-backup \
     --initial-cluster=master=https://127.0.0.1:2380 \
     --initial-cluster-token etcd-cluster-1 \
     --initial-advertise-peer-urls=https://127.0.0.1:2380 \
     snapshot restore /opt/snapshot-pre-boot.db

 4. Modify /etc/kubernetes/manifests/etcd.yaml
 Update --data-dir to use new target location
 --data-dir=/var/lib/etcd-from-backup

 Update new initial-cluster-token to specify new cluster
 --initial-cluster-token=etcd-cluster-1

 Update volumes and volume mounts to point to new path
      volumeMounts:
          - mountPath: /var/lib/etcd-from-backup
            name: etcd-data
          - mountPath: /etc/kubernetes/pki/etcd
            name: etcd-certs
   hostNetwork: true
   priorityClassName: system-cluster-critical
   volumes:
   - hostPath:
       path: /var/lib/etcd-from-backup
       type: DirectoryOrCreate
     name: etcd-data
   - hostPath:
       path: /etc/kubernetes/pki/etcd
       type: DirectoryOrCreate
     name: etcd-certs