是的,可以将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 后,您可以创建PersistentVolume 和PersistentVolumeClaim。
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@、pvc 和 pod。
$ kubectl get pv,pvc,pod
No resources found
现在,如果我要重新创建 pv、pvc,并在 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'