步驟 4:將應用程式部署到 Amazon EKS 並將資料寫入資料表 - Amazon Keyspaces (適用於 Apache Cassandra)

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

步驟 4:將應用程式部署到 Amazon EKS 並將資料寫入資料表

在本教學的這個步驟中,您要為應用程式設定 Amazon EKS 部署,並確認應用程式正在執行且可以連線到 Amazon Keyspaces。

若要將應用程式部署到 Amazon EKS,您需要在名為deployment.yaml的檔案中設定所有相關設定。然後 Amazon EKS 會使用此檔案來部署應用程式。檔案中的中繼資料應包含下列資訊:

  • 應用程式名稱應用程式的名稱。在本教程中,我們使用my-keyspaces-app.

  • Kubernetes 命名空間是 Amazon EKS 叢集的命名空間。在本教程中,我們使用my-eks-namespace.

  • Amazon EKS 服務帳戶名稱 Amazon EKS 服務帳戶的名稱。在本教程中,我們使用my-eks-serviceaccount.

  • 影像名稱應用程式影像的名稱。在本教程中,我們使用my-keyspaces-app.

  • 圖像 URI 來自 Amazon ECR 的碼頭圖像 URI。

  • AWS 帳戶 ID 您的 AWS 帳戶 ID。

  • IAM 角色 ARN 是為服務帳戶建立的 IAM 角色所建立的 ARN。在本教程中,我們使用my-iam-role.

  • AWS 區域 您在其中創建了 Amazon EKS 集群的 Amazon EKS 集群。 AWS 區域

在此步驟中,您會部署並執行連線到 Amazon Keyspaces 並將資料寫入資料表的應用程式。

  1. 設定 deployment.yaml 檔案。您需要替換以下值:

    • name

    • namespace

    • serviceAccountName

    • image

    • AWS_ROLE_ARN value

    • AWS 區域 在 CASSANDRA_HOST

    • AWS_REGION

    您可以使用下列檔案做為範例。

    apiVersion: apps/v1 kind: Deployment metadata: name: my-keyspaces-app namespace: my-eks-namespace spec: replicas: 1 selector: matchLabels: app: my-keyspaces-app template: metadata: labels: app: my-keyspaces-app spec: serviceAccountName: my-eks-serviceaccount containers: - name: my-keyspaces-app image: 111122223333.dkr.ecr.aws-region.amazonaws.com/my-ecr-repository:latest ports: - containerPort: 8080 env: - name: CASSANDRA_HOST value: "cassandra.aws-region.amazonaws.com:9142" - name: CASSANDRA_DC value: "aws-region" - name: AWS_WEB_IDENTITY_TOKEN_FILE value: /var/run/secrets/eks.amazonaws.com/serviceaccount/token - name: AWS_ROLE_ARN value: "arn:aws:iam::111122223333:role/my-iam-role" - name: AWS_REGION value: "aws-region"
  2. 部署 deployment.yaml

    kubectl apply -f deployment.yaml

    輸出應該是這樣的。

    deployment.apps/my-keyspaces-app created
  3. 檢查 Amazon EKS 叢集命名空間中網繭的狀態。

    kubectl get pods -n my-eks-namespace

    輸出看起來應該類似於這個例子。

    NAME READY STATUS RESTARTS AGE my-keyspaces-app-123abcde4f-g5hij 1/1 Running 0 75s

    有關更多詳細信息,您可以使用以下命令。

    kubectl describe pod my-keyspaces-app-123abcde4f-g5hij -n my-eks-namespace
    Name: my-keyspaces-app-123abcde4f-g5hij Namespace: my-eks-namespace Priority: 2000001000 Priority Class Name: system-node-critical Service Account: my-eks-serviceaccount Node: fargate-ip-192-168-102-209.ec2.internal/192.168.102.209 Start Time: Thu, 23 Nov 2023 12:15:43 +0000 Labels: app=my-keyspaces-app eks.amazonaws.com/fargate-profile=my-fargate-profile pod-template-hash=6c56fccc56 Annotations: CapacityProvisioned: 0.25vCPU 0.5GB Logging: LoggingDisabled: LOGGING_CONFIGMAP_NOT_FOUND Status: Running IP: 192.168.102.209 IPs: IP: 192.168.102.209 Controlled By: ReplicaSet/my-keyspaces-app-6c56fccc56 Containers: my-keyspaces-app: Container ID: containerd://41ff7811d33ae4bc398755800abcdc132335d51d74f218ba81da0700a6f8c67b Image: 111122223333.dkr.ecr.aws-region.amazonaws.com/my_eks_repository:latest Image ID: 111122223333.dkr.ecr.aws-region.amazonaws.com/my_eks_repository@sha256:fd3c6430fc5251661efce99741c72c1b4b03061474940200d0524b84a951439c Port: 8080/TCP Host Port: 0/TCP State: Running Started: Thu, 23 Nov 2023 12:15:19 +0000 Finished: Thu, 23 Nov 2023 12:16:17 +0000 Ready: True Restart Count: 1 Environment: CASSANDRA_HOST: cassandra.aws-region.amazonaws.com:9142 CASSANDRA_DC: aws-region AWS_WEB_IDENTITY_TOKEN_FILE: /var/run/secrets/eks.amazonaws.com/serviceaccount/token AWS_ROLE_ARN: arn:aws:iam::111122223333:role/my-iam-role AWS_REGION: aws-region AWS_STS_REGIONAL_ENDPOINTS: regional Mounts: /var/run/secrets/eks.amazonaws.com/serviceaccount from aws-iam-token (ro) /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-fssbf (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: aws-iam-token: Type: Projected (a volume that contains injected data from multiple sources) TokenExpirationSeconds: 86400 kube-api-access-fssbf: Type: Projected (a volume that contains injected data from multiple sources) TokenExpirationSeconds: 3607 ConfigMapName: kube-root-ca.crt ConfigMapOptional: <nil> DownwardAPI: true QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s node.kubernetes.io/unreachable:NoExecute op=Exists for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Warning LoggingDisabled 2m13s fargate-scheduler Disabled logging because aws-logging configmap was not found. configmap "aws-logging" not found Normal Scheduled 89s fargate-scheduler Successfully assigned my-eks-namespace/my-keyspaces-app-6c56fccc56-mgs2m to fargate-ip-192-168-102-209.ec2.internal Normal Pulled 75s kubelet Successfully pulled image "111122223333.dkr.ecr.aws-region.amazonaws.com/my_eks_repository:latest" in 13.027s (13.027s including waiting) Normal Pulling 54s (x2 over 88s) kubelet Pulling image "111122223333.dkr.ecr.aws-region.amazonaws.com/my_eks_repository:latest" Normal Created 54s (x2 over 75s) kubelet Created container my-keyspaces-app Normal Pulled 54s kubelet Successfully pulled image "111122223333.dkr.ecr.aws-region.amazonaws.com/my_eks_repository:latest" in 222ms (222ms including waiting) Normal Started 53s (x2 over 75s) kubelet Started container my-keyspaces-app
  4. 檢查 Pod 的日誌以確認您的應用程式正在執行,並且可以連線到 Amazon Keyspaces 表格。您可以使用以下命令執行此操作。請務必取代部署的名稱。

    kubectl logs -f my-keyspaces-app-123abcde4f-g5hij -n my-eks-namespace

    您應該能夠看到應用程式記錄項目,確認與 Amazon Keyspaces 的連線,如下列範例所示。

    2:47:20.553 [s0-admin-0] DEBUG c.d.o.d.i.c.metadata.MetadataManager - [s0] Adding initial contact points [Node(endPoint=cassandra.aws-region.amazonaws.com/1.222.333.44:9142, hostId=null, hashCode=e750d92)] 22:47:20.562 [s0-admin-1] DEBUG c.d.o.d.i.c.c.ControlConnection - [s0] Initializing with event types [SCHEMA_CHANGE, STATUS_CHANGE, TOPOLOGY_CHANGE] 22:47:20.564 [s0-admin-1] DEBUG c.d.o.d.i.core.context.EventBus - [s0] Registering com.datastax.oss.driver.internal.core.metadata.LoadBalancingPolicyWrapper$$Lambda$812/0x0000000801105e88@769afb95 for class com.datastax.oss.driver.internal.core.metadata.NodeStateEvent 22:47:20.566 [s0-admin-1] DEBUG c.d.o.d.i.c.c.ControlConnection - [s0] Trying to establish a connection to Node(endPoint=cassandra.us-east-1.amazonaws.com/1.222.333.44:9142, hostId=null, hashCode=e750d92)
  5. 在 Amazon Keyspaces 資料表上執行下列 CQL 查詢,以確認已將一列資料寫入資料表:

    SELECT * from aws.user;

    您應該會看到下列輸出:

    fname | lname | username | last_update_date ----------+-------+----------+----------------------------- random | k | test | 2023-12-07 13:58:31.57+0000