问题描述
我想基于此将 Azure 共享磁盘挂载到多个部署/节点: https://docs.microsoft.com/en-us/azure/virtual-machines/disks-shared
因此,我在 Azure 门户中创建了一个共享磁盘,并在尝试将其安装到 Kubernetes 中的部署时出现错误:
“卷“azuredisk”的多重附加错误卷已被 pod(s) 使用...”
是否可以在 Kubernetes 中使用共享磁盘?如果是这样怎么办? 感谢您的提示。
解决方法
Yes,you can,能力为 GA。
Azure 共享磁盘可以挂载为 ReadWriteMany,这意味着您可以将其挂载到多个节点和 Pod。它需要 Azure Disk CSI driver,需要注意的是目前仅支持 Raw Block 卷,因此应用程序负责管理对共享磁盘上的写入、读取、锁定、缓存、挂载和防护的控制。作为原始块设备公开。这意味着您将原始块设备(磁盘)作为 volumeDevice
而不是 volumeMount
挂载到 pod 容器。
The documentation examples 主要指向如何创建存储类来动态配置静态 Azure 共享磁盘,但我也静态创建了一个并将其挂载到不同节点上的多个 Pod。
动态配置共享 Azure 磁盘
- 创建存储类和 PVC
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: managed-csi
provisioner: disk.csi.azure.com
parameters:
skuname: Premium_LRS # Currently shared disk only available with premium SSD
maxShares: "2"
cachingMode: None # ReadOnly cache is not available for premium SSD with maxShares>1
reclaimPolicy: Delete
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pvc-azuredisk
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 256Gi # minimum size of shared disk is 256GB (P15)
volumeMode: Block
storageClassName: managed-csi
- 创建具有 2 个副本的部署并在 Spec 中指定 volumeDevices、devicePath
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx
name: deployment-azuredisk
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
name: deployment-azuredisk
spec:
containers:
- name: deployment-azuredisk
image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
volumeDevices:
- name: azuredisk
devicePath: /dev/sdx
volumes:
- name: azuredisk
persistentVolumeClaim:
claimName: pvc-azuredisk
使用静态配置的 Azure 共享磁盘
使用已通过 ARM、Azure 门户或 Azure CLI 配置的 Azure 共享磁盘。
- 定义引用 DiskURI 和 DiskName 的 PersistentVolume (PV):
apiVersion: v1
kind: PersistentVolume
metadata:
name: azuredisk-shared-block
spec:
capacity:
storage: "256Gi" # 256 is the minimum size allowed for shared disk
volumeMode: Block # PV and PVC volumeMode must be 'Block'
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
azureDisk:
kind: Managed
diskURI: /subscriptions/<subscription>/resourcegroups/<group>/providers/Microsoft.Compute/disks/<disk-name>
diskName: <disk-name>
cachingMode: None # Caching mode must be 'None'
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-azuredisk-managed
spec:
resources:
requests:
storage: 256Gi
volumeMode: Block
accessModes:
- ReadWriteMany
volumeName: azuredisk-shared-block # The name of the PV (above)
安装此 PVC 对于动态和静态配置的共享磁盘是相同的。参考上面的部署。