Skip to content

Commit ae1aea0

Browse files
Y-Rookieleon-inf
andauthored
feat: add cluster definition topology and component version for redis (#501)
Co-authored-by: Leon <[email protected]>
1 parent 5ea8ea0 commit ae1aea0

19 files changed

+244
-55
lines changed

addons/redis-cluster/templates/_helpers.tpl

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Define redis cluster shardingSpec with ComponentDefinition.
66
shards: {{ .Values.redisCluster.shardCount }}
77
template:
88
name: redis
9-
componentDef: redis-cluster
9+
componentDef: redis-cluster-7.0
1010
replicas: {{ .Values.replicas }}
1111
{{- if .Values.nodePortEnabled }}
1212
services:
@@ -36,8 +36,6 @@ Define redis ComponentSpec with ComponentDefinition.
3636
*/}}
3737
{{- define "redis-cluster.componentSpec" }}
3838
- name: redis
39-
componentDef: redis
40-
{{- include "kblib.componentMonitor" . | indent 2 }}
4139
{{- include "redis-cluster.replicaCount" . | indent 2 }}
4240
{{- if .Values.nodePortEnabled }}
4341
services:
@@ -52,20 +50,13 @@ Define redis ComponentSpec with ComponentDefinition.
5250
type: Noop
5351
{{- include "kblib.componentResources" . | indent 2 }}
5452
{{- include "kblib.componentStorages" . | indent 2 }}
55-
{{- if and (eq .Values.mode "replication") .Values.twemproxy.enabled }}
56-
{{- include "redis-cluster.twemproxyComponentSpec" . }}
57-
{{- end }}
58-
{{- if and (eq .Values.mode "replication") .Values.sentinel.enabled }}
59-
{{- include "redis-cluster.sentinelComponentSpec" . }}
60-
{{- end }}
6153
{{- end }}
6254

6355
{{/*
6456
Define redis sentinel ComponentSpec with ComponentDefinition.
6557
*/}}
6658
{{- define "redis-cluster.sentinelComponentSpec" }}
67-
- componentDef: redis-sentinel
68-
name: redis-sentinel
59+
- name: redis-sentinel
6960
replicas: {{ .Values.sentinel.replicas }}
7061
{{- if .Values.nodePortEnabled }}
7162
services:
@@ -95,7 +86,6 @@ Define redis twemproxy ComponentSpec with ComponentDefinition.
9586
*/}}
9687
{{- define "redis-cluster.twemproxyComponentSpec" }}
9788
- name: redis-twemproxy
98-
componentDef: redis-twemproxy
9989
serviceAccountName: {{ include "kblib.serviceAccountName" . }}
10090
replicas: {{ .Values.twemproxy.replicas }}
10191
resources:
@@ -184,6 +174,8 @@ replication mode: 2
184174
replicas: 1
185175
{{- else if eq .Values.mode "replication" }}
186176
replicas: {{ max .Values.replicas 2 }}
177+
{{- else if eq .Values.mode "replication-twemproxy" }}
178+
replicas: {{ max .Values.replicas 2 }}
187179
{{- end }}
188180
{{- end }}
189181

addons/redis-cluster/templates/cluster.yaml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,24 @@
44
clusterDefinitionRef: redis
55
componentSpecs:
66
{{- include "redis-cluster.legacyComponentSpec" . | indent 2 }}
7-
{{- else }}
7+
{{- else if eq .Values.mode "cluster" }}
8+
{{- /* TODO: when clusterDefinition topology supports sharding, migrate the cluster mode to topology as well */ -}}
89
{{- include "kblib.clusterCommon" . }}
9-
{{- if eq .Values.mode "cluster" }}
1010
shardingSpecs:
1111
{{- include "redis-cluster.shardingSpec" . | indent 2 }}
12-
{{- else }}
12+
{{- else }}
13+
{{- include "kblib.clusterCommon" . }}
14+
clusterDefinitionRef: redis
15+
topology: {{ .Values.mode }}
1316
componentSpecs:
14-
{{- include "redis-cluster.componentSpec" . | indent 2 }}
17+
{{- if eq .Values.mode "replication" }}
18+
{{- include "redis-cluster.componentSpec" . | indent 2 }}
19+
{{- include "redis-cluster.sentinelComponentSpec" . | indent 2 }}
20+
{{- else if eq .Values.mode "replication-twemproxy" }}
21+
{{- include "redis-cluster.componentSpec" . | indent 2 }}
22+
{{- include "redis-cluster.sentinelComponentSpec" . | indent 2 }}
23+
{{- include "redis-cluster.twemproxyComponentSpec" . | indent 2 }}
24+
{{- else }}
25+
{{- include "redis-cluster.componentSpec" . | indent 2 }}
1526
{{- end }}
1627
{{- end }}

addons/redis-cluster/values.schema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"standalone",
2424
"replication",
2525
"cluster",
26-
"sentinel"
26+
"sentinel",
27+
"replication-twemproxy"
2728
]
2829
},
2930
"replicas": {

addons/redis-cluster/values.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# This is a YAML-formatted file.
33
# Declare variables to be passed into your templates.
44
# if useLegacyCompDef is false ,cluster will be rendered by compDef rather than componentDefRef in cluster definition
5-
useLegacyCompDef: true
5+
useLegacyCompDef: false
66

77

88
## @param version redis cluster version
@@ -12,6 +12,7 @@ version: redis-7.0.6
1212
## @param mode redis cluster topology mode, standalone, replication and cluster
1313
## standalone: single redis instance
1414
## replication: primary-secondary replication with redis sentinel, refer: https://redis.io/docs/management/sentinel
15+
## replication-twemproxy: primary-secondary replication with sentinel and twemproxy, it is just a demo for how to use twemproxy with redis, not recommended for production
1516
## cluster: official redis cluster, refer: https://redis.io/docs/reference/cluster-spec. if mode is cluster, please set useLegacyCompDef to false.
1617
mode: replication
1718

addons/redis/Chart.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ keywords:
1515
- database
1616
- nosql
1717
- replication
18+
- redis cluster
1819

1920
maintainers:
2021
- name: ApeCloud

addons/redis/scripts/redis-sentinel-setup.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ set -ex
1313
{{- $primary_pod := "" }}
1414
{{- range $i, $e := $.cluster.spec.componentSpecs }}
1515
{{- if index $e "componentDefRef" }}
16-
{{- if eq $e.componentDefRef "redis-sentinel" }}
16+
{{- /* xxxx-7.0 suffix just for compatible config render do not panic, this script has been deprecated in KubeBlock v0.10 */}}
17+
{{- if eq $e.componentDefRef "redis-sentinel-7.0" }}
1718
{{- $sentinel_component = $e }}
18-
{{- else if eq $e.componentDefRef "redis" }}
19+
{{- else if eq $e.componentDefRef "redis-7.0" }}
1920
{{- $redis_component = $e }}
2021
{{- end }}
2122
{{- end }}
2223
{{- if index $e "componentDef" }}
23-
{{- if eq $e.componentDef "redis-sentinel" }}
24+
{{- if eq $e.componentDef "redis-sentinel-7.0" }}
2425
{{- $sentinel_component = $e }}
25-
{{- else if eq $e.componentDef "redis" }}
26+
{{- else if eq $e.componentDef "redis-7.0" }}
2627
{{- $redis_component = $e }}
2728
{{- end }}
2829
{{- end }}

addons/redis/scripts/redis-sentinel-start.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ set -ex
99
{{- $redis_component := fromJson "{}" }}
1010
{{- range $i, $e := $.cluster.spec.componentSpecs }}
1111
{{- if index $e "componentDefRef" }}
12-
{{- if eq $e.componentDefRef "redis" }}
12+
{{- if eq $e.componentDefRef "redis-7.0" }}
1313
{{- $redis_component = $e }}
1414
{{- end }}
1515
{{- end }}
1616
{{- if index $e "componentDef" }}
17-
{{- if eq $e.componentDef "redis" }}
17+
{{- if eq $e.componentDef "redis-7.0" }}
1818
{{- $redis_component = $e }}
1919
{{- end }}
2020
{{- end }}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/sh
2+
set -ex
3+
4+
build_redis_twemproxy_conf() {
5+
## check REDIS_SERVICE_NAME and REDIS_SERVICE_PORT env exist
6+
if [ -z "$REDIS_SERVICE_NAME" ] || [ -z "$REDIS_SERVICE_PORT" ]; then
7+
echo "REDIS_SERVICE_NAME and REDIS_SERVICE_PORT must be set"
8+
exit 1
9+
fi
10+
11+
echo "alpha:" > /etc/proxy/nutcracker.conf
12+
# shellcheck disable=SC2129
13+
echo " listen: 127.0.0.1:22121" >> /etc/proxy/nutcracker.conf
14+
echo " hash: fnv1a_64" >> /etc/proxy/nutcracker.conf
15+
echo " distribution: ketama" >> /etc/proxy/nutcracker.conf
16+
echo " auto_eject_hosts: true" >> /etc/proxy/nutcracker.conf
17+
echo " redis: true" >> /etc/proxy/nutcracker.conf
18+
echo " server_retry_timeout: 2000" >> /etc/proxy/nutcracker.conf
19+
echo " server_failure_limit: 1" >> /etc/proxy/nutcracker.conf
20+
echo " servers:" >> /etc/proxy/nutcracker.conf
21+
echo " - $REDIS_SERVICE_NAME:$REDIS_SERVICE_PORT:1 $KB_CLUSTER_NAME" >> /etc/proxy/nutcracker.conf
22+
}
23+
24+
build_redis_twemproxy_conf

addons/redis/scripts/redis-twemproxy-setup.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ set -ex
77
{{- $redis_component := fromJson "{}" }}
88
{{- range $i, $e := $.cluster.spec.componentSpecs }}
99
{{- if index $e "componentDefRef" }}
10-
{{- if eq $e.componentDefRef "redis-twemproxy" }}
10+
{{- if eq $e.componentDefRef "redis-twemproxy-0.5" }}
1111
{{- $proxy_component = $e }}
12-
{{- else if eq $e.componentDefRef "redis" }}
12+
{{- else if eq $e.componentDefRef "redis-7.0" }}
1313
{{- $redis_component = $e }}
1414
{{- end }}
1515
{{- end }}
1616
{{- if index $e "componentDef" }}
17-
{{- if eq $e.componentDef "redis-twemproxy" }}
17+
{{- if eq $e.componentDef "redis-twemproxy-0.5" }}
1818
{{- $proxy_component = $e }}
19-
{{- else if eq $e.componentDef "redis" }}
19+
{{- else if eq $e.componentDef "redis-7.0" }}
2020
{{- $redis_component = $e }}
2121
{{- end }}
2222
{{- end }}

addons/redis/templates/_helpers.tpl

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,74 @@ Expand the name of the chart.
99
Define redis component defintion name
1010
*/}}
1111
{{- define "redis.componentDefName" -}}
12-
{{- if eq (len .Values.compDefinitionVersionSuffix) 0 -}}
12+
{{- if eq (len .Values.compDefinitionVersion.redis) 0 -}}
1313
redis
1414
{{- else -}}
15-
{{- printf "redis-%s" .Values.compDefinitionVersionSuffix -}}
15+
{{- printf "redis-%s" .Values.compDefinitionVersion.redis -}}
1616
{{- end -}}
1717
{{- end -}}
1818

19+
{{/*
20+
Define redis component defintion name prefix
21+
*/}}
22+
{{- define "redis.componentDefNamePrefix" -}}
23+
{{- printf "redis-%s" .Values.compDefinitionVersion.redis -}}
24+
{{- end -}}
25+
1926
{{/*
2027
Define redis-sentinel component defintion name
2128
*/}}
2229
{{- define "redis-sentinel.componentDefName" -}}
23-
{{- if eq (len .Values.compDefinitionVersionSuffix) 0 -}}
30+
{{- if eq (len .Values.compDefinitionVersion.sentinel) 0 -}}
2431
redis-sentinel
2532
{{- else -}}
26-
{{- printf "redis-sentinel-%s" .Values.compDefinitionVersionSuffix -}}
33+
{{- printf "redis-sentinel-%s" .Values.compDefinitionVersion.sentinel -}}
2734
{{- end -}}
2835
{{- end -}}
2936

37+
{{/*
38+
Define redis-sentinel component defintion name prefix
39+
*/}}
40+
{{- define "redis-sentinel.componentDefNamePrefix" -}}
41+
{{- printf "redis-sentinel-%s" .Values.compDefinitionVersion.sentinel -}}
42+
{{- end -}}
43+
3044
{{/*
3145
Define redis-cluster component defintion name
3246
*/}}
3347
{{- define "redis-cluster.componentDefName" -}}
34-
{{- if eq (len .Values.compDefinitionVersionSuffix) 0 -}}
48+
{{- if eq (len .Values.compDefinitionVersion.redisCluster) 0 -}}
3549
redis-cluster
3650
{{- else -}}
37-
{{- printf "redis-cluster-%s" .Values.compDefinitionVersionSuffix -}}
51+
{{- printf "redis-cluster-%s" .Values.compDefinitionVersion.redisCluster -}}
3852
{{- end -}}
3953
{{- end -}}
4054

55+
{{/*
56+
Define redis-cluster component defintion name prefix
57+
*/}}
58+
{{- define "redis-cluster.componentDefNamePrefix" -}}
59+
{{- printf "redis-cluster-%s" .Values.compDefinitionVersion.redisCluster -}}
60+
{{- end -}}
61+
4162
{{/*
4263
Define redis-twemproxy component defintion name
4364
*/}}
4465
{{- define "redis-twemproxy.componentDefName" -}}
45-
{{- if eq (len .Values.compDefinitionVersionSuffix) 0 -}}
66+
{{- if eq (len .Values.compDefinitionVersion.twemproxy) 0 -}}
4667
redis-twemproxy
4768
{{- else -}}
48-
{{- printf "redis-twemproxy-%s" .Values.compDefinitionVersionSuffix -}}
69+
{{- printf "redis-twemproxy-%s" .Values.compDefinitionVersion.twemproxy -}}
4970
{{- end -}}
5071
{{- end -}}
5172

73+
{{/*
74+
Define redis-twemproxy component defintion name prefix
75+
*/}}
76+
{{- define "redis-twemproxy.componentDefNamePrefix" -}}
77+
{{- printf "redis-twemproxy-%s" .Values.compDefinitionVersion.twemproxy -}}
78+
{{- end -}}
79+
5280
{{/*
5381
Create a default fully qualified app name.
5482
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
@@ -105,7 +133,7 @@ Define image
105133
{{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.repository }}:{{ .Values.image.tag }}
106134
{{- end }}
107135

108-
{{- define "redis-proxy.image" -}}
136+
{{- define "redis-twemproxy.image" -}}
109137
{{ .Values.redisTwemproxyImage.registry | default "docker.io" }}/{{ .Values.redisTwemproxyImage.repository }}:{{ .Values.redisTwemproxyImage.tag }}
110138
{{- end }}
111139

addons/redis/templates/clusterdefinition.yaml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,3 +403,46 @@ spec:
403403
- sh
404404
- -c
405405
- /scripts/redis-sentinel-ping.sh 1
406+
topologies:
407+
- name: standalone
408+
components:
409+
- name: redis
410+
compDef: {{ include "redis.componentDefNamePrefix" . }}
411+
default: true
412+
- name: replication
413+
components:
414+
- name: redis
415+
compDef: {{ include "redis.componentDefNamePrefix" . }}
416+
- name: redis-sentinel
417+
compDef: {{ include "redis-sentinel.componentDefNamePrefix" . }}
418+
orders:
419+
provision:
420+
- redis-sentinel
421+
- redis
422+
terminate:
423+
- redis-sentinel
424+
- redis
425+
update:
426+
- redis-sentinel
427+
- redis
428+
- name: replication-twemproxy
429+
components:
430+
- name: redis
431+
compDef: {{ include "redis.componentDefNamePrefix" . }}
432+
- name: redis-sentinel
433+
compDef: {{ include "redis-sentinel.componentDefNamePrefix" . }}
434+
- name: redis-twemproxy
435+
compDef: {{ include "redis-twemproxy.componentDefNamePrefix" . }}
436+
orders:
437+
provision:
438+
- redis-sentinel
439+
- redis
440+
- redis-twemproxy
441+
terminate:
442+
- redis-twemproxy
443+
- redis-sentinel
444+
- redis
445+
update:
446+
- redis-sentinel
447+
- redis-twemproxy
448+
- redis

addons/redis/templates/clusterversion.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ spec:
3434
imagePullPolicy: {{ default .Values.busyboxImage.pullPolicy "IfNotPresent" }}
3535
containers:
3636
- name: redis-twemproxy
37-
image: {{ include "redis-proxy.image" . }}
37+
image: {{ include "redis-twemproxy.image" . }}
3838
imagePullPolicy: {{ default .Values.redisTwemproxyImage.pullPolicy "IfNotPresent" }}

0 commit comments

Comments
 (0)