【Azure K8S | AKS】在AKS中创建 StatefulSet 示例

发布时间 2023-08-08 19:58:53作者: 路边两盏灯

问题描述

  1. 【Azure K8S | AKS】在AKS集群中创建 PVC(PersistentVolumeClaim)和 PV(PersistentVolume) 示例
  2. 【Azure K8S|AKS】进入AKS的POD中查看文件,例如PVC Volume Mounts使用情况
  3. 【Azure K8S | AKS】在不丢失文件/不影响POD运行的情况下增加PVC的大小

基于前三篇博文中使用的PV, PVC,Disk来创建StatefulSet应用。并查看其多个POD中所Mount文件夹是否共享?

问题解答

首先,在Kubernetes的官网中找到了 stateful set应用的yaml文件(https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/)。

因为示例中的VolumClaimTemplates设定会为StatefulSet的应用的每一个Replicas(POD)自动生成PVC, PV。

如果要使用已创建好的PV, PVC则需要修改Yaml文件( mystatefulset.yaml )为以下内容:

apiVersion: v1
kind: Service
metadata:
  name: nginx1
  labels:
    app: nginx1
spec:
  ports:
  - port: 80
    name: pvcweb
  clusterIP: None
  selector:
    app: nginx1
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: pvcweb
spec:
  selector:
    matchLabels:
      app: nginx1 # 必须匹配 .spec.template.metadata.labels
  serviceName: "nginx1"
  replicas: 3 # 默认值是 1
  minReadySeconds: 10 # 默认值是 0
  template:
    metadata:
      labels:
        app: nginx1 # 必须匹配 .spec.selector.matchLabels
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: nginx1
        image: mcr.microsoft.com/oss/nginx/nginx:1.15.5-alpine
        ports:
        - containerPort: 80
          name: pvcweb
        volumeMounts:
        - name: testpvc
          mountPath: /nmt/nginx
      volumes:
        - name: testpvc
          persistentVolumeClaim:
            claimName: test-pvc-001

使用 kubectl apply -f mystatefulset.yaml 部署道AKS后,查看 statefulset 和pod 状态

如图所示, 名为 pvcweb 的 statefulset应用有3个Pod, pvcweb-0,pvcweb-1,pvcweb-2。

接下来,通过 kubectl exec -it 进入3个Pod中创建文件,并验证在其他Pod是否能共享。

kubectl exec  -it pvcweb-0  --  /bin/sh

cd /nmt/nginx
echo "this is pvcweb-0 file" > file0.txt


kubectl exec  -it pvcweb-1  --  /bin/sh

cd /nmt/nginx
echo "this is pvcweb-1 file" > file1.txt


kubectl exec  -it pvcweb-2  --  /bin/sh

cd /nmt/nginx
echo "this is pvcweb-2 file" > file2.txt


kubectl exec  -it pvcweb-0  -- df -h /nmt/nginx
kubectl exec  -it pvcweb-1  -- df -h /nmt/nginx
kubectl exec  -it pvcweb-2  -- df -h /nmt/nginx

根据实验证明,StatefulSet下的所有POD都共享所设置的PVC中文件。

 

参考资料

StatefulSets:https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/