Google Kubernetes Engine (GKE) 为您在集群中自动部署和管理 Compute Engine 永久性磁盘容器存储接口 (CSI) 驱动程序提供了一种简单的方法。
Compute Engine 永久性磁盘 CSI 驱动程序版本与 GKE 版本号相关联。Compute Engine 永久性磁盘 CSI 驱动程序版本通常是发布 GKE 版本时可用的最新驱动程序。当集群升级到最新的 GKE 补丁程序时,驱动程序会自动更新。
使用 Compute Engine 永久性磁盘 CSI 驱动程序的优势
使用 Compute Engine 永久性磁盘 CSI 驱动程序代替 Kubernetes 树内 gcePersistentDisk 卷插件具有以下优势:
- CSI 驱动程序是 Kubernetes 中存储扩展程序的未来。Kubernetes 已宣布,树内卷插件预计将从 Kubernetes 1.21 版中移除。如需了解详情,请参阅 Kubernetes 树内卷到 CSI 卷迁移已进入 Beta 版阶段。移除树内卷插件后,使用树内卷插件的现有卷将改为通过 CSI 驱动程序进行通信。
- 它可以自动部署和管理永久性磁盘驱动程序,而不必手动进行设置,也不必使用树内卷插件。
- 它会在 GKE 中提供额外的永久性磁盘功能。例如:
- Bug 修复和功能更新独立于次要的 Kubernetes 版本而发布。此发布时间表通常会加快发布节奏。
要求
如需使用 Compute Engine 永久性磁盘 CSI 驱动程序,您必须使用 GKE 1.14 版本或更高版本。
在新集群上启用 Compute Engine 永久性磁盘 CSI 驱动程序
使用 1.18.10-gke.2100 或更高版本的新建集群和使用 1.19.3-gke.2100 或更高版本的新建集群会默认启用 Compute Engine 永久性磁盘 CSI 驱动程序。如果您使用这些版本之一创建了集群,则无需在本部分中执行任何步骤。
如果要创建未自动启用 Compute Engine 永久性磁盘 CSI 驱动程序的集群,您可以使用 gcloud
命令行工具或 Google Cloud Console。
如需在创建集群时启用该驱动程序,请完成以下步骤:
gcloud
gcloud container clusters create CLUSTER-NAME \
--addons=GcePersistentDiskCsiDriver \
--cluster-version=VERSION
替换以下内容:
CLUSTER-NAME
:您的集群的名称。VERSION
:GKE 版本号。您必须选择 1.14 或更高版本才能使用此功能。
如需查看完整的标志列表,请参阅 gcloud container clusters create
文档。
控制台
访问 Cloud Console 中的 Google Kubernetes Engine 菜单。
点击 add_box 创建。
根据需要配置集群。
在导航窗格的集群下,点击功能。
选中启用 Compute Engine 永久性磁盘 CSI 驱动程序 (Enable Compute Engine persistent disk CSI Driver) 复选框。
点击创建。
启用 Compute Engine 永久性磁盘 CSI 驱动程序后,您可以使用驱动程序和预配工具名称 pd.csi.storage.gke.io
在 Kubernetes 卷中使用该驱动程序。
在现有集群上启用 Compute Engine 永久性磁盘 CSI 驱动程序
如需在现有集群中启用 Compute Engine 永久性磁盘 CSI 驱动程序,请使用 gcloud
命令行工具或 Google Cloud Console。
如需在现有集群上启用该驱动程序,请完成以下步骤:
gcloud
gcloud container clusters update CLUSTER-NAME \
--update-addons=GcePersistentDiskCsiDriver=ENABLED
将 CLUSTER-NAME
替换为现有集群的名称。
控制台
在 Cloud Console 中,转到 Google Kubernetes Engine 菜单。
在集群列表中,点击您要修改的集群的名称。
在功能下,点击 Compute Engine 永久性磁盘 CSI 驱动程序字段旁边的 edit 修改 Compute Engine CSI 驱动程序。
选中启用 Compute Engine Persistent Disk CSI 驱动程序复选框。
点击保存更改。
停用 Compute Engine 永久性磁盘 CSI 驱动程序
使用 gcloud
命令行工具或 Google Cloud Console 停用 Compute Engine 永久性磁盘 CSI 驱动程序。
如需在现有集群上停用该驱动程序,请完成以下步骤:
gcloud
gcloud container clusters update CLUSTER-NAME \
--update-addons=GcePersistentDiskCsiDriver=DISABLED
将 CLUSTER-NAME
替换为现有集群的名称。
控制台
在 Cloud Console 中,转到 Google Kubernetes Engine 菜单。
在集群列表中,点击您要修改的集群的名称。
在功能下,点击 Compute Engine 永久性磁盘 CSI 驱动程序字段旁边的 edit 修改 Compute Engine CSI 驱动程序。
取消选中启用 Compute Engine Persistent Disk CSI 驱动程序复选框。
点击保存更改。
使用 Compute Engine 永久性磁盘 CSI 驱动程序
以下几个部分介绍了在 GKE 中使用 CSI 驱动程序支持的 Kubernetes 卷的典型过程。
创建一个 StorageClass
启用 Compute Engine 永久性磁盘 CSI 驱动程序后,GKE 会自动安装以下 StorageClass:
standard-rwo
(使用平衡永久性磁盘)premium-rwo
(使用 SSD 永久性磁盘)
1.17 及更低版本的一些旧式集群可能改用 singlewriter-standard
或 standard-singlewriter
StorageClass,后者使用标准永久性磁盘。
您可以通过运行以下命令找到已安装的 StorageClass 的名称:
kubectl get sc
您还可以通过在预配工具字段中添加 pd.csi.storage.gke.io
来安装其他使用 Compute Engine 永久性磁盘 CSI 驱动程序的 StorageClass。
例如,您可以使用以下名为 pd-example-class.yaml
的文件创建一个 StorageClass:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: pd-example
provisioner: pd.csi.storage.gke.io
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true
parameters:
type: pd-balanced
任何永久性磁盘类型均可在 type
参数中指定(例如 pd-ssd
、pd-standard
或 pd-balanced
)。
创建 pd-example-class.yaml
文件后,请运行以下命令:
kubectl create -f pd-example-class.yaml
创建一个 PersistentVolumeClaim
您可以创建一个引用 Compute Engine 永久性磁盘 CSI 驱动程序的 StorageClass 的 PersistentVolumeClaim。
以下名为 pvc-example.yaml
的文件使用预安装的存储类别 standard-rwo
:
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: podpvc
spec:
accessModes:
- ReadWriteOnce
storageClassName: standard-rwo
resources:
requests:
storage: 6Gi
创建 PersistentVolumeClaim 清单后,请运行以下命令:
kubectl create -f pvc-example.yaml
在预安装的 StorageClass (standard-rwo
) 中,将 volumeBindingMode
设置为 WaitForFirstConsumer
。如果将 volumeBindingMode
设置为 WaitForFirstConsumer
,则在调度引用 PersistentVolumeClaim 的 Pod 之前,系统不会预配 PersistentVolume。如果将 StorageClass 中的 volumeBindingMode
设置为 Immediate
(或者将其省略),则在创建 PersistentVolumeClaim 后,系统会预配基于永久性磁盘的 PersistentVolume。
创建使用该卷的 Pod
将 Pod 与 PersistentVolume 搭配使用时,我们建议您使用工作负载控制器(例如 Deployment 或 StatefulSet)。虽然您通常不会使用一个独立的 Pod,但以下示例为简单起见使用了一个 Pod。
以下示例将使用您在上一部分中创建的卷:
apiVersion: v1
kind: Pod
metadata:
name: web-server
spec:
containers:
- name: web-server
image: nginx
volumeMounts:
- mountPath: /var/lib/www/html
name: mypvc
volumes:
- name: mypvc
persistentVolumeClaim:
claimName: podpvc
readOnly: false
已知问题
由于 CSI 节点 ID 规范中设定 128 个字符限制以及 GKE 生成实例名称的方式,则在某些节点池的新 GKE 集群或现有 GKE 集群上安装 Compute Engine 永久性磁盘 CSI 驱动程序可能会失败。如需了解详情,请参阅该 GitHub 问题。
此问题已在以下版本中得到修复:
- 1.16.15-gke.1700 及更高版本
- 1.17.9-6300 及更高版本
- 1.18.6-4801 及更高版本
如果您使用的是较低版本的集群,请将其升级到所列的某一版本以解决该问题。