Skip to content

Instantly share code, notes, and snippets.

@earayu
Created March 17, 2025 04:09
Show Gist options
  • Save earayu/87680d9517ffb3ab03d4d43c62af25f7 to your computer and use it in GitHub Desktop.
Save earayu/87680d9517ffb3ab03d4d43c62af25f7 to your computer and use it in GitHub Desktop.
Install PostgreSQL on Kubernetes with KubeBlocks
# Install PostgreSQL on Kubernetes with KubeBlocks
## Introduction
Managing databases on Kubernetes has always been a challenge—until **KubeBlocks** arrived!
KubeBlocks provides a **unified API**, allowing you to manage **MySQL, PostgreSQL, MongoDB, Redis, Pulsar, ClickHouse**, and more in a consistent way without learning multiple database operation tools.
In this tutorial, we will focus on **PostgreSQL** and demonstrate how to install and connect to a PostgreSQL database on Kubernetes using KubeBlocks.
---
## Why Choose KubeBlocks?
Kubernetes excels at managing **stateless applications**, but handling **stateful applications like databases** often requires:
✅ Finding the right database operator
✅ Learning different APIs and configuration methods
✅ Managing **backups, scaling, monitoring**, and other operational tasks
**KubeBlocks simplifies all of this!**
🔹 **Unified API** — Manage different databases with the same commands
🔹 **High Availability** — Production-grade failover and recovery mechanisms
🔹 **Built-in Monitoring** — Pre-configured **Prometheus & Grafana** dashboards
---
## **Installing KubeBlocks**
### **Step 1: Install KubeBlocks CLI**
First, install the **KubeBlocks CLI**, which is used to manage database clusters:
```bash
export KB_CLI_VERSION=v0.9.2
curl -fsSL https://kubeblocks.io/installer/install_cli.sh | bash -s $KB_CLI_VERSION
```
Verify the installation:
```bash
kbcli --help
```
You should see a list of available KubeBlocks CLI commands.
---
### **Step 2: Deploy KubeBlocks on Kubernetes**
### **2.1 Install KubeBlocks CRDs (Custom Resource Definitions)**
```bash
export KB_VERSION=v0.9.2
kubectl create -f https://github.com/apecloud/kubeblocks/releases/download/$KB_VERSION/kubeblocks_crds.yaml
```
### **2.2 Add KubeBlocks Helm Repository**
```bash
helm repo add kubeblocks https://apecloud.github.io/helm-charts
helm repo update
```
### **2.3 Install KubeBlocks Using Helm**
```bash
helm -n kb-system install kubeblocks kubeblocks/kubeblocks --version 0.9.2 \
--set image.registry=docker.io \
--set dataProtection.image.registry=docker.io \
--set addonChartsImage.registry=docker.io \
--create-namespace
```
Once installed, check if KubeBlocks is running:
```bash
kubectl get pods -n kb-system
```
You should see **KubeBlocks-related pods in Running state**.
---
## **Installing PostgreSQL**
### **Step 3: Deploy a PostgreSQL Cluster with KubeBlocks**
Now, let's use KubeBlocks to **quickly launch a PostgreSQL cluster**!
Run the following command to create a PostgreSQL cluster:
```bash
kbcli cluster create my-postgresql --cluster-definition postgresql
```
After a few minutes, check the PostgreSQL cluster status:
```bash
kubectl get pods -n default
```
You should see an output similar to:
```bash
NAME READY STATUS RESTARTS AGE
my-postgresql-postgresql-0 4/4 Running 0 79s
my-postgresql-postgresql-1 4/4 Running 0 79s
```
This indicates that the PostgreSQL cluster is successfully running! 🎉
---
## **Connecting to PostgreSQL and Running SQL Commands**
### **Step 4: Connect to PostgreSQL**
```bash
kbcli cluster connect my-postgresql
```
After running the above command, you will enter the **PostgreSQL interactive shell**, where you can execute SQL statements:
```sql
CREATE DATABASE testdb;
\c testdb
CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR(100), age INT);
INSERT INTO users (name, age) VALUES ('Alice', 30), ('Bob', 25);
SELECT * FROM users;
```
You should see the PostgreSQL query results, indicating successful database operations! 🚀
---
## **Summary**
✅ **Installed KubeBlocks** and deployed PostgreSQL on Kubernetes
✅ **Used KubeBlocks’ unified API** to quickly create a PostgreSQL cluster
✅ **Connected to PostgreSQL and executed SQL commands**
---
## **Why KubeBlocks is the Future of Database Management?**
🚀 **Supports multiple databases**: MySQL, PostgreSQL, MongoDB, Redis, Pulsar, ClickHouse, and more
💡 **Unified API for seamless management**, eliminating the need to learn multiple database operation tools
⚡ **Production-grade high availability, monitoring, and scalability**
📢 **What’s Next?**
🔹 **Try other databases**: Experiment with **MySQL, MongoDB, or Redis**
🔹 **Explore advanced features**: Such as **auto-scaling, backup, and restore**
🔹 **Join the community**: Star [KubeBlocks on GitHub](https://github.com/apecloud/kubeblocks) and participate in discussions!
💬 **Got questions?** Comment below or join our **Slack community**!
Happy Coding! 🚀
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment