본문 바로가기
IT/Kubernetes

[Kubernetes] StatefulSet

by 깅지수 2022. 5. 27.

 

* pet vs cattle

 (대체 불가능한/서버가 죽으면 안되는/각자의 고유성을 가진 vs 대체 가능한)

The History of Pets vs Cattle and How to Use the Analogy Properly | Cloudscaling

 

The History of Pets vs Cattle and How to Use the Analogy Properly

I have been meaning to write this post for a long time, but one thing or another has gotten in the way. It’s important to me to provide an accurate history, ...

cloudscaling.com

 

 

* Headless + StatefulSet = pod를 특정 가능

 

Headless Service

myweb-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: myweb-svc
spec:
  type: ClusterIP
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 8080

myweb-svc-headless.yaml

apiVersion: v1
kind: Service
metadata:
  name: myweb-svc-headless
spec:
  type: ClusterIP
  clusterIP: None	# <- Headless Service
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 8080

myweb-rs.yaml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myweb-rs
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
      env: dev
  template:
    metadata:
      labels:
        app: web
        env: dev
    spec:
      containers:
        - name: myweb
          image: ghcr.io/c1t1d0s7/go-myweb
          ports:
            - containerPort: 8080
              protocol: TCP
kubectl run nettool -it --image ghcr.io/c1t1d0s7/network-multitool --rm

> host myweb-svc
> host myweb-svc-headless
 

 

* Master/Slave 구분을 위해 StatefulSet+Headless 서비스 사용

 

StatefulSet

: pod 집합의 배포와 scaling을 관리 (deployment와 유사) & pod들의 순서 및 고유성 보장

고유한 네트워크 식별자 & 스토리지 제공 / pod 마다 각자 고유의 Volume을 갖게 되는 셈

 

예제1

myweb-svc-headless.yaml

apiVersion: v1
kind: Service
metadata:
  name: myweb-svc-headless
spec:
  type: ClusterIP
  clusterIP: None # <-- Headless Service
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 8080

myweb-sts.yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: myweb-sts
spec:
  replicas: 3
  serviceName: myweb-svc-headless
  selector:
    matchLabels:
      app: web
      env: dev
  template:
    metadata:
      labels:
        app: web
        env: dev
    spec:
      containers:
        - name: myweb
          image: ghcr.io/c1t1d0s7/go-myweb
          ports:
            - containerPort: 8080
              protocol: TCP
kubectl run nettool -it --image ghcr.io/c1t1d0s7/network-multitool --rm

> host myweb-svc-headless
> host myweb-sts-0.myweb-svc-headless
> host myweb-sts-1.myweb-svc-headless
> host myweb-sts-2.myweb-svc-headless

 

 

예제2: PVC 템플릿

myweb-sts-vol.yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: myweb-sts-vol
spec:
  replicas: 3
  serviceName: myweb-svc-headless
  selector:
    matchLabels:
      app: web
      env: dev
  template:
    metadata:
      labels:
        app: web
        env: dev
    spec:
      containers:
        - name: myweb
          image: ghcr.io/c1t1d0s7/go-myweb:alpine
          ports:
            - containerPort: 8080
              protocol: TCP
          volumeMounts:
            - name: myweb-pvc
              mountPath: /data
  volumeClaimTemplates:
    - metadata:
        name: myweb-pvc
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 1G
        storageClassName: nfs-client

 

 

 

 

예제 3 : Run a Replicated Stateful Application | Kubernetes

 

Run a Replicated Stateful Application

This page shows how to run a replicated stateful application using a StatefulSet controller. This application is a replicated MySQL database. The example topology has a single primary server and multiple replicas, using asynchronous row-based replication.

kubernetes.io

'IT > Kubernetes' 카테고리의 다른 글

[Kubernetes] Pod Scheduling  (0) 2022.05.27
[Kubernetes] Auto Scaling  (0) 2022.05.27
[Kubernetes] Deployments  (0) 2022.05.25
[Kubernetes] ConfigMap & Secret  (0) 2022.05.25
[Kubernetes] Volume  (0) 2022.05.25