Versions Compared

Key

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

...

Info


Info

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

Code Block
apiVersion: v1
kind: 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확인

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                 disk.csi.azure.com   Delete          WaitForFirstConsumer   true                   34h
managed-csi             disk.csi.azure.com   Delete          WaitForFirstConsumer   true                   34h
managed-csi-premium     disk.csi.azure.com   Delete          WaitForFirstConsumer   true                   34h
managed-premium         disk.csi.azure.com   Delete          WaitForFirstConsumer   true                   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
        image: registry.k8s.io/nginx-slim:0.8
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: www
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: azurefile
      resources:
        requests:
          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        READY   STATUS    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        ClusterIP   None         <none>        80/TCP    3s

NAME                   READY   AGE
statefulset.apps/web   0/3     3s

sansae@sansaeAir15m2 k8s-lab-workspace % k get all         
NAME        READY   STATUS    RESTARTS   AGE
pod/web-0   1/1     Running   0          2m47s
pod/web-1   1/1     Running   0          117s
pod/web-2   1/1     Running   0          97s

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

NAME                   READY   AGE
statefulset.apps/web   3/3     2m47s

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

sansae@sansaeAir15m2 k8s-lab-workspace % k describe statefulset web 
Name:               web
Namespace:          default
CreationTimestamp:  Sat, 09 Mar 2024 21:52:14 +0900
Selector:           app=nginx
Labels:             <none>
Annotations:        <none>
Replicas:           3 desired | 3 total
Update Strategy:    RollingUpdate
  Partition:        0
Pods Status:        3 Running / 0 Waiting / 0 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
  ----    ------            ----  ----                    -------
  Normal  SuccessfulCreate  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
  Normal  SuccessfulCreate  47m   statefulset-controller  create Claim www-web-1 Pod web-1 in StatefulSet web success
  Normal  SuccessfulCreate  47m   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:/#