9

我有一个带有 2 个副本的简单部署。

我希望每个副本都有相同的存储文件夹(共享应用程序上传文件夹)

我一直在玩索赔和数量,但还没有优势,所以寻求快速帮助/示例。

apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: 'test-tomcat'
      labels:
        app: test-tomcat
    spec:
      selector:
        matchLabels:
          app: test-tomcat
      replicas: 3
      template:
        metadata:
          name: 'test-tomcat'
          labels:
            app: test-tomcat
        spec:
          volumes:
          - name: 'data'
            persistentVolumeClaim:
              claimName: claim
          containers:
          - image: 'tomcat:9-alpine'
            volumeMounts:
            - name: 'data'
              mountPath: '/app/data'
            imagePullPolicy: Always
            name: 'tomcat'
            command: ['bin/catalina.sh', 'jpda', 'run']


kind: PersistentVolume
apiVersion: v1
metadata:
  name: volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 2Gi
  accessModes:
  - ReadWriteMany
  hostPath:
    path: "/mnt/data"


kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: claim
spec:
  storageClassName: manual
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
4

1 回答 1

14

首先,您需要决定使用哪种类型的持久卷。以下是本地集群的几个示例:

  • HostPath - 节点上的本地路径。因此,如果第一个 Pod 位于 Node1 上,而第二个 Pod 位于 Node2 上,则存储空间会有所不同。要解决此问题,您可以使用以下选项之一。主机路径示例:

    kind: PersistentVolume
    apiVersion: v1
    metadata:
      name: example-pv
      labels:
        type: local
    spec:
      storageClassName: manual
      capacity:
        storage: 3Gi
      accessModes:
        - ReadWriteOnce
      hostPath:
        path: "/mnt/data"
    
  • NFS - 该类型的 PersistentVolume 使用网络文件系统。NFS 是一种分布式文件系统协议,允许您在服务器上挂载远程目录。在 Kubernetes 中使用 NFS 之前需要安装 NFS 服务器;这是如何在 Ubuntu 上设置 NFS 挂载的示例。Kubernetes 中的示例:

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: example-pv
    spec:
      capacity:
        storage: 3Gi
      volumeMode: Filesystem
      accessModes:
        - ReadWriteOnce
      persistentVolumeReclaimPolicy: Recycle
      storageClassName: slow
      mountOptions:
        - hard
        - nfsvers=4.1
      nfs:
        path: /tmp
        server: 172.17.0.2
    
  • GlusterFS - GlusterFS 是一个可扩展的分布式文件系统,它将来自多个服务器的磁盘存储资源聚合到一个全局命名空间中。至于 NFS,在 Kubernetes 中使用前需要安装 GlusterFS;这是带有说明的链接,还有一个带有示例的链接。Kubernetes 中的示例:

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: example-pv 
      annotations:
        pv.beta.kubernetes.io/gid: "590" 
    spec:
      capacity:
        storage: 3Gi 
      accessModes: 
        - ReadWriteMany
      glusterfs:
        endpoints: glusterfs-cluster 
        path: myVol1 
        readOnly: false
      persistentVolumeReclaimPolicy: Retain
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: glusterfs-cluster 
    spec:
      ports:
      - port: 1
    ---
    apiVersion: v1
    kind: Endpoints
    metadata:
      name: glusterfs-cluster 
    subsets:
      - addresses:
          - ip: 192.168.122.221 
        ports:
          - port: 1 
      - addresses:
          - ip: 192.168.122.222 
        ports:
          - port: 1 
      - addresses:
          - ip: 192.168.122.223 
        ports:
          - port: 1 
    

创建 PersistentVolume 后,您需要创建 PersistaentVolumeClaim。PersistaentVolumeClaim 是 Pod 用来从存储中请求卷的资源。创建 PersistentVolumeClaim 后,Kubernetes 控制平面会查找满足声明要求的 PersistentVolume。例子:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: example-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi

最后一步,您需要配置一个 Pod 以使用 PersistentVolumeClaim。这是示例:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: 'test-tomcat'
  labels:
    app: test-tomcat
spec:
  selector:
    matchLabels:
      app: test-tomcat
  replicas: 3
  template:
    metadata:
      name: 'test-tomcat'
      labels:
        app: test-tomcat
    spec:
      volumes:
      - name: 'data'
        persistentVolumeClaim:
          claimName: example-pv-claim #name of the claim should be the same as defined before
      containers:
      - image: 'tomcat:9-alpine'
        volumeMounts:
        - name: 'data'
          mountPath: '/app/data'
        imagePullPolicy: Always
        name: 'tomcat'
        command: ['bin/catalina.sh', 'jpda', 'run']
于 2018-09-04T11:08:21.040 回答