Home [Kubernetes]Controller: StatefulSet
Post
Cancel

[Kubernetes]Controller: StatefulSet

StatefulSet

Pod의 상태(이름, 볼륨 등)를 유지해주는 컨트롤러

  • 상태만 보장하므로, 노드 배치는 랜덤
  • scale up 시 pod 이름 증가, down 시 pod 이름 감소

Definition

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: sf-nginx
spec:
  replicas: 3
  serviceName: sf-nginx-service
  podManagementPolicy: Parallel # Default: OrderedReady
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      name: nginx-pod
      labels:
        app: webui
  spec:
  containers:
  - name: nginx-container
    image: nginx:1.14

Example

  • 생성
1
2
3
4
5
6
7
8
master@k8s-master:~$ kubectl create -f statefulset.yaml
statefulset.apps/sf-nginx created

master@k8s-master:~$ kubectl get pods
NAME         READY   STATUS    RESTARTS   AGE
sf-nginx-0   1/1     Running   0          5s
sf-nginx-1   1/1     Running   0          5s
sf-nginx-2   1/1     Running   0          5s
  • Scale up
1
2
3
4
5
6
7
8
master@k8s-master:~$ kubectl scale statefulset sf-nginx --replicas=4
statefulset.apps/sf-nginx scaled
master@k8s-master:~$ kubectl get pods
NAME         READY   STATUS    RESTARTS   AGE
sf-nginx-0   1/1     Running   0          10m
sf-nginx-1   1/1     Running   0          10m
sf-nginx-2   1/1     Running   0          10m
sf-nginx-3   1/1     Running   0          5s
  • Scale down
1
2
3
4
5
6
master@k8s-master:~$ kubectl scale statefulset sf-nginx --replicas=2
statefulset.apps/sf-nginx scaled
master@k8s-master:~$ kubectl get pods
NAME         READY   STATUS    RESTARTS   AGE
sf-nginx-0   1/1     Running   0          11m
sf-nginx-1   1/1     Running   0          11m
This post is licensed under CC BY 4.0 by the author.