【问题标题】:GKE how to use existing compute engine disk as persistent volumes?GKE 如何使用现有的计算引擎磁盘作为持久卷?
【发布时间】:2021-06-10 09:40:23
【问题描述】:

我可能必须重建 GKE 集群,但计算引擎磁盘不会被删除,需要重新用作 pod 的持久卷。我还没有找到说明如何将现有 GCP 计算引擎磁盘链接为 pod 的持久卷的文档。

是否可以将现有的 GCP 计算引擎磁盘与 GKE 存储类和持久卷一起使用?

【问题讨论】:

标签: google-cloud-platform google-compute-engine google-kubernetes-engine


【解决方案1】:

是的,可以Persistent Disk as Persistent Volume 重复用于另一个集群,但是有一个限制:

永久磁盘必须与集群节点位于同一区域。

如果PD 位于不同的区域,集群将找不到该磁盘。

在文档Using preexisting persistent disks as PersistentVolumes 中,您可以找到如何重用永久性磁盘的信息和示例。

如果您还没有创建Persistent Disk,您可以根据Creating and attaching a disk 文档创建它。对于此测试,我使用了以下磁盘:

gcloud compute disks create pd-name \
  --size 10G \
  --type pd-standard \
  --zone europe-west3-b 

如果您创建的PD 小于200G,您将得到低于Warning,一切都取决于您的需求。在europe-west3-b 区域中,pd-standard 类型可以在10GB - 65536GB 之间进行存储。

You have selected a disk size of under [200GB]. This may result in poor I/O performance. For more information, see: https://developers.google.com/com
pute/docs/disks#performance.

请记住,您可能会在不同的区域获得不同类型的 Persistent Disk。有关更多详细信息,您可以查看Disk Types 文档或运行$ gcloud compute disk-types list

拥有Persistent Disk 后,您可以创建PersistentVolumePersistentVolumeClaim

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv
spec:
  storageClassName: "test"
  capacity:
    storage: 10G
  accessModes:
    - ReadWriteOnce
  claimRef:
    namespace: default
    name: pv-claim
  gcePersistentDisk:
    pdName: pd-name
    fsType: ext4
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-claim
spec:
  storageClassName: "test"
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10G
---
kind: Pod
apiVersion: v1
metadata:
  name: task-pv-pod
spec:
  volumes:
    - name: task-pv-storage
      persistentVolumeClaim:
       claimName: pv-claim
  containers:
    - name: task-pv-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/data"
          name: task-pv-storage

测试

$ kubectl get pv,pvc,pod
NAME                  CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM              STORAGECLASS   REASON   AGE
persistentvolume/pv   10G        RWO            Retain           Bound    default/pv-claim   test                    22s

NAME                             STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
persistentvolumeclaim/pv-claim   Bound    pv       10G        RWO            test           22s

NAME              READY   STATUS    RESTARTS   AGE
pod/task-pv-pod   1/1     Running   0          21s

将一些信息写入磁盘

$ kubectl exec -ti task-pv-pod -- bin/bash
root@task-pv-pod:/# cd /usr/share/nginx/html
root@task-pv-pod:/usr/share/nginx/html# echo "This is test message from Nginx pod" >> message.txt

现在我删除了所有以前的资源:@​​987654343@、pvcpod

$ kubectl get pv,pvc,pod
No resources found

现在,如果我要重新创建 pvpvc,并在 pod 中进行细微更改,例如 busybox

  containers:
    - name: busybox
      image: busybox
      command: ["/bin/sh"]
      args: ["-c", "while true; do echo hello; sleep 10;done"]
      volumeMounts:
        - mountPath: "/usr/data"
          name: task-pv-storage

会反弹

$ kubectl get pv,pvc,po
NAME                  CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM              STORAGECLASS   REASON   AGE
persistentvolume/pv   10G        RWO            Retain           Bound    default/pv-claim                           43m

NAME                             STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
persistentvolumeclaim/pv-claim   Bound    pv       10G        RWO                           43m

NAME          READY   STATUS    RESTARTS   AGE
pod/busybox   1/1     Running   0          3m43s

busybox pod 中,我将能够找到Message.txt

$ kubectl exec -ti busybox -- bin/sh
/ # cd usr
/ # cd usr/data
/usr/data # ls
lost+found   message.txt
/usr/data # cat message.txt 
This is test message from Nginx pod

作为附加信息,您将无法同时在 2 个集群中使用它,如果您尝试会得到错误:

AttachVolume.Attach failed for volume "pv" : googleapi: Error 400: RESOURCE_IN_USE_B
Y_ANOTHER_RESOURCE - The disk resource 'projects/<myproject>/zones/europe-west3-b/disks/pd-name' is already being used by 'projects/<myproject>/zones/europe-west3-b/instances/gke-cluster-3-default-pool-bb545f05-t5hc'

【讨论】:

  • @John 你还有什么问题吗?
  • 没有问题。这就解释了。谢谢
猜你喜欢
  • 1970-01-01
  • 2014-10-17
  • 2021-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-26
  • 2014-03-11
  • 2018-12-09
相关资源
最近更新 更多