

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 PGO 简化 Amazon EKS 上的 PostgreSQL 部署
<a name="streamline-postgresql-deployments-amazon-eks-pgo"></a>

*Shalaka Dengale，Amazon Web Services*

## Summary
<a name="streamline-postgresql-deployments-amazon-eks-pgo-summary"></a>

此模式会将来自 Crunchy Data 的 Postgres Operator（PGO）与 Amazon Elastic Kubernetes Service（Amazon EKS）集成在一起，以便简化云原生环境中的 PostgreSQL 部署。PGO 能够以自动化、可扩展的方式来管理 Kubernetes 中的 PostgreSQL 数据库。将 PGO 与 Amazon EKS 结合使用时，它将成为一个强大的平台，用于高效地部署、管理和扩展 PostgreSQL 数据库。

这一集成提供了下面这些主要优势：
+ 自动部署：简化 PostgreSQL 集群的部署和管理。
+ 自定义资源定义 (CRDs)：****使用 Kubernetes 原语进行 PostgreSQL 管理。
+ 高可用性：支持自动失效转移和同步复制。
+ 自动备份和恢复：****简化备份和还原过程。
+ 水平扩展：****为 PostgreSQL 集群启用动态扩展。
+ 版本升级：帮助进行滚动升级，并最大限度地减少停机时间。
+ 安全性：实施加密、访问控制和身份验证机制。

## 先决条件和限制
<a name="streamline-postgresql-deployments-amazon-eks-pgo-prereqs"></a>

**先决条件**
+ 活跃 AWS 账户的.
+ [AWS 命令行界面（AWS CLI）版本 2](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html)，已在 Linux、macOS 或 Windows 上安装并配置。
+ [AWS CLI CLI Config](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-quickstart.html)， AWS 用于从命令行连接资源。
+ [eksctl](https://github.com/eksctl-io/eksctl#installation)，已在 Linux、macOS 或 Windows 上安装并配置。
+ `kubectl`，已安装并配置为访问 Amazon EKS 集群上的资源。有关详情，请参阅 Amazon EKS 文档中的[设置 kubectl 和 eksctl](https://docs.aws.amazon.com/eks/latest/userguide/install-kubectl.html)。 
+ 您的计算机终端被配置为访问 Amazon EKS 集群。有关详情，请参阅 Amazon EKS 文档中的[将计算机配置为与您的集群通信](https://docs.aws.amazon.com/eks/latest/userguide/getting-started-console.html#eks-configure-kubectl)。

**产品版本**
+ Kubernetes 版本 1.21 到 1.24 或更高版本（请参阅 [PGO 文档](https://access.crunchydata.com/documentation/postgres-operator/5.2.5/)）。
+ PostgreSQL 版本 10 或更高版本。此模式使用 PostgreSQL 版本 16。

**限制**
+ 有些 AWS 服务 并非全部可用 AWS 区域。有关区域可用性，请参阅[按区域划分的AWS 服务](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/)。有关特定端点，请参阅[服务端点和配额](https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html)页面，然后选择相应服务的链接。

## 架构
<a name="streamline-postgresql-deployments-amazon-eks-pgo-architecture"></a>

**目标技术堆栈**
+ Amazon EKS
+ Amazon Virtual Private Cloud (Amazon VPC)
+ Amazon Elastic Compute Cloud（Amazon EC2）

**目标架构**

![使用具有三个可用区和两个副本的 PGO 的架构 PgBouncer，以及 PGO 操作员。](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/4c164012-7527-4ebe-b6a7-c129600328d6/images/26a5572b-405b-4634-b96a-91254c3ea2c1.png)


此模式将构建一个架构，其中包含一个具有三个节点的 Amazon EKS 集群。每个节点都在后端的一组 EC2 实例上运行。此 PostgreSQL 设置遵循主副本架构，这对于需要执行大量读取操作的使用案例特别有效。此架构包括以下组件：
+ **主数据库容器（pg-primary）**托管着主 PostgreSQL 实例，所有写入操作都将定向到此实例。
+ **辅助副本容器（pg-replica）**托管着 PostgreSQL 实例，这些实例将从主数据库复制数据并处理读取操作。
+ **PgBouncer**是 PGO 中包含的 PostgreSQL 数据库的轻量级连接池器。它位于客户端与 PostgreSQL 服务器之间，并充当各个数据库连接的媒介。
+ 在此 Kubernetes 环境中，**PGO** 可以自动部署和管理 PostgreSQL 集群。
+ **Patroni** 是一款开源工具，负责管理和自动执行 PostgreSQL 的高可用性配置。它包含在 PGO 中。当在 Kubernetes 中将 Patroni 与 PGO 结合使用时，它将发挥至关重要的作用，确保 PostgreSQL 集群的弹性和容错能力。有关详情，请参阅 [Patroni 文档](https://patroni.readthedocs.io/en/latest/)。

此工作流包含以下步骤：
+ **部署 PGO Operator**。您需要将 PGO Operator 部署到 Amazon EKS 上运行的 Kubernetes 集群上。可以使用 Kubernetes 清单或 Helm 图表来完成此操作。此模式使用 Kubernetes 清单。
+ **定义 PostgreSQL 实例**。当操作符运行时，您可以创建自定义资源 (CRs) 来指定 PostgreSQL 实例的所需状态。这包括存储、复制和高可用性设置等配置。
+ **Operator 管理**。您可以通过 Kubernetes API 对象与操作员进行交互，例如 CRs 创建、更新或删除 PostgreSQL 实例。
+ **监控和维护**。您可以监控 Amazon EKS 上运行的 PostgreSQL 实例的运行状况和性能。Operator 通常会提供用于进行监控的指标和日志记录功能。您可以根据需要执行例行维护任务，例如升级和修补。有关详情，请参阅 Amazon EKS 文档中的[监控集群性能和查看日志](https://docs.aws.amazon.com/eks/latest/userguide/eks-observe.html)。
+ **扩展和备份**：您可以使用 Operator 提供的功能来扩展 PostgreSQL 实例以及管理备份。

此模式不包含监控、维护和备份操作。

**自动化和扩展**
+ 您可以使用自动 CloudFormation 创建基础架构。有关详情，请参阅 Amazon EKS 文档中的[使用 CloudFormation创建 Amazon EKS 资源](https://docs.aws.amazon.com/eks/latest/userguide/creating-resources-with-cloudformation.html)。
+ 您可以使用 GitVersion 或 Jenkins 内部版本号来自动部署数据库实例。

## 工具
<a name="streamline-postgresql-deployments-amazon-eks-pgo-tools"></a>

**AWS 服务**
+ [亚马逊 Elastic Kubernetes Service（亚马逊](https://docs.aws.amazon.com/eks/latest/userguide/getting-started.html) EKS）可帮助你在上面运行 AWS Kubernetes，而无需安装或维护自己的 Kubernetes 控制平面或节点。 
+ [AWS Command Line Interface (AWS CLI)](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) 是一个开源工具，可帮助您 AWS 服务 通过命令行外壳中的命令进行交互。

**其他工具**
+ [eksctl](https://eksctl.io/) 是一款简单的命令行工具，用于在 Amazon EKS 上创建集群。
+ [kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/) 是一个命令行实用程序，用于对 Kubernetes 集群运行命令。
+ [PGO](https://github.com/CrunchyData/postgres-operator) 会自动管理和扩展 Kubernetes 中的 PostgreSQL 数据库。

## 最佳实践
<a name="streamline-postgresql-deployments-amazon-eks-pgo-best-practices"></a>

请遵循以下最佳实践，以便确保顺利、高效地进行部署：
+ **保护您的 EKS 集群**。为您的 EKS 集群实施安全最佳实践，例如为服务账户 AWS Identity and Access Management (IRSA)、网络策略和 VPC 安全组使用 (IRSA) 角色。限制对 EKS 集群 API 服务器的访问，并使用 TLS 对节点与 API 服务器之间的通信进行加密。
+ 确保 Amazon EKS 上运行的 PGO 版本与 Kubernetes **版本兼容**。有些 PGO 功能可能需要特定的 Kubernetes 版本，或者引入了兼容性限制。有关详情，请参阅 PGO 文档中的[组件和兼容性](https://access.crunchydata.com/documentation/postgres-operator/5.2.5/references/components/)。
+ 为 PGO 部署**规划资源分配**，包括 CPU、内存和存储。考虑 PGO 以及它管理的 PostgreSQL 实例的资源需求。监控资源使用情况，并根据需要来扩展资源。
+ **专为高可用性而设计**。设计您的 PGO 部署以便实现高可用性，从而最大限度地减少停机时间并确保可靠性。跨多个可用区部署 PGO 的多个副本，以便获得容错能力。
+ 为 PGO 管理的 PostgreSQL 数据库**实施备份和还原程序**。使用 PGO 提供的功能，或者使用与 Kubernetes 和 Amazon EKS 兼容的第三方备份解决方案。
+ 为 PGO 部署**设置监控和日志记录**，以便跟踪性能、运行状况和事件。使用 Prometheus 等工具来监控指标，并使用 Grafana 进行可视化。配置日志记录以便捕获 PGO 日志，从而进行问题排查和审核。
+ 正确**配置网络**，以便能够在 PGO、PostgreSQL 实例以及 Kubernetes 集群中的其他服务之间进行通信。使用 Amazon VPC 联网功能和 Kubernetes 联网插件（例如 Calico 或 [Amazon VPC CNI](https://github.com/aws/amazon-vpc-cni-k8s)）来实施网络策略和隔离流量。
+ 考虑性能、持久性和可扩展性等因素，为 PostgreSQL 数据库**选择合适的存储选项**。使用亚马逊 Elastic Block Store (Amazon EBS) 卷 AWS 或托管存储服务进行永久存储。有关详情，请参阅 Amazon EKS 文档中的[使用 Amazon EBS 存储 Kubernetes 卷](https://docs.aws.amazon.com/eks/latest/userguide/ebs-csi.html)。
+ **使用基础设施即代码 (IaC) 工具**，例如 CloudFormation 在 Amazon EKS 上自动部署和配置 PGO。将基础设施组件（包括 EKS 集群、网络和 PGO 资源）定义为代码，以便实现一致性、可重复性和版本控制。

## 操作说明
<a name="streamline-postgresql-deployments-amazon-eks-pgo-epics"></a>

### 创建一个 IAM 角色
<a name="create-an-iam-role"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建 IAM 角色。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/streamline-postgresql-deployments-amazon-eks-pgo.html) | AWS 管理员 | 

### 创建一个 Amazon EKS 集群。
<a name="create-an-eks-cluster"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建一个 Amazon EKS 集群。 | 如果您已部署一个集群，请跳过此步骤。否则，请使用 `eksctl` Terraform 或在当前 AWS 账户 集群中部署 Amazon EKS 集群。 CloudFormation此模式使用 `eksctl` 来部署集群。此模式将 Amazon EC2 用作 Amazon EKS 的一个节点组。如果要使用 AWS Fargate，请参阅 [eksctl](https://eksctl.io/usage/schema/#managedNodeGroups) 文档中的`managedNodeGroups`配置。[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/streamline-postgresql-deployments-amazon-eks-pgo.html) | AWS 管理员、Terraform 或 eksctl 管理员、Kubernetes 管理员 | 
| 验证此集群的状态。 | 运行如下命令，查看此集群中的节点的当前状态：<pre>kubectl get nodes</pre>如果遇到错误，请参阅 Amazon EKS 文档中的[问题排查部分](https://docs.aws.amazon.com/eks/latest/userguide/troubleshooting.html)。 | AWS 管理员、Terraform 或 eksctl 管理员、Kubernetes 管理员 | 

### 创建 OIDC 身份提供程序
<a name="create-an-oidc-identity-provider"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 启用 IAM OIDC 提供程序。 | Amazon EBS Container Storage Interface（CSI）驱动程序的一个先决条件是，您的集群必须具有一个现有的 IAM OpenID Connect（OIDC）提供程序。使用如下命令，启用 IAM OIDC 提供程序：<pre>eksctl utils associate-iam-oidc-provider --region={region} --cluster={YourClusterNameHere} --approve</pre>有关此步骤的详情，请参阅 [Amazon EKS 文档](https://docs.aws.amazon.com/eks/latest/userguide/ebs-csi.html)。 | AWS 管理员 | 
| 为 Amazon EBS CSI 驱动程序创建一个 IAM 角色。 | 使用如下 `eksctl` 命令，为 CSI 驱动程序创建 IAM 角色：<pre>eksctl create iamserviceaccount \<br />  --region {RegionName} \<br />  --name ebs-csi-controller-sa \<br />  --namespace kube-system \<br />  --cluster {YourClusterNameHere} \<br />  --attach-policy-arn arn:aws:iam::aws:policy/service-role/AmazonEBSCSIDriverPolicy \<br />  --approve \<br />  --role-only \<br />  --role-name AmazonEKS_EBS_CSI_DriverRole</pre>如果使用加密的 Amazon EBS 驱动器，您必须进一步配置此策略。有关说明，请参阅 [Amazon EBS SCI 驱动程序文档](https://github.com/kubernetes-sigs/aws-ebs-csi-driver/blob/master/docs/install.md#installation-1)。 | AWS 管理员 | 
| 添加 Amazon EBS CSI 驱动程序。 | 使用如下 `eksctl` 命令，添加 Amazon EBS CSI 驱动程序：<pre>eksctl create addon \<br />  --name aws-ebs-csi-driver \<br />  --cluster <YourClusterName> service-account-role-arn arn:aws:iam::$(aws sts get-caller-identity \<br />  --query Account \<br />  --output text):role/AmazonEKS_EBS_CSI_DriverRole \<br />  --force</pre> | AWS 管理员 | 

### 安装 PGO
<a name="install-pgo"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 克隆 PGO 存储库。 | 克隆 PGO 的 GitHub 存储库：<pre>git clone https://github.com/CrunchyData/postgres-operator-examples.git </pre> | AWS DevOps | 
| 提供用于创建服务账户的角色详细信息。 | 要授予 Amazon EKS 集群访问所需 AWS 资源的权限，请在`service_account.yaml`文件中指定您之前创建的 OIDC 角色的亚马逊资源名称 (ARN)。[GitHub](https://github.com/CrunchyData/postgres-operator/blob/main/config/rbac/cluster/service_account.yaml)<pre>cd postgres-operator-examples</pre><pre>---<br />metadata:<br />  annotations:<br />    eks.amazonaws.com/role-arn: arn:aws:iam::<accountId>:role/<role_name> # Update the OIDC role ARN created earlier</pre> | AWS 管理员、Kubernetes 管理员 | 
| 创建此命名空间和 PGO 先决条件。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/streamline-postgresql-deployments-amazon-eks-pgo.html) | Kubernetes 管理员 | 
| 确认已创建容器组（pod）。 | 确认已创建此命名空间和默认配置：<pre>kubectl get pods -n postgres-operator</pre> | AWS 管理员、Kubernetes 管理员 | 
| 验证 PVCs。 | 使用以下命令验证永久卷声明 (PVCs)：<pre>kubectl describe pvc -n postgres-operator</pre> | AWS 管理员、Kubernetes 管理员 | 

### 创建并部署 Operator
<a name="create-and-deploy-an-operator"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建一个 Operator。 | 修改位于 `/kustomize/postgres/postgres.yaml` 的文件的内容，以便与以下内容相匹配：<pre>spec:<br />  instances:<br />    - name: pg-1<br />      replicas: 3<br />  patroni:<br />    dynamicConfiguration:<br />      postgresql:<br />      pg_hba:<br />        - "host all all 0.0.0.0/0 trust" # this line enabled logical replication with programmatic access<br />        - "host all postgres 127.0.0.1/32 md5"<br />      synchronous_mode: true<br />  users:<br />  - name: replicator<br />    databases:<br />      - testdb<br />    options: "REPLICATION"</pre>这些更新将执行以下操作：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/streamline-postgresql-deployments-amazon-eks-pgo.html) | AWS 管理员、数据库管理员、Kubernetes 管理员 | 
| 部署此 Operator。 | 部署此 PGO Operator，以便能够简化 Kubernetes 环境中的 PostgreSQL 数据库的管理和操作：<pre>kubectl apply -k kustomize/postgres</pre> | AWS 管理员、数据库管理员、Kubernetes 管理员 | 
| 验证部署。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/streamline-postgresql-deployments-amazon-eks-pgo.html)从命令输出中，记下主副本（`primary_pod_name`）和只读副本（`read_pod_name`）。您将在后续步骤中使用这些副本。 | AWS 管理员、数据库管理员、Kubernetes 管理员 | 

### 验证流式复制
<a name="verify-streaming-replication"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 向主副本写入数据。 | 使用以下命令，连接到 PostgreSQL 主副本，并向数据库写入数据：<pre>kubectl exec -it <primary_pod_name> bash -n postgres-operator</pre><pre>psql</pre><pre>CREATE TABLE customers (firstname text, customer_id serial, date_created timestamp);<br />\dt</pre> | AWS 管理员、Kubernetes 管理员 | 
| 确认只读副本具有相同的数据。 | 连接到 PostgreSQL 只读副本，并检查流式复制功能是否正常运行：<pre>kubectl exec -it {read_pod_name} bash -n postgres-operator</pre><pre>psql</pre><pre>\dt</pre>只读副本应当具有您在上一步中在主副本中创建的表。 | AWS 管理员、Kubernetes 管理员 | 

## 问题排查
<a name="streamline-postgresql-deployments-amazon-eks-pgo-troubleshooting"></a>


| 问题 | 解决方案 | 
| --- | --- | 
| 容器组（pod）无法启动。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/streamline-postgresql-deployments-amazon-eks-pgo.html) | 
| 副本明显落后于主数据库。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/streamline-postgresql-deployments-amazon-eks-pgo.html) | 
| 您无法了解 PostgreSQL 集群的性能和运行状况。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/streamline-postgresql-deployments-amazon-eks-pgo.html) | 
| 复制功能无法正常工作。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/streamline-postgresql-deployments-amazon-eks-pgo.html) | 

## 相关资源
<a name="streamline-postgresql-deployments-amazon-eks-pgo-resources"></a>
+ [Amazon Elastic Kubernetes Service](https://docs.aws.amazon.com/whitepapers/latest/overview-deployment-options/amazon-elastic-kubernetes-service.html)（*AWS 上的部署选项概述*白皮书）
+  [CloudFormation](https://docs.aws.amazon.com/whitepapers/latest/overview-deployment-options/aws-cloudformation.html)（*AWS 上的部署选项概述*白皮书）
+ [开始使用 Amazon EKS – eksctl](https://docs.aws.amazon.com/eks/latest/userguide/getting-started-eksctl.html)（《*Amazon EKS 用户指南*》）
+ [设置 kubectl 和 eksctl](https://docs.aws.amazon.com/eks/latest/userguide/install-kubectl.html)（《*Amazon EKS 用户指南*》）
+ [创建用于 OpenID Connect 联合身份验证的角色](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-idp_oidc.html)（《*IAM 用户指南*》）
+ [配置 AWS CLI（*AWS CLI 用户指南*）的设置](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)
+ [Crunchy Postgres for Kubernetes 文档](https://access.crunchydata.com/documentation/postgres-operator/latest)
+ [Crunch & Learn：Crunchy Postgres for Kubernetes 5.0](https://www.youtube-nocookie.com/embed/IIf9WZO3K50)（视频）