0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

プリザンターの添付ファイルをクラウドPaaS環境で永続化する方法

0
Posted at

はじめに

プリザンターを Azure App Service や AWS ECS、Google Cloud Run などの PaaS 環境にデプロイする場合、添付ファイルの保存先をどうするかは重要な検討事項です。PaaS 環境ではコンテナやインスタンスが再起動・スケールするとローカルストレージの内容が消失するため、添付ファイルを永続化する仕組みが必要になります。

Azure の場合は Azure Files をマウントする方法がマニュアルに記載されていますが、AWS や Google Cloud の PaaS を使う場合はどう対応すればいいのか、情報がまとまっていません。

この記事では、プリザンターの添付ファイル格納の仕組みを整理した上で、各クラウドサービスの PaaS 環境における永続化方法をまとめます。

バージョン 1.5.7.0 を対象にしています

プリザンターの添付ファイル格納方式

BinaryStorage.json の概要

プリザンターでは、添付ファイルの格納方式を BinaryStorage.json で設定します。

Implem.Pleasanter/App_Data/Parameters/BinaryStorage.json
{
    "Provider": "Rds",
    "AzureBlobStorageAccountUri": null,
    "AzureBlobContainerName": null,
    "Path": null,
    "Attachments": true,
    "Images": true,
    "UseStorageSelect": false,
    "TemporaryBinaryStorageProvider": null
}

格納方式の比較

Provider に指定できる値は Rds・Local・AzureBlob の 3 種類です。

項目 Rds Local AzureBlob
格納先 データベース(Binaries テーブル) ファイルシステム Azure Blob Storage
データ形式 バイナリカラム(Bin) バイナリファイル(GUID 名) BLOB(Attachments/{Guid})
DB 負荷 大きい(ファイルサイズ分) 小さい(メタデータのみ) 小さい(メタデータのみ)
バックアップ DB バックアップに含まれる 別途ファイルバックアップが必要 ストレージアカウント側で管理
PaaS 対応 追加設定不要 永続ストレージのマウントが必要 マウント不要(HTTPS でアクセス)

AzureBlob はバージョン 1.5.7.0 で追加されたプロバイダです。1.5.6.0 以前は Rds と Local の 2 択でした。

PaaS 環境での選択指針

PaaS 環境で添付ファイルを扱う場合、大きく 3 つの方針があります。

  • Rds モード: データベースに格納するため PaaS 環境でも追加設定は不要です。ただしデータベースの容量・コストに直接影響します
  • Local モード: ファイルシステムに格納するため、PaaS 環境ではクラウドファイルストレージをマウントする必要があります
  • AzureBlob モード: Azure Blob Storage に直接格納します。ファイルシステムのマウントが不要なため、Azure 上の PaaS では最も相性の良い構成です

Rds モードで運用する場合

Provider を Rds に設定すれば、添付ファイルはすべてデータベースに格納されるため、ファイルストレージのマウントは不要です。

BinaryStorage.json
{
    "Provider": "Rds"
}

PaaS 環境では最もシンプルな構成ですが、以下の点に注意が必要です。

  • データベースの容量が添付ファイルの分だけ増加します
  • SQL Server Express を使用している場合、10GB の容量制限があります
  • 大量の添付ファイルを扱う場合、データベースのパフォーマンスに影響する可能性があります
  • データベースのバックアップサイズが大きくなります

大量の添付ファイルを扱う場合や、ファイルサイズが大きい場合は、Local モードでの運用を検討してください。

Local モードで運用する場合

Provider を Local に設定し、Path にマウント先のパスを指定します。PaaS 環境では、各クラウドサービスのファイルストレージサービスをマウントして使用します。

BinaryStorage.json
{
    "Provider": "Local",
    "Path": "/mnt/pleasanter-files"
}

以降のセクションでは、各クラウドサービスごとのマウント方法を説明します。

Azure

Azure App Service + Azure Blob Storage(1.5.7.0 以降の推奨)

バージョン 1.5.7.0 で AzureBlob プロバイダが追加され、ファイル共有をマウントせずに Azure Blob Storage へ直接格納できるようになりました。認証は DefaultAzureCredential を使うため、App Service のマネージド ID をそのまま利用できます(接続文字列やアカウントキーの保持が不要です)。

構成イメージ

手順

1. ストレージアカウントとコンテナの作成
az storage account create \
  --name stpleasanterblob \
  --resource-group rg-pleasanter \
  --location japaneast \
  --sku Standard_LRS

az storage container create \
  --account-name stpleasanterblob \
  --name pleasanter-binaries \
  --auth-mode login
2. App Service のマネージド ID にロールを付与
# システム割り当てマネージド ID を有効化
PRINCIPAL_ID=$(az webapp identity assign \
  --resource-group rg-pleasanter \
  --name app-pleasanter \
  --query principalId -o tsv)

# ストレージ BLOB データ共同作成者ロールを付与
az role assignment create \
  --assignee "$PRINCIPAL_ID" \
  --role "Storage Blob Data Contributor" \
  --scope "/subscriptions/<subscription-id>/resourceGroups/rg-pleasanter/providers/Microsoft.Storage/storageAccounts/stpleasanterblob"
3. BinaryStorage.json の設定
BinaryStorage.json
{
    "Provider": "AzureBlob",
    "AzureBlobStorageAccountUri": "https://stpleasanterblob.blob.core.windows.net",
    "AzureBlobContainerName": "pleasanter-binaries"
}

この 2 つの値は環境変数でも指定できます。コンテナ名を省略した場合の既定値は pleasanter-binaries です。

環境変数 対応する設定
Implem.Pleasanter_BinaryStorage_AzureBlobStorageAccountUri AzureBlobStorageAccountUri
Implem.Pleasanter_BinaryStorage_AzureBlobContainerName AzureBlobContainerName

添付ファイルは Attachments/{Guid} というオブジェクト名で BLOB に保存されます。

AzureBlobBinaryStorageProvider にはサーキットブレーカーが組み込まれており、接続・認証に連続して失敗すると 30 秒 → 2 分 → 10 分と段階的に遮断されます。遮断中は IOException が返り、失敗の内容は SysLogs に記録されます。

Azure App Service + Azure Files

Azure App Service では、Azure Files(SMB ファイル共有)をパスマッピングでマウントできます。

構成イメージ

手順

1. ストレージアカウントの作成

Azure Portal または Azure CLI でストレージアカウントとファイル共有を作成します。

# リソースグループの作成
az group create --name rg-pleasanter --location japaneast

# ストレージアカウントの作成
az storage account create \
  --name stpleasanterfiles \
  --resource-group rg-pleasanter \
  --location japaneast \
  --sku Standard_LRS

# ファイル共有の作成
az storage share-rm create \
  --storage-account stpleasanterfiles \
  --name pleasanter-attachments \
  --quota 100
2. App Service にマウント

Azure Portal の場合は「構成」>「パス マッピング」から設定します。Azure CLI の場合は以下のコマンドで設定します。

# ストレージアカウントキーの取得
STORAGE_KEY=$(az storage account keys list \
  --account-name stpleasanterfiles \
  --resource-group rg-pleasanter \
  --query '[0].value' -o tsv)

# App Service にマウント
az webapp config storage-account add \
  --resource-group rg-pleasanter \
  --name app-pleasanter \
  --custom-id PleasanterFiles \
  --storage-type AzureFiles \
  --share-name pleasanter-attachments \
  --account-name stpleasanterfiles \
  --access-key "$STORAGE_KEY" \
  --mount-path /mnt/pleasanter-files
3. BinaryStorage.json の設定
BinaryStorage.json
{
    "Provider": "Local",
    "Path": "/mnt/pleasanter-files"
}

Azure App Service のストレージマウントについては「Azure Storage をローカル共有としてマウントする」を参照してください。

Azure Container Apps + Azure Files

Azure Container Apps でも同様に Azure Files をマウントできます。

# Container Apps 環境にストレージを追加
az containerapp env storage set \
  --name cae-pleasanter \
  --resource-group rg-pleasanter \
  --storage-name pleasanterfiles \
  --azure-file-account-name stpleasanterfiles \
  --azure-file-account-key "$STORAGE_KEY" \
  --azure-file-share-name pleasanter-attachments \
  --access-mode ReadWrite

Container Apps のアプリ定義(YAML)でボリュームマウントを設定します。

properties:
  template:
    containers:
      - name: pleasanter
        image: your-registry/pleasanter:latest
        volumeMounts:
          - volumeName: pleasanter-files
            mountPath: /mnt/pleasanter-files
    volumes:
      - name: pleasanter-files
        storageType: AzureFile
        storageName: pleasanterfiles

AWS

AWS ECS(Fargate)+ Amazon EFS

AWS の PaaS 環境では、Amazon EFS(Elastic File System)を使用します。EFS は NFS v4 プロトコルのマネージドファイルストレージで、ECS タスクから直接マウントできます。

構成イメージ

手順

1. EFS ファイルシステムの作成
# EFS ファイルシステムの作成
aws efs create-file-system \
  --performance-mode generalPurpose \
  --throughput-mode bursting \
  --tags Key=Name,Value=pleasanter-attachments \
  --region ap-northeast-1

# マウントターゲットの作成(VPC のサブネットごとに作成)
aws efs create-mount-target \
  --file-system-id fs-0123456789abcdef0 \
  --subnet-id subnet-0123456789abcdef0 \
  --security-groups sg-0123456789abcdef0
2. ECS タスク定義で EFS を設定

タスク定義の JSON でボリュームとマウントポイントを設定します。

task-definition.json
{
    "family": "pleasanter",
    "networkMode": "awsvpc",
    "requiresCompatibilities": ["FARGATE"],
    "cpu": "1024",
    "memory": "2048",
    "volumes": [
        {
            "name": "pleasanter-files",
            "efsVolumeConfiguration": {
                "fileSystemId": "fs-0123456789abcdef0",
                "rootDirectory": "/pleasanter-attachments",
                "transitEncryption": "ENABLED"
            }
        }
    ],
    "containerDefinitions": [
        {
            "name": "pleasanter",
            "image": "your-registry/pleasanter:latest",
            "mountPoints": [
                {
                    "sourceVolume": "pleasanter-files",
                    "containerPath": "/mnt/pleasanter-files",
                    "readOnly": false
                }
            ]
        }
    ]
}
3. BinaryStorage.json の設定
BinaryStorage.json
{
    "Provider": "Local",
    "Path": "/mnt/pleasanter-files"
}

EFS のマウント方法については「Amazon EFS ファイルシステムのマウント」を参照してください。

AWS Elastic Beanstalk + Amazon EFS

Elastic Beanstalk の場合は、.ebextensions を使って EFS をマウントします。

.ebextensions/01-efs-mount.config
packages:
  yum:
    amazon-efs-utils: []

commands:
  01_mount_efs:
    command: |
      mkdir -p /mnt/pleasanter-files
      mount -t efs -o tls fs-0123456789abcdef0:/ /mnt/pleasanter-files
      chown webapp:webapp /mnt/pleasanter-files
    test: "! mountpoint -q /mnt/pleasanter-files"

Google Cloud

Cloud Run + Filestore(NFS)

Google Cloud の PaaS 環境では、Filestore(NFS マネージドサービス)を使用します。Cloud Run の第 2 世代実行環境では NFS ボリュームのマウントがサポートされています。

構成イメージ

手順

1. Filestore インスタンスの作成
# Filestore インスタンスの作成
gcloud filestore instances create pleasanter-files \
  --zone=asia-northeast1-a \
  --tier=BASIC_HDD \
  --file-share=name=pleasanter_attachments,capacity=1TB \
  --network=name=default
2. Cloud Run サービスで NFS ボリュームをマウント

Cloud Run の YAML 定義でボリュームマウントを設定します。

service.yaml
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: pleasanter
spec:
  template:
    metadata:
      annotations:
        run.googleapis.com/execution-environment: gen2
        run.googleapis.com/network-interfaces: '[{"network":"default","subnetwork":"default"}]'
    spec:
      containers:
        - image: your-registry/pleasanter:latest
          volumeMounts:
            - name: pleasanter-files
              mountPath: /mnt/pleasanter-files
      volumes:
        - name: pleasanter-files
          nfs:
            server: 10.0.0.2
            path: /pleasanter_attachments
            readOnly: false
# デプロイ
gcloud run services replace service.yaml --region=asia-northeast1
3. BinaryStorage.json の設定
BinaryStorage.json
{
    "Provider": "Local",
    "Path": "/mnt/pleasanter-files"
}

Cloud Run での NFS マウントについては「Cloud Run でネットワーク ファイル システムを使用する」を参照してください。

GKE(Google Kubernetes Engine)+ Filestore

GKE の場合は、Filestore CSI ドライバーを使用して PersistentVolume としてマウントします。

pv-filestore.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pleasanter-pv
spec:
  capacity:
    storage: 100Gi
  accessModes:
    - ReadWriteMany
  nfs:
    server: 10.0.0.2
    path: /pleasanter_attachments
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pleasanter-pvc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 100Gi
  volumeName: pleasanter-pv

Pod の定義でボリュームをマウントします。

deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pleasanter
spec:
  template:
    spec:
      containers:
        - name: pleasanter
          image: your-registry/pleasanter:latest
          volumeMounts:
            - name: pleasanter-files
              mountPath: /mnt/pleasanter-files
      volumes:
        - name: pleasanter-files
          persistentVolumeClaim:
            claimName: pleasanter-pvc

Docker Compose 環境

ローカル開発や小規模な Docker Compose 環境では、ホストのディレクトリをバインドマウントするか、Docker ボリュームを使用します。

docker-compose.yml
services:
  pleasanter:
    image: your-registry/pleasanter:latest
    volumes:
      - pleasanter-files:/mnt/pleasanter-files
    environment:
      - ASPNETCORE_URLS=http://+:5000

volumes:
  pleasanter-files:
    driver: local
BinaryStorage.json
{
    "Provider": "Local",
    "Path": "/mnt/pleasanter-files"
}

ロードバランサ環境での注意点

複数のインスタンスでプリザンターを運用する場合、一時ファイルの保存先に注意が必要です。

TemporaryBinaryStorageProvider と Provider の関係

BinaryStorage.json には一時ファイルの保存先を指定する TemporaryBinaryStorageProvider 設定があります。これを Rds にすると、Web UI からのアップロード時に一時ファイルがデータベースの Binaries テーブルに BinaryType = 'Temporary' として格納されます。ロードバランサ配下でアップロード用リクエストと保存用リクエストが別インスタンスに振り分けられても一時ファイルを見失わないための仕組みです。

バージョン 1.5.7.0 では、レコード保存時に一時ファイルを DB から外部ストレージへ移し替える処理(MoveBinFromRdsToExternal)が追加されました。Provider が Local や AzureBlob(=外部ストレージのプロバイダが存在する構成)であれば、Bin カラムのデータは外部ストレージへアップロードされたうえで NULL に更新されます。

Implem.Pleasanter/Libraries/DataTypes/Attachment.cs
// TemporaryBinaryStorageProvider が "Rds" かつ Web UI の場合
if (Parameters.BinaryStorage.TemporaryBinaryStorageProvider == BinaryStorageProviderNames.Rds
    && context.Api != true)
{
    if (provider != null)
    {
        // 一時ファイルの Bin を外部ストレージへ移送
        MoveBinFromRdsToExternal(
            context: context,
            provider: provider);
    }
    statements.Add(Rds.UpdateBinaries(
        param: Rds.BinariesParam()
            .TenantId(context.TenantId)
            .ReferenceId(referenceId, _using: referenceId != 0)
            .Guid(Guid)
            .BinaryType("Attachments")
            .Bin(raw: "NULL", _using: provider != null)   // 移送済みなら NULL にする
            // ...
        where: Rds.BinariesWhere().Guid(Guid)));
}

MoveBinFromRdsToExternal() は Binaries テーブルから BinaryType = 'Temporary' のレコードを読み出し、Attachments/{Guid} というオブジェクト名で外部ストレージへアップロードします。アップロードに失敗した場合は SysLogs に記録したうえで例外を送出するため、DB 側だけ更新されて実体が消えるということはありません。

バージョン 1.5.6.0 以前は移送処理がなく、TemporaryBinaryStorageProvider を Rds にすると Provider が Local でもファイルが Bin カラムに残り続け、Local モードのメリット(DB サイズの削減)が得られませんでした。1.5.6.0 以前を使っている場合は、次のセクションの構成を検討してください。

PaaS 環境での推奨構成

1.5.7.0 以降であれば、Provider を Local または AzureBlob にしたうえで TemporaryBinaryStorageProvider を Rds にする構成が使えます。一時ファイルはインスタンスをまたいで共有され、保存時には外部ストレージへ移送されます。

BinaryStorage.json(1.5.7.0 以降・ロードバランサ配下)
{
    "Provider": "AzureBlob",
    "AzureBlobStorageAccountUri": "https://stpleasanterblob.blob.core.windows.net",
    "AzureBlobContainerName": "pleasanter-binaries",
    "TemporaryBinaryStorageProvider": "Rds"
}

1.5.6.0 以前を使う場合、ロードバランサ配下で Provider を Local にするなら、TemporaryBinaryStorageProvider を Rds にするのではなく、一時ファイルの保存先も含めて共有ストレージでカバーする構成にします。この記事で紹介した Azure Files・Amazon EFS・Filestore をマウントすれば、すべてのインスタンスから同じファイルシステムにアクセスできるため、一時ファイルの問題は発生しません。

BinaryStorage.json(推奨)
{
    "Provider": "Local",
    "Path": "/mnt/pleasanter-files"
}

一方、添付ファイルの容量を気にしない場合は Provider を Rds にしてすべてを DB に格納する構成がもっともシンプルです。

BinaryStorage.json(DB 格納の場合)
{
    "Provider": "Rds"
}

まとめ

クラウド PaaS 環境でプリザンターの添付ファイルを永続化する方法を各クラウドサービス別にまとめました。

  • PaaS 環境では Rds モード(DB 格納)が最もシンプルですが、ファイル容量が大きい場合は Local モードか AzureBlob モードが適しています
  • Azure 上で運用する場合は、1.5.7.0 で追加された AzureBlob モードがマウント不要でもっとも扱いやすい構成です
  • Local モードでは、各クラウドサービスのファイルストレージをマウントして使用します
  • ロードバランサ配下では TemporaryBinaryStorageProvider を Rds にすると一時ファイルをインスタンス間で共有できます(1.5.7.0 以降はレコード保存時に外部ストレージへ移送されます)
クラウド PaaS サービス ファイルストレージ マウント方式
Azure App Service Azure Blob Storage マウント不要(Provider: AzureBlob)
Azure App Service Azure Files パスマッピング
Azure Container Apps Azure Files ボリュームマウント
AWS ECS(Fargate) Amazon EFS EFS ボリューム
AWS Elastic Beanstalk Amazon EFS .ebextensions
Google Cloud Cloud Run Filestore NFS ボリューム
Google Cloud GKE Filestore PersistentVolume
Docker Docker Compose ホストディレクトリ バインドマウント

マウント方式を採る場合、プリザンター側の設定は BinaryStorage.json の Provider を Local にして Path にマウント先のパスを指定するだけです。クラウドサービス側のストレージ設定が異なるだけで、プリザンターから見ると同じファイルシステムとして透過的に扱えます。Azure Blob Storage を使う場合だけはファイルシステムを介さないため、Provider を AzureBlob にしてストレージアカウント URI とコンテナ名を指定します。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?