Skip to content

Commit

Permalink
chore: update pdbx examples and cmpd (#1433)
Browse files Browse the repository at this point in the history
  • Loading branch information
shanshanying authored Jan 23, 2025
1 parent 1145cd8 commit 9e07deb
Show file tree
Hide file tree
Showing 20 changed files with 202 additions and 157 deletions.
52 changes: 11 additions & 41 deletions addons-cluster/polardbx/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -1,51 +1,21 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "polardbx.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
Define the cluster name.
We truncate at 15 chars because KubeBlocks will concatenate the names of other resources with cluster name
*/}}
{{- define "polardbx.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- define "polardbx-cluster.name" -}}
{{- $name := default .Release.Name .Values.clusterName }}
{{- if not (regexMatch "^[a-z]([-a-z0-9]*[a-z0-9])?$" $name) }}
{{ fail (printf "Release name %q is invalid. It must match the regex %q." $name "^[a-z]([-a-z0-9]*[a-z0-9])?$") }}
{{- end }}
{{- if gt (len $name) 16 }}
{{ fail (printf "Release name %q is invalid, must be no more than 6 characters" $name) }}
{{- end }}
{{- if gt (add (len $name) (len .Release.Namespace)) 18 }}
{{ fail (printf "Combined length of release name %q and namespace %q must be no more than 18 characters" $name .Release.Namespace) }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "polardbx.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "polardbx.labels" -}}
helm.sh/chart: {{ include "polardbx.chart" . }}
{{ include "polardbx.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "polardbx.selectorLabels" -}}
app.kubernetes.io/name: {{ include "polardbx.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- $name }}
{{- end }}
6 changes: 4 additions & 2 deletions addons-cluster/polardbx/templates/cluster.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
name: {{ include "polardbx.name" . }}
name: {{ include "polardbx-cluster.name" . }}
namespace: {{ .Release.Namespace }}
labels:
{{ include "polardbx.labels" . | nindent 4 }}
{{ include "kblib.clusterLabels" . | nindent 4 }}
spec:
terminationPolicy: {{ .Values.extra.terminationPolicy }}
componentSpecs:
Expand All @@ -28,6 +28,7 @@ spec:
volumeClaimTemplates:
- name: data
spec:
storageClassName: {{ .Values.gms.persistence.data.storageClass | quote }}
accessModes:
- ReadWriteOnce
resources:
Expand Down Expand Up @@ -56,6 +57,7 @@ spec:
volumeClaimTemplates:
- name: data
spec:
storageClassName: {{ .persistence.data.storageClass | quote }}
accessModes:
- ReadWriteOnce
resources:
Expand Down
2 changes: 1 addition & 1 deletion addons-cluster/polardbx/values.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## cluster settings for polardbx cluster
nameOverride: pxc
clusterName: pxc

gms:
## @param gms.replicas data replicas of gms instance
Expand Down
90 changes: 45 additions & 45 deletions addons/polardbx/dashboards/polardbx-overview.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion addons/polardbx/scripts/metadb-setup.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ until mysql -h$GMS_SVC_NAME -P$GMS_SVC_PORT -u$metaDbUser -p$metaDbNonEncPasswd
done

function generate_dn_init_sql() {
echo "$DN_HEADLESS_SVC_NAME" | tr ',' '\n' | while IFS= read -r item
echo $DN_POD_FQDN_LIST
echo "$DN_POD_FQDN_LIST" | tr ',' '\n' | while IFS= read -r item
do
DN_HOSTNAME=$item
DN_NAME=$(echo "$DN_HOSTNAME" | cut -d'.' -f2 | sed s/-headless//)
Expand Down
2 changes: 1 addition & 1 deletion addons/polardbx/templates/cmpd-cdc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ spec:
- name: metaDbNonEncPasswd
value: $(SERVICE_PASSWORD)
- name: dnPasswordKey
value: "$(metaDbNonEncPasswd)$(metaDbNonEncPasswd)"
value: "$(metaDbNonEncPasswd)"
- name: cpu_cores
valueFrom:
resourceFieldRef:
Expand Down
11 changes: 5 additions & 6 deletions addons/polardbx/templates/cmpd-cn.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,12 @@ spec:
port:
name: mysql
option: Required
- name: DN_HEADLESS_SVC_NAME
- name: DN_POD_FQDN_LIST
valueFrom:
serviceVarRef:
componentVarRef:
compDef: polardbx-dn
optional: false
name: headless
host: Required
podFQDNs: Required
- name: DN_SVC_PORT
valueFrom:
serviceVarRef:
Expand Down Expand Up @@ -94,7 +93,7 @@ spec:
- name: metaDbNonEncPasswd
value: $(SERVICE_PASSWORD)
- name: dnPasswordKey
value: "$(metaDbNonEncPasswd)$(metaDbNonEncPasswd)"
value: "$(metaDbNonEncPasswd)"
- name: switchCloud
value: aliyun
- name: metaDbConn
Expand Down Expand Up @@ -150,7 +149,7 @@ spec:
- name: metaDbConn
value: "mysql -h$(GMS_SVC_NAME) -P3306 -u$(metaDbUser) -p$(metaDbPasswd) -D$(metaDbName)"
- name: dnPasswordKey
value: "$(metaDbNonEncPasswd)$(metaDbNonEncPasswd)"
value: "$(metaDbNonEncPasswd)"
- name: metaDbXprotoPort
value: "0"
- name: storageDbXprotoPort
Expand Down
3 changes: 2 additions & 1 deletion addons/polardbx/templates/cmpd-dn.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ spec:
- sh
- -c
- |
mysql -h127.0.0.1 -P3306 -uroot -N -B -e "select role from information_schema.alisql_cluster_local" | xargs echo -n
role=$(mysql -h127.0.0.1 -P3306 -uroot -N -B -e "select role from information_schema.alisql_cluster_local" | tr '[:upper:]' '[:lower:]' | xargs echo -n)
echo -n "$role"
timeoutSeconds: {{ .Values.roleProbe.timeoutSeconds }}
periodSeconds: {{ .Values.roleProbe.periodSeconds }}
failureThreshold: {{ .Values.roleProbe.failureThreshold }}
Expand Down
7 changes: 4 additions & 3 deletions addons/polardbx/templates/cmpd-gms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ spec:
- name: polardbx_root
initAccount: true
passwordGenerationPolicy:
length: 10
numDigits: 5
length: 16
numDigits: 0
numSymbols: 0
letterCase: MixedCases
updateStrategy: Serial
Expand Down Expand Up @@ -75,7 +75,8 @@ spec:
- sh
- -c
- |
mysql -h127.0.0.1 -P3306 -uroot -N -B -e "select role from information_schema.alisql_cluster_local" | xargs echo -n
role=$(mysql -h127.0.0.1 -P3306 -uroot -N -B -e "select role from information_schema.alisql_cluster_local" | tr '[:upper:]' '[:lower:]' | xargs echo -n)
echo -n "$role"
timeoutSeconds: {{ .Values.roleProbe.timeoutSeconds }}
periodSeconds: {{ .Values.roleProbe.periodSeconds }}
failureThreshold: {{ .Values.roleProbe.failureThreshold }}
Expand Down
70 changes: 64 additions & 6 deletions examples/polardbx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ PolarDB-X is a cloud native distributed SQL Database designed for high concurren

| Horizontal<br/>scaling | Vertical <br/>scaling | Expand<br/>volume | Restart | Stop/Start | Configure | Expose | Switchover |
|------------------------|-----------------------|-------------------|-----------|------------|-----------|--------|------------|
| Yes (cn) | Yes | Yes | Yes | Yes | No | Yes | No |
| Yes (cn, cdc) | Yes | Yes | Yes | Yes | No | Yes | No |

### Versions

Expand All @@ -28,7 +28,19 @@ PolarDB-X is a cloud native distributed SQL Database designed for high concurren

### [Create](cluster.yaml)

Create a polardbx cluster
> [!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:
>
> ```sql
> show create table information_schema.ALISQL_CLUSTER_GLOBAL;
> ```
Create a polardbx cluster with four components: cn, dn, cdc, and gms.
```bash
kubectl apply -f examples/polardbx/cluster.yaml
Expand All @@ -40,24 +52,49 @@ As PolarDB-X is a distributed database with multiple components. You may prefer
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:

```bash
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.

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

### Horizontal scaling

#### [Scale-out](scale-out.yaml)

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

```bash
kubectl apply -f examples/polardbx/scale-out.yaml
```

#### [Scale-in](scale-in.yaml)

Horizontal scaling in polardbx cluster by deleting ONE cn replica:
Horizontal scaling in polardbx cluster by deleting ONE `cn` replica:

```bash
kubectl apply -f examples/polardbx/scale-in.yaml
```

### [Vertical scaling](verticalscale.yaml)

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:

```bash
kubectl apply -f examples/polardbx/verticalscale.yaml
```

### [Expand volume](volumeexpand.yaml)

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.
Expand Down Expand Up @@ -103,12 +140,33 @@ Start the stopped cluster
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](../docs/install-prometheus.md) to install the Prometheus Operator.

#### Create PodMonitor

Apply the `PodMonitor` file to monitor the cluster:

```bash
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](https://github.com/apecloud/kubeblocks-addons/blob/main/addons/polardbx/dashboards/polardbx-overview.json) 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

```bash
kubectl patch cluster polardbx-cluster -p '{"spec":{"terminationPolicy":"WipeOut"}}' --type="merge"
kubectl patch cluster pxc -p '{"spec":{"terminationPolicy":"WipeOut"}}' --type="merge"

kubectl delete cluster polardbx-cluster
kubectl delete cluster pxc
```
16 changes: 8 additions & 8 deletions examples/polardbx/cluster-with-schedule-policy.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
name: polardbx-cluster-with-sp
name: pxc
namespace: default
spec:
# Specifies the behavior when a Cluster is deleted.
Expand All @@ -24,14 +24,14 @@ spec:
- podAffinityTerm:
labelSelector:
matchLabels:
app.kubernetes.io/instance: polardbx-cluster
app.kubernetes.io/instance: pxc
apps.kubeblocks.io/component-name: gms
topologyKey: kubernetes.io/hostname
weight: 100
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app.kubernetes.io/instance: polardbx-cluster
app.kubernetes.io/instance: pxc
apps.kubeblocks.io/component-name: gms
topologyKey: kubernetes.io/hostname
replicas: 1
Expand Down Expand Up @@ -71,21 +71,21 @@ spec:
- name: cn
componentDef: polardbx-cn
# this ia an simple example of schedulingPolicy
# pods cannot be scheduled to the node with any POD from the same cluster `polardbx-cluster`
# pods cannot be scheduled to the node with any POD from the same cluster `pxc`
schedulingPolicy:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- podAffinityTerm:
labelSelector:
matchLabels:
app.kubernetes.io/instance: polardbx-cluster
app.kubernetes.io/instance: pxc
topologyKey: kubernetes.io/hostname
weight: 100
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app.kubernetes.io/instance: polardbx-cluster
app.kubernetes.io/instance: pxc
topologyKey: kubernetes.io/hostname
replicas: 1
resources:
Expand All @@ -106,14 +106,14 @@ spec:
- podAffinityTerm:
labelSelector:
matchLabels:
app.kubernetes.io/instance: polardbx-cluster
app.kubernetes.io/instance: pxc
apps.kubeblocks.io/component-name: dn
topologyKey: kubernetes.io/hostname
weight: 100
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app.kubernetes.io/instance: polardbx-cluster
app.kubernetes.io/instance: pxc
apps.kubeblocks.io/component-name: dn
topologyKey: kubernetes.io/hostname
replicas: 1
Expand Down
Loading

0 comments on commit 9e07deb

Please sign in to comment.