2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ARO(Azure Red Hat OpenShift)で静的に作成されたAzure Filesを利用する

Last updated at Posted at 2023-02-28

はじめに

今回はARO(Azure Red Hat OpenShift)でAzure Filesを利用するための作業メモになります。
AROでAzure FilesのStorageClassを作成してDynamic Provisioningを実現する方法はAzureの公式ドキュメントに詳しくまとまっているので、そちらを確認いただくのが良いかと思います。

今回は、あらかじめ作成しておいたAzure Filesを様々なPodからマウントして共有ストレージとして活用する環境を構築します。

ScreenShot.png

前提条件

  • 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-testNamespaceを利用します。

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ストレージ名を指定します。

azfiles-pv.yaml
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点です。

  1. spec.storageClassName""を指定してください。この書き方をしないと、デフォルトのStorageClassを参照してしまい、うまくPVと紐づけられません。
  2. spec.volumeNameに先ほど作成したPV名を指定します。
azfiles-pvc.yaml
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をデプロイしてみましょう。

job1.yaml
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
job2.yaml
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つのファイルを読み込みます。

check-job.yaml
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-fileJobのPodのログを確認すると、2つのファイルの中身が出力されています。これによりそれぞれのPodが1つのAzure Filesストレージにアクセスできており、Read/Writeも実行できていることがわかります。

oc logs check-text-file-bz2tr
Job1 - Hello, World!
Job2 - Hello, World!
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?