我们已经定义了我们的 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) .
应用程序配置
持久卷声明
您的应用程序,例如Deployment
或 StatefulSet
可以拥有此 PVC 资源
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-azurefile
spec:
accessModes:
- ReadWriteMany
storageClassName: my-azurefile
resources:
requests:
storage: 5Gi
然后,您需要创建一个 StorageClass
资源,该资源对于每种类型的环境可能都是唯一的,但需要具有相同的名称并支持相同的访问权限模式。如果环境不支持动态卷配置,您可能还需要手动创建PersistentVolume
资源。
不同环境下的示例:
- 链接文档 Dynamically create and use a persistent volume with Azure Files in AKS)针对 Azure 进行了描述。
- 参见AWS EFS doc用于在 AWS 中创建
ReadWriteMany
卷。 - Blog about ReadWriteMany storage in Minikube
使用持久卷声明的 Pod
您通常使用 Deployment
或 StatefulSet
来部署应用,但声明 Pod 模板的部分是相似的,只是您可能想要使用 volumeClaimTemplate
code> 而不是 StatefulSet
的 PersistentVolumeClaim
。
查看 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/