Kubernetes CSI实践 - 使用阿里云静态OSS卷

阿里云的存储有:NAS、CPFS、OSS 、LVM等。

从阿里云Kubernetes CSI插件GitHub上了解到,OSS CSI插件支持OSS存储桶挂载,但不支持provision volume,即使用Storage动态创建pv;

下面是使用PV挂载桶方式的实践。

环境准备

已经部署好的kubernetes集群,我是使用kubeadm安装的开源版本v1.15.2

集群创建、依赖配置、CSI插件部署等请参考:CSI部署详解

创建OSS Bucket

到OSS控制台创建OSS Bucekt:参考文档

注意:相同region的OSS Bucket 可以使用内网地址进行挂载;

创建Secret、静态PV、PVC

本示例通过Secret来配置用户的AK信息:

apiVersion: v1
kind: Secret
metadata:
  name: oss-secret
  namespace: default
stringData:
  akId: ***
  akSecret: ***

通过上面模板创建secret,注意namespace需要和应用所在namespaces一致;

配置:akId、akSecret参数;

通过下面模板创建静态卷PV、PVC:


apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: oss-pvc
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 5Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: oss-csi-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  csi:
    driver: ossplugin.csi.alibabacloud.com
    volumeHandle: data-id
    nodePublishSecretRef:
      name: oss-secret
      namespace: default
    volumeAttributes:
      bucket: "oss"
      url: "oss-cn-hangzhou.aliyuncs.com"
      otherOpts: "-o max_stat_cache_size=0 -o allow_other"

nodePublishSecretRef:定义挂载pv时通过secret对象来获取ak信息;

volumeHandle:PV Handler,可以配置为一个随机值;

bucket、url、otherOpts:分别表示OSS Bucket、挂载点、挂载可选参数;


# kubectl get pvc
NAME      STATUS   VOLUME       CAPACITY   ACCESS MODES   STORAGECLASS   AGE
oss-pvc   Bound    oss-csi-pv   5Gi        RWX                           20s

# kubectl get pv
NAME         CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM             STORAGECLASS   REASON   AGE
oss-csi-pv   5Gi        RWX            Retain           Bound    default/oss-pvc                           21s

直接配置PV、PVC的AK

上面步骤是通过secret为plugin提供ak信息,也可以直接在pv中配置AK信息。如下面模板所示:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: oss-csi-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  csi:
    driver: ossplugin.csi.alibabacloud.com
    volumeHandle: data-id
    volumeAttributes:
      bucket: "oss"
      url: "oss-cn-hangzhou.aliyuncs.com"
      otherOpts: "-o max_stat_cache_size=0 -o allow_other"
      akId: "***"
      akSecret: "***"

创建应用


apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
        volumeMounts:
          - name: oss-pvc
            mountPath: "/data"
      volumes:
        - name: oss-pvc
          persistentVolumeClaim:
            claimName: oss-pvc

验证挂载、高可用

查看pod,验证OSS挂载成功,创建测试文件;


[root@liabio oss-data]# kubectl get pod 
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-6c69c664fc-tfmf4   1/1     Running   0          13m
[root@liabio oss-data]# kubectl exec -ti nginx-deployment-6c69c664fc-tfmf4  bash
root@nginx-deployment-6c69c664fc-tfmf4:/# ls /data/
miniprog
root@nginx-deployment-6c69c664fc-tfmf4:/#

和阿里云OSS控制台中的一样:
在这里插入图片描述

参考

OSS CSI Plugin官方github
阿里云官方:通过 YAML 使用 OSS 静态存储卷
阿里云Kubernetes CSI实践 - 静态OSS卷
阿里云Kubernetes CSI实践 - 部署详解
Kubernetes如何选择存储以及什么方式使用存储

你可能感兴趣的:(存储,kubernetes)