kubernetes GKE:在多个区域中使用现有磁盘

9udxz4iz  于 12个月前  发布在  Kubernetes
关注(0)|答案(2)|浏览(146)

我想将现有的GCP磁盘挂载到GKE区域集群的Pod中。
以下是this指南:

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv
spec:
  storageClassName: ""
  capacity:
    storage: 100G
  accessModes:
    - ReadOnlyMany
  claimRef:
    name: pvc
    namespace: default
  csi:
    driver: pd.csi.storage.gke.io
    volumeHandle: "projects/<proj>/regions/<region>/disks/<disk_name>"
    fsType: ext4
    readOnly: true

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc
  namespace: default
spec:
  storageClassName: ""
  volumeName: pv
  accessModes:
    - ReadOnlyMany
  resources:
    requests:
      storage: 100G

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test
  template:
    metadata:
      labels:
        app: test
    spec:
      containers:
        - name: sd
          image: alpine
          volumeMounts:
            - name: app
              mountPath: /app
              readOnly: true
      volumes:
        - name: app
          persistentVolumeClaim:
            claimName: pvc
            readOnly: true

字符串
让我们假设磁盘是区域性的,并且在区域A和B中可用。
如果节点将在区域A或B中创建,它将按预期工作,但如果在区域C中创建,则会失败,因为磁盘无法装入。
有没有一种方法可以让GKE在多个区域中复制磁盘,并根据副本位置挂载相应的磁盘/区域?或者有没有一种方法可以将PV指向多个磁盘(每个区域1个),并让GKE根据副本的区域选择正确的磁盘?
谢谢

8tntrjer

8tntrjer1#

首先,在“云原生”软件中,磁盘通常仅在单个可用区中可用。在“云原生”软件中,软件负责将数据复制到多个区域,这是部署为 * 有状态 * 软件的软件。也就是说,GCP确实提供区域磁盘。

GCP区域盘

可以在GCP上使用区域持久盘。要做到这一点,您必须创建一个特定的StorageClass,并在您的PersistentVolumeClaim中指定该storageClassName。有关如何做到这一点,请参阅Provisioning regional persistent disks

8zzbczxx

8zzbczxx2#

每个分区使用PersistentVolume,让PersistentVolumeClaim根据pod分区选择正确的分区:

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-a
  labels:
    app: test
spec:
  storageClassName: ""
  capacity:
    storage: 50G
  accessModes:
    - ReadOnlyMany
  csi:
    driver: pd.csi.storage.gke.io
    volumeHandle: "projects/<project>/zones/<zone_a>/disks/<disk_a>"
    fsType: ext4
    readOnly: true
  nodeAffinity:
    required:
      nodeSelectorTerms:
        - matchExpressions:
          - key: topology.kubernetes.io/zone
            operator: In
            values:
              - <zone_a>

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-b
  labels:
    app: test
spec:
  storageClassName: ""
  capacity:
    storage: 50G
  accessModes:
    - ReadOnlyMany
  csi:
    driver: pd.csi.storage.gke.io
    volumeHandle: "projects/<project>/zones/<zone_b>/disks/<disk_b>"
    fsType: ext4
    readOnly: true
  nodeAffinity:
    required:
      nodeSelectorTerms:
        - matchExpressions:
          - key: topology.kubernetes.io/zone
            operator: In
            values:
              - <zone_b>

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc
spec:
  storageClassName: ""
  selector:
    matchLabels:
      app: test
  accessModes:
    - ReadOnlyMany
  resources:
    requests:
      storage: 50G

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: test
spec:
  replicas: 2
  selector:
    matchLabels:
      app: test
  template:
    metadata:
      labels:
        app: test
    spec:
      containers:
        - name: test
          image: docker.io/alpine
          command:
            - sh
            - -c
          args:
            - sleep 9999
          volumeMounts:
            - name: disk
              mountPath: /disk
              readOnly: true
      volumes:
        - name: disk
          persistentVolumeClaim:
            claimName: pvc
            readOnly: true

字符串

相关问题