共用方式為


在 Azure Red Hat OpenShift 4 上建立 Azure 檔案 StorageClass

在本文中,您會建立使用 Azure 檔案儲存體動態佈建 ReadWriteMany (RWX) 儲存體的 Azure Red Hat OpenShift 4 StorageClass。 您將了解如何:

  • 設定必要條件並安裝必要的工具
  • 使用 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 (ARO) 叢集的資源群組之外建立資源群組。 此資源群組包含 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

設定權限

設定資源群組權限

ARO 服務主體需要新 Azure 儲存體帳戶資源群組上的「listKeys」權限。 指派「參與者」角色以達成此目的。

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

設定 ARO 叢集權限

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 資訊清單內需要有儲存體帳戶的詳細資料,ARO 叢集才能知道要查看目前資源群組外部的儲存體帳戶。

在儲存體佈建期間,系統會為掛接認證建立以 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」將 symlinks 對應至用戶端可以使用的表單
  • 在用戶端停用權限檢查的「noperm」

建議參數:

  • 如果用戶端已經透過現有的掛階點連線,則「nossharesock」停用重複使用通訊端
  • 「actimeo=30」(或更新版本) 可增加 CIFS 用戶端快取檔案和目錄屬性的時間
  • 「nobrl」可停用將位元組範圍鎖定要求傳送至伺服器,以及具有 posix 鎖定挑戰的應用程式

變更預設 StorageClass (選擇性)

ARO 上的預設 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 範本,您必須部署已啟用提取祕密功能的 ARO 叢集。 如需詳細資訊,請參閱取得 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 支援的資源。