polardbx

by apecloud

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

PolarDB-X is a cloud native distributed SQL Database designed for high concurrency, massive storage, complex querying scenarios.

About this chart

PolarDB-X

PolarDB-X is a cloud native distributed SQL Database designed for high concurrency, massive storage, complex querying scenarios.

Features In KubeBlocks

Lifecycle Management

HorizontalscalingVertical scalingExpandvolumeRestartStop/StartConfigureExposeSwitchover
Yes (cn, cdc)YesYesYesYesNoYesNo

Versions

Versions
2.3.0

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

  • PolarDB-X Addon Enabled, refer to Install Addons

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

    kubectl create ns demo
    

Examples

Create

[!IMPORTANT] Make sure the clusterName and namespace are short such that the length of following string is less than 64 characters

  • <clusterName>-gms-<id>.<clusterName>-gms-headless.<namesapce>.svc.cluster.local:11306

This is a constraint of the PolarDB-X database. The column IP_PORT defined in table information_schema.ALISQL_CLUSTER_GLOBAL is of type VARCHAR(64). You may login to the database and run the following SQL to check the definition of the table:

show create table information_schema.ALISQL_CLUSTER_GLOBAL;

Create a polardbx cluster with four components: cn, dn, cdc, and gms.

# cat examples/polardbx/cluster.yaml
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
  name: pxc
  namespace: demo
spec:
  terminationPolicy: Delete
  componentSpecs:
    - name: gms
      componentDef: polardbx-gms
      replicas: 1
      resources:
        limits:
          cpu: "1"
          memory: "1Gi"
        requests:
          cpu: "1"
          memory: "1Gi"
      volumeClaimTemplates:
        - name: data
          spec:
            storageClassName: ""
            accessModes:
              - ReadWriteOnce
            resources:
              requests:
                storage: 20Gi
    - name: dn-0
      componentDef: polardbx-dn
      replicas: 1
      resources:
        limits:
          cpu: "1"
          memory: "1Gi"
        requests:
          cpu: "1"
          memory: "1Gi"
      volumeClaimTemplates:
        - name: data
          spec:
            storageClassName: ""
            accessModes:
              - ReadWriteOnce
            resources:
              requests:
                storage: 20Gi
    - name: cn
      componentDef: polardbx-cn
      replicas: 2
      resources:
        limits:
          cpu: "1"
          memory: "1Gi"
        requests:
          cpu: "1"
          memory: "1Gi"
    - name: cdc
      componentDef: polardbx-cdc
      replicas: 1
      resources:
        limits:
          cpu: "1"
          memory: "1Gi"
kubectl apply -f examples/polardbx/cluster.yaml

As PolarDB-X is a distributed database with multiple components. You may prefer to distribute replicas to different nodes to avoid single point of failure. Here is an example of how to distribute replicas to different nodes using schedulingPolicy API:

kubectl apply -f examples/polardbx/cluster-with-schedule-policy.yaml

To connect to the database, you can use the following command to get the connection information:

kubectl port-forward svc/pxc-cn 3306:3306
mysql -h127.0.0.1 -u$USER_NAME -p$PASSWORD

Credentials can be found in the secret pxc-gms-account-polardbx-root in the namespace where the cluster is deployed.

kubectl get secret -n demo pxc-gms-account-polardbx-root -o jsonpath="{.data.password}" | base64 --decode
kubectl get secret -n demo pxc-gms-account-polardbx-root -o jsonpath="{.data.username}" | base64 --decode

Horizontal scaling

Scale-out

Horizontal scaling out polardbx cluster by adding ONE more cn replica:

# cat examples/polardbx/scale-out.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
  name: polardbx-scale-out-cn
  namespace: demo
spec:
  # Specifies the name of the Cluster resource that this operation is targeting.
  clusterName: pxc
  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.
    # - cn
    # - cdc
  - componentName: cn
    # 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
kubectl apply -f examples/polardbx/scale-out.yaml

Scale-in

Horizontal scaling in polardbx cluster by deleting ONE cn replica:

# cat examples/polardbx/scale-in.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
  name: polardbx-scale-in-cn
  namespace: demo
spec:
  # Specifies the name of the Cluster resource that this operation is targeting.
  clusterName: pxc
  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.
    # - cn
    # - cdc
  - componentName: cn
    # 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/polardbx/scale-in.yaml

Vertical scaling

Vertical scaling involves increasing or decreasing resources to an existing database cluster. Resources that can be scaled include:, CPU cores/processing power and Memory (RAM).

To vertical scaling up or down specified component, you can apply the following yaml file:

# cat examples/polardbx/verticalscale.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
  name: polardbx-verticalscaling
  namespace: demo
spec:
  # Specifies the name of the Cluster resource that this operation is targeting.
  clusterName: pxc
  type: VerticalScaling
  # Lists VerticalScaling objects, each specifying a component and its desired compute resources for vertical scaling.
  verticalScaling:
    # - cn
    # - cdc
  - componentName: cn
    # 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: '2Gi'
    limits:
      cpu: '1'
      memory: '2Gi'

kubectl apply -f examples/polardbx/verticalscale.yaml

Expand volume

Volume expansion is the ability to increase the size of a Persistent Volume Claim (PVC) after it's created. It is introduced in Kubernetes v1.11 and goes GA in Kubernetes v1.24. It allows Kubernetes users to simply edit their PersistentVolumeClaim objects without requiring any downtime at all if possible.

[!NOTE] Make sure the storage class you use supports volume expansion.

Check the storage class with following command:

kubectl get storageclass

If the ALLOWVOLUMEEXPANSION column is true, the storage class supports volume expansion.

To increase size of volume storage with the specified components in the cluster

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

kubectl apply -f examples/polardbx/volumeexpand.yaml

Restart

Restart the specified components in the cluster

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

kubectl apply -f examples/polardbx/restart.yaml

Stop

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

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

kubectl apply -f examples/polardbx/stop.yaml

Start

Start the stopped cluster

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

kubectl apply -f examples/polardbx/start.yaml

Observability

There are various ways to monitor the cluster. Here we use Prometheus and Grafana to demonstrate how to monitor the cluster.

Installing the Prometheus Operator

You may skip this step if you have already installed the Prometheus Operator. Or you can follow the steps in How to install the Prometheus Operator to install the Prometheus Operator.

Create PodMonitor

Apply the PodMonitor file to monitor the cluster:

# cat examples/polardbx/pod-monitor.yaml

apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
  name: pxc-pod-monitor
  namespace: demo
  labels:               # this is labels set in `prometheus.spec.podMonitorSelector`
    release: prometheus
spec:
  jobLabel: app.kubernetes.io/managed-by
  # defines the labels which are transferred from the
  # associated Kubernetes `Pod` object onto the ingested metrics
  # set the lables w.r.t you own needs
  podTargetLabels:
  - app.kubernetes.io/instance
  - app.kubernetes.io/managed-by
  - apps.kubeblocks.io/component-name
  - apps.kubeblocks.io/pod-name
  - kubeblocks.io/role
  podMetricsEndpoints:
    - path: /metrics
      port: metrics
      scheme: http
  namespaceSelector:
    matchNames:
      - demo
  selector:
    matchLabels:
      app.kubernetes.io/instance: pxc
kubectl apply -f examples/polardbx/pod-monitor.yaml

Access the Grafana Dashboard

Login to the Grafana dashboard and import the dashboard PolarDB-X Dashboard Overview to monitor the cluster.

Delete

If you want to delete the cluster and all its resource, you can modify the termination policy and then delete the cluster

kubectl patch cluster -n demo pxc -p '{"spec":{"terminationPolicy":"WipeOut"}}' --type="merge"

kubectl delete cluster -n demo pxc

Deploy polardbx with OpenTenant

Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has polardbx 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 chartpolardbx 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