使用NAS動態儲存卷建立有狀態應用

kanjunbao發表於2018-11-05

介紹:

目前動態生成NAS儲存卷的實現:在一個已有檔案系統上,自動生成子檔案系統(子目錄),並生成目標儲存卷(PV);

生成的PV名字為:pvc-${pv-uid}

映象介紹:

registry.cn-hangzhou.aliyuncs.com/acs/alicloud-nas-controller:v3.1.0-k8s1.11

部署:

部署前要先建立一個NAS檔案系統,並新增掛載點。注意:掛載點需要和叢集處於同一個VPC內部。

部署模板中,建立Storageclass的時候可以定義reclaimPolicy型別,可以為Retain或者Delete;

根據自己NAS掛載點修改NFS_SERVER、server的值;

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: alicloud-nas
mountOptions:
- vers=4.0
provisioner: alicloud/nas
reclaimPolicy: Retain

---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: alicloud-nas-controller
  namespace: kube-system
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: alicloud-nas-controller
    spec:
      tolerations:
      - effect: NoSchedule
        operator: Exists
        key: node-role.kubernetes.io/master
      - effect: NoSchedule
        operator: Exists
        key: node.cloudprovider.kubernetes.io/uninitialized
      nodeSelector:
         node-role.kubernetes.io/master: ""
      serviceAccount: admin
      containers:
        - name: alicloud-nas-controller
          image: registry.cn-hangzhou.aliyuncs.com/acs/alicloud-nas-controller:v3.1.0-k8s1.11
          volumeMounts:
          - mountPath: /persistentvolumes
            name: nfs-client-root
          env:
            - name: PROVISIONER_NAME
              value: alicloud/nas
            - name: NFS_SERVER
              value: 0cd8b4a576-mmi32.cn-hangzhou.nas.aliyuncs.com
            - name: NFS_PATH
              value: /
      volumes:
        - name: nfs-client-root
          nfs:
            server: 0cd8b4a576-mmi32.cn-hangzhou.nas.aliyuncs.com
            path: /

測試:

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: "nginx"
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        volumeMounts:
        - mountPath: "/data"
          name: html
  volumeClaimTemplates:
  - metadata:
      name: html
    spec:
      accessModes:
        - ReadWriteOnce
      storageClassName: alicloud-nas
      resources:
        requests:
          storage: 2Gi
# kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM                STORAGECLASS          REASON    AGE
pvc-945ca15d-e0ab-11e8-9d2f-00163e145c08   2Gi        RWO            Retain           Bound     default/html-web-0   alicloud-nas-retain             13m
pvc-9a162bb2-e0ab-11e8-9d2f-00163e145c08   2Gi        RWO            Retain           Bound     default/html-web-1   alicloud-nas-retain             13m


相關文章