博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在Kubernetes集群动态使用 NAS 持久卷
阅读量:6570 次
发布时间:2019-06-24

本文共 6878 字,大约阅读时间需要 22 分钟。

1. 介绍:

本文介绍的动态生成NAS存储卷的方案:在一个已有文件系统上,自动生成一个目录,这个目录定义为目标存储卷;

镜像地址:registry.cn-hangzhou.aliyuncs.com/acs/alicloud-nas-controller:v1.11.5.4-433631d-aliyun

默认生成资源:

生成的PV名字为:pvc-${pvc-uid}
生成目录的名字:namespace-pvcname-pvname

可以再pvc的annotations中如下声明,自定义名字:

生成的pv、目录名字为下面定义的名字。

annotations:    pv-name-created: replace-user-id

2. 部署NAS Controller

创建alicloud-nas-controller,实现动态provider nas pv;

创建alicloud-nas storageclass,为nas pv provision 提供模板;

apiVersion: storage.k8s.io/v1kind: StorageClassmetadata:  name: alicloud-nasprovisioner: alicloud/nasreclaimPolicy: Deleteparameters:  drivertype: flexvolume  nfsversion: "4.0"  options: ""---kind: DeploymentapiVersion: extensions/v1beta1metadata:  name: alicloud-nas-controller  namespace: kube-systemspec:  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      serviceAccount: admin      containers:        - name: alicloud-nas-controller          image: registry.cn-hangzhou.aliyuncs.com/acs/alicloud-nas-controller:v1.11.5.4-433631d-aliyun          imagePullPolicy: Always          volumeMounts:          - mountPath: /persistentvolumes            name: nfs-client-root          env:            - name: NFS_SERVER              value: 154154b095-**.cn-beijing.nas.aliyuncs.com            - name: NFS_PATH              value: /      volumes:      - name: nfs-client-root        flexVolume:          driver: alicloud/nas          options:            path: /            server: 154154b095-**.cn-beijing.nas.aliyuncs.com            vers: "4.0"

StorageClass使用说明:

drivertype: 用来表示生成pv存储类型,可选nfs, flexvolume.    nfs: 默认选项,表示使用k8s原生NFS驱动挂载;    flexvolume: 表示使用阿里云提供的Flexvolume NAS驱动挂载;nfsversion: 挂载nfs使用的版本,支持3,4.0.默认为4.0;    drivertype为flexvolume的时候在这里配置;    为nfs的时候通过mountOptions 配置;options:为挂载nfs的可选项配置;    drivertype为flexvolume的时候在这里配置;    为nfs的时候通过mountOptions 配置;

StorageClass举例:

## 使用kubernetes提供的NFS驱动,并配置mountOptions,reclaimPolicy为Delete;apiVersion: storage.k8s.io/v1kind: StorageClassmetadata:  name: alicloud-nas-nfsmountOptions:- vers=4.0- noresvportprovisioner: alicloud/nasreclaimPolicy: Delete## 使用阿里云提供的Flexvolume NAS驱动,配置nfs版本、options;apiVersion: storage.k8s.io/v1kind: StorageClassmetadata:  name: alicloud-nas-flexprovisioner: alicloud/nasreclaimPolicy: Deleteparameters:  drivertype: flexvolume  nfsversion: "3"  options: "noresvport"

3. 创建应用-Deployment:

kind: PersistentVolumeClaimapiVersion: v1metadata:  name: replace-user-id  annotations:    pv-name-created: replace-user-idspec:  storageClassName: alicloud-nas  accessModes:    - ReadWriteMany  resources:    requests:      storage: 5Gi---apiVersion: extensions/v1beta1kind: Deploymentmetadata:  name: "deploy-nas"spec:  replicas: 1  strategy:    type: Recreate  template:    metadata:      labels:        app: deploy-nas    spec:      containers:        - name: "nginx"          image: "nginx"          volumeMounts:              - name: pvc-nas                mountPath: "/data"      volumes:      - name: pvc-nas        persistentVolumeClaim:            claimName: replace-user-id执行:# userID="hello-123"# cat deploy.yaml | sed  "s/replace-user-id/\"$userID\"/g" | kubectl create -f -# kubectl get pod | grep deploy-nasdeploy-nas-85696b6bfc-t5dmh         1/1       Running          0          28m# kubectl get pvc | grep hellhello-123       Bound     hello-123                                  5Gi        RWX            alicloud-nas-flex   28m# kubectl get pv | grep hellhello-123                                  5Gi        RWX            Delete           Bound      default/hello-123       alicloud-nas-flex                       28m# Nas目录下查看生成目录:# ls -l | grep hellodrwxrwxrwx  2 root root 4096 2月  19 09:58 hello-123

4. 创建应用-StatefulSet:

使用volumeTemplateClaim不支持使用pv-name-created配置pv名字;

apiVersion: v1kind: Servicemetadata:  name: nginx  labels:    app: nginxspec:  ports:  - port: 80    name: web  clusterIP: None  selector:    app: nginx---apiVersion: apps/v1beta1kind: StatefulSetmetadata:  name: webspec:  replicas: 2  serviceName: "nginx"  template:    metadata:      labels:        app: nginx    spec:      containers:      - name: nginx        image: nginx:alpine        volumeMounts:        - mountPath: "/data"          name: pvc-sts  volumeClaimTemplates:  - metadata:      name: pvc-sts    spec:      accessModes:        - ReadWriteOnce      storageClassName: alicloud-nas-flex      resources:        requests:          storage: 2Gi          创建后查看:# kubectl get pod | grep webweb-0                               1/1       Running          0          7sweb-1                               1/1       Running          0          4s# kubectl get pvc | grep webpvc-sts-web-0   Bound     pvc-65ab251a-33ec-11e9-a151-00163e066784   2Gi        RWO            alicloud-nas-flex   13mpvc-sts-web-1   Bound     pvc-8437c50e-33ed-11e9-a151-00163e066784   2Gi        RWO            alicloud-nas-flex   5m# kubectl get pv | grep webpvc-65ab251a-33ec-11e9-a151-00163e066784   2Gi        RWO            Delete           Bound      default/pvc-sts-web-0   alicloud-nas-flex                       13mpvc-8437c50e-33ed-11e9-a151-00163e066784   2Gi        RWO            Delete           Bound      default/pvc-sts-web-1   alicloud-nas-flex                       5m# Nas目录下查看生成目录:# ls -l | grep stsdrwxrwxrwx  2 root root 4096 2月  19 10:16 default-pvc-sts-web-0-pvc-65ab251a-33ec-11e9-a151-00163e066784drwxrwxrwx  2 root root 4096 2月  19 10:24 default-pvc-sts-web-1-pvc-8437c50e-33ed-11e9-a151-00163e066784

5. 创建应用-Pod:

kind: PersistentVolumeClaimapiVersion: v1metadata:  name: replace-user-id  annotations:    pv-name-created: replace-user-idspec:  storageClassName: alicloud-nas-flex  accessModes:    - ReadWriteMany  resources:    requests:      storage: 5Gi---apiVersion: v1kind: Podmetadata:  name: "nas-pod"spec:  containers:    - name: "nginx"      image: "nginx"      volumeMounts:          - name: pvc-nas            mountPath: "/data"  volumes:  - name: pvc-nas    persistentVolumeClaim:        claimName: replace-user-id       # userID="pod-123"# cat pod.yaml | sed  "s/replace-user-id/\"$userID\"/g" | kubectl create -f -# kubectl get pod | grep podnas-pod                             1/1       Running          0          32s# kubectl get pvc | grep podpod-123         Bound     pod-123                                    5Gi        RWX            alicloud-nas-flex   44s# kubectl get pv | grep podpod-123                                    5Gi        RWX            Delete           Bound      default/pod-123         alicloud-nas-flex                       48s# ls -l | grep poddrwxrwxrwx  2 root root 4096 2月  19 10:54 pod-123


本文作者:kanjunbao

本文为云栖社区原创内容,未经允许不得转载。

你可能感兴趣的文章
Python 处理EXCEL的CSV文档分列求SUM
查看>>
Activity Window View的关系
查看>>
poj 1182 食物链 并查集的又一个用法
查看>>
(转)用javamail发送带附件的邮件
查看>>
联想笔记本如何关闭功能键,快捷键,如Fn+F1与F1切换
查看>>
蓝桥杯 算法训练 操作格子(线段树,点更新)
查看>>
泛型实现中没有正确lock引用类型的一个隐藏bug分析
查看>>
win7 64系统安装oracle客户端使用PL/SQL Developer工具
查看>>
silverlight中Combox绑定数据以及动态绑定默认选定项的用法
查看>>
浅谈算法和数据结构: 十 平衡查找树之B树
查看>>
【Algorithm】插入排序
查看>>
WCF寄宿到Windows Service
查看>>
Ajax.ActionLink()方法的使用
查看>>
csdn 泄露用户密码害人不浅啊。
查看>>
ThinkPadT440 Ubuntu14.04 RTL8192EE 链接无线网
查看>>
OpenCV Windows7 VC6.0安装以及HelloWorld
查看>>
苹果开发人员账号注冊流程
查看>>
微铺子点单系统具体介绍 - 争做国内最专业的微信商店平台,微信外卖订餐系统!...
查看>>
ExecuteScalar
查看>>
hdu1213 How Many Tables
查看>>