Kubernetes

StatefulSet

dongb 2023. 2. 8. 13:57
반응형

Stateless Application VS Stateful Application

  Stateless Application Stateful Application
역할 단순 복제 각자 역할을 가짐
재생성 같은 서비스의 역할을 하는 앱을 생성  
앱 이름 달라도 상관 없음 Down된 앱과 같은 앱을 생성  
앱 이름 같아야 함    
Volume 하나의 Volume을 공유하여 사용 가능 앱 각자의 Volume을 사용해야 함
네트워크 앱들이 트래픽을 균등하게 나누어 가짐 각 앱의 특징에 맞게 트래픽을 가짐
예시 Apache, Nginx MariaDB, Redis, MongoDB
Controller ReplicaSet StatefulSet
Service Service Headless Service

StatefulSet Controller

  ReplicaSet StatefulSet
pod 이름 {Pod 이름}-{랜덤 값} {Pod 이름}-{index}
동시 생성 replicas만큼 동시에 Pod 생성 replicas만큼 순서대로 Pod 생성
동시 삭제 replicas만큼 동시에 Pod 삭제 replicas만큼 순서대로 Pod 삭제
PVC 생성 PVC 직접 생성 해야 함 volumeClaimTemplate을 통해 PVC 동적 생성
단일 PVC 여부 Pod들이 하나의 PV와 PVC를 공유하여 사용 Pod마다 PVC와 PV가 생성
Service Pod의 이름을 예측하기 어렵기 때문에 HeadlessService 연결하기 힘듬 Pod의 이름을 예측할 수 있기 때문에 HeadlessService로 Pod에 접근 가능
# 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: something
# StatefulSet with PVC
apiVersion: apps/v1
kind: statefulSet
metadata:
  name: stateful-pvc
spec:
  replicas: 1
  selector:
    matchLabels:
      type: db2
  serviceName: "stateful-headless"
  template:
    metadata:
      labels:
        type: db2
    spec:
      containers:
      - name: container
        image: something
        volumeMounts:
        - name: volume # 이름이 아래와 동일해야함
          mountPath: /applog
  volumeClaimTemplate:
  - metadata:
      name: volume # 이름이 위와 동일해야함
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 1G
      storageClassName: "fast"
apiVersion: v1
kind: Service
metadata:
  name: stateful-headless
spec:
  selector:
    type: db2
  ports:
    - port: 80
      targetPort: 8080
  clusterIP: None
 
반응형