본문 바로가기
DevOps

[Kubernetes] Statefulset

by wahu 2022. 8. 30.

StatefulSet이란?

  • 각각의 App마다 고유의 역할이 있다.
  • 볼륨이 필요하다
  • 네트워크 트래픽이 각 App의 특징에 맞게 들어온다

 

StatefulSet Pod 특징

  • 0부터 순차적으로 생성 된다. (Ordinal Index)
  • replicas 가 0이되면 index가 높은 순으로 순차 삭제 된다.
  • volumeClaimTemplates 사용하여 Pod가 추가될 때 마다 새로운 PVC가 생성돼서 연결 된다.
  • Headless 서비스를 사용하면 예측 가능한 Domain Name을 생성해 준다.

 

StatefulSet 예시

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: stateful-db
spec:
  replicas: 1
  selector:
    matchLabels:
      type: db
  template:
    metadata:
      labels:
        type: db
    spec:
      containers:
      - name: container
        image: kubetm/app
      terminationGracePeriodSeconds: 10

 

PersistentVolumeClaim 예시

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: replica-pvc1
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 1G
  storageClassName: "fast"
 
---
# ReplicaSet
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: replica-pvc
spec:
  replicas: 1
  selector:
    matchLabels:
      type: web2
  template:
    metadata:
      labels:
        type: web2
    spec:
      nodeSelector:
        kubernetes.io/hostname: k8s-node1
      containers:
      - name: container
        image: kubetm/init
        volumeMounts:
        - name: storageos
          mountPath: /applog
      volumes:
      - name: storageos
        persistentVolumeClaim:
          claimName: replica-pvc1
      terminationGracePeriodSeconds: 10

 

 

StatefulSet

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: stateful-pvc
spec:
  replicas: 1
  selector:
    matchLabels:
      type: db2
  serviceName: "stateful-headless" # headless 임
  template: 
    metadata:
      labels:
        type: db2
    spec:
      containers:
      - name: container
        image: kubetm/app
        volumeMounts:
        - name: volume
          mountPath: /applog
      terminationGracePeriodSeconds: 10
  volumeClaimTemplates:
  - metadata:
      name: volume # volumeMounts name과 같아야 함
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 1G
      storageClassName: "fast"

 

Headless Service 예시

  • request pod로 접속해서 nslookup stateful-headless 확인
  • curl stateful-pvc-0.stateful-headless:8080/hostname으로 연결 테스트
apiVersion: v1
kind: Service
metadata:
  name: stateful-headless
spec:
  selector:
    type: db2 # pod select
  ports:
    - port: 80
      targetPort: 8080    
  clusterIP: None
  
---
# Pod
apiVersion: v1
kind: Pod
metadata:
  name: request-pod
spec:
  containers:
  - name: container
    image: kubetm/init

 

출처

- https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/

- https://kubernetes.io/docs/tutorials/stateful-application/basic-stateful-set/

- https://www.inflearn.com/course/쿠버네티스-기초

'DevOps' 카테고리의 다른 글

[AWS] EKS Karpenter  (0) 2022.07.09
[AWS] EKS vs ECS  (0) 2022.04.17
[AWS] IAM#1  (0) 2022.04.13
[Git] Style Guide  (0) 2021.11.06
[Git] git 명령어 참고  (0) 2021.10.13

댓글