《Windows Azure Platform 系列文章目錄》
在Azure AKS中,可以掛載Azure File作為存儲卷。
主要分為三個步驟:
1.創建Azure 存儲賬戶,Azure File和AKS Secret
2.創建PV和PVC
3.創建pod並掛載Azure File
1.首先,我們在Azure CLI運行下面的命令。
主要作用是創建新的Azure存儲賬戶,並在該存儲賬戶下,創建Azure File
az cloud set --name AzureChinaCloud az login # 修改下面的參數,會創建新的Azure存儲賬戶 AKS_PERS_STORAGE_ACCOUNT_NAME=leiaks01storage AKS_PERS_RESOURCE_GROUP=MC_aks-rg_leiaks01_chinaeast2 AKS_PERS_LOCATION=chinaeast2 AKS_PERS_SHARE_NAME=aksshare # Create a resource group az group create --name $AKS_PERS_RESOURCE_GROUP --location $AKS_PERS_LOCATION # Create a storage account az storage account create -n $AKS_PERS_STORAGE_ACCOUNT_NAME -g $AKS_PERS_RESOURCE_GROUP -l $AKS_PERS_LOCATION --sku Standard_LRS # Export the connection string as an environment variable, this is used when creating the Azure file share export AZURE_STORAGE_CONNECTION_STRING=$(az storage account show-connection-string -n $AKS_PERS_STORAGE_ACCOUNT_NAME -g $AKS_PERS_RESOURCE_GROUP -o tsv) # Create the file share az storage share create -n $AKS_PERS_SHARE_NAME --connection-string $AZURE_STORAGE_CONNECTION_STRING # Get storage account key STORAGE_KEY=$(az storage account keys list --resource-group $AKS_PERS_RESOURCE_GROUP --account-name $AKS_PERS_STORAGE_ACCOUNT_NAME --query "[0].value" -o tsv) # Echo storage account name and key echo Storage account name: $AKS_PERS_STORAGE_ACCOUNT_NAME echo Storage account key: $STORAGE_KEY # 創建AKS Secret kubectl create secret generic azure-secret --from-literal=azurestorageaccountname=$AKS_PERS_STORAGE_ACCOUNT_NAME --from-literal=azurestorageaccountkey=$STORAGE_KEY #cd /mnt/d/Work/Doc/FY21/Customer/swire/AKS/aksfile
2.使用kubectl 創建pv,如下圖:
apiVersion: v1
kind: PersistentVolume
metadata:
name: azurefile
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteMany
azureFile:
secretName: azure-secret
shareName: aksshare
readOnly: false
mountOptions:
- dir_mode=0777
- file_mode=0777
- uid=1000
- gid=1000
- mfsymlinks
- nobrl
執行完畢后,我們執行命令:
kubectl get pv
執行結果,可以查看到pv
3.使用kubectl 創建pvc,如下圖:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: azurefile
spec:
accessModes:
- ReadWriteMany
storageClassName: ""
resources:
requests:
storage: 5Gi
執行完畢后,我們執行命令:
kubectl get pvc
可以查看到pvc
4.最后,我們創建一個pod,並掛載pvc。
注意,掛載到pod上的路徑是:/mnt/azure
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- image: mcr.microsoft.com/oss/nginx/nginx:1.15.5-alpine
name: mypod
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 250m
memory: 256Mi
volumeMounts:
- name: azure
mountPath: /mnt/azure
volumes:
- name: azure
persistentVolumeClaim:
claimName: azurefile
我們執行kubectl get pod,可以查看到創建成功的pod
我們可以執行命令:
kubectl exec mypod touch /mnt/azure/hello
在Azure File里創建1個新的文件,文件名為hello。
這個文件可以在Azure Portal里面查看到,截圖略。
或者我們可以使用下面的命令,通過交互式命令訪問到pod
#交互式命令,訪問pod
kubectl exec -it mypod -- sh
#創建文件
/ # cd /mnt/azure
/mnt/azure # touch file1.txt
#查詢這個文件
/mnt/azure # ls
file1.txt
這個文件可以在Azure Portal里面查看到,截圖略。