Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


Info
iconfalse
title목차

Table of Contents

StatefulSet개요

Info
iconfalse
  • 애플리케이션의 상태를 저장하고 관리하는 데 사용되는 쿠버네티스 객체
  • 기존의 포드를 삭제하고 생성할 때 상태를 유지해 줍니다.


Info
  • 안정된, 고유한 네트워크 식별자
  • 안정된, 지속성을 갖는 스토리지
  • 순차적인, 정상배포와 스케일링
  • 순차적인, 자동 롤링 업데이트


Info
  • Statefulset 관련 Volume이 삭제되지 않으므로 관리가 필요
  • Pod의 Storage는 PV나 StorageClass로 Provisioning이 필요
  • RollingUpdate를 수행하는 경우 수동으로 복구해야 할 수 있음 (기존의 스토리지와 충돌)
  • Pod 네트워크ID를 유지하기 위해 Headless서비스 필요


StatefulSet Manifest

Info
iconfalse

https://dev-k8sref-io.web.app/docs/workloads/statefulset-v1/

Headless 서비스 작성 방법 (ClusterIP가 없는 서비스)

code
Info
iconfalse
  • apiVersion:

  • apps/v1


Info

Headless 서비스 작성 방법 (ClusterIP가 없는 서비스)

Code Block
linenumberstrue
apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: nginx

StatefulSet 작성 방법

Info

Deployment와 전반적인 설정은 유사합니다. Deployment와 다른 속성들만 설명 합니다.

  •  serviceName
    • serviceName으로 고유한 네트워크ID를 생성합니다.
  • terminationGracePeriodSeconds
    • 종료 요청(SIGTERM)후 Pod가 삭제되기까지 Delay시간을 줍니다.
  • volumeClaimTemplates
    • 안정적인 스토리지 제공을 위해 PVC를 작성합니다.
  • volumeMounts
    • 영구 스토리지를 연결하고자 하는 위치

StorageClass확인


spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: ngin

StorageClass확인

Code Block
titleStorageClass
linenumberstrue
collapsetrue
sansae@sansaeAir15m2 k8s-lab-workspace % k get storageclass
NAME                    PROVISIONER          RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
azurefile               file.csi.azure.com   Delete          Immediate              true                   34h
azurefile-csi           file.csi.azure.com   Delete          Immediate              true                   34h
azurefile-csi-premium   file.csi.azure.com   Delete          Immediate              true                   34h
azurefile-premium       file.csi.azure.com   Delete          Immediate              true                   34h
default (default)       disk.csi.azure.com   Delete          WaitForFirstConsumer   true                   34h
managed
Code Block
titleStorageClass
linenumberstrue
collapsetrue
sansae@sansaeAir15m2 k8s-lab-workspace % k get storageclass
NAME                    PROVISIONER  disk.csi.azure.com   Delete     RECLAIMPOLICY   VOLUMEBINDINGMODE  WaitForFirstConsumer   true ALLOWVOLUMEEXPANSION   AGE
azurefile               file.csi.azure.com34h
managed-csi     Delete        disk.csi.azure.com  Immediate Delete          WaitForFirstConsumer   true                   34h
azurefilemanaged-csi-premium           filedisk.csi.azure.com   Delete          Immediate           WaitForFirstConsumer   true                   34h
azurefile-csi-premium   file.csi.azure.com   34h
managed-premium Delete        disk.csi.azure.com  Immediate Delete          WaitForFirstConsumer   true                   34h
azurefile-premium       file.csi.azure.com   Delete  sansae@sansaeAir15m2 k8s-lab-workspace % 

Statefulset Manifest

Code Block
titlestatefulset.yaml
linenumberstrue
StatefulSet Manifest
apiVersion: v1
kind: Service
metadata:
  name: nginx-stps
  labels:
    app: nginx
spec:
  ports:
 Immediate - port: 80
    name: web
  clusterIP: None
  selector:
 true   app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  selector:
        34h
default (default)matchLabels:
      app:  disk.csi.azure.com   Delete          WaitForFirstConsumer   true    nginx
  serviceName: "nginx"
  replicas: 3 # 기본값은 1
  minReadySeconds: 10 # 기본값은 0
  template:
    metadata:
      labels:
        app: 34hnginx
managed    spec:
      terminationGracePeriodSeconds: 10
      disk.csi.azure.comcontainers:
   Delete   - name: nginx
     WaitForFirstConsumer   true   image: registry.k8s.io/nginx-slim:0.8
        ports:
        34h
managed-csi containerPort: 80
           disk.csi.azure.comname: web
   Delete     volumeMounts:
     WaitForFirstConsumer   true- name: www
            mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - 34h
managed-csi-premiummetadata:
     disk.csi.azure.com   Delete name: www
    spec:
      accessModes: WaitForFirstConsumer[ "ReadWriteOnce" ]
 true     storageClassName: azurefile
          resources:
   34h
managed-premium     requests:
    disk.csi.azure.com   Delete   storage: 1Gi


Statefulset 실습

Info
iconfalse
Code Block
linenumberstrue
sansae@sansaeAir15m2 k8s-lab-workspace % k apply -f WaitForFirstConsumer   true     statefulset.yaml 
service/nginx-stps created
statefulset.apps/web created
sansae@sansaeAir15m2 k8s-lab-workspace % k get all
NAME        READY   STATUS   34h
sansae@sansaeAir15m2 k8s-lab-workspace % 

StatefulSet Manifest

Code Block
titlestatefulset.yaml
linenumberstrue
apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: nginx
  serviceName: "nginx"
  replicas: 3 # 기본값은 1
  minReadySeconds: 10 # 기본값은 0
  template:
    metadata:
      labels:
        app: nginx
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: nginx
 RESTARTS   AGE
pod/web-0   0/1     Pending   0          3s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP   29s
service/nginx-stps        ClusterIP   None         <none>        80/TCP    3s

NAME                image: registry.k8s.io/nginx-slim:0.8
  READY   AGE
statefulset.apps/web   ports:
0/3        - containerPort: 80
  3s

sansae@sansaeAir15m2 k8s-lab-workspace % k get all        name: web
NAME        volumeMounts:
READY   STATUS    RESTARTS - name: wwwAGE
pod/web-0   1/1     Running  mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
 0   - metadata:
      name: www
    spec:
2m47s
pod/web-1   1/1     Running   accessModes:0 [ "ReadWriteOnce" ]
      storageClassName: azurefile117s
pod/web-2   1/1   resources:
  Running    0  requests:
        97s

NAME  storage: 1Gi
Code Block
linenumberstrue
sansae@sansaeAir15m2 k8s-lab-workspace % k apply -f statefulset.yaml 
service/nginx created
statefulset.apps/web created
sansae@sansaeAir15m2 k8s-lab-workspace % k get all
NAME              TYPE        CLUSTER-IP  READY EXTERNAL-IP  STATUS PORT(S)   RESTARTSAGE
service/kubernetes   AGE
pod/web-0ClusterIP   10.0/.0.1     Pending<none>   0     443/TCP     3s

NAME3m13s
service/nginx-stps        ClusterIP   None      TYPE   <none>     CLUSTER-IP   EXTERNAL-IP80/TCP   PORT(S)   AGE
service/kubernetes 2m47s

NAME      ClusterIP   10.0.0.1     <none>     READY   443/TCPAGE
statefulset.apps/web   29s
service/nginx3/3     2m47s

sansae@sansaeAir15m2 k8s-lab-workspace % k ClusterIPget statefulset
NAME  None READY   AGE
web    3/3 <none>    48m

sansae@sansaeAir15m2 k8s-lab-workspace % k 80/TCPdescribe statefulset  web 3s

NAMEName:               web
Namespace:    READY   AGE
statefulset.apps/web   0/3default
CreationTimestamp:  Sat, 09 Mar 3s

sansae@sansaeAir15m2 k8s-lab-workspace % k get all2024 21:52:14 +0900
Selector:            
NAMEapp=nginx
Labels:        READY   STATUS   <none>
Annotations: RESTARTS   AGE
pod/web-0   1/1  <none>
Replicas:    Running   0    3 desired | 3 total
Update  2m47s
pod/web-1Strategy:    1/1RollingUpdate
  Partition:   Running   0  0
Pods Status:       117s
pod/web-2 3 Running 1/1 0 Waiting / 0 RunningSucceeded /  0 Failed
Pod Template:
  Labels:  app=nginx
    97s

NAMEContainers:
   nginx:
    Image:        registry.k8s.io/nginx-slim:0.8
  TYPE  Port:      CLUSTER-IP   EXTERNAL-IP80/TCP
   PORT(S) Host Port: AGE
service/kubernetes   ClusterIP0/TCP
   10.0.0.1   Environment:  <none>
    Mounts:
    443/TCP   3m13s
service/usr/share/nginx/html from www (rw)
  Volumes:   ClusterIP<none>
Volume Claims:
  Name: None         <none>www
  StorageClass:  azurefile
    80/TCP  Labels:  2m47s

NAME      <none>
  Annotations:   <none>
  Capacity:      READY1Gi
   AGE
statefulset.apps/web   3/3 Access Modes:  [ReadWriteOnce]
Events:
  Type    2m47s

sansae@sansaeAir15m2 k8s-lab-workspace % k get statefulset
NAMEReason        READY   AGE
web Age   3/3From      48m

sansae@sansaeAir15m2 k8s-lab-workspace % k describe statefulset web 
Name:       Message
  ----    ------  web
Namespace:          default
CreationTimestamp:  Sat, 09 Mar 2024 21:52:14 +0900
Selector:----  ----                app=nginx
Labels:    -------
  Normal  SuccessfulCreate  48m   <none>
Annotations:statefulset-controller  create Claim www-web-0 Pod web-0 in <none>
Replicas:StatefulSet web success
  Normal  SuccessfulCreate  48m   3statefulset-controller desired |create 3 total
Update Strategy:    RollingUpdatePod web-0 in StatefulSet web successful
  Partition:Normal  SuccessfulCreate  47m   statefulset-controller 0
Pods Status:create Claim www-web-1 Pod web-1 in StatefulSet web 3success
 Running /Normal 0 WaitingSuccessfulCreate / 047m Succeeded / 0 Failed
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        registry.k8s.io/nginx-slim:0.8
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:
      /usr/share/nginx/html from www (rw)
  Volumes:  <none>
Volume Claims:
  Name:          www
  StorageClass:  azurefile
  Labels:        <none>
  Annotations:   <none>
  Capacity:      1Gi
  Access Modes:  [ReadWriteOnce]
Events:
  Type    Reason            Age   From                    Message
  ----    ------            ----  ----            statefulset-controller  create Pod web-1 in StatefulSet web successful
  Normal  SuccessfulCreate  47m   statefulset-controller  create Claim www-web-2 Pod web-2 in StatefulSet web success
  Normal  SuccessfulCreate  47m   statefulset-controller  create Pod web-2 in StatefulSet web successful

# Headless Service확인
sansae@sansaeAir15m2 k8s-lab-workspace % k exec -it util -- bash
root@util:/# curl nginx-stps
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.11.1</center>
</body>
</html>

root@util:/# nslookup nginx-stps
Server:		10.0.0.10
Address:	10.0.0.10#53

Name:	nginx-stps.default.svc.cluster.local
Address: 10.244.0.106
Name:	nginx-stps.default.svc.cluster.local
Address: 10.244.0.107
Name:	nginx-stps.default.svc.cluster.local
Address: 10.244.0.108

root@util:/# exit

sansae@sansaeAir15m2 k8s-lab-workspace % k get pod -o wide
NAME    READY   STATUS    RESTARTS   AGE     IP        -------
   Normal  SuccessfulCreateNODE  48m   statefulset-controller  create Claim www-web-0 Pod web-0 in StatefulSet web success
  Normal  SuccessfulCreate  48m   statefulset-controller  create Pod web-0 in StatefulSet web successful
NOMINATED NODE Normal  SuccessfulCreateREADINESS GATES
util 47m   statefulset-controller1/1  create Claim www-web-1 PodRunning web-1 in StatefulSet0 web success
  Normal  SuccessfulCreate  47m  14m statefulset-controller  create Pod web-1 in StatefulSet web successful
  Normal  SuccessfulCreate  47m   statefulset-controller  create Claim www-web-2 Pod web-2 in StatefulSet web success
  Normal  SuccessfulCreate  47m   statefulset-controller  create Pod web-2 in StatefulSet web successful

Headless Service확인

Code Block
linenumberstrue
sansae@sansaeAir15m2 k8s-lab-workspace % k exec -it util -- bash
root@util:/# curl nginx-stps
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.11.1</center>
</body>
</html>
root@util:/# nslookup nginx-stps
Server:		10.0.0.10
Address:	10.0.0.10#53

Name:	nginx-stps.default.svc.cluster.local
Address: 10.244.0.106
Name:	nginx-stps.default.svc.cluster.local
Address: 10.244.0.107
Name:	nginx-stps.default.svc.cluster.local
Address: 10.244.0.108

root@util:/#  10.244.0.105   aks-agentpool-33019784-vmss000000   <none>           <none>
web-0   1/1     Running   0          8m4s    10.244.0.106   aks-agentpool-33019784-vmss000000   <none>           <none>
web-1   1/1     Running   0          7m44s   10.244.0.107   aks-agentpool-33019784-vmss000000   <none>           <none>
web-2   1/1     Running   0          7m24s   10.244.0.108   aks-agentpool-33019784-vmss000000   <none>           <none>