はじめに
今回はARO(Azure Red Hat OpenShift)でAzure Filesを利用するための作業メモになります。
AROでAzure FilesのStorageClassを作成してDynamic Provisioningを実現する方法はAzureの公式ドキュメントに詳しくまとまっているので、そちらを確認いただくのが良いかと思います。
今回は、あらかじめ作成しておいたAzure Filesを様々なPodからマウントして共有ストレージとして活用する環境を構築します。
前提条件
- Azureサブスクリプション
- AROクラスター(4.10.40)
- cluster-admin権限
作業ログ
まずAROをデプロイしたResource Groupにストレージアカウントを作成します。今回はaro-rg-01
というリソースグループにazurefilesa
というストレージアカウントを作成します。
STORAGE_ACCOUNT_NAME="azurefilessa"
RESOURCE_GROUP="aro-rg-01"
az storage account create -n $STORAGE_ACCOUNT_NAME -g $RESOURCE_GROUP --sku Standard_LRS
次に作成したサービスアカウントの接続文字列をエクスポートします。
AZURE_STORAGE_CONNECTION_STRING=$(az storage account show-connection-string -n $STORAGE_ACCOUNT_NAME -g $RESOURCE_GROUP -o tsv)
ファイル共有(Azure Files)を作成します。今回は"testshare"という名前にしました。
SHARE_NAME="testshare"
az storage share create -n $SHARE_NAME --connection-string $AZURE_STORAGE_CONNECTION_STRING
作成したストレージアカウントのキー情報をエクスポートしておきます。
STORAGE_KEY=$(az storage account keys list --resource-group $RESOURCE_GROUP --account-name $STORAGE_ACCOUNT_NAME --query "[0].value" -o tsv)
ファイル共有にアクセスするための資格情報をSecretとしてOpenShiftにデプロイします。今回はazure-files-test
Namespaceを利用します。
oc new-project azure-files-test
oc create secret generic azure-secret --from-literal=azurestorageaccountname=$STORAGE_ACCOUNT_NAME --from-literal=azurestorageaccountkey=$STORAGE_KEY
次に作成したAzure FilesストレージをARO上でPVとして登録します。
以下のyamlファイルを作成してデプロイしていきます。
ここでspec.azureFile.secretName
に先ほど作成したSecret名を、spec.azureFile.shareName
にAzure Filesストレージ名を指定します。
apiVersion: v1
kind: PersistentVolume
metadata:
name: azurefile-pv
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
azureFile:
secretName: azure-secret
shareName: testshare
readOnly: false
oc apply -f azfiles-pv.yaml
PVができたので、合わせてPVCも作成します。
ここでのポイントは2点です。
-
spec.storageClassName
に""
を指定してください。この書き方をしないと、デフォルトのStorageClassを参照してしまい、うまくPVと紐づけられません。 -
spec.volumeName
に先ほど作成したPV名を指定します。
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: azurefile
spec:
accessModes:
- ReadWriteMany
storageClassName: ""
volumeName: azurefile-pv
resources:
requests:
storage: 5Gi
oc apply -f azfiles-pvc.yaml
ではAzure Filesストレージにテキストファイルを作成するJobをデプロイしていきます。
以下の2つのJobをデプロイしてみましょう。
apiVersion: batch/v1
kind: Job
metadata:
name: create-text-file-1
spec:
template:
metadata:
name: create-text-file-1
spec:
containers:
- name: create-text-file-1
image: nginx:latest
command: [ "sh", "-c", "echo 'Job1 - Hello, World!' > /mnt/output/hello-job1.txt" ]
volumeMounts:
- name: azure
mountPath: /mnt/output
restartPolicy: Never
volumes:
- name: azure
persistentVolumeClaim:
claimName: azurefile
apiVersion: batch/v1
kind: Job
metadata:
name: create-text-file-2
spec:
template:
metadata:
name: create-text-file-2
spec:
containers:
- name: create-text-file-2
image: nginx:latest
command: [ "sh", "-c", "echo 'Job2 - Hello, World!' > /mnt/output/hello-job2.txt" ]
volumeMounts:
- name: azure
mountPath: /mnt/output
restartPolicy: Never
volumes:
- name: azure
persistentVolumeClaim:
claimName: azurefile
oc apply -f job1.yaml -f job2.yaml
これで2つのJobが同じAzure Filesストレージ上にそれぞれ別のファイルを作成しました。
では実際に確認していきましょう。
次のJobでは、ストレージ上にある2つのファイルを読み込みます。
apiVersion: batch/v1
kind: Job
metadata:
name: check-text-file
spec:
template:
metadata:
name: check-text-file
spec:
containers:
- name: check-text-file
image: nginx:latest
command: [ "sh", "-c", "cat /mnt/output/hello-job1.txt /mnt/output/hello-job2.txt" ]
volumeMounts:
- name: azure
mountPath: /mnt/output
restartPolicy: Never
volumes:
- name: azure
persistentVolumeClaim:
claimName: azurefile
oc apply -f check-job.yaml
全て実行すると以下のような状態になります。
oc get pods
NAME READY STATUS RESTARTS AGE
check-text-file-bz2tr 0/1 Completed 0 22s
create-text-file-1-xvls5 0/1 Completed 0 111s
create-text-file-2-d78fs 0/1 Completed 0 81s
check-text-file
JobのPodのログを確認すると、2つのファイルの中身が出力されています。これによりそれぞれのPodが1つのAzure Filesストレージにアクセスできており、Read/Writeも実行できていることがわかります。
oc logs check-text-file-bz2tr
Job1 - Hello, World!
Job2 - Hello, World!