본문 바로가기

Kubernetes

[Kubernetes] StatefulSet

반응형

StatefulSet 이란?

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

  . Pod 이름

  . Pod의 볼륨(스토리지)

 

rc-nginx.yaml

apiVersion: v1
kind: ReplicationController
metadata:
  name: rc-nginx
spec:
  replicas: 3
  selector:
    app: webui
  template:
    metadata:
      name: nginx-pod
      labels:
        app: webui
    spec:
      containers:
      - name: nginx-container
        image: nginx:1.14

 

pod의 이름은 random hash 값으로 지정(fv24x, st9h1, xmvns)

# kubectl create -f rc-nginx.yaml 
replicationcontroller/rc-nginx created

# kubectl get pods -o wide 
NAME             READY   STATUS    RESTARTS   AGE     IP          NODE                NOMINATED NODE   READINESS GATES
rc-nginx-fv24x   1/1     Running   0          2m44s   10.44.0.2   node2.example.com   <none>           <none>
rc-nginx-st9hl   1/1     Running   0          2m44s   10.44.0.1   node2.example.com   <none>           <none>
rc-nginx-xmvns   1/1     Running   0          2m44s   10.36.0.1   node1.example.com   <none>           <none>

 

 ReplicationController는 delete명령으로 pod 삭제시 pod의 개수를 보장하고 이름을 보장하지는 않음

# kubectl delete pod rc-nginx-fv24x 
pod "rc-nginx-fv24x" deleted

# kubectl get pods -o wide 
NAME             READY   STATUS    RESTARTS   AGE     IP          NODE                NOMINATED NODE   READINESS GATES
rc-nginx-pgjhj   1/1     Running   0          12s     10.36.0.2   node1.example.com   <none>           <none>
rc-nginx-st9hl   1/1     Running   0          3m30s   10.44.0.1   node2.example.com   <none>           <none>
rc-nginx-xmvns   1/1     Running   0          3m30s   10.36.0.1   node1.example.com   <none>           <none>

 

statefulset-exam.yaml

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

statefulset은 serviceName을 꼭 명시해야함

podManagementPolicy: OrderedReady (pod를 순차적으로 실행)

podManagementPolicy: Parallel (pod를 병렬로 실행)

 

statefulset 의 경우 NAME을 보면 0,1,2 이름을 보장함

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

# kubectl get pods -o wide 
NAME             READY   STATUS    RESTARTS   AGE   IP          NODE                NOMINATED NODE   READINESS GATES
rc-nginx-pgjhj   1/1     Running   0          13m   10.36.0.2   node1.example.com   <none>           <none>
rc-nginx-st9hl   1/1     Running   0          16m   10.44.0.1   node2.example.com   <none>           <none>
rc-nginx-xmvns   1/1     Running   0          16m   10.36.0.1   node1.example.com   <none>           <none>
sf-nginx-0       1/1     Running   0          17s   10.44.0.2   node2.example.com   <none>           <none>
sf-nginx-1       1/1     Running   0          17s   10.44.0.3   node2.example.com   <none>           <none>
sf-nginx-2       1/1     Running   0          17s   10.36.0.3   node1.example.com   <none>           <none>

 

sf-nginx-1 을 삭제할 경우 sf-nginx-1 이름으로 다시 생성됨

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

# kubectl get pods -o wide 
NAME             READY   STATUS    RESTARTS   AGE    IP          NODE                NOMINATED NODE   READINESS GATES
rc-nginx-pgjhj   1/1     Running   0          14m    10.36.0.2   node1.example.com   <none>           <none>
rc-nginx-st9hl   1/1     Running   0          18m    10.44.0.1   node2.example.com   <none>           <none>
rc-nginx-xmvns   1/1     Running   0          18m    10.36.0.1   node1.example.com   <none>           <none>
sf-nginx-0       1/1     Running   0          106s   10.44.0.2   node2.example.com   <none>           <none>
sf-nginx-1       1/1     Running   0          25s    10.44.0.3   node2.example.com   <none>           <none>
sf-nginx-2       1/1     Running   0          106s   10.36.0.3   node1.example.com   <none>           <none>

 

kubectl scale [object] [pod명] --replicas : object의 replicas 수를 scale하는 명령어

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

# kubectl get pods -o wide 
NAME             READY   STATUS    RESTARTS   AGE     IP          NODE                NOMINATED NODE   READINESS GATES
rc-nginx-pgjhj   1/1     Running   0          16m     10.36.0.2   node1.example.com   <none>           <none>
rc-nginx-st9hl   1/1     Running   0          19m     10.44.0.1   node2.example.com   <none>           <none>
rc-nginx-xmvns   1/1     Running   0          19m     10.36.0.1   node1.example.com   <none>           <none>
sf-nginx-0       1/1     Running   0          3m28s   10.44.0.2   node2.example.com   <none>           <none>
sf-nginx-1       1/1     Running   0          2m7s    10.44.0.3   node2.example.com   <none>           <none>
sf-nginx-2       1/1     Running   0          3m28s   10.36.0.3   node1.example.com   <none>           <none>
sf-nginx-3       1/1     Running   0          3s      10.36.0.4   node1.example.com   <none>           <none>

# kubectl scale statefulset sf-nginx --replicas=2
statefulset.apps/sf-nginx scaled

# kubectl get pods -o wide 
NAME             READY   STATUS    RESTARTS   AGE     IP          NODE                NOMINATED NODE   READINESS GATES
rc-nginx-pgjhj   1/1     Running   0          16m     10.36.0.2   node1.example.com   <none>           <none>
rc-nginx-st9hl   1/1     Running   0          20m     10.44.0.1   node2.example.com   <none>           <none>
rc-nginx-xmvns   1/1     Running   0          20m     10.36.0.1   node1.example.com   <none>           <none>
sf-nginx-0       1/1     Running   0          3m55s   10.44.0.2   node2.example.com   <none>           <none>
sf-nginx-1       1/1     Running   0          2m34s   10.44.0.3   node2.example.com   <none>           <none>

 

edit 명령어를 이용하여 nginx:1.14 → 1.15 버전 변경

# kubectl edit statefulsets.apps sf-nginx 

# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: apps/v1
kind: StatefulSet
metadata:
  creationTimestamp: "2022-12-12T12:50:19Z"
  generation: 4
  name: sf-nginx
  namespace: default
  resourceVersion: "82038"
  uid: 45664fcd-4d16-4d7b-a1ed-3a32cb20c2bb
spec:
  podManagementPolicy: Parallel
  replicas: 2
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: webui
  serviceName: sf-service
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: webui
      name: nginx-pod
    spec:
      containers:
      - image: nginx:1.15
        imagePullPolicy: IfNotPresent
        name: nginx-container
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst

 

롤링 업데이트를 진행하여 nginx 버전 업그레이드

# kubectl get pods -o wide 
NAME             READY   STATUS    RESTARTS   AGE    IP          NODE                NOMINATED NODE   READINESS GATES
rc-nginx-pgjhj   1/1     Running   0          21m    10.36.0.2   node1.example.com   <none>           <none>
rc-nginx-st9hl   1/1     Running   0          24m    10.44.0.1   node2.example.com   <none>           <none>
rc-nginx-xmvns   1/1     Running   0          24m    10.36.0.1   node1.example.com   <none>           <none>
sf-nginx-0       1/1     Running   0          3m6s   10.44.0.2   node2.example.com   <none>           <none>
sf-nginx-1       1/1     Running   0          3m9s   10.36.0.3   node1.example.com   <none>           <none>

# kubectl describe pod sf-nginx-0
Name:             sf-nginx-0
Namespace:        default
Priority:         0
Service Account:  default
Node:             node2.example.com/10.100.0.102
Start Time:       Mon, 12 Dec 2022 21:55:44 +0900
Labels:           app=webui
                  controller-revision-hash=sf-nginx-7c9755b47
                  statefulset.kubernetes.io/pod-name=sf-nginx-0
Annotations:      <none>
Status:           Running
IP:               10.44.0.2
IPs:
  IP:           10.44.0.2
Controlled By:  StatefulSet/sf-nginx
Containers:
  nginx-container:
    Container ID:   containerd://83b5c7e5c4ac31cae16671c63dbc4951cf31a0615b8af90fd5527ba9d99332a5
    Image:          nginx:1.15
    Image ID:       docker.io/library/nginx@sha256:23b4dcdf0d34d4a129755fc6f52e1c6e23bb34ea011b315d87e193033bcd1b68
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Mon, 12 Dec 2022 21:55:45 +0900
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-xrfrg (ro)

 

롤백도 가능

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

 

 

[참고]

- https://www.youtube.com/watch?v=Mx3y9un1KeI

반응형