본문 바로가기
CICD

[Kubernetes] StatefulSet(Controller)

by 장중앙 2022. 2. 8.

Application의 2종류

  • Stateless Application
    • App이 여러개 배포되더라도 같은 서비스의 역할 -> 트래픽을 분산
    • 대표적으로 Web Server {ex) Apache, NGINX, IIS 등}
    • 삭제, 재생성시, 어차피 같은 역할을 하는 서비스를 생성 이름 상관 X
    • 각각의 App마다 볼륨이 필요하지 않음
  • Stateful Application
    • 각각의 App마다 자신의 역할이 존재 -> 역할에 따라 목적이 있는 연결
    • 대표적으로 Database {ex) MongoDB, MariaDB 등}
    • 삭제, 재생성 시, APP이름이 고유식별에 사용하기 때문에 같은 이름으로 재생성
    • 역할이 다른 만큼, 각각의 App마다 볼륨이 필요함

 

이러한 Stateless/Stateful Application을 쿠버네티스에서는 ReplicaSet/StatefulSet으로 이용할 수 있게 지원

-> 이번 포스팅은 StatefulSet을 주제로 다룸

 

StatefulSet

StatefulSet은 애플리이션의 상태를 저장하고 관리하는데 사용하는 쿠버네티스 Controller 객체

기존 Pod를 삭제하고 생성하면 완전히 새로운 가상환경이 시작되지만 StatefulSet을 이용하여 Pod의 상태 유지가 가능해짐 (StatefulSet으로 생성되는 Pod는 영구 식별자를 가지고 상태를 유지함 )

 

* ServiceName : Headless 특성을 이용하여 특정 원하는 StatefulSet의 Pod에 연결가능(Pod 접속가능)

 

ReplicaSet vs StatefulSet 

생성/삭제에 따른 차이

PVC연결에 따른 차이


실습

1. StatefulSet Controller

 

ReplicaSet 생성

terminationGracePeriodSeconds 옵션으로 삭제 요청후 10초 뒤에 삭제되게 끔 구성

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

 

생성된 ReplicaSet 확인

 

StatefulSet 생성

ReplicaSet와 같은 terminationGracePeriodSeconds 옵션 부여

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

 

생성된 StatefulSet확인

 

 

ReplicaSet과 StatefulSet의 replicas를 1 -> 3으로 변경후 확인/비교

- replicaSet

    - 동시에 2개의 Pod가 생성됨

    - Pod 이름이 랜덤하게 만들어지지만

- StatefulSet

    - 순차적으로 한개씩 Pod가 추가됨

    - Pod 이름이 순차적인 인덱스가 부여되는 형태로 부여

 

ReplicaSet과 StatefulSet의 Pod를 삭제

- ReplicaSet

    - 삭제와 동시에 새로운 Pod가 생성되지만 삭제된 Pod와 상관없는 이름으로 생성

- StatefulSet

    - 완전히 삭제된 후 삭제된 Pod와 같은 이름으로 생성된다

 

ReplicaSet과 StatefulSet의 Replicas를 0으로 수정

- ReplicaSet

    - 모든 Pod가 동시에 삭제, terminationGracePeriodSeconds옵션인 10초 후에 삭제된다.

- StatefulSet

    - index가 높은 Pod부터 순차적으로 삭제, terminationGracePeriodSeconds옵션인 10초 마다 1개의 Pod가 삭제됨


댓글