목차

Kubernetes StatefulSet

  • StatefulSet으로 실행시킨 Pod는 Deployment(deploy-app-5skdw)와 다르게 pod-0, pod-1과 같은 순서값과 안적적인 네트워크 ID를 Pod에 할당
  • Pod는 오름차순으로 생성되고, 다음 Pod는 이전 Pod가 준비되고 실행상태가 된 후에만 생성, 삭제는 큰 수의 Pod부터 삭제됨 → “OrderedReady”
  • Pod를 순서없이 병렬로 실행되거나 종료 시킬려면 “Parallel” 사용(spec.podManagemetPolicy: “Parallel”)
    • kubectl explain statefulset.spe.podManagementPolicy
  • sts-0-pod를 지워도 sts-0-pod 이름으로 재생성됨
  • 복제본 증가 시 자동으로 PV/PVC 생성
  • 복제본 축소 시 사용했던 PV/PVC는 삭제되지 않고 유지
  • 유지되는 이유는 스토리지지가 고유한 상태를 유지해야하는데 scale로 인해 삭제되면 상태 데이토도 같이 삭제 되기 때문
  • StatefulSet은 Headless service를 사용해야한다
  • StatefulSet은 논리적으로 Pod집합을 만들어주는 Service만 있으면 되므로 Headless Service를 사용한다.
  • IP가 없어도 DNS에 등록되므로 nslookup을 통해 Pod의 주소를 확인하여 연결 가능하다.
  • https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/
  • visit-cnt-stfs.yaml
apiVersion: v1
kind: Service
metadata:
  name: visit-cnt-stfs-svc
  labels:
    app: visit-cnt-stfs
spec:
  ports:
  - port: 8080
    protocol: TCP
  selector:
    app: visit-cnt-stfs
  clusterIP: None #headless
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: visit-cnt-stfs
spec:
  selector:
    matchLabels:
      app: visit-cnt-stfs
  serviceName: visit-cnt-stfs-svc
  replicas: 2
  template:
    metadata:
      labels:
        app: visit-cnt-stfs
    spec:
      containers:
      - name: stfs-container
        image: dbgurum/mynode:fc.stfs
        ports:
        - name: http
          containerPort: 8080
        volumeMounts:
        - name: data
          mountPath: /var/data
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      resources:
        requests:
          storage: 1Gi
      accessModes:
      - ReadWriteOnce
      storageClassName: \"openebs-hostpath\" #StorageClass 이해 필요
$ kubectl apply -f visit-cnt-stfs.yaml
service/visit-cnt-stfs-svc created

$ kubectl get sts,po,svc -o wide | grep visit
statefulset.apps/visit-cnt-stfs   2/2     46s   stfs-container   dbgurum/mynode:fc.stfs
pod/visit-cnt-stfs-0                    1/1     Running   0          46s     10.111.156.102   k8s-node1    <none>           <none>
pod/visit-cnt-stfs-1                    1/1     Running   0          30s     10.109.131.27    k8s-node2    <none>           <none>
service/visit-cnt-stfs-svc         ClusterIP   None            <none>        8080/TCP    46s   app=visit-cnt-stfs

#scale 확장
$ kubectl scale sts visit-cnt-stfs --replicas 4

$ kubectl get sts,po,svc,pv -o wide | grep visit
statefulset.apps/visit-cnt-stfs   4/4     2m42s   stfs-container   dbgurum/mynode:fc.stfs
pod/visit-cnt-stfs-0                    1/1     Running   0          2m42s   10.111.156.102   k8s-node1    <none>           <none>
pod/visit-cnt-stfs-1                    1/1     Running   0          2m26s   10.109.131.27    k8s-node2    <none>           <none>
pod/visit-cnt-stfs-2                    1/1     Running   0          68s     10.109.131.29    k8s-node2    <none>           <none>
pod/visit-cnt-stfs-3                    1/1     Running   0          62s     10.111.156.104   k8s-node1    <none>           <none>
service/visit-cnt-stfs-svc         ClusterIP   None            <none>        8080/TCP    2m42s   app=visit-cnt-stfs
persistentvolume/pvc-02ea5060-56b1-45f3-80b2-8fa66bd43481   1Gi        RWO            Delete           Bound    default/data-visit-cnt-stfs-3   openebs-hostpath            58s     Filesystem
persistentvolume/pvc-1ef8c4b4-467c-4fd8-8b0d-b42afc561316   1Gi        RWO            Delete           Bound    default/data-visit-cnt-stfs-1   openebs-hostpath            2m23s   Filesystem
persistentvolume/pvc-50de5316-9381-4341-8a0e-c934888756b2   1Gi        RWO            Delete           Bound    default/data-visit-cnt-stfs-2   openebs-hostpath            64s     Filesystem
persistentvolume/pvc-5ddc0d34-5183-4f7a-84a8-a10106d93b6d   1Gi        RWO            Delete           Bound    default/data-visit-cnt-stfs-0   openebs-hostpath            2m39s   Filesystem

#scale 축소
$ kubectl scale sts visit-cnt-stfs --replicas 2

#Pod가 줄었지만 volume 유지 확인
#높은 번호 부터 삭제
$ kubectl get sts,po,svc,pv -o wide | grep visit
statefulset.apps/visit-cnt-stfs   2/2     6m24s   stfs-container   dbgurum/mynode:fc.stfs
pod/visit-cnt-stfs-0                    1/1     Running   0          6m24s   10.111.156.102   k8s-node1    <none>           <none>
pod/visit-cnt-stfs-1                    1/1     Running   0          6m8s    10.109.131.27    k8s-node2    <none>           <none>
service/visit-cnt-stfs-svc         ClusterIP   None            <none>        8080/TCP    6m24s   app=visit-cnt-stfs
persistentvolume/pvc-02ea5060-56b1-45f3-80b2-8fa66bd43481   1Gi        RWO            Delete           Bound    default/data-visit-cnt-stfs-3   openebs-hostpath            4m40s   Filesystem
persistentvolume/pvc-1ef8c4b4-467c-4fd8-8b0d-b42afc561316   1Gi        RWO            Delete           Bound    default/data-visit-cnt-stfs-1   openebs-hostpath            6m5s    Filesystem
persistentvolume/pvc-50de5316-9381-4341-8a0e-c934888756b2   1Gi        RWO            Delete           Bound    default/data-visit-cnt-stfs-2   openebs-hostpath            4m46s   Filesystem
persistentvolume/pvc-5ddc0d34-5183-4f7a-84a8-a10106d93b6d   1Gi        RWO            Delete           Bound    default/data-visit-cnt-stfs-0   openebs-hostpath            6m21s   Filesystem