vanilla-postgresql

by apecloud

Helm chartv1.2.0-alpha.0App 14.7.0Verified publisher

Vanilla-PostgreSQL is compatible with the native PostgreSQL kernel, enabling it to quickly provide HA solutions for various variants based on the native PostgreSQL kernel.

About this chart

vanilla-postgresql

Vanilla-PostgreSQL is compatible with the native PostgreSQL kernel, enabling it to quickly provide HA solutions for various variants based on the native PostgreSQL kernel.

Features In KubeBlocks

Lifecycle Management

TopologyHorizontal scalingVertical scalingExpand volumeRestartStop/StartConfigureExposeSwitchover
vanilla-postgresqlYesYesYesYesYesYesYesYes

Backup and Restore

FeatureMethodDescription
Full Backupvanilla-pg-basebackupuses pg_basebackup, a PostgreSQL utility to create a base backup

Versions

Major VersionsDescription
1212.15.0
1414.7.0
1515.7.0, 15.6.1-138

Prerequisites

  • Kubernetes cluster >= v1.21

  • kubectl installed, refer to K8s Install Tools

  • Helm, refer to Installing Helm

  • KubeBlocks installed and running, refer to Install Kubeblocks

  • Vanilla PostgreSQL Addon Enabled, refer to Install Addons

  • Create K8s Namespace demo, to keep resources created in this tutorial isolated:

    kubectl create ns demo
    

Examples

Create

Create a Vanilla-PostgreSQL cluster with one primary and one secondary instance:

# cat examples/vanilla-postgresql/cluster.yaml
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
  name: vanpg-cluster
  namespace: demo
spec:
  # Specifies the behavior when a Cluster is deleted.
  # Valid options are: [DoNotTerminate, Delete, WipeOut] (`Halt` is deprecated since KB 0.9)
  # - `DoNotTerminate`: Prevents deletion of the Cluster. This policy ensures that all resources remain intact.
  # - `Delete`: Extends the `Halt` policy by also removing PVCs, leading to a thorough cleanup while removing all persistent data.
  # - `WipeOut`: An aggressive policy that deletes all Cluster resources, including volume snapshots and backups in external storage. This results in complete data removal and should be used cautiously, primarily in non-production environments to avoid irreversible data loss.
  terminationPolicy: Delete
  # Specifies the name of the ClusterDefinition to use when creating a Cluster.
  # Note: DO NOT UPDATE THIS FIELD
  # The value must be `postgresql` to create a PostgreSQL Cluster
  clusterDef: vanilla-postgresql
  # Specifies the name of the ClusterTopology to be used when creating the
  # Cluster.
  # Valid options are: [replication]
  topology: vanilla-postgresql
  # Specifies a list of ClusterComponentSpec objects used to define the
  # individual Components that make up a Cluster.
  # This field allows for detailed configuration of each Component within the Cluster
  componentSpecs:
    - name: postgresql
      # ServiceVersion specifies the version of the Service expected to be
      # provisioned by this Component.
      # Valid options are: [12.15.0,14.7.0,15.6.1-138,15.7.0]
      serviceVersion: "14.7.0"
      # Determines whether metrics exporter information is annotated on the
      # Component's headless Service.
      # Valid options are [true, false]
      disableExporter: false
      # Specifies Labels to override or add for underlying Pods, PVCs, Account & TLS
      # Secrets, Services Owned by Component.
      # Update `replicas` to your need.
      replicas: 2
      # Specifies the resources required by the Component.
      resources:
        limits:
          cpu: "0.5"
          memory: "0.5Gi"
        requests:
          cpu: "0.5"
          memory: "0.5Gi"
      # Specifies a list of PersistentVolumeClaim templates that define the storage
      # requirements for the Component.
      volumeClaimTemplates:
        # Refers to the name of a volumeMount defined in
        # `componentDefinition.spec.runtime.containers[*].volumeMounts
        - name: data
          spec:
            # The name of the StorageClass required by the claim.
            # If not specified, the StorageClass annotated with
            # `storageclass.kubernetes.io/is-default-class=true` will be used by default
            storageClassName: ""
            accessModes:
              - ReadWriteOnce
            resources:
              requests:
                # Set the storage size as needed
                storage: 20Gi
kubectl apply -f examples/vanilla-postgresql/cluster.yaml

And you will see the Vanilla-PostgreSQL cluster status goes Running after a while:

kubectl get -n demo cluster vanpg-cluster

and two pods are Running with roles primary and secondary separately. To check the roles of the pods, you can use following command:

# replace `vanpg-cluster` with your cluster name
kubectl get pod -n demo -l  app.kubernetes.io/instance=vanpg-cluster -L kubeblocks.io/role

If you want to create a Vanilla-PostgreSQL cluster of specified version, set the spec.componentSpecs.serviceVersion field in the yaml file before applying it:

# snippet of cluster.yaml
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
spec:
  terminationPolicy: Delete
  clusterDef: vanilla-postgresql
  topology: vanilla-postgresql
  componentSpecs:
    - name: postgresql
      # ServiceVersion specifies the version of the Service expected to be
      # provisioned by this Component.
      # Valid options are: [12.15.0,14.7.0,15.7.0,15.6.1-138]
      serviceVersion: "14.7.0"

The list of supported versions can be found by following command:

kubectl get cmpv vanilla-postgresql

And the expected output is like:

NAME                 VERSIONS                                      STATUS      AGE
vanilla-postgresql   12.15.0,14.7.0,15.7.0,15.6.1-138               Available   Xd

Horizontal scaling

Scale-out

Horizontal scaling out Vanilla-PostgreSQL cluster by adding ONE more replica:

# cat examples/vanilla-postgresql/scale-out.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
  name: vanpg-scale-out
  namespace: demo
spec:
  # Specifies the name of the Cluster resource that this operation is targeting.
  clusterName: vanpg-cluster
  type: HorizontalScaling
  # Lists HorizontalScaling objects, each specifying scaling requirements for a Component, including desired total replica counts, configurations for new instances, modifications for existing instances, and instance downscaling options
  horizontalScaling:
    # Specifies the name of the Component.
  - componentName: postgresql
    # Specifies the replica changes for scaling out components
    scaleOut:
      # Specifies the replica changes for the component.
      # add one more replica to current component
      replicaChanges: 1

kubectl apply -f examples/vanilla-postgresql/scale-out.yaml

After applying the operation, you will see a new pod created and the Vanilla-PostgreSQL cluster status goes from Updating to Running, and the newly created pod has a new role secondary.

And you can check the progress of the scaling operation with following command:

kubectl describe -n demo ops vanpg-scale-out

Scale-in

Horizontal scaling in Vanilla-PostgreSQL cluster by deleting ONE replica:

# cat examples/vanilla-postgresql/scale-in.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
  name: vanpg-scale-in
  namespace: demo
spec:
  # Specifies the name of the Cluster resource that this operation is targeting.
  clusterName: vanpg-cluster
  type: HorizontalScaling
  # Lists HorizontalScaling objects, each specifying scaling requirements for a Component, including desired total replica counts, configurations for new instances, modifications for existing instances, and instance downscaling options
  horizontalScaling:
    # Specifies the name of the Component.
  - componentName: postgresql
    # Specifies the replica changes for scaling in components
    scaleIn:
      # Specifies the replica changes for the component.
      # add one more replica to current component
      replicaChanges: 1

kubectl apply -f examples/vanilla-postgresql/scale-in.yaml

Scale-in/out using Cluster API

Alternatively, you can update the replicas field in the spec.componentSpecs.replicas section to your desired non-zero number.

# snippet of cluster.yaml
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
spec:
  componentSpecs:
    - name: postgresql
      serviceVersion: "14.7.0"
      replicas: 2 # Update `replicas` to 1 for scaling in, and to 3 for scaling out

Vertical scaling

Vertical scaling up or down specified components requests and limits cpu or memory resource in the cluster

# cat examples/vanilla-postgresql/verticalscale.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
  name: vanpg-verticalscaling
  namespace: demo
spec:
  # Specifies the name of the Cluster resource that this operation is targeting.
  clusterName: vanpg-cluster
  type: VerticalScaling
  # Lists VerticalScaling objects, each specifying a component and its desired compute resources for vertical scaling.
  verticalScaling:
    - componentName: postgresql
      # VerticalScaling refers to the process of adjusting the compute resources (e.g., CPU, memory) allocated to a Component. It defines the parameters required for the operation.
      requests:
        cpu: '1'
        memory: 1Gi
      limits:
        cpu: '1'
        memory: 1Gi

kubectl apply -f examples/vanilla-postgresql/verticalscale.yaml

Expand volume

Increase size of volume storage with the specified components in the cluster

# cat examples/vanilla-postgresql/volumeexpand.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
  name: vanpg-volumeexpansion
  namespace: demo
spec:
  # Specifies the name of the Cluster resource that this operation is targeting.
  clusterName: vanpg-cluster
  type: VolumeExpansion
  # Lists VolumeExpansion objects, each specifying a component and its corresponding volumeClaimTemplates that requires storage expansion.
  volumeExpansion:
    # Specifies the name of the Component.
    - componentName: postgresql
      # volumeClaimTemplates specifies the storage size and volumeClaimTemplate name.
      volumeClaimTemplates:
        - name: data
          storage: 30Gi

kubectl apply -f examples/vanilla-postgresql/volumeexpand.yaml

Restart

Restart the specified components in the cluster

# cat examples/vanilla-postgresql/restart.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
  name: vanpg-restart
  namespace: demo
spec:
  # Specifies the name of the Cluster resource that this operation is targeting.
  clusterName: vanpg-cluster
  type: Restart
  # Lists Components to be restarted. ComponentOps specifies the Component to be operated on.
  restart:
    # Specifies the name of the Component.
    - componentName: postgresql
kubectl apply -f examples/vanilla-postgresql/restart.yaml

Stop

Stop the cluster and release all the pods of the cluster, but the storage will be reserved

# cat examples/vanilla-postgresql/stop.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
  name: vanpg-stop
  namespace: demo
spec:
  # Specifies the name of the Cluster resource that this operation is targeting.
  clusterName: vanpg-cluster
  type: Stop

kubectl apply -f examples/vanilla-postgresql/stop.yaml

Start

Start the stopped cluster

# cat examples/vanilla-postgresql/start.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
  name: vanpg-start
  namespace: demo
spec:
  # Specifies the name of the Cluster resource that this operation is targeting.
  clusterName: vanpg-cluster
  type: Start

kubectl apply -f examples/vanilla-postgresql/start.yaml

Switchover

A switchover in database clusters is a planned operation that transfers the primary (leader) role from one database instance to another. The goal of a switchover is to ensure that the database cluster remains available and operational during the transition.

Switchover without preferred candidates

To perform a switchover without any preferred candidates, you can apply the following yaml file:

# cat examples/vanilla-postgresql/switchover.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
  name: vanpg-switchover
  namespace: demo
spec:
  # Specifies the name of the Cluster resource that this operation is targeting.
  clusterName: vanpg-cluster
  type: Switchover
  # Lists Switchover objects, each specifying a Component to perform the switchover operation.
  switchover:
    # Specifies the name of the Component.
  - componentName: postgresql
    # Specifies the instance whose role will be transferred.
    # A typical usage is to transfer the leader role in a consensus system.
    instanceName: vanpg-cluster-postgresql-0

kubectl apply -f examples/vanilla-postgresql/switchover.yaml

By applying this yaml file, KubeBlocks will perform a switchover operation defined in Vanilla-PostgreSQL's component definition, and you can check out the details in componentdefinition.spec.lifecycleActions.switchover.

You may get the switchover operation details with following command:

kubectl get cluster -n demo vanpg-cluster -ojson | jq '.spec.componentSpecs[0].componentDef' | xargs kubectl get cmpd -ojson | jq '.spec.lifecycleActions.switchover'

Switchover with candidate specified

Switchover a specified instance as the new primary or le

Deploy vanilla-postgresql with OpenTenant

Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has vanilla-postgresql on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.

  1. Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
  2. Place the chartvanilla-postgresql is pinned at v1.2.0-alpha.0 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
  3. ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below
opentenant validate opentenant.yaml
terraform init && terraform apply