Streamline PostgreSQL deployments on Amazon EKS by using PGO
Created by Shalaka Dengale (AWS)
Summary
This pattern integrates the Postgres Operator from Crunchy Data (PGO) with Amazon Elastic Kubernetes Service (Amazon EKS) to streamline PostgreSQL deployments in cloud-native environments. PGO provides automation and scalability for managing PostgreSQL databases in Kubernetes. When you combine PGO with Amazon EKS, it forms a robust platform for deploying, managing, and scaling PostgreSQL databases efficiently.
This integration provides the following key benefits:
Automated deployment: Simplifies PostgreSQL cluster deployment and management.
Custom resource definitions (CRDs): Uses Kubernetes primitives for PostgreSQL management.
High availability: Supports automatic failover and synchronous replication.
Automated backups and restores: Streamlines backup and restore processes.
Horizontal scaling: Enables dynamic scaling of PostgreSQL clusters.
Version upgrades: Facilitates rolling upgrades with minimal downtime.
Security: Enforces encryption, access controls, and authentication mechanisms.
Prerequisites and limitations
Prerequisites
An active AWS account.
AWS Command Line Interface (AWS CLI) version 2, installed and configured on Linux, macOS, or Windows.
AWS CLI Config, to connect AWS resources from the command line.
eksctl
, installed and configured on Linux, macOS, or Windows. kubectl
, installed and configured to access resources on your Amazon EKS cluster. For more information, see Set up kubectl and eksctl in the Amazon EKS documentation.Your computer terminal configured to access the Amazon EKS cluster. For more information, see Configure your computer to communicate with your cluster in the Amazon EKS documentation.
Product versions
Kubernetes versions 1.21–1.24 or later (see the PGO documentation
). PostgreSQL version 10 or later. This pattern uses PostgreSQL version 16.
Limitations
Some AWS services aren’t available in all AWS Regions. For Region availability, see AWS services by Region
. For specific endpoints, see the Service endpoints and quotas page, and choose the link for the service.
Architecture
Target technology stack
Amazon EKS
Amazon Virtual Private Cloud (Amazon VPC)
Amazon Elastic Compute Cloud (Amazon EC2)
Target architecture

This pattern builds an architecture that contains an Amazon EKS cluster with three nodes. Each node runs on a set of EC2 instances in the backend. This PostgreSQL setup follows a primary replica architecture, which is particularly effective for read-heavy use cases. The architecture includes the following components:
Primary database container (pg-primary) hosts the main PostgreSQL instance where all write operations are directed.
Secondary replica containers (pg-replica) host the PostgreSQL instances that replicate the data from the primary database and handle read operations.
PgBouncer is a lightweight connection pooler for PostgreSQL databases that's included with PGO. It sits between the client and the PostgreSQL server, and acts as an intermediary for database connections.
PGO automates the deployment and management of PostgreSQL clusters in this Kubernetes environment.
Patroni is an open-source tool that manages and automates high availability configurations for PostgreSQL. It's included with PGO. When you use Patroni with PGO in Kubernetes, it plays a crucial role in ensuring the resilience and fault tolerance of a PostgreSQL cluster. For more information, see the Patroni documentation
.
The workflow includes these steps:
Deploy the PGO operator. You deploy the PGO operator on your Kubernetes cluster that runs on Amazon EKS. This can be done by using Kubernetes manifests or Helm charts. This pattern uses Kubernetes manifests.
Define PostgreSQL instances. When the operator is running, you create custom resources (CRs) to specify the desired state of PostgreSQL instances. This includes configurations such as storage, replication, and high availability settings.
Operator management. You interact with the operator through Kubernetes API objects such as CRs to create, update, or delete PostgreSQL instances.
Monitoring and maintenance. You can monitor the health and performance of the PostgreSQL instances running on Amazon EKS. Operators often provide metrics and logging for monitoring purposes. You can perform routine maintenance tasks such as upgrades and patching as necessary. For more information, see Monitor your cluster performance and view logs in the Amazon EKS documentation.
Scaling and backup: You can use the features provided by the operator to scale PostgreSQL instances and manage backups.
This pattern doesn't cover monitoring, maintenance, and backup operations.
Automation and scale
You can use AWS CloudFormation to automate the infrastructure creation. For more information, see Create Amazon EKS resources with AWS CloudFormation in the Amazon EKS documentation.
You can use GitVersion or Jenkins build numbers to automate the deployment of database instances.
Tools
AWS services
Amazon Elastic Kubernetes Service (Amazon EKS) helps you run Kubernetes on AWS without needing to install or maintain your own Kubernetes control plane or nodes.
AWS Command Line Interface (AWS CLI) is an open-source tool that helps you interact with AWS services through commands in your command line shell.
Other tools
Best practices
Follow these best practices to ensure a smooth and efficient deployment:
Secure your EKS cluster. Implement security best practices for your EKS cluster, such as using AWS Identity and Access Management (IAM) roles for service accounts (IRSA), network policies, and VPC security groups. Limit access to the EKS cluster API server, and encrypt communications between nodes and the API server by using TLS.
Ensure version compatibility between PGO and Kubernetes running on Amazon EKS. Some PGO features might require specific Kubernetes versions or introduce compatibility limitations. For more information, see Components and Compatibility
in the PGO documentation. Plan resource allocation for your PGO deployment, including CPU, memory, and storage. Consider the resource requirements of both PGO and the PostgreSQL instances it manages. Monitor resource usage and scale resources as needed.
Design for high availability. Design your PGO deployment for high availability to minimize downtime and ensure reliability. Deploy multiple replicas of PGO across multiple Availability Zones for fault tolerance.
Implement backup and restore procedures for your PostgreSQL databases that PGO manages. Use features provided by PGO or third-party backup solutions that are compatible with Kubernetes and Amazon EKS.
Set up monitoring and logging for your PGO deployment to track performance, health, and events. Use tools such as Prometheus for monitoring metrics and Grafana for visualization. Configure logging to capture PGO logs for troubleshooting and auditing.
Configure networking properly to allow communications between PGO, PostgreSQL instances, and other services in your Kubernetes cluster. Use Amazon VPC networking features and Kubernetes networking plugins such as Calico or Amazon VPC CNI
for network policy enforcement and traffic isolation. Choose appropriate storage options for your PostgreSQL databases, considering factors such as performance, durability, and scalability. Use Amazon Elastic Block Store (Amazon EBS) volumes or AWS managed storage services for persistent storage. For more information, see Store Kubernetes volumes with Amazon EBS in the Amazon EKS documentation.
Use infrastructure as code (IaC) tools such as AWS CloudFormation to automate the deployment and configuration of PGO on Amazon EKS. Define infrastructure components—including the EKS cluster, networking, and PGO resources—as code for consistency, repeatability, and version control.
Epics
Task | Description | Skills required |
---|---|---|
Create an IAM role. |
| AWS administrator |
Task | Description | Skills required |
---|---|---|
Create an Amazon EKS cluster. | If you've already deployed a cluster, skip this step. Otherwise, deploy an Amazon EKS cluster in your current AWS account by using NoteThis pattern uses Amazon EC2 as a node group for Amazon EKS. If you want to use AWS Fargate, see the
| AWS administrator, Terraform or eksctl administrator, Kubernetes administrator |
Validate the status of the cluster. | Run the following command to see the current status of nodes in the cluster:
If you encounter errors, see the troubleshooting section of the Amazon EKS documentation. | AWS administrator, Terraform or eksctl administrator, Kubernetes administrator |
Task | Description | Skills required |
---|---|---|
Enable the IAM OIDC provider. | As a prerequisite for the Amazon EBS Container Storage Interface (CSI) driver, you must have an existing IAM OpenID Connect (OIDC) provider for your cluster. Enable the IAM OIDC provider by using the following command:
For more information about this step, see the Amazon EKS documentation. | AWS administrator |
Create an IAM role for the Amazon EBS CSI driver. | Use the following
If you use encrypted Amazon EBS drives, you have to configure the policy further. For instructions, see the Amazon EBS SCI driver documentation | AWS administrator |
Add the Amazon EBS CSI driver. | Use the following
| AWS administrator |
Task | Description | Skills required |
---|---|---|
Clone the PGO repository. | Clone the GitHub repository for PGO:
| AWS DevOps |
Provide the role details for service account creation. | To grant the Amazon EKS cluster access to the required AWS resources, specify the Amazon Resource Name (ARN) of the OIDC role that you created earlier in the
| AWS administrator, Kubernetes administrator |
Create the namespace and PGO prerequisites. |
| Kunernetes administrator |
Verify the creation of pods. | Verify that the namespace and default configuration were created:
| AWS administrator, Kubernetes administrator |
Verify PVCs. | Use the following command to verify persistent volume claims (PVCs):
| AWS administrator, Kubernetes administrator |
Task | Description | Skills required |
---|---|---|
Create an operator. | Revise the contents of the file located at
These updates do the following:
| AWS administrator, DBA, Kubernetes administrator |
Deploy the operator. | Deploy the PGO operator to enable the streamlined management and operation of PostgreSQL databases in Kubernetes environments:
| AWS administrator, DBA, Kubernetes administrator |
Verify the deployment. |
From the command output, note the primary replica ( | AWS administrator, DBA, Kubernetes administrator |
Task | Description | Skills required |
---|---|---|
Write data to the primary replica. | Use the following commands to connect to the PostgreSQL primary replica and write data to the database:
| AWS administrator, Kubernetes administrator |
Confirm that the read replica has the same data. | Connect to the PostgreSQL read replica and check whether the streaming replication is working correctly:
The read replica should have the table that you created in the primary replica in the previous step. | AWS administrator, Kubernetes administrator |
Troubleshooting
Issue | Solution |
---|---|
The pod doesn’t start. |
|
Replicas are significantly behind the primary database. |
|
You don’t have visibility into the performance and health of the PostgreSQL cluster. |
|
Replication doesn’t work. |
|
Related resources
Amazon Elastic Kubernetes Service (Overview of Deployment Options on AWS whitepaper)
AWS CloudFormation (Overview of Deployment Options on AWS whitepaper)
Get started with Amazon EKS – eksctl (Amazon EKS User Guide)
Set up kubectl and eksctl (Amazon EKS User Guide)
Create a role for OpenID Connect federation (IAM User Guide)
Configuring settings for the AWS CLI (AWS CLI User Guide)