코알못

[CKA] kubernetes Controller - StatefulSet 본문

ETC

[CKA] kubernetes Controller - StatefulSet

코린이s 2023. 9. 9. 20:10
728x90

Pod의 상태를 유지해주는 컨트롤러이다.

우리가 이전시간에 배운 ReplicationController 를 보면 replicas 를 3개로 설정하여 create 하면 pod 가 3개 생성된다.

여기서 파드 하나를 제거하면 파드가 재 생성이 되는데 해당 파드에 연결된 볼륨이 삭제 되고, 파드의 이름도 다른 해시 코드가 붙어 다른 이름으로 생성된다.

StatefulSet 은 이와 다르게 Pod 이름, 볼륨이 유지 되는 성격을 가지고 있다!

serviceName 필드 지정이 필요하며 아직 Service에 대해 배우지 않았으니 우선 지정이 필요하다는것만 알아두자!

# sf-nginx.yaml

apiVersion: v1
kind: StatefulSet
metadata:
  name: sf-nginx
spec:
  replicas: 3
  serviceName: sf-service
  selector:
    app: webui
  template:
    metadata:
      name: pod-nginx
      labels:
        app: webui
    spec:
      containers:
      - name: container-nginx
        image: nginx:1.14

이제 파드를 생성해보자! 아래 -w 를 통해 파드 상태 실시간으로 보면 0번 생성이 완료 된 후에 1번이 생기고 그 다음 2번이 생기게 된다.

$ kubectl create -f sf-nginx.yaml
statefulset.apps/sf-nginx created

$ kubectl get pod -w
sf-nginx-0       0/1     ContainerCreating   0          1s
sf-nginx-0       1/1     Running             0          1s
sf-nginx-1       0/1     Pending             0          0s
sf-nginx-1       0/1     Pending             0          0s
sf-nginx-1       0/1     ContainerCreating   0          0s
sf-nginx-1       1/1     Running             0          1s
sf-nginx-2       0/1     Pending             0          0s
sf-nginx-2       0/1     Pending             0          0s
sf-nginx-2       0/1     ContainerCreating   0          0s
sf-nginx-2       1/1     Running             0          1s

생성 자체를 동시에 하고 싶다면 아래와 같이 podManagementPolicy 설정을 Parallel로 변경 하면 되며 한번 변경하여 테스트 해보자!

우선 기존에 생성한 것을 제거 하고 

$ kubectl delete -f sf-nginx.yaml

 

설정 값을 아래와 같이 변경한다.

podManagementPolicy는 default로 OrderedReady이며 순차적으로 파드가 생성되게 되며 Parallel로 변경할시 동시에 실행 할 수 있다.

# sf-nginx.yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: sf-nginx
spec:
  replicas: 3
  serviceName: sf-service
  podManagementPolicy: Parallel
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      name: pod-nginx
      labels:
        app: webui
    spec:
      containers:
      - name: container-nginx
        image: nginx:1.14

실행해보면 이전과 다르게 동시에 0,1,2 파드가 뜨고 러닝 상태가 된것을 알 수 있다.

$ kubectl create -f sf-nginx.yaml
statefulset.apps/sf-nginx created

$ kubectl get pod -w
NAME         READY   STATUS              RESTARTS   AGE
sf-nginx-0   0/1     ContainerCreating   0          1s
sf-nginx-1   0/1     ContainerCreating   0          1s
sf-nginx-2   0/1     ContainerCreating   0          1s
sf-nginx-2   1/1     Running             0          2s
sf-nginx-1   1/1     Running             0          2s
sf-nginx-0   1/1     Running             0          2s

이 상태에서 1번을 지웠을시 이름이 그대로 생성되는지 확인해보면!

$ kubectl delete pod sf-nginx-1
pod "sf-nginx-1" deleted

정상적으로 다시 1번이 생성 되었다.

$ kubectl get pod
NAME         READY   STATUS    RESTARTS   AGE
sf-nginx-0   1/1     Running   0          46m
sf-nginx-1   1/1     Running   0          5s
sf-nginx-2   1/1     Running   0          46m

scale 아웃을 해보자!

$ kubectl scale statefulset sf-nginx --replicas=4
statefulset.apps/sf-nginx scaled

추가로 파드 3번이 생성 되었으며

[node1 ~]$ kubectl get pod
NAME         READY   STATUS    RESTARTS   AGE
sf-nginx-0   1/1     Running   0          48m
sf-nginx-1   1/1     Running   0          97s
sf-nginx-2   1/1     Running   0          48m
sf-nginx-3   1/1     Running   0          4s

여기서 scale in 으로 2개로 수를 줄이면 예상대로 번호 순서대로 줄어 든다!

[node1 ~]$ kubectl scale statefulset sf-nginx --replicas=2
statefulset.apps/sf-nginx scaled
[node1 ~]$ kubectl get pod
NAME         READY   STATUS    RESTARTS   AGE
sf-nginx-0   1/1     Running   0          48m
sf-nginx-1   1/1     Running   0          104s

이처럼 파드 이름이 보장된다는것을 알 수 있다!

동일하게 롤링 업데이트도 가능하니 테스트 해보자!

우선 edit를 통해 1.14 버전을 1.15로 변경한다.

$ kubectl edit statefulset sf-nginx
statefulset.apps/sf-nginx edited

정상적으로 1.15가 배포 되었음을 알 수 있으며

$ kubectl get statefulset -o wide
NAME       READY   AGE   CONTAINERS        IMAGES
sf-nginx   2/2     54m   container-nginx   nginx:1.15

롤백을 시켜 1.14로 변경 해본다

$ kubectl rollout undo statefulset sf-nginx
statefulset.apps/sf-nginx rolled back

확인해보면 정상적으로 롤백 되었다.

$ kubectl get statefulset -o wide
NAME       READY   AGE   CONTAINERS        IMAGES
sf-nginx   2/2     55m   container-nginx   nginx:1.14

이제 모두 제거한다.

$ kubectl delete -f sf-nginx.yaml 
statefulset.apps "sf-nginx" deleted

끝!

728x90

'ETC' 카테고리의 다른 글

[CKA] kubernetes Service  (0) 2023.09.10
[CKA] kubernetes Controller - JobController  (0) 2023.09.10
[CKA] kubernetes Controller - DaemonSet  (0) 2023.09.03
[CKA] kubernetes Controller - Deployment  (0) 2023.09.03
[CKA] kubernetes Controller - ReplicaSet  (0) 2023.09.03
Comments