使用nfs 作为PV 学习StatefulSet

使用 quay.io/external_storage/nfs-client-provisioner 作为动态pv的storageClass来测试StatefuleSet。

注意nfs-client-provisioner 会不时连接不到api-server,原因不详。

先上关键配置,StorageClass及nfs 对应的deamon(deployment):

---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: managed-nfs-storage
provisioner: fuseim.pri/ifs # or choose another name, must match deployment's env PROVISIONER_NAME'
parameters:
  archiveOnDelete: "false"
allowedTopologies:
- matchLabelExpressions:
  - key: kubernetes.io/hostname
    values:
    - node1.localdomain
apiVersion
---
: apps/v1
kind: Deployment
metadata:
  name: nfs-client-provisioner
  labels:
    app: nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: default
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: nfs-client-provisioner
  template:
    metadata:
      labels:
        app: nfs-client-provisioner
    spec:
      serviceAccountName: nfs-client-provisioner
      containers:
        - name: nfs-client-provisioner
          image: quay.io/external_storage/nfs-client-provisioner:latest
          imagePullPolicy: IfNotPresent
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: fuseim.pri/ifs
            - name: NFS_SERVER
              value: 192.168.157.132
            - name: NFS_PATH
              value: /root/nfsroot
      volumes:
        - name: nfs-client-root
          nfs:
            server: 192.168.157.132
            path: /root/nfsroot

然后是StatefulSet的yaml

---
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: 1
  updateStrategy: 
    type: RollingUpdate
    rollingUpdate: 
      partition: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.9
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: example-local-claim
          mountPath: /data1
  volumeClaimTemplates:
  - metadata:
      name: example-local-claim
    spec:
      accessModes:
      - ReadWriteOnce
      storageClassName: managed-nfs-storage  
      resources:
        requests:
          storage: 1Mi

顺利运行后先查看pod定义,发现自动生成如下内容

  volumes:
  - name: example-local-claim
    persistentVolumeClaim:
      claimName: example-local-claim-web-0

claimName的格式为volumeClaimTemplates里面的

  volumeClaimTemplates:
  - metadata:
      name: example-local-claim

里面name+podname

查看pvc信息

[root@node1 ~]# kubectl get pvc example-local-claim-web-0 -o yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  annotations:
    pv.kubernetes.io/bind-completed: "yes"
    pv.kubernetes.io/bound-by-controller: "yes"
    volume.beta.kubernetes.io/storage-provisioner: fuseim.pri/ifs
  creationTimestamp: "2020-04-03T08:52:08Z"
  finalizers:
  - kubernetes.io/pvc-protection
  labels:
    app: nginx
  name: example-local-claim-web-0
  namespace: default
  resourceVersion: "609339"
  selfLink: /api/v1/namespaces/default/persistentvolumeclaims/example-local-claim-web-0
  uid: 5970b55f-0230-4fce-a762-c3bb264e5c4c
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 1Mi
  storageClassName: managed-nfs-storage
  volumeMode: Filesystem
  volumeName: pvc-5970b55f-0230-4fce-a762-c3bb264e5c4c
status:
  accessModes:
  - ReadWriteOnce
  capacity:
    storage: 1Mi
  phase: Bound

自动绑定了一个名称随机的pv : volumeName: pvc-5970b55f-0230-4fce-a762-c3bb264e5c4c

查看pv信息

[root@node1 ~]# kubectl get pv -o yaml
apiVersion: v1
items:
- apiVersion: v1
  kind: PersistentVolume
  metadata:
    annotations:
      pv.kubernetes.io/provisioned-by: fuseim.pri/ifs
    creationTimestamp: "2020-04-03T09:13:19Z"
    finalizers:
    - kubernetes.io/pv-protection
    name: pvc-5970b55f-0230-4fce-a762-c3bb264e5c4c
    resourceVersion: "609336"
    selfLink: /api/v1/persistentvolumes/pvc-5970b55f-0230-4fce-a762-c3bb264e5c4c
    uid: e5d4627d-83fe-4c7c-8891-0f943b4f2952
  spec:
    accessModes:
    - ReadWriteOnce
    capacity:
      storage: 1Mi
    claimRef:
      apiVersion: v1
      kind: PersistentVolumeClaim
      name: example-local-claim-web-0
      namespace: default
      resourceVersion: "606112"
      uid: 5970b55f-0230-4fce-a762-c3bb264e5c4c
    nfs:
      path: /root/nfsroot/default-example-local-claim-web-0-pvc-5970b55f-0230-4fce-a762-c3bb264e5c4c
      server: 192.168.157.132
    persistentVolumeReclaimPolicy: Delete
    storageClassName: managed-nfs-storage
    volumeMode: Filesystem
  status:
    phase: Bound
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""

可以看到自动在nfs server生成了一个目录:default-example-local-claim-web-0-pvc-5970b55f-0230-4fce-a762-c3bb264e5c4c

你可能感兴趣的:(CNCF,docker)