k8s/CKA

Practice Test / Backup and Restore Methods

부엉이사장 2024. 9. 12. 09:58
Summary
  • 버전확인은 etcd pod의 describe나 logs로 확인
  • etcd cluster ip는  listen-client-urls으로 grep하면됨
  • kubectl -n 네임스페이스 이런식으로 치고 get pod 머시기 하면 자동완성 됨
  • backup은 6번
  • restore은 8번
  • restore에서는 백업파일경로랑, data dir을 설정해줘야함. 보통은 /var/lib쪽에 하나 만들음
  • etcdctl명령어치고나서 etcd pod에서 volume의 hostpath를 저 data dir로 바꿔주샘
  • 난 --data-dir을 바꿔서 한동안 에러떳음 ㅡㅡ 이거 다시 수정하고 volume쪽 수정해주면 좀 지났다가 다시 복구됨.
export samp="--dry-run=client -o yaml"
export grep="grep --color=auto -iC 3"
export now="--force --grace-period 0"

 

$samp, $grep, $now붙은건 이 명령어로 약어만든거니까 주의


 

# 1

웹이 돌아가는 k8s클러스터가 있는데 현재 설정을 확인하자는것 같음.

문제는 현재 deployment가 몇개있냐고 함.

kubectl get deployment

 

 

# 2

ETCD의 버전이 뭐냐고함

kubectl -n kube-system get pods etcd-controlplane

kube system ns에 etcd머시기 pod를 가 있는지 확인. 

kubectl -n kube-system describe pod etcd-controlplane

얘도 어쨋든 pod라 image로 돌아감.

버전을 보니 3.5.12버전

 

솔루션에선 logs로 확인하데

kubectl -n kube-system logs etcd-controlplane |grep version

 

 

 

# 3

어떤 아이피로 마스터 노드가 etcd클러스터에 접근할수 있냐고 함.

kubectl -n kube-system describe pod etcd-controlplane |grep listen

저 listen-client-urls이 엔드포인트임

저 아이피가 나중에 backup할때 사용되는거니 잘 알아두자. 

(보통 문제에서 그냥 아이피 자체를 주는경우도 있음)

 

 

 

 

# 4

etcd서버의 certificate파일이 어디있냐고 함

 

 

 

Operating etcd clusters for Kubernetes

etcd is a consistent and highly-available key value store used as Kubernetes' backing store for all cluster data. If your Kubernetes cluster uses etcd as its backing store, make sure you have a back up plan for the data. You can find in-depth information a

kubernetes.io

공식문서 링크임.

검색은 operate etcd하면됨

좀따 쓸 etcd 백업 코드인데(아래꺼)

저기 cert가 cert-file경로 적는곳임.

kubectl -n kube-system describe pod etcd-controlplane

etcd pod를 describe하면, 

command쪽에 다 있음.

저기 cert-file을 보면 ~~~server.crt파일임

 

 

 

# 5

ca certificate 파일은 어딨냐고함.

--cacert은 trusted-ca-file이라네?

 

 

맨마지막 --trusted-ca-file의 경로임

 

 

# 6

백업을 /opt/snapshot-pre-boot.db파일로 해보래.

ls /opt/

일단 해당경로에 백업파일은 없음. 여기에 백업파일 생기면 백업된거임.

공식문서에서 저 두번째 박스 코드를 이제 치면 됨.

참고로

kubectl config get-contexts
kubectl config current-context

이걸로 현제 컨텍스트 확인해보는게 중요하다.

kode kloud는 하나밖에 없어서 그냥 진행.

 

ETCDCTL_API=3 etcdctl --endpoints=https://127.0.0.1:2379 \
  --cacert=<trusted-ca-file> --cert=<cert-file> --key=<key-file> \
  snapshot save <backup-file-location>

얘가 양식

 

- endpoints에는 

kubectl -n kube-system describe pod etcd-controlplane |grep listen

3번에서 확인했던 아이피를 적어주면 됨.

 

- cacert에는 저기 맨마지막 trusted 머시기 경로 적어주면됨

- cert에는 위에서 네번째줄 cert-file경로 적어주면됨

- key에는 중간에 있는 key-file경로 적어주면 됨

- backup-file-location은 이렇게 만든 백업파일이 어느경로에 어떤 이름으로 생성될거냐 하는거니까 문제에서 준 경로 써주면 됨.

 

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

완성

확인을 해보면

ls /opt

잘 생성된걸 볼 수 있음

 

 

 

# 8

 

다음날아침 회의전화를 받았는데 우리 어플에 접근이 안된대. 클러스터를 확인해보라는데?

 

실제로 리소스들이 다 사라짐 ㄷㄷ

 

svc는 하나있긴한데 그냥 답이 다업다는거래..

 

 

 

# 9

restore해보래

아까 6번?에서 만든 백업파일로 restore해보라는거임.

공식문서에 restore이라는 단어로 검색해보면 etcdctl명령어 쓰는 방법 나옴.

etcdctl --data-dir <data-dir-location> snapshot restore snapshot.db

양식

data-dir은 /var/lib/에 경로 생성해주면 되고(꼭 var/lib에 안만들어도되는듯?)

아까 백업파일 경로는 /opt/snapshot-pre-boot.db이거였음

ETCDCTL_API=3 etcdctl  --data-dir /var/lib/etcd-from-backup snapshot restore /opt/snapshot-pre-boot.db

해당 디렉토리가 새로 잘 생겨났음.

 

그리고 etcd pod에서 저 데이터 디렉토리 path를 수정해줘야함.

etcd pod는 static pod로 운영되니까

/etc/kubernetes/manifest에서 etcd yaml을 수정해줘야함.

찾았다 요놈.

저 밑에 볼륨마운트 path를 /var/lib/etcd-from-backup요걸로 수정 후 저장.

pod와 deployment, svc가 다시 잘 생성됐음.

 

static pod는 지우면 다시 생기니까 지웠다가 다시 만들어도 됨.