メインコンテンツまでスキップ

DynamoDB テーブルのプロビジョニング

実際の DynamoDB テーブルを Kubernetes リソースとして定義できるようになりました。マニフェストを見てみましょう:

~/environment/eks-workshop/modules/fastpaths/eks-capabilities/ack/dynamodb/table.yaml
apiVersion: dynamodb.services.k8s.aws/v1alpha1
kind: Table
metadata:
name: items
namespace: carts
spec:
tableName: ${EKS_CLUSTER_AUTO_NAME}-carts-fastpath
billingMode: PAY_PER_REQUEST
keySchema:
- attributeName: id
keyType: HASH
attributeDefinitions:
- attributeName: id
attributeType: "S"
- attributeName: customerId
attributeType: "S"
globalSecondaryIndexes:
- indexName: idx_global_customerId
keySchema:
- attributeName: customerId
keyType: HASH
- attributeName: id
keyType: RANGE
projection:
projectionType: "ALL"
A

ACK DynamoDB コントローラーの Table カスタムリソースを使用します。

B

並行したワークショップの実行が衝突しないように、クラスター名 (${EKS_CLUSTER_AUTO_NAME}-carts-fastpath) に基づいてテーブルに名前を付けます。

C

オンデマンド料金を使用します。

D

carts サービスが期待するものに一致する、パーティションキーのスキーマを定義します。

E

サービスが顧客ごとにカートをクエリできるように、customerId に global secondary index を追加します。

備考

この YAML は DynamoDB CreateTable API を密接に反映しています。API を通じて表現できるものは、ここでも表現できます。

マニフェストを適用します:

~$kubectl kustomize ~/environment/eks-workshop/modules/fastpaths/eks-capabilities/ack/dynamodb \
| envsubst | kubectl apply -f -
table.dynamodb.services.k8s.aws/items created

Capability の DynamoDB コントローラーは、新しい Table リソースを検出し、対応する AWS リソースをプロビジョニングします。ACK.ResourceSynced condition を待ちます。これは、すべての ACK リソースが正常に調整されたことを示す方法です:

~$kubectl wait table.dynamodb.services.k8s.aws items \
-n carts --for=condition=ACK.ResourceSynced --timeout=10m
table.dynamodb.services.k8s.aws/items condition met

リソースのステータスを確認します:

~$kubectl get table.dynamodb.services.k8s.aws items -n carts \
-o jsonpath='{.status.tableStatus}{"\n"}'
ACTIVE

最後に、テーブルが AWS に存在することを確認します:

~$aws dynamodb describe-table \
--table-name "$EKS_CAP_DDB_TABLE" \
--query 'Table.TableStatus' --output text
ACTIVE

Kubernetes API から離れることなく、実際の DynamoDB テーブルを作成しました。Capability は、コントローラーのインフラストラクチャと DynamoDB API を呼び出すために必要な IAM パーミッションの両方を処理しました。