配置了 Azure 文件共享的 Pod我还需要 PersistentVolume 和 PVC 吗? 应用配置

问题描述

我们已经用定义了我们的 YAML

apiVersion: v1
kind: Pod
Metadata:
  name: mypod
spec:
  containers:
  - image: mcr.microsoft.com/oss/Nginx/Nginx:1.15.5-alpine
    name: mypod
    volumeMounts:
      - name: azure
        mountPath: /mnt/azure
  volumes:
  - name: azure
    azureFile:
      secretName: azure-secret
      shareName: aksshare
      readOnly: false

我们将在部署之前使用 kubectl 命令创建密钥:

$AKS_PERS_STORAGE_ACCOUNT_NAME
$STORAGE_KEY

kubectl create secret generic azure-secret --from-literal=azurestorageaccountname=$AKS_PERS_STORAGE_ACCOUNT_NAME \
--from-literal=azurestorageaccountkey=$STORAGE_KEY

我们已经将该现有文件共享作为 Azure 文件共享资源,并且我们已将文件存储在其中。

如果我们需要管理和定义 yaml,我很困惑 kind: PersistentVolumekind: PersistentVolumeClaim

还是上面的YAML就完全够了? 只有当我们没有在 Azure 上创建文件共享时才需要 PV 和 PVC? 我已经阅读了文档 https://kubernetes.io/docs/concepts/storage/persistent-volumes/,但在需要定义它们以及在整个部署过程中完全不使用它们时仍然感到困惑。

解决方法

你的 Pod Yaml 没问题。

Kubernetes Persistent Volumes 是一个较新的抽象。如果您的应用程序改为使用 PersistentVolumeClaim,则它与您使用的存储类型(在您的情况下为 Azure 文件共享)分离,因此您的应用程序可以部署到例如桌面上的 AWS 或 Google Cloud 或 Minikube,无需任何更改。您的集群需要对 PersistentVolumes 提供一些支持,并且该部分可以绑定到特定的存储系统。

因此,要将您的应用 yaml 与特定基础架构分离,最好使用 PersistentVolumeClaims

持久卷示例

我不了解 Azure 文件共享,但有关于 Dynamically create and use a persistent volume with Azure Files in Azure Kubernetes Service (AKS) 的很好的文档。

应用配置

持续的体积声明

您的应用,例如DeploymentStatefulSet 可以拥有此 PVC 资源

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-azurefile
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: my-azurefile
  resources:
    requests:
      storage: 5Gi

然后您需要创建一个 StorageClass 资源,该资源对于每种类型的环境可能是唯一的,但需要具有相同的名称并支持相同的访问模式强>。如果环境不支持动态卷配置,您可能还需要手动创建 PersistentVolume 资源。

不同环境下的例子:

Pod 使用 Persistent Volume Claim

您通常使用 DeploymentStatefulSet 部署应用程序,但声明 Pod 模板的部分是相似的,只是您可能想要使用 volumeClaimTemplate 而不是 PersistentVolumeClaim StatefulSet

查看 Create a Pod using a PersistentVolumeClaim 上的完整示例

apiVersion: v1
kind: Pod
metadata:
  name: task-pv-pod
spec:
  volumes:
    - name: file-share
      persistentVolumeClaim:
        claimName: my-azurefile    # this must match your name of PVC
  containers:
    - name: task-pv-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: file-share