From b07f52b335b6208590443f12e70e485b8399ae43 Mon Sep 17 00:00:00 2001 From: earayu Date: Wed, 14 May 2025 11:00:40 +0800 Subject: [PATCH] feat: add database helm --- deploy/kubeblocks-databases/Chart.yaml | 6 + deploy/kubeblocks-databases/NOTES.txt | 21 +++ deploy/kubeblocks-databases/README.md | 165 ++++++++++++++++++ .../templates/_helpers.tpl | 0 .../templates/elasticsearch-cluster.yaml | 31 ++++ .../templates/mongodb-cluster.yaml | 27 +++ .../templates/postgresql-cluster.yaml | 35 ++++ .../templates/qdrant-cluster.yaml | 27 +++ .../templates/redis-cluster.yaml | 36 ++++ deploy/kubeblocks-databases/values.yaml | 86 +++++++++ 10 files changed, 434 insertions(+) create mode 100644 deploy/kubeblocks-databases/Chart.yaml create mode 100644 deploy/kubeblocks-databases/NOTES.txt create mode 100644 deploy/kubeblocks-databases/README.md create mode 100644 deploy/kubeblocks-databases/templates/_helpers.tpl create mode 100644 deploy/kubeblocks-databases/templates/elasticsearch-cluster.yaml create mode 100644 deploy/kubeblocks-databases/templates/mongodb-cluster.yaml create mode 100644 deploy/kubeblocks-databases/templates/postgresql-cluster.yaml create mode 100644 deploy/kubeblocks-databases/templates/qdrant-cluster.yaml create mode 100644 deploy/kubeblocks-databases/templates/redis-cluster.yaml create mode 100644 deploy/kubeblocks-databases/values.yaml diff --git a/deploy/kubeblocks-databases/Chart.yaml b/deploy/kubeblocks-databases/Chart.yaml new file mode 100644 index 00000000..7768906a --- /dev/null +++ b/deploy/kubeblocks-databases/Chart.yaml @@ -0,0 +1,6 @@ +apiVersion: v2 +name: kubeblocks-databases +description: A Helm chart to deploy PostgreSQL, Redis, Elasticsearch, and Qdrant clusters using KubeBlocks. +type: application +version: 0.1.0 +appVersion: "1.0" # Or the version of KubeBlocks you are targeting diff --git a/deploy/kubeblocks-databases/NOTES.txt b/deploy/kubeblocks-databases/NOTES.txt new file mode 100644 index 00000000..351e6ca4 --- /dev/null +++ b/deploy/kubeblocks-databases/NOTES.txt @@ -0,0 +1,21 @@ +This Helm chart has deployed KubeBlocks database clusters as configured in your values.yaml. + +Enabled clusters: +{{- if .Values.postgresql.enabled }} + - PostgreSQL: {{ .Values.postgresql.name }} in namespace {{ .Values.global.namespace }} +{{- end }} +{{- if .Values.redis.enabled }} + - Redis: {{ .Values.redis.name }} in namespace {{ .Values.global.namespace }} +{{- end }} +{{- if .Values.elasticsearch.enabled }} + - Elasticsearch: {{ .Values.elasticsearch.name }} in namespace {{ .Values.global.namespace }} +{{- end }} +{{- if .Values.qdrant.enabled }} + - Qdrant: {{ .Values.qdrant.name }} in namespace {{ .Values.global.namespace }} +{{- end }} + +You can check the status of your clusters using kubectl: + kubectl get clusters -n {{ .Values.global.namespace }} + kubectl get pods -n {{ .Values.global.namespace }} + +For KubeBlocks specific commands, you might use the kbcli tool if installed. diff --git a/deploy/kubeblocks-databases/README.md b/deploy/kubeblocks-databases/README.md new file mode 100644 index 00000000..54b69b7d --- /dev/null +++ b/deploy/kubeblocks-databases/README.md @@ -0,0 +1,165 @@ +# KubeBlocks Databases Helm Chart + +This Helm chart deploys and manages multiple database clusters (PostgreSQL, Redis, Elasticsearch, Qdrant) using [KubeBlocks](https://kubeblocks.io/). + +## Prerequisites + +* Kubernetes cluster (version compatible with KubeBlocks) +* [Helm](https://helm.sh/docs/intro/install/) (version 3+) installed. +* [KubeBlocks](https://kubeblocks.io/docs/preview/user_docs/installation) installed in your Kubernetes cluster. +* `kubectl` configured to interact with your cluster. + +```bash +kubectl create namespace kb-system +kbcli kubeblocks install --version=1.0.0-beta.47 --namespace kb-system +``` + + +## Installation + +```bash +helm repo remove kubeblocks +helm repo add kubeblocks https://apecloud.github.io/helm-charts +helm repo update + +helm upgrade --install kb-addon-elasticsearch kubeblocks/elasticsearch --namespace kb-system --version 1.0.0-alpha.0 +helm upgrade --install kb-addon-qdrant kubeblocks/qdrant --namespace kb-system --version 1.0.0-alpha.0 +helm upgrade --install kb-addon-postgresql kubeblocks/postgresql --namespace kb-system --version 1.0.0-alpha.0 +helm upgrade --install kb-addon-redis kubeblocks/redis --namespace kb-system --version 1.0.0-alpha.0 +``` + +```bash +kubectl create namespace demo +kubectl create secret generic postgresql-secret \ + --namespace=demo \ + --from-literal=username=postgres \ + --from-literal=password=postgres +kubectl create secret generic redis-secret \ + --namespace=demo \ + --from-literal=username=default \ + --from-literal=password=password +helm install kb-databases ./kubeblocks-databases -n demo --create-namespace \ +--set redis.customSecretName=redis-secret,redis.customSecretNamespace=demo,postgresql.customSecretName=postgresql-secret,postgresql.customSecretNamespace=demo +``` + +generate template: +```bash +helm template kb-databases ./kubeblocks-databases -n demo --create-namespace \ +--set redis.customSecretName=redis-secret,redis.customSecretNamespace=demo,postgresql.customSecretName=postgresql-secret,postgresql.customSecretNamespace=demo \ +> rendered.yaml +``` + +## Verification + +After installation, you can check the status of the deployed KubeBlocks clusters: + +```bash +kubectl get clusters -n demo +kubectl get pods -n demo +``` + + +You should see the `Cluster` resources for the enabled databases and their corresponding pods. The `NOTES.txt` output from Helm will also provide some of this information. + +```bash +kubectl get clusters -n demo +NAME CLUSTER-DEFINITION TERMINATION-POLICY STATUS AGE +es-cluster Delete Running 121m +pg-cluster postgresql Delete Creating 121m +qdrant-cluster qdrant Delete Running 121m +redis-standalone redis Delete Running 121m + +kubectl get pods -n demo +NAME READY STATUS RESTARTS AGE +es-cluster-mdit-0 3/3 Running 0 110m +pg-cluster-postgresql-0 5/5 Running 0 121m +qdrant-cluster-qdrant-0 2/2 Running 0 117m +redis-standalone-redis-0 3/3 Running 0 121m +``` + +## Connect + +port-forward: +```bash +echo "Starting Elasticsearch port-forward..." +kubectl port-forward -n demo service/es-cluster-mdit-http 9200:9200 & +ES_PID=$! +echo "Elasticsearch port-forward process ID: $ES_PID" +echo "Starting Qdrant port-forward..." +kubectl port-forward -n demo service/qdrant-cluster-qdrant-qdrant 6333:6333 & +QDRANT_PID=$! +echo "Qdrant port-forward process ID: $QDRANT_PID" +echo "Starting PostgreSQL port-forward..." +kubectl port-forward -n demo service/pg-cluster-postgresql-postgresql 5432:5432 & +PG_PID=$! +echo "PostgreSQL port-forward process ID: $PG_PID" +echo "Starting Redis port-forward..." +kubectl port-forward -n demo service/redis-standalone-redis-redis 6379:6379 & +REDIS_PID=$! +echo "Redis port-forward process ID: $REDIS_PID" +echo "All port-forwards have been started" +echo "Press Ctrl+C to stop all port-forwards" +# Capture Ctrl+C signal and clean up all processes +trap "kill $ES_PID $QDRANT_PID $PG_PID $REDIS_PID; echo 'All port-forwards stopped'; exit" INT +# Wait for any child process to finish +wait +``` + +## Uninstallation + +To uninstall the deployed database clusters: + +```bash +helm uninstall kb-databases -n demo +``` +This will remove all Kubernetes resources associated with this Helm release, including the KubeBlocks `Cluster` objects. Depending on the `terminationPolicy` and KubeBlocks behavior, PVCs might also be deleted. + + +## Configuration + +The primary way to configure the deployments is through the `values.yaml` file. + +### Global Settings + +These settings apply to all database clusters deployed by this chart: + +```yaml +global: + namespace: "demo" + terminationPolicy: "Delete" # Options: DoNotTerminate, Delete, WipeOut +``` + +### Per-Database Settings + +Each database (PostgreSQL, Redis, Elasticsearch, Qdrant) has its own configuration block. Here's an example for PostgreSQL: + +```yaml +postgresql: + enabled: true # Set to true to deploy this database, false to skip + name: "pg-cluster" # Name of the KubeBlocks Cluster resource + serviceVersion: "14.7.2" # Database engine version + disableExporter: false # true to disable metrics exporter, false to enable + replicas: 2 # Number of replicas for the main component + resources: # CPU and Memory requests/limits + limits: + cpu: "0.5" + memory: "0.5Gi" + requests: + cpu: "0.5" + memory: "0.5Gi" + storage: "20Gi" # Storage size for the data volume (e.g., PVC) +``` + +Refer to `values.yaml` for the full set of configurable options for each database. + +**Key configurable parameters for each database:** + +* `enabled`: (boolean) Deploy this database cluster. +* `name`: (string) Name for the KubeBlocks `Cluster` resource. +* `serviceVersion`: (string) Specific version of the database engine. +* `disableExporter`: (boolean) Enable/disable the metrics exporter. (Note: For Elasticsearch, this might be handled differently by its `componentDef`). +* `replicas`: (integer) Number of replicas for the primary database component. +* `resources`: (object) Standard Kubernetes resource requests and limits. +* `storage`: (string) Storage capacity for persistent volumes (e.g., "10Gi", "100Gi"). +* `topology`: (string, for Redis) e.g., "standalone", "replication". +* `componentDef`: (string, for Elasticsearch) e.g., "elasticsearch-8". diff --git a/deploy/kubeblocks-databases/templates/_helpers.tpl b/deploy/kubeblocks-databases/templates/_helpers.tpl new file mode 100644 index 00000000..e69de29b diff --git a/deploy/kubeblocks-databases/templates/elasticsearch-cluster.yaml b/deploy/kubeblocks-databases/templates/elasticsearch-cluster.yaml new file mode 100644 index 00000000..c0b2a92e --- /dev/null +++ b/deploy/kubeblocks-databases/templates/elasticsearch-cluster.yaml @@ -0,0 +1,31 @@ +{{- if .Values.elasticsearch.enabled }} +apiVersion: apps.kubeblocks.io/v1 +kind: Cluster +metadata: + name: {{ .Values.elasticsearch.name }} + namespace: {{ .Values.global.namespace }} +spec: + terminationPolicy: {{ .Values.global.terminationPolicy }} + # Elasticsearch example provided doesn't specify clusterDef or topology at the spec level + # It's often defined by the componentDef within componentSpecs for KubeBlocks' ES + componentSpecs: + - name: mdit # Component name from your example, can be made configurable if needed + componentDef: {{ .Values.elasticsearch.componentDef }} + serviceVersion: "{{ .Values.elasticsearch.serviceVersion }}" + replicas: {{ .Values.elasticsearch.replicas }} + configs: # Hardcoding single-node config as per your example and simplicity request + - name: es-cm + variables: + mode: "single-node" + resources: + {{- toYaml .Values.elasticsearch.resources | nindent 8 }} + volumeClaimTemplates: + - name: data + spec: + storageClassName: "" + accessModes: + - ReadWriteOnce + resources: + requests: + storage: {{ .Values.elasticsearch.storage }} +{{- end }} diff --git a/deploy/kubeblocks-databases/templates/mongodb-cluster.yaml b/deploy/kubeblocks-databases/templates/mongodb-cluster.yaml new file mode 100644 index 00000000..153dfb9d --- /dev/null +++ b/deploy/kubeblocks-databases/templates/mongodb-cluster.yaml @@ -0,0 +1,27 @@ +{{- if .Values.mongodb.enabled }} +apiVersion: apps.kubeblocks.io/v1 +kind: Cluster +metadata: + name: {{ .Values.mongodb.name }} + namespace: {{ .Values.global.namespace }} +spec: + terminationPolicy: {{ .Values.global.terminationPolicy }} + clusterDef: mongodb + topology: {{ .Values.mongodb.topology }} + componentSpecs: + - name: mongodb + serviceVersion: "{{ .Values.mongodb.serviceVersion }}" + disableExporter: {{ .Values.mongodb.disableExporter }} + replicas: {{ .Values.mongodb.replicas }} + resources: + {{- toYaml .Values.mongodb.resources | nindent 8 }} + volumeClaimTemplates: + - name: data + spec: + storageClassName: "{{ .Values.mongodb.storageClassName }}" + accessModes: + - ReadWriteOnce + resources: + requests: + storage: {{ .Values.mongodb.storage }} +{{- end }} diff --git a/deploy/kubeblocks-databases/templates/postgresql-cluster.yaml b/deploy/kubeblocks-databases/templates/postgresql-cluster.yaml new file mode 100644 index 00000000..fbb1db7b --- /dev/null +++ b/deploy/kubeblocks-databases/templates/postgresql-cluster.yaml @@ -0,0 +1,35 @@ +{{- if .Values.postgresql.enabled }} +apiVersion: apps.kubeblocks.io/v1 +kind: Cluster +metadata: + name: {{ .Values.postgresql.name }} + namespace: {{ .Values.global.namespace }} +spec: + terminationPolicy: {{ .Values.global.terminationPolicy }} + clusterDef: postgresql + topology: replication # As per your example + componentSpecs: + - name: postgresql # Default component name for PostgreSQL + serviceVersion: "{{ .Values.postgresql.serviceVersion }}" + disableExporter: {{ .Values.postgresql.disableExporter }} + labels: + # Specific label for Patroni scope + apps.kubeblocks.postgres.patroni/scope: {{ .Values.postgresql.name }}-postgresql + replicas: {{ .Values.postgresql.replicas }} + systemAccounts: + - name: postgres + secretRef: + name: {{ .Values.postgresql.customSecretName }} + namespace: {{ .Values.postgresql.customSecretNamespace }} + resources: + {{- toYaml .Values.postgresql.resources | nindent 8 }} + volumeClaimTemplates: + - name: data + spec: + storageClassName: "" # Or make this configurable if needed + accessModes: + - ReadWriteOnce + resources: + requests: + storage: {{ .Values.postgresql.storage }} +{{- end }} diff --git a/deploy/kubeblocks-databases/templates/qdrant-cluster.yaml b/deploy/kubeblocks-databases/templates/qdrant-cluster.yaml new file mode 100644 index 00000000..13ef0fc5 --- /dev/null +++ b/deploy/kubeblocks-databases/templates/qdrant-cluster.yaml @@ -0,0 +1,27 @@ +{{- if .Values.qdrant.enabled }} +apiVersion: apps.kubeblocks.io/v1 +kind: Cluster +metadata: + name: {{ .Values.qdrant.name }} + namespace: {{ .Values.global.namespace }} +spec: + terminationPolicy: {{ .Values.global.terminationPolicy }} + clusterDef: qdrant + topology: cluster # As per your example + componentSpecs: + - name: qdrant # Default component name for Qdrant + serviceVersion: "{{ .Values.qdrant.serviceVersion }}" + disableExporter: {{ .Values.qdrant.disableExporter }} + replicas: {{ .Values.qdrant.replicas }} + resources: + {{- toYaml .Values.qdrant.resources | nindent 8 }} + volumeClaimTemplates: + - name: data + spec: + storageClassName: "" + accessModes: + - ReadWriteOnce + resources: + requests: + storage: {{ .Values.qdrant.storage }} +{{- end }} diff --git a/deploy/kubeblocks-databases/templates/redis-cluster.yaml b/deploy/kubeblocks-databases/templates/redis-cluster.yaml new file mode 100644 index 00000000..a83ca194 --- /dev/null +++ b/deploy/kubeblocks-databases/templates/redis-cluster.yaml @@ -0,0 +1,36 @@ +{{- if .Values.redis.enabled }} +apiVersion: apps.kubeblocks.io/v1 +kind: Cluster +metadata: + name: {{ .Values.redis.name }} + namespace: {{ .Values.global.namespace }} +spec: + terminationPolicy: {{ .Values.global.terminationPolicy }} + clusterDef: redis + topology: {{ .Values.redis.topology }} # Use topology from values + componentSpecs: + - name: redis # Main Redis component + {{- if .Values.redis.serviceVersion }} # serviceVersion is optional for some clusterDefs + serviceVersion: "{{ .Values.redis.serviceVersion }}" + {{- end }} + {{- if (not ( eq .Values.redis.disableExporter nil )) }} # disableExporter is also optional + disableExporter: {{ .Values.redis.disableExporter }} + {{- end }} + replicas: {{ .Values.redis.replicas }} + systemAccounts: + - name: default + secretRef: + name: {{ .Values.redis.customSecretName }} + namespace: {{ .Values.redis.customSecretNamespace }} + resources: + {{- toYaml .Values.redis.resources | nindent 8 }} + volumeClaimTemplates: + - name: data + spec: + storageClassName: "" # Or make this configurable + accessModes: + - ReadWriteOnce + resources: + requests: + storage: {{ .Values.redis.storage }} +{{- end }} diff --git a/deploy/kubeblocks-databases/values.yaml b/deploy/kubeblocks-databases/values.yaml new file mode 100644 index 00000000..254ab1b6 --- /dev/null +++ b/deploy/kubeblocks-databases/values.yaml @@ -0,0 +1,86 @@ +# Global settings applicable to all database clusters +global: + namespace: "demo" # The namespace where all clusters will be deployed + terminationPolicy: "Delete" # Common termination policy + +postgresql: + enabled: true + name: "pg-cluster" # Name for the PostgreSQL cluster + serviceVersion: "14.7.2" + disableExporter: false # Corresponds to Kubeblocks disableExporter + customSecretName: + customSecretNamespace: + replicas: 1 + resources: + limits: + cpu: "1" + memory: "1Gi" + requests: + cpu: "1" + memory: "1Gi" + storage: "5Gi" + +redis: + enabled: true + name: "redis-standalone" # Name for the Redis cluster + topology: "standalone" # Explicitly set topology + serviceVersion: "7.2.4" # Keep or update as needed + disableExporter: false # Keep or update as needed + customSecretName: + customSecretNamespace: + replicas: 1 # Standalone typically means 1 replica + resources: + limits: + cpu: "1" + memory: "1Gi" + requests: + cpu: "1" + memory: "1Gi" + storage: "5Gi" + +elasticsearch: + enabled: true + name: "es-cluster" # Name for the Elasticsearch cluster + componentDef: "elasticsearch-8" # Example: "elasticsearch-8" + serviceVersion: "8.8.2" + replicas: 1 # For the 'mdit' component (or whatever the main ES component is named) + resources: + limits: + cpu: "1" + memory: "1Gi" + requests: + cpu: "1" + memory: "1Gi" + storage: "5Gi" + +qdrant: + enabled: true + name: "qdrant-cluster" # Name for the Qdrant cluster + serviceVersion: "1.10.0" + disableExporter: false + replicas: 1 + resources: + limits: + cpu: "1" + memory: "1Gi" + requests: + cpu: "1" + memory: "1Gi" + storage: "5Gi" + +mongodb: + enabled: false + name: "mongo-cluster" + serviceVersion: "6.0.16" + topology: replicaset + disableExporter: false + replicas: 3 + resources: + limits: + cpu: "0.5" + memory: "0.5Gi" + requests: + cpu: "0.5" + memory: "0.5Gi" + storageClassName: "" + storage: 20Gi