Kubernetes - StatefulSet

category DevOps/Kubernetes 2021. 11. 22. 21:50
728x90
반응형

StatefulSet

정의

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

  • Pod 이름
  • Pod의 볼륨(스토리지)

생성

root@master:~# cat > sf-nginx.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: sf-nginx
spec:
  replicas: 3
  serviceName: df-nginx-service
  podManagementPolicy: Parallel
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      name: nginx-pod
      labels:
        app: webui
    spec:
      containers:
      - name: nginx-container
        image: nginx:1.14
root@master:~# kubectl create -f sf-nginx.yaml 
statefulset.apps/sf-nginx created

조회

pod 이름이 순차적으로 0, 1, 2 순으로 생성된 것을 확인할 수 있다.

root@master:~# kubectl get statefulsets.apps -o wide
NAME       READY   AGE   CONTAINERS        IMAGES
sf-nginx   3/3     50s   nginx-container   nginx:1.14
root@master:~# kubectl get pods -o wide
NAME         READY   STATUS    RESTARTS   AGE   IP          NODE                NOMINATED NODE   READINESS GATES
sf-nginx-0   1/1     Running   0          20s   10.36.0.1   node1.example.com   <none>           <none>
sf-nginx-1   1/1     Running   0          20s   10.44.0.2   node2.example.com   <none>           <none>
sf-nginx-2   1/1     Running   0          20s   10.44.0.1   node2.example.com   <none>           <none>

replicas 개수 수정 (scale 명령어)

scale를 업을 시키거나 다운을 시켜도 pod 이름을 유지해주는 것을 알 수 있다.

root@master:~# kubectl scale statefulset sf-nginx --replicas=4
statefulset.apps/sf-nginx scaled
root@master:~# kubectl get pods -o wide
NAME         READY   STATUS    RESTARTS   AGE     IP          NODE                NOMINATED NODE   READINESS GATES
sf-nginx-0   1/1     Running   0          3m11s   10.36.0.1   node1.example.com   <none>           <none>
sf-nginx-1   1/1     Running   0          3m11s   10.44.0.2   node2.example.com   <none>           <none>
sf-nginx-2   1/1     Running   0          3m11s   10.44.0.1   node2.example.com   <none>           <none>
sf-nginx-3   1/1     Running   0          4s      10.36.0.2   node1.example.com   <none>           <none>
root@master:~# kubectl scale statefulset sf-nginx --replicas=2
statefulset.apps/sf-nginx scaled
root@master:~# kubectl get pods -o wide
NAME         READY   STATUS    RESTARTS   AGE     IP          NODE                NOMINATED NODE   READINESS GATES
sf-nginx-0   1/1     Running   0          3m19s   10.36.0.1   node1.example.com   <none>           <none>
sf-nginx-1   1/1     Running   0          3m19s   10.44.0.2   node2.example.com   <none>           <none>

삭제

root@master:~# kubectl delete statefulsets.apps sf-nginx 
statefulset.apps "sf-nginx" deleted
root@master:~# kubectl get statefulsets.apps -o wide
No resources found in default namespace.
root@master:~# kubectl get pods -o wide
No resources found in default namespace.
728x90
반응형

'DevOps > Kubernetes' 카테고리의 다른 글

Kubernetes - CronJob  (0) 2021.11.24
Kubernetes -Job Controller  (0) 2021.11.23
Kubernetes - ReplicaSet  (0) 2021.11.21
Kubernetes - ReplicationController  (0) 2021.11.19
Kubernetes - pod resource 요청 및 제한  (0) 2021.11.18