By running a PostgreSQL database on Kubernetes, you can create uniformly managed, cloud-native production deployments with the flexibility to deploy a personalized database-as-a-service tailored to your specific needs.
Using an Operator allows you to provide additional context to Kubernetes to manage a stateful application. An Operator is also helpful when using an open source database like PostgreSQL to help with actions including provisioning, scaling, high availability, and user management.
Let's explore how to get PostgreSQL up and running on Kubernetes.
Set up the PostgreSQL operator
The first step to using PostgreSQL with Kubernetes is installing an Operator. You can get up and running with the open source Crunchy PostgreSQL Operator on any Kubernetes-based environment with the help of Crunchy's quickstart script for Linux.
The quickstart script has a few prerequisites:
- The Wget utility installed
- kubectl installed
- A StorageClass defined on your Kubernetes cluster
- Access to a Kubernetes user account with cluster-admin privileges. This is required to install the Operator RBAC rules
- A namespace to hold the PostgreSQL Operator
Executing the script will give you a default PostgreSQL Operator deployment that assumes dynamic storage and a StorageClass named standard. User-provided values are allowed by the script to override these defaults.
You can download the quickstart script and set it to be executable with the following commands:
chmod +x ./quickstart.sh
Then you can execute the quickstart script:
After the script prompts you for some basic information about your Kubernetes cluster, it performs the following operations:
- Downloads the Operator configuration files
- Sets the $HOME/.pgouser file to default settings
- Deploys the Operator as a Kubernetes Deployment
- Sets your .bashrc to include the Operator environmental variables
- Sets your $HOME/.bash_completion file to be the pgo bash_completion file
During the quickstart's execution, you'll be prompted to set up the RBAC rules for your Kubernetes cluster. In a separate terminal, execute the command the quickstart command tells you to use.
Once the script completes, you'll get information on setting up a port forward to the PostgreSQL Operator pod. In a separate terminal, execute the port forward; this will allow you to begin executing commands to the PostgreSQL Operator! Try creating a cluster by entering:
pgo create cluster mynewcluster
You can test that your cluster is up and running with by entering:
pgo test mynewcluster
You can now manage your PostgreSQL databases in your Kubernetes environment! You can find a full reference to commands, including those for scaling, high availability, backups, and more, in the documentation.
Parts of this article are based on Get Started Running PostgreSQL on Kubernetes that the author wrote for the Crunchy blog.