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

标签 azure kubernetes azure-aks persistent-volumes azure-files

我们已经定义了我们的 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 命令创建 key :

$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 种类:持久卷种类: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有一定的支持,并且该部分可以绑定(bind)到特定的存储系统。

因此,要将您的应用 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

您通常使用 DeploymentStatefulSet 来部署应用,但声明 Pod 模板的部分是相似的,只是您可能想要使用 volumeClaimTemplate code> 而不是 StatefulSetPersistentVolumeClaim

查看 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

关于azure - 配置了 Azure 文件共享的 Pod。我还需要 PersistentVolume 和 PVC 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66016287/

相关文章:

azure - 使用动态 IP 地址访问 Azure SQL 数据库

azure CloudBlobDirectory.ListBlobs() 返回 "The specified resource does not exist.",但 fetchAttributes() 使用相同的数据

kubernetes - cert-manager 正在使用 acme 响应器创建新的入口,而不是修改现有的

Azure:如何以编程方式创建事件订阅

azure - 如何使用Azure数据工厂将数据从MySQL增量导入到Azure数据仓库?

AWS 上挂载卷的 Mysql k8s Pod 失败

kubernetes - 错误 - 无法附加或安装卷 : unmounted volumes=[data]

python - 如何使用具有扩展功能的 kubernetes 来处理作业队列

azure - 在 Azure AKS 仪表板中查看 CPU 和内存使用情况

azure - 您可以从不同区域在 AKS Kubernetes 群集上安装 NetApp 卷吗?