在本文中,您會建立適用於 Azure Red Hat OpenShift 4 的 StorageClass,以動態方式使用 Azure 檔案服務布建 ReadWriteMany (RWX) 記憶體。 您將瞭解如何:
- 設定必要條件並安裝必要的工具
- 使用 Azure 檔案佈建程式建立 Azure Red Hat OpenShift 4 StorageClass
如果您選擇在本機安裝和使用 CLI,本教學課程會要求您執行 Azure CLI 2.6.0 版或更新版本。 若要尋找版本,請執行 az --version 命令。 如果您需要安裝或升級,請參閱安裝 Azure CLI。
開始之前
將 Azure Red Hat OpenShift 4 叢集部署至您的訂用帳戶,請參閱 建立 Azure Red Hat OpenShift 4 叢集。
設定 Azure 儲存體帳戶
此步驟會在 Azure Red Hat OpenShift 叢集的資源群組外部建立資源群組。 此資源群組包含由 Azure Red Hat OpenShift 動態配置者創建的 Azure 檔案共享。
AZURE_FILES_RESOURCE_GROUP=aro_azure_files
LOCATION=eastus
az group create -l $LOCATION -n $AZURE_FILES_RESOURCE_GROUP
AZURE_STORAGE_ACCOUNT_NAME=aroazurefilessa
az storage account create \
--name $AZURE_STORAGE_ACCOUNT_NAME \
--resource-group $AZURE_FILES_RESOURCE_GROUP \
--kind StorageV2 \
--sku Standard_LRS
設定權限
設定資源群組權限
服務主體需要 listKeys 新 Azure 儲存體帳戶資源群組的許可權。 指派貢獻者角色。
ARO_RESOURCE_GROUP=aro-rg
CLUSTER=cluster
ARO_SERVICE_PRINCIPAL_ID=$(az aro show -g $ARO_RESOURCE_GROUP -n $CLUSTER --query servicePrincipalProfile.clientId -o tsv)
az role assignment create --role Contributor --scope /subscriptions/mySubscriptionID/resourceGroups/$AZURE_FILES_RESOURCE_GROUP --assignee $ARO_SERVICE_PRINCIPAL_ID
設定叢集許可權
OpenShift 永續性磁碟區繫結器服務帳戶需要讀取秘密的能力。 建立並指派 OpenShift 叢集角色。
ARO_API_SERVER=$(az aro list --query "[?contains(name,'$CLUSTER')].[apiserverProfile.url]" -o tsv)
oc login -u kubeadmin -p $(az aro list-credentials -g $ARO_RESOURCE_GROUP -n $CLUSTER --query=kubeadminPassword -o tsv) $ARO_API_SERVER
oc create clusterrole azure-secret-reader \
--verb=create,get \
--resource=secrets
oc adm policy add-cluster-role-to-user azure-secret-reader system:serviceaccount:kube-system:persistent-volume-binder
使用 Azure 檔案儲存體佈建程式建立 StorageClass
此步驟使用 Azure 檔案儲存體佈建程式來建立 StorageClass。 在 StorageClass 資訊清單中,需要儲存體帳戶的詳細數據,讓叢集知道要查看目前資源群組外部的儲存體帳戶。
在儲存體佈建期間,系統會為掛接認證建立以 secretName 命名的秘密。 在多租戶的情境中,建議明確設定 secretNamespace 的值,否則其他使用者可能會讀取儲存帳戶認證。
cat << EOF >> azure-storageclass-azure-file.yaml
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: azure-file
provisioner: file.csi.azure.com
mountOptions:
- dir_mode=0777
- file_mode=0777
- uid=0
- gid=0
- mfsymlinks
- cache=strict
- actimeo=30
- noperm
parameters:
location: $LOCATION
secretNamespace: kube-system
skuName: Standard_LRS
storageAccount: $AZURE_STORAGE_ACCOUNT_NAME
resourceGroup: $AZURE_FILES_RESOURCE_GROUP
reclaimPolicy: Delete
volumeBindingMode: Immediate
EOF
oc create -f azure-storageclass-azure-file.yaml
Azure 檔案儲存體的掛接選項通常取決於您部署的工作負載,以及應用程式的需求。 特別是針對 Azure 檔案儲存體,您應該考慮使用的其他參數。
必要參數:
-
mfsymlinks將符號連結對應至用戶端可以使用的格式 -
noperm在用戶端停用許可權檢查
建議參數:
-
nossharesock表示如果客戶端已透過現有的裝入點連接,則停用重複使用套接字 -
actimeo=30(或更高版本)以增加 CIFS 用戶端快取檔案和目錄屬性的時間 -
nobrl來停用將位元組範圍鎖要求傳送至伺服器,以及為具有 posix 鎖問題的應用程式提供解決方案。
變更預設 StorageClass (選擇性)
預設的 StorageClass 稱為 managed-premium,並使用 azure-disk 佈建程式。 藉由對 StorageClass 指令清單發出修補程式命令來變更此設定。
oc patch storageclass managed-premium -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"false"}}}'
oc patch storageclass azure-file -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
確認 Azure 檔案儲存體 StorageClass (選擇性)
建立新的應用程式,並向其指派儲存體。
備註
若要使用 httpd-example 範本,您必須在部署叢集時啟用提取密碼。 如需詳細資訊,請參閱取得 Red Hat 提取祕密。
oc new-project azfiletest
oc new-app httpd-example
#Wait for the pod to become Ready
curl $(oc get route httpd-example -n azfiletest -o jsonpath={.spec.host})
#If you have set the storage class by default, you can omit the --claim-class parameter
oc set volume dc/httpd-example --add --name=v1 -t pvc --claim-size=1G -m /data --claim-class='azure-file'
#Wait for the new deployment to rollout
export POD=$(oc get pods --field-selector=status.phase==Running -o jsonpath={.items[].metadata.name})
oc exec $POD -- bash -c "echo 'azure file storage' >> /data/test.txt"
oc exec $POD -- bash -c "cat /data/test.txt"
azure file storage
test.txt 檔案可透過 Azure 入口網站中的記憶體總管來看見。
後續步驟
在本文中,您已使用 Microsoft Azure 檔案儲存體和 Azure Red Hat OpenShift 4 建立動態的永續性儲存體。 您已學到如何做到以下幾點:
- 建立儲存體帳戶
- 使用 Azure 檔案儲存體佈建程式在 Azure Red Hat OpenShift 4 叢集上設定 StorageClass
請前往下一篇文章,以瞭解 Azure Red Hat OpenShift 4 支援的資源。