如何通过 Kubernetes CSI 使用 YRCloudFile 容器存储

如何通过 Kubernetes CSI 使用 YRCloudFile 容器存储

创建 PVC

首先创建一个PVC,指定需要存储的大小和读写模式,支持的读写模式有:

  • ReadWriteMany:该模式支持多挂载,多个Pod间共享存储
  • ReadWriteOnce:只支持一个Pod挂载
  • ReadOnlyMany:只读模式
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: busybox-pvc
  namespace: default
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 20Gi
  storageClassName: yrcloudfile-sc

PVC 创建完成之后,Kubernetes CSI 插件会根据申请信息自动创建对应的 PV,对于动态创建的 PV,默认的回收策略是 Delete,如果有重要数据需要设置成 Retain,命令如下:

kubectl patch pv pvc-6f18cebb-b287-11e9-b044-080027b9e2db -p '{"spec":{"persistentVolumeReclaimPolicy": "Delete"}}'

创建 Pod

apiVersion: apps/v1
kind: Deployment
metadata:
  name: busybox
  labels:
    app: busybox
spec:
  replicas: 1
  selector:
    matchLabels:
      app: busybox
  template:
    metadata:
      labels:
        app: busybox
    spec:
      containers:
        - image: busybox
          command:
            - sh
            - -c
            - 'while true; do date > /mnt/data/index.html; hostname >> /mnt/data/index.html; sleep $(($RANDOM % 5 + 5)); done'
          imagePullPolicy: IfNotPresent
          name: busybox
          volumeMounts:
            - name: busybox-pvc
              mountPath: "/mnt/data"
      volumes:
        - name: busybox-pvc
          persistentVolumeClaim:
            claimName: busybox-pvc

2. 无状态服务(Deployment)静态创建并使用 PV

手动创建存储目录

到对应存储目录下或者 UI 上手动创建目录 k8s-busybox-pv

创建 PV

apiVersion: v1
kind: PersistentVolume
metadata:
  name: busybox-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  csi:
    driver: csi-yrcloudfileplugin
    fsType: ext4
    volumeHandle: k8s-busybox-pv
  storageClassName: yrcloudfile-sc
  persistentVolumeReclaimPolicy: Retain

创建 PVC

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: busybox-pvc
  namespace: default
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  storageClassName: yrcloudfile-sc
  volumeName: busybox-pv

创建 Pod

apiVersion: apps/v1
kind: Deployment
metadata:
  name: busybox
  labels:
    app: busybox
spec:
  replicas: 1
  selector:
    matchLabels:
      app: busybox
  template:
    metadata:
      labels:
        app: busybox
    spec:
      containers:
        - image: busybox
          command:
            - sh
            - -c
            - 'while true; do date > /mnt/data/index.html; hostname >> /mnt/data/index.html; sleep $(($RANDOM % 5 + 5)); done'
          imagePullPolicy: IfNotPresent
          name: busybox
          volumeMounts:
            - name: busybox-pvc
              mountPath: "/mnt/data"
      volumes:
        - name: busybox-pvc
          persistentVolumeClaim:
            claimName: busybox-pvc

3. 有状态服务(StatefulSet)通过 Template 使用 PV

apiVersion: apps/v1beta2
kind: StatefulSet
metadata:
  labels:
    app: mysql
  name: mysql
  namespace: default
spec:
  replicas: 1
  serviceName: mysql-service
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - env:
            - name: MYSQL_MAJOR
              value: '5.7'
            - name: MYSQL_ROOT_PASSWORD
              value: Passw0rd
          image: 'mysql:5.7'
          imagePullPolicy: IfNotPresent
          name: mysql
          ports:
            - containerPort: 3306
              protocol: TCP
            - containerPort: 33060
              protocol: TCP
          volumeMounts:
            - mountPath: /var/lib/mysql
              name: mysql-pvc
  volumeClaimTemplates:
  - metadata:
      name: mysql-pvc
    spec:
      accessModes:
        - ReadWriteOnce
      storageClassName: yrcloudfile-sc
      resources:
        requests:
          storage: 10Gi
miaowing
A Javascript & Typescript developer
Posted on 2019-10-19

Share this post

Scan to open in WeChat

Stay tuned

  • Add me on WeChat

  • Subscribe to my newsletter