본문 바로가기
Cloud/Kubernetes

Kubernetes PodDisruptionBudget(PDB) 활용

by _Nate 2023. 5. 3.
반응형

Kubernetes를 사용하다 보면 최소한의 pod수를 유지해야 하는 경우가 있다

이런경우 PodDisruptionBudget(이하 PDB) 을 통해 최소한의 pod수, 또는 최대한의 unavailable한 pod수를 지정할 수 있다.


1. PDB 생성

  • PDB설정의 대상이 될 nginx deploy
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deploy
  namespace: test
spec:
  replicas: 10
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:

 

  • pdb.yaml
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: nginx-pdb
  namespace: test
spec:
  minAvailable: 8
  selector:
    matchLabels:
      app: nginx
  • selector로 app:nginx를 지정하여 동일 namespace내의 app:nginx label을 가진 deploy를 대상으로 적용된다.
  • minAvailable의 개수를 8로 지정하여 최소 8개의 nginx pod는 running상태로 유지된다.
반응형

2. 테스트

  • 10개의 nginx pod를 생성했다.
test@DESKTOP-F55SF6V:~ (⎈|test-cluster:default)# k get po -n test -o wide
NAME                            READY   STATUS    NODE                                             
nginx-deploy-589b6c4c7c-2l4f6   1/1     Running   ip-172-16-152-223.ap-northeast-2.compute.internal
nginx-deploy-589b6c4c7c-6tqmx   1/1     Running   ip-172-16-154-173.ap-northeast-2.compute.internal
nginx-deploy-589b6c4c7c-855cf   1/1     Running   ip-172-16-130-148.ap-northeast-2.compute.internal
nginx-deploy-589b6c4c7c-csw9x   1/1     Running   ip-172-16-152-223.ap-northeast-2.compute.internal
nginx-deploy-589b6c4c7c-dbpbz   1/1     Running   ip-172-16-138-104.ap-northeast-2.compute.internal
nginx-deploy-589b6c4c7c-dvzvs   1/1     Running   ip-172-16-130-148.ap-northeast-2.compute.internal
nginx-deploy-589b6c4c7c-fl5bx   1/1     Running   ip-172-16-154-173.ap-northeast-2.compute.internal
nginx-deploy-589b6c4c7c-hdxbn   1/1     Running   ip-172-16-130-148.ap-northeast-2.compute.internal
nginx-deploy-589b6c4c7c-kkfvh   1/1     Running   ip-172-16-138-104.ap-northeast-2.compute.internal
nginx-deploy-589b6c4c7c-n2bpj   1/1     Running   ip-172-16-130-148.ap-northeast-2.compute.internal

 

  • ip-172-16-130-148.ap-northeast-2.compute.internal노드를 drain 시킨다.
test@DESKTOP-F55SF6V:~ (⎈|test-cluster:default)# k drain ip-172-16-130-148.ap-northeast-2.compute.internal --ignore-daemonsets
node/ip-172-16-130-148.ap-northeast-2.compute.internal already cordoned
evicting pod test/nginx-deploy-589b6c4c7c-n2bpj
evicting pod test/nginx-deploy-589b6c4c7c-dvzvs
evicting pod test/nginx-deploy-589b6c4c7c-hdxbn
evicting pod test/nginx-deploy-589b6c4c7c-855cf
error when evicting pods/"nginx-deploy-589b6c4c7c-n2bpj" -n "test" (will retry after 5s): Cannot evict pod as it would violate the pod's disruption budget.
error when evicting pods/"nginx-deploy-589b6c4c7c-855cf" -n "test" (will retry after 5s): Cannot evict pod as it would violate the pod's disruption budget.
pod/nginx-deploy-589b6c4c7c-dvzvs evicted
pod/nginx-deploy-589b6c4c7c-hdxbn evicted
evicting pod test/nginx-deploy-589b6c4c7c-n2bpj
evicting pod test/nginx-deploy-589b6c4c7c-855cf
pod/nginx-deploy-589b6c4c7c-855cf evicted
pod/nginx-deploy-589b6c4c7c-n2bpj evicted
node/ip-172-16-130-148.ap-northeast-2.compute.internal drained
  • 중간에 2개의 pod가 PDB때문에 evicting 되지 못한다는 메시지가 나온다.(최소 8개를 유지하기 위해)
  • 잠시 후 retry를 통해 evicting되지 못하던 pod도 모두 evict된 것을 확인할 수 있다.
  • drain하는 과정에서 deploy들의 available 상태를 확인했다.
test@DESKTOP-F55SF6V:~ (⎈|test-cluster:default)# k get deploy -n test -w
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deploy   10/10   10           10          12h
nginx-deploy   9/10    9            9           12h
nginx-deploy   9/10    10           9           12h
nginx-deploy   8/10    9            8           12h
nginx-deploy   8/10    10           8           12h
nginx-deploy   9/10    10           9           12h
nginx-deploy   10/10   10           10          12h
nginx-deploy   9/10    9            9           12h
nginx-deploy   9/10    10           9           12h
nginx-deploy   8/10    9            8           12h
nginx-deploy   8/10    10           8           12h
nginx-deploy   9/10    10           9           12h
nginx-deploy   10/10   10           10          12h
  • AVAILABLE의 수가 8 아래로 내려가지 않는것을 볼 수 있다. 8개 이상을 유지하기 위해 evicting에 실패했던 것이다.

 

3. PDB 설정

옵션명 설명
minAvailable(count) 위에서 테스트한 내용으로 최소 유지되어야 할 pod의 개수
minAvailable(percent) 전체 replicas기준 최소 유지되어야 할 pod의 비율(%)
maxUnavailable(count) 최대 허용 가능한 비정상 pod의 수

kubernetes upgrade등의 작업을 할 때 무중단으로 수행하는 경우 꼭 필요한 설정이다.

node의 pod limit등을 고려하여 그 값을 설정하도록 하자

반응형