Operations
Backup And Restores
Custom Secret
Monitoring
tpl
This guide explains how to perform horizontal scaling (scale-out and scale-in) on a PostgreSQL cluster managed by KubeBlocks. You'll learn how to use both OpsRequest and direct Cluster API updates to achieve this.
Before proceeding, ensure the following:
kubectl create ns demo
namespace/demo created
KubeBlocks uses a declarative approach for managing PostgreSQL clusters. Below is an example configuration for deploying a PostgreSQL cluster with 2 replicas (1 primary, 1 replicas).
Apply the following YAML configuration to deploy the cluster:
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
name: pg-cluster
namespace: demo
spec:
terminationPolicy: Delete
clusterDef: postgresql
topology: replication
componentSpecs:
- name: postgresql
serviceVersion: 16.4.0
labels:
apps.kubeblocks.postgres.patroni/scope: pg-cluster-postgresql
disableExporter: true
replicas: 2
resources:
limits:
cpu: "0.5"
memory: "0.5Gi"
requests:
cpu: "0.5"
memory: "0.5Gi"
volumeClaimTemplates:
- name: data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
Monitor the cluster status until it transitions to the Running state:
kubectl get cluster pg-cluster -n demo -w
Expected Output:
NAME CLUSTER-DEFINITION TERMINATION-POLICY STATUS AGE
pg-cluster postgresql Delete Creating 50s
pg-cluster postgresql Delete Running 4m2s
Once the cluster status becomes Running, your PostgreSQL cluster is ready for use.
If you are creating the cluster for the very first time, it may take some time to pull images before running.
Expected Workflow:
Pending
to Running
with secondary
roleUpdating
to Running
Option 1: Using Horizontal Scaling OpsRequest
Scale out the PostgreSQL cluster by adding 1 replica:
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: pg-cluster-scale-out-ops
namespace: demo
spec:
clusterName: pg-cluster
type: HorizontalScaling
horizontalScaling:
- componentName: postgresql
# Specifies the replica changes for scaling in components
scaleOut:
# Specifies the replica changes for the component.
# add one more replica to current component
replicaChanges: 1
Monitor the progress of the scaling operation:
kubectl get ops pg-cluster-scale-out-ops -n demo -w
Expected Result:
NAME TYPE CLUSTER STATUS PROGRESS AGE
pg-scale-out HorizontalScaling pg-cluster Running 0/1 8s
pg-scale-out HorizontalScaling pg-cluster Running 1/1 24s
pg-scale-out HorizontalScaling pg-cluster Succeed 1/1 24s
Option 2: Direct Cluster API Update
Alternatively, you can perform a direct update to the replicas
field in the Cluster resource:
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
name: pg-cluster
namespace: demo
spec:
terminationPolicy: Delete
clusterDef: postgresql
topology: replication
componentSpecs:
- name: postgresql
serviceVersion: 16.4.0
labels:
apps.kubeblocks.postgres.patroni/scope: pg-cluster-postgresql
disableExporter: true
replicas: 3 # increase replicas to scale-out
resources:
requests:
cpu: "1"
memory: "1Gi"
limits:
cpu: "1"
memory: "1Gi"
volumeClaimTemplates:
- name: data
spec:
storageClassName: ""
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
Or you can patch the cluster CR with command:
kubectl patch cluster pg-cluster -n demo --type=json -p='[{"op": "replace", "path": "/spec/componentSpecs/0/replicas", "value": 3}]'
After applying the operation, you will see a new pod created and the PostgreSQL cluster status goes from Updating
to Running
, and the newly created pod has a new role secondary
.
kubectl get pods -n demo -l app.kubernetes.io/instance=pg-cluster
Example Output (3 Pods):
NAME READY STATUS RESTARTS AGE
pg-cluster-postgresql-0 4/4 Running 0 13m
pg-cluster-postgresql-1 4/4 Running 0 12m
pg-cluster-postgresql-2 4/4 Running 0 5m5s
New replicas automatically join as secondary nodes.
kubectl get pods -n demo -l app.kubernetes.io/instance=pg-cluster -L kubeblocks.io/role
Example Output:
NAME READY STATUS RESTARTS AGE ROLE
pg-cluster-postgresql-0 4/4 Running 0 13m primary
pg-cluster-postgresql-1 4/4 Running 0 12m secondary
pg-cluster-postgresql-2 4/4 Running 0 5m54s secondary
Expected Workflow:
Updating
to Running
If the replica being scaled-in happens to be a primary replica, KubeBlocks will trigger a Switchover actions. And this pod will not be terminated until this Switchover action succeeds.
Option 1: Using Horizontal Scaling OpsRequest
Scale in the PostgreSQL cluster by removing ONE replica:
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: pg-cluster-scale-in-ops
namespace: demo
spec:
clusterName: pg-cluster
type: HorizontalScaling
horizontalScaling:
- componentName: postgresql
# Specifies the replica changes for scaling in components
scaleIn:
# Specifies the replica changes for the component.
# remove one replica from current component
replicaChanges: 1
Monitor progress:
kubectl get ops pg-cluster-scale-in-ops -n demo -w
Expected Result:
NAME TYPE CLUSTER STATUS PROGRESS AGE
pg-scale-in HorizontalScaling pg-cluster Running 0/1 8s
pg-scale-in HorizontalScaling pg-cluster Running 1/1 24s
pg-scale-in HorizontalScaling pg-cluster Succeed 1/1 24s
Option 2: Direct Cluster API Update
Alternatively, you can perform a direct update to the replicas
field in the Cluster resource:
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
name: pg-cluster
namespace: demo
spec:
terminationPolicy: Delete
clusterDef: postgresql
topology: replication
componentSpecs:
- name: postgresql
serviceVersion: 16.4.0
labels:
apps.kubeblocks.postgres.patroni/scope: pg-cluster-postgresql
disableExporter: true
replicas: 1 # decrease replicas to scale-in
resources:
requests:
cpu: "1"
memory: "1Gi"
limits:
cpu: "1"
memory: "1Gi"
volumeClaimTemplates:
- name: data
spec:
storageClassName: ""
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
Or you can patch the cluster CR with command:
kubectl patch cluster pg-cluster -n demo --type=json -p='[{"op": "replace", "path": "/spec/componentSpecs/0/replicas", "value": 1}]'
Example Output (ONE Pod):
kubectl get pods -n demo -l app.kubernetes.io/instance=pg-cluster
NAME READY STATUS RESTARTS AGE
pg-cluster-postgresql-0 4/4 Running 0 16m
When performing horizontal scaling:
To remove all created resources, delete the PostgreSQL cluster along with its namespace:
kubectl delete cluster pg-cluster -n demo
kubectl delete ns demo
In this guide you learned how to:
KubeBlocks ensures seamless scaling with minimal disruption to your database operations. with minimal disruption to your database operations.