Skip to content

Commit 7a0b0b5

Browse files
sejongkhackerwins
andauthored
Add Helm chart for MongoDB sharded cluster (#769)
This PR adds a Helm chart for MongoDB, supporting both standalone mode and sharded cluster mode. Shard rules, including shard keys, shard methods, and unique constraints, are defined and managed in the values.yaml. This chart depends on Bitnami's mongodb-sharded chart (https://github.com/bitnami/charts/tree/main/bitnami/mongodb-sharded). There are a few known issues with it: A problem with livenessProbe and readinessProbe - Issues: bitnami/charts#21957 - Solution: use custom livenessProbe and readinessProbe instead Lack of ARM64 support in the Bitnami mongodb-sharded container - Issues: - bitnami/charts#7305 (comment) - bitnami/containers#40947 - Solution: use the official Mongo 6.0 image instead and set up the cluster via a Job. --------- Co-authored-by: Youngteac Hong <[email protected]>
1 parent b399a68 commit 7a0b0b5

File tree

14 files changed

+446
-46
lines changed

14 files changed

+446
-46
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ output.txt
6868
.vscode
6969

7070
# Chart dependencies
71-
/build/charts/yorkie-cluster/charts/*.tgz
71+
**/charts/*.tgz
7272
/build/charts/yorkie-argocd/charts/
7373
/build/charts/yorkie-monitoring/charts/
74+
/build/charts/yorkie-mongodb/charts/

build/charts/yorkie-cluster/charts/mongodb/Chart.yaml

-24
This file was deleted.

build/charts/yorkie-cluster/values.yaml

+5-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Configuration for Yorkie cluster
22
yorkie:
33
name: yorkie
4-
namespace: &namespace yorkie
4+
namespace: yorkie
55

66
autoscaling:
77
enabled: false
@@ -13,26 +13,15 @@ yorkie:
1313
tag: ""
1414

1515
args:
16-
dbUrl: &mongoUrl mongodb
17-
dbPort: &mongoPort 27017
16+
dbUrl: mongodb.mongodb.svc.cluster.local
17+
dbPort: 27017
1818

1919
ports:
2020
rpcPort: 8080
2121
profilingPort: 8081
2222

2323
resources: {}
2424

25-
# Configuration for internal mongoDB
26-
mongodb:
27-
enabled: true
28-
29-
namespace: *namespace
30-
name: *mongoUrl
31-
port: *mongoPort
32-
33-
storageClassName: standard
34-
storageSize: 1Gi
35-
3625
# Configuration for istio ingress gateway
3726
ingressGateway:
3827
consistentHash:
@@ -43,7 +32,7 @@ ingressGateway:
4332
maglev:
4433
enabled: true
4534
tableSize: 65537
46-
35+
4736
ringHash:
4837
enabled: false
4938
minimumRingSize: 1024
@@ -63,7 +52,7 @@ ingress:
6352
hosts:
6453
enabled: false
6554
apiHost: api.yorkie.dev
66-
55+
6756
alb:
6857
enabled: false
6958
certArn: arn:aws:acm:ap-northeast-2:123412341234:certificate/1234-1234-1234-1234-1234
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
dependencies:
2+
- name: mongodb-sharded
3+
repository: https://charts.bitnami.com/bitnami
4+
version: 7.2.2
5+
digest: sha256:ae306fa705473ca5e99f02fcca27ebb5900cf4bdf4357472f8b5006c9c4fca24
6+
generated: "2024-01-23T20:44:36.400805+09:00"
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: v2
2+
name: yorkie-mongodb
3+
description: yorkie-mongodb provides the deployment of MongoDB for the yorkie server, supporting both standalone mode and sharded cluster mode.
4+
type: application
5+
icon: https://raw.githubusercontent.com/yorkie-team/yorkie-team.github.io/main/public/favicon-512x512.png
6+
maintainers:
7+
- name: hackerwins
8+
9+
- name: krapie
10+
11+
12+
version: 0.4.13
13+
appVersion: "0.4.13"
14+
kubeVersion: ">=1.23.0-0"
15+
16+
dependencies:
17+
- name: mongodb-sharded
18+
version: "7.2.2"
19+
repository: "https://charts.bitnami.com/bitnami"
20+
condition: sharded.enabled

build/charts/yorkie-mongodb/README.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# yorkie-mongodb
2+
3+
Installs MongoDB for the yorkie server, supporting both standalone mode and sharded cluster mode for storing yorkie data.
4+
5+
## Prerequisites
6+
7+
- Kubernetes 1.24+
8+
- Helm 3+
9+
10+
## Get Helm Repository Info
11+
12+
```bash
13+
helm repo add yorkie-team https://yorkie-team.github.io/yorkie/helm-charts
14+
helm repo update
15+
```
16+
17+
_See [`helm repo`](https://helm.sh/docs/helm/helm_repo/) for command documentation._
18+
19+
## Install Helm Chart
20+
21+
```bash
22+
# Create mongodb namespace
23+
kubectl create namespace mongodb
24+
25+
# Install yorkie monitoring helm chart
26+
helm install [RELEASE_NAME] yorkie-team/yorkie-mongodb --namespace mongodb --set=sharded.enabled=true
27+
```
28+
29+
_See [configuration](#configuration) below for custom installation_
30+
31+
_See [`helm install`](https://helm.sh/docs/helm/helm_install/) for command documentation._
32+
33+
## Dependencies
34+
35+
By default this chart installs additional, dependent charts:
36+
37+
- [mongodb-sharded](https://github.com/bitnami/charts/tree/main/bitnami/mongodb-sharded)
38+
39+
_See [`helm dependency`](https://helm.sh/docs/helm/helm_dependency/) for command documentation._
40+
41+
## Uninstall Helm Chart
42+
43+
```bash
44+
helm uninstall [RELEASE_NAME] -n mongodb
45+
```
46+
47+
This removes all the Kubernetes components associated with the chart and deletes the release.
48+
49+
_See [`helm uninstall`](https://helm.sh/docs/helm/helm_uninstall/) for command documentation._
50+
51+
## Upgrading Chart
52+
53+
```bash
54+
helm upgrade [RELEASE_NAME] yorkie-team/yorkie-mongodb -n mongodb
55+
```
56+
57+
With Helm v3, CRDs created by this chart are not updated by default and should be manually updated.
58+
Consult also the [Helm Documentation on CRDs](https://helm.sh/docs/chart_best_practices/custom_resource_definitions).
59+
60+
_See [`helm upgrade`](https://helm.sh/docs/helm/helm_upgrade/) for command documentation._
61+
62+
## Configuration
63+
64+
See [Customizing the Chart Before Installing](https://helm.sh/docs/intro/using_helm/#customizing-the-chart-before-installing). To see all configurable options with detailed comments:
65+
66+
```console
67+
helm show values yorkie-team/yorkie-mongodb
68+
```
69+
70+
You may also `helm show values` on this chart's [dependencies](#dependencies) for additional options.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--- Install Complete ---
2+
{{ .Release.Name }} successfully installed!
3+
4+
For next steps, follow:
5+
$ curl https://github.com/yorkie-team/yorkie/tree/main/charts/yorkie-mongodb
6+
7+
To learn more about the release, try:
8+
$ helm status {{ .Release.Name }}
9+
$ helm get all {{ .Release.Name }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{{- define "configReplName" -}}
2+
{{- $name := index . 0 -}}
3+
{{- printf "%s-configsvr" $name -}}
4+
{{- end -}}
5+
6+
{{- define "configReplAddr" -}}
7+
{{- $name := index . 0 -}}
8+
{{- $replIndex := index . 1 -}}
9+
{{- $domainSuffix := index . 2 -}}
10+
{{- printf "%s-configsvr-%d.%s-headless.%s" $name $replIndex $name $domainSuffix -}}
11+
{{- end -}}
12+
13+
{{- define "shardReplName" -}}
14+
{{- $name := index . 0 -}}
15+
{{- $index := index . 1 -}}
16+
{{- printf "%s-shard-%d" $name $index -}}
17+
{{- end -}}
18+
19+
{{- define "shardReplAddr" -}}
20+
{{- $name := index . 0 -}}
21+
{{- $shardIndex := index . 1 -}}
22+
{{- $replIndex := index . 2 -}}
23+
{{- $domainSuffix := index . 3 -}}
24+
{{- printf "%s-shard%d-data-%d.%s-headless.%s" $name $shardIndex $replIndex $name $domainSuffix -}}
25+
{{- end -}}
26+
27+
{{- define "mongosAddr" -}}
28+
{{- $name := index . 0 -}}
29+
{{- $domainSuffix := index . 1 -}}
30+
{{- printf "%s-mongos-0.%s.%s" $name $name $domainSuffix -}}
31+
{{- end -}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{{- if and .Values.sharded.enabled .Values.sharded.setup.enabled }}
2+
{{- $domainSuffix := printf "%s.svc.%s:%d" $.Values.namespace $.Values.clusterDomain ($.Values.port | int ) }}
3+
apiVersion: v1
4+
kind: ConfigMap
5+
metadata:
6+
name: {{ .Values.name }}-setup-script
7+
namespace: {{ .Values.namespace }}
8+
labels:
9+
app.kubernetes.io/component: mongodb
10+
app.kubernetes.io/part-of: yorkie
11+
data:
12+
setup.sh: |
13+
#!/bin/bash
14+
15+
function waitUntilReady {
16+
while true; do
17+
mongosh $1 --quiet --eval "db.runCommand('ping').ok"
18+
if [ $? -eq 0 ]; then
19+
break
20+
fi
21+
sleep 5
22+
done
23+
}
24+
25+
{{ $configsvrAddr := include "configReplAddr" (list $.Values.name 0 $domainSuffix) }}
26+
echo "Wait until config server is ready..."
27+
configsvrAddr="{{ $configsvrAddr }}"
28+
echo "Config server address: ${configsvrAddr}"
29+
waitUntilReady $configsvrAddr
30+
echo "Configure config server"
31+
mongosh $configsvrAddr --eval 'rs.initiate({"_id":"{{ include "configReplName" (list $.Values.name) }}", "members":[{"_id":0,"host":"{{ $configsvrAddr }}","priority":5}]})'
32+
33+
{{ range $i, $e := until ($.Values.sharded.shards | int) }}
34+
{{ $shardsvrAddr := include "shardReplAddr" (list $.Values.name $i 0 $domainSuffix) }}
35+
echo "{{ printf "Wait until shard%d is ready..." $i }}"
36+
shardsvrAddr="{{ $shardsvrAddr }}"
37+
echo "{{ printf "Shard%d address: %s" $i $shardsvrAddr }}"
38+
waitUntilReady $shardsvrAddr
39+
echo "{{ printf "Configure shard%d" $i }}"
40+
mongosh $shardsvrAddr --eval 'rs.initiate({"_id":"{{ include "shardReplName" (list $.Values.name $i) }}", "members":[{"_id":0,"host":"{{ $shardsvrAddr }}","priority":5}]})'
41+
{{ end }}
42+
43+
{{ $mongosAddr := include "mongosAddr" (list $.Values.name $domainSuffix) }}
44+
echo "Wait until mongos is ready..."
45+
mongosAddr="{{ $mongosAddr }}"
46+
echo "Mongos address: ${mongosAddr}"
47+
waitUntilReady $mongosAddr
48+
echo "Configure mongos"
49+
mongosh $mongosAddr --eval <<EOF
50+
{{- range $i, $e := until ($.Values.sharded.shards | int) }}
51+
{{- range $j, $e := until ($.Values.sharded.replicaCount.shardsvr | int) }}
52+
sh.addShard("{{ printf "%s/%s" (include "shardReplName" (list $.Values.name $i)) (include "shardReplAddr" (list $.Values.name $i $j $domainSuffix)) }}");
53+
{{- end }}
54+
{{- end }}
55+
sh.enableSharding("{{ .Values.sharded.setup.database }}");
56+
{{- range .Values.sharded.setup.rules }}
57+
sh.shardCollection(
58+
"{{ printf "%s.%s" $.Values.sharded.setup.database .collectionName }}",
59+
{
60+
{{- range .shardKeys }}
61+
{{ printf "%s: %s," .name .method }}
62+
{{- end }}
63+
},
64+
{{ .unique }}
65+
);
66+
{{- end }}
67+
EOF
68+
69+
{{ end -}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{{- if and .Values.sharded.enabled .Values.sharded.setup.enabled }}
2+
apiVersion: batch/v1
3+
kind: Job
4+
metadata:
5+
name: {{ .Values.name }}-setup-job
6+
namespace: {{ .Values.namespace }}
7+
labels:
8+
app.kubernetes.io/component: mongodb
9+
app.kubernetes.io/part-of: yorkie
10+
annotations:
11+
"helm.sh/hook": post-install,post-upgrade
12+
"helm.sh/hook-delete-policy": before-hook-creation
13+
spec:
14+
template:
15+
spec:
16+
restartPolicy: {{ .Values.sharded.setup.restartPolicy }}
17+
containers:
18+
- name: setup
19+
image: {{ printf "%s/%s:%s" $.Values.image.registry $.Values.image.repository $.Values.image.tag }}
20+
command: [ "/bin/bash", "/etc/config/setup.sh" ]
21+
volumeMounts:
22+
- name: setup-script
23+
mountPath: /etc/config
24+
volumes:
25+
- name: setup-script
26+
configMap:
27+
name: {{ .Values.name }}-setup-script
28+
backoffLimit: {{ .Values.sharded.setup.backoffLimit }}
29+
{{ end }}

build/charts/yorkie-cluster/charts/mongodb/templates/service.yaml build/charts/yorkie-mongodb/templates/standalone/service.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{ if .Values.enabled -}}
1+
{{ if not .Values.sharded.enabled -}}
22
apiVersion: v1
33
kind: Service
44
metadata:

build/charts/yorkie-cluster/charts/mongodb/templates/statefulset.yaml build/charts/yorkie-mongodb/templates/standalone/statefulset.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{ if .Values.enabled -}}
1+
{{ if not .Values.sharded.enabled -}}
22
apiVersion: apps/v1
33
kind: StatefulSet
44
metadata:
@@ -25,7 +25,7 @@ spec:
2525
terminationGracePeriodSeconds: 60
2626
containers:
2727
- name: mongodb
28-
image: mongo:4.4.1
28+
image: {{ printf "%s/%s:%s" $.Values.image.registry $.Values.image.repository $.Values.image.tag }}
2929
ports:
3030
- containerPort: {{ .Values.port }}
3131
volumeMounts:
@@ -39,8 +39,8 @@ spec:
3939
name: {{ .Values.name }}-storage
4040
spec:
4141
accessModes: ["ReadWriteOnce"]
42-
storageClassName: {{ .Values.storageClassName }}
42+
storageClassName: {{ .Values.persistence.storageClass }}
4343
resources:
4444
requests:
45-
storage: {{ .Values.storageSize }}
45+
storage: {{ .Values.persistence.dataSize }}
4646
{{ end }}

0 commit comments

Comments
 (0)