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

vel*_*vel 2 azure kubernetes azure-files persistent-volumes azure-aks

我们已经定义了我们的 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
Run Code Online (Sandbox Code Playgroud)

我们将在部署之前使用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
Run Code Online (Sandbox Code Playgroud)

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

我很困惑我们是否需要管理和定义 yamls kind: PersistentVolumekind: PersistentVolumeClaim

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

Jon*_*nas 5

你的 Pod Yaml 没问题。

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

因此,要将应用程序 yaml 与特定基础设施解耦,最好使用PersistentVolumeClaims.

持久卷示例

我不了解 Azure 文件共享,但有关于 在 Azure Kubernetes 服务 (AKS) 中通过 Azure 文件动态创建和使用持久卷的详细文档。

应用程序配置

持久数量声明

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

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-azurefile
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: my-azurefile
  resources:
    requests:
      storage: 5Gi
Run Code Online (Sandbox Code Playgroud)

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

不同环境下的示例:

使用持久卷声明的 Pod

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

请参阅使用 PersistentVolumeClaim 创建 Pod的完整示例

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
Run Code Online (Sandbox Code Playgroud)