全部产品
Search
文档中心

动态挂载云盘(PVC)

更新时间: 2021-11-02

本文介绍如何通过PV/PVC的方式动态挂载阿里云云盘。

前提条件

  • K8s集群中已正确部署Virtual kubelet(VK)。

    阿里云ASK集群已默认集成VK,其它集群需要您自行部署VK。更多信息,请参见对接概述

  • K8s集群中已正确部署Disk Controller(即alicloud-disk-controller组件)。

安装Disk Controller

如果您的集群中没有安装Disk Controller,可以通过以下YAML文件进行手动安装。配置时,请替换以下信息:

  • special.keyid:请替换为您的AccessKey ID。

  • special.keysecret:请替换为您的AccessKey Secret。

  • image:请替换为最新版本的imgae。

    关于alicloud-disk-controller组件的版本记录,请参见alicloud-disk-controller

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: alicloud-disk-essd
provisioner: alicloud/disk
parameters:
  type: cloud_essd
---
kind: StorageClass
apiVersion: storage.k8s.io/v1beta1
metadata:
  name: alicloud-disk-efficiency
provisioner: alicloud/disk
parameters:
  type: cloud_efficiency

---
kind: StorageClass
apiVersion: storage.k8s.io/v1beta1
metadata:
  name: alicloud-disk-ssd
provisioner: alicloud/disk
parameters:
  type: cloud_ssd

---
kind: StorageClass
apiVersion: storage.k8s.io/v1beta1
metadata:
  name: alicloud-disk-available
provisioner: alicloud/disk
parameters:
  type: available

---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: alicloud-disk-controller-runner
rules:
  - apiGroups: [""]
    resources: ["persistentvolumes"]
    verbs: ["get", "list", "watch", "create", "delete"]
  - apiGroups: [""]
    resources: ["persistentvolumeclaims"]
    verbs: ["get", "list", "watch", "update"]
  - apiGroups: ["storage.k8s.io"]
    resources: ["storageclasses"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["events"]
    verbs: ["list", "watch", "create", "update", "patch"]

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: alicloud-disk-controller
  namespace: kube-system

---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: run-alicloud-disk-controller
subjects:
  - kind: ServiceAccount
    name: alicloud-disk-controller
    namespace: kube-system
roleRef:
  kind: ClusterRole
  name: alicloud-disk-controller-runner
  apiGroup: rbac.authorization.k8s.io

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: cloud-config
  namespace: kube-system
data:
  special.keyid: "xxx"
  special.keysecret: "xxx"

---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: alicloud-disk-controller
  namespace: kube-system
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: alicloud-disk-controller
    spec:
      serviceAccount: alicloud-disk-controller
      containers:
        - name: alicloud-disk-controller
          image: registry.cn-hangzhou.aliyuncs.com/acs/alicloud-disk-controller:v1.16.9.55-c43698d4-aliyun
          env:
          - name: ACCESS_KEY_ID
            valueFrom:
              configMapKeyRef:
                name: cloud-config
                key: special.keyid
          - name: ACCESS_KEY_SECRET
            valueFrom:
              configMapKeyRef:
                name: cloud-config
                key: special.keysecret
          volumeMounts:
            - name: cloud-config
              mountPath: /etc/kubernetes/
            - name: logdir
              mountPath: /var/log/alicloud/
      volumes:
        - name: cloud-config
          emptyDir: {}
        - name: logdir
          emptyDir: {}

步骤一:创建PVC

  1. 准备PVC的YAML配置文件。

    创建pvc_disk.yaml文件,文件内容如下:

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: essd-disk-pvc
    spec:
      accessModes:
      - ReadWriteMany
      resources:
        requests:
          storage: 20Gi
      storageClassName: alicloud-disk-essd
  2. 创建PVC。

    kubectl create -f pvc_disk.yaml
  3. 查看结果。

    kubectl get pvc essd-disk-pvc

    预期返回:

    NAME           STATUS   VOLUME                   CAPACITY   ACCESS MODES   STORAGECLASS                     AGE
    essd-disk-pvc   Bound    d-2ze19bgda4tmpl9wywzt   20Gi       RWX            alicloud-disk-ssd-cn-beijing-h   13s

步骤二:创建Pod

  1. 准备Pod的YAML配置文件。

    创建pod.yaml文件,文件内容如下:

    apiVersion: v1
    kind: Pod
    metadata:
      name: test-pvc-disk
    spec:
      containers:
      - image: nginx:latest
        name: test-container
        volumeMounts:
        - mountPath: /data
          name: disk-volume
      volumes:
      - name: disk-volume
        persistentVolumeClaim:
          claimName: essd-disk-pvc
  2. 创建Pod。

    kubectl create -f pod.yaml
  3. 查看结果。

    查看Pod内的文件目录,系统已自动创建云盘并将云盘挂载到挂载目录/data。预期返回如下:

    # kubectl get pod test-pvc-disk
    NAME            READY   STATUS    RESTARTS   AGE
    test-pvc-disk   1/1     Running   0          38s
    # kubectl exec -it test-pvc-disk bash
    root@test-pvc-disk:/# ls /data/
    lost+found