Skip to content

Commit 28de9a7

Browse files
authored
feat[elasticsearch]: support zone awareness shard allocation (#2185)
1 parent 1592733 commit 28de9a7

File tree

12 files changed

+152
-13
lines changed

12 files changed

+152
-13
lines changed

addons/elasticsearch/configs/elasticsearch-6.yml.tpl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{{- $clusterName := .CLUSTER_NAME }}
22
{{- $defaultRoles := .ELASTICSEARCH_ROLES }}
33
{{- $namespace := .CLUSTER_NAMESPACE }}
4+
{{- $zoneAwareEnabled := .ZONE_AWARE_ENABLED }}
45

56
{{- $mode := "multi-node" }}
67
{{- if index . "mode" }}
@@ -17,7 +18,14 @@ cluster:
1718
routing:
1819
allocation:
1920
awareness:
21+
{{- if eq $zoneAwareEnabled "true" }}
22+
attributes: zone,k8s_node_name
23+
force:
24+
zone:
25+
values: ${ALL_ZONES}
26+
{{- else }}
2027
attributes: k8s_node_name
28+
{{- end }}
2129
# In ES 6.x, discovery configuration is handled differently
2230
discovery:
2331
{{- if eq $mode "multi-node" }}
@@ -64,6 +72,9 @@ network:
6472
node:
6573
attr:
6674
k8s_node_name: ${NODE_NAME}
75+
{{- if eq $zoneAwareEnabled "true" }}
76+
zone: ${CURRENT_ZONE}
77+
{{- end }}
6778
name: ${POD_NAME}
6879
store:
6980
allow_mmap: false

addons/elasticsearch/configs/elasticsearch-7.yml.tpl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{{- $clusterName := .CLUSTER_NAME }}
22
{{- $defaultRoles := .ELASTICSEARCH_ROLES }}
33
{{- $namespace := .CLUSTER_NAMESPACE }}
4+
{{- $zoneAwareEnabled := .ZONE_AWARE_ENABLED }}
45

56
{{- $mode := "multi-node" }}
67
{{- if index . "mode" }}
@@ -17,7 +18,14 @@ cluster:
1718
routing:
1819
allocation:
1920
awareness:
21+
{{- if eq $zoneAwareEnabled "true" }}
22+
attributes: zone,k8s_node_name
23+
force:
24+
zone:
25+
values: ${ALL_ZONES}
26+
{{- else }}
2027
attributes: k8s_node_name
28+
{{- end }}
2129
# INITIAL_MASTER_NODES_BLOCK_START
2230
{{- if eq $mode "multi-node" }}
2331
initial_master_nodes:
@@ -67,6 +75,9 @@ network:
6775
node:
6876
attr:
6977
k8s_node_name: ${NODE_NAME}
78+
{{- if eq $zoneAwareEnabled "true" }}
79+
zone: ${CURRENT_ZONE}
80+
{{- end }}
7081
name: ${POD_NAME}
7182
store:
7283
allow_mmap: false

addons/elasticsearch/configs/elasticsearch-8.yml.tpl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{{- $clusterName := .CLUSTER_NAME }}
22
{{- $defaultRoles := .ELASTICSEARCH_ROLES }}
33
{{- $namespace := .CLUSTER_NAMESPACE }}
4+
{{- $zoneAwareEnabled := .ZONE_AWARE_ENABLED }}
45

56
{{- $mode := "multi-node" }}
67
{{- if index . "mode" }}
@@ -12,7 +13,14 @@ cluster:
1213
routing:
1314
allocation:
1415
awareness:
16+
{{- if eq $zoneAwareEnabled "true" }}
17+
attributes: zone,k8s_node_name
18+
force:
19+
zone:
20+
values: ${ALL_ZONES}
21+
{{- else }}
1522
attributes: k8s_node_name
23+
{{- end }}
1624
# INITIAL_MASTER_NODES_BLOCK_START
1725
{{- if eq $mode "multi-node" }}
1826
initial_master_nodes:
@@ -62,6 +70,9 @@ network:
6270
node:
6371
attr:
6472
k8s_node_name: ${NODE_NAME}
73+
{{- if eq $zoneAwareEnabled "true" }}
74+
zone: ${CURRENT_ZONE}
75+
{{- end }}
6576
name: ${POD_NAME}
6677
store:
6778
allow_mmap: false
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env sh
2+
cp /etc/pki/tls/* /usr/share/elasticsearch/config/
3+
# Parse zone from zone-aware-mapping file based on NODE_NAME
4+
# Zone-aware-mapping file format: YAML format with zone name as key and comma-separated node names as value
5+
# Empty node list is allowed for a zone
6+
# Example:
7+
# zone1: node1,node2
8+
# zone2: node3,node4,node5
9+
# zone3:
10+
ZONE_AWARE_MAPPING_FILE="/mnt/zone-aware-mapping/mapping"
11+
if [ "${ZONE_AWARE_ENABLED}" = "true" ]; then
12+
if [ ! -f "${ZONE_AWARE_MAPPING_FILE}" ]; then
13+
echo "Error: ZONE_AWARE_ENABLED is true but zone-aware-mapping file not found at ${ZONE_AWARE_MAPPING_FILE}"
14+
exit 1
15+
fi
16+
if [ -z "${NODE_NAME}" ]; then
17+
echo "Error: ZONE_AWARE_ENABLED is true but NODE_NAME is not set"
18+
exit 1
19+
fi
20+
CURRENT_ZONE=""
21+
ZONES=()
22+
# Parse YAML format: each line is "zone_name: node1,node2,..."
23+
while IFS= read -r LINE || [ -n "${LINE}" ]; do
24+
# Skip empty lines and comments
25+
LINE="${LINE%%#*}" # Remove comments
26+
LINE=$(echo "${LINE}" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') # Trim whitespace
27+
if [ -z "${LINE}" ]; then
28+
continue
29+
fi
30+
# Split by first colon to get zone name and node list
31+
ZONE_NAME="${LINE%%:*}"
32+
NODE_LIST="${LINE#*:}"
33+
# Trim whitespace
34+
ZONE_NAME=$(echo "${ZONE_NAME}" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
35+
NODE_LIST=$(echo "${NODE_LIST}" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
36+
# Skip if zone name is empty (node list can be empty)
37+
if [ -z "${ZONE_NAME}" ]; then
38+
continue
39+
fi
40+
# Add zone to ZONES array (even if node list is empty)
41+
ZONES+=("${ZONE_NAME}")
42+
# Check if current node is in this zone (only if not found yet and node list is not empty)
43+
if [ -z "${CURRENT_ZONE}" ] && [ -n "${NODE_LIST}" ]; then
44+
IFS=',' read -ra NODES <<< "${NODE_LIST}"
45+
for NODE in "${NODES[@]}"; do
46+
NODE_TRIMMED=$(echo "${NODE}" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
47+
if [ -n "${NODE_TRIMMED}" ] && [ "${NODE_TRIMMED}" = "${NODE_NAME}" ]; then
48+
export CURRENT_ZONE="${ZONE_NAME}"
49+
break
50+
fi
51+
done
52+
fi
53+
done < "${ZONE_AWARE_MAPPING_FILE}"
54+
if [ -z "${CURRENT_ZONE}" ]; then
55+
echo "Error: ZONE_AWARE_ENABLED is true but failed to find zone for node ${NODE_NAME} in zone-aware-mapping file"
56+
exit 1
57+
fi
58+
echo "CURRENT_ZONE: ${CURRENT_ZONE}"
59+
# Check if ZONES is empty
60+
if [ ${#ZONES[@]} -eq 0 ]; then
61+
echo "Error: ZONE_AWARE_ENABLED is true but failed to find zones in zone-aware-mapping file"
62+
exit 1
63+
fi
64+
export ALL_ZONES=$(IFS=,; echo "${ZONES[*]}")
65+
echo "ALL_ZONES: ${ALL_ZONES}"
66+
fi
67+
68+
# remove initial master nodes block if cluster has been formed
69+
if [ -f "${CLUSTER_FORMED_FILE}" ]; then
70+
sed -i '/# INITIAL_MASTER_NODES_BLOCK_START/,/# INITIAL_MASTER_NODES_BLOCK_END/d' config/elasticsearch.yml
71+
fi
72+
if [ -f /bin/tini ]; then
73+
/bin/tini -- /usr/local/bin/docker-entrypoint.sh
74+
elif [ -f /tini ]; then
75+
/tini -- /usr/local/bin/docker-entrypoint.sh
76+
else
77+
/usr/local/bin/docker-entrypoint.sh
78+
fi

addons/elasticsearch/templates/_helpers.tpl

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -392,19 +392,7 @@ runtime:
392392
- sh
393393
- -c
394394
- |
395-
cp /etc/pki/tls/* /usr/share/elasticsearch/config/
396-
# remove initial master nodes block if cluster has been formed
397-
if [ -f "${CLUSTER_FORMED_FILE}" ]; then
398-
sed -i '/# INITIAL_MASTER_NODES_BLOCK_START/,/# INITIAL_MASTER_NODES_BLOCK_END/d' config/elasticsearch.yml
399-
fi
400-
if [ -f /bin/tini ]; then
401-
/bin/tini -- /usr/local/bin/docker-entrypoint.sh
402-
elif [ -f /tini ]; then
403-
/tini -- /usr/local/bin/docker-entrypoint.sh
404-
else
405-
/usr/local/bin/docker-entrypoint.sh
406-
fi
407-
395+
{{- .Files.Get "scripts/entrypoint.sh" | nindent 10 }}
408396
env:
409397
- name: POD_IP
410398
valueFrom:
@@ -495,6 +483,11 @@ runtime:
495483
readOnly: true
496484
- mountPath: /tmp
497485
name: tmp-volume
486+
{{- if .Values.zoneAware.enabled }}
487+
- mountPath: /mnt/zone-aware-mapping
488+
name: zone-aware-mapping
489+
readOnly: true
490+
{{- end }}
498491
- name: exporter
499492
command:
500493
- /bin/elasticsearch_exporter
@@ -607,6 +600,11 @@ runtime:
607600
name: local-plugins
608601
- emptyDir: { }
609602
name: plugins
603+
{{- if .Values.zoneAware.enabled }}
604+
- name: zone-aware-mapping
605+
configMap:
606+
name: {{ .Values.zoneAware.configMap }}
607+
{{- end }}
610608
{{- end }}
611609
612610
{{- define "kibana.common" }}

addons/elasticsearch/templates/cmpd-es-data-6.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,6 @@ spec:
100100
valueFrom:
101101
tlsVarRef:
102102
enabled: Optional
103+
- name: ZONE_AWARE_ENABLED
104+
value: {{ .Values.zoneAware.enabled | quote }}
103105
{{ include "elasticsearch.common" . | nindent 2 }}

addons/elasticsearch/templates/cmpd-es-data-7.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,6 @@ spec:
9898
valueFrom:
9999
tlsVarRef:
100100
enabled: Optional
101+
- name: ZONE_AWARE_ENABLED
102+
value: {{ .Values.zoneAware.enabled | quote }}
101103
{{ include "elasticsearch.common" . | nindent 2 }}

addons/elasticsearch/templates/cmpd-es-data-8.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,6 @@ spec:
9898
valueFrom:
9999
tlsVarRef:
100100
enabled: Optional
101+
- name: ZONE_AWARE_ENABLED
102+
value: {{ .Values.zoneAware.enabled | quote }}
101103
{{ include "elasticsearch.common" . | nindent 2 }}

addons/elasticsearch/templates/cmpd-es-master-6.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,6 @@ spec:
9797
valueFrom:
9898
tlsVarRef:
9999
enabled: Optional
100+
- name: ZONE_AWARE_ENABLED
101+
value: {{ .Values.zoneAware.enabled | quote }}
100102
{{ include "elasticsearch.common" . | nindent 2 }}

addons/elasticsearch/templates/cmpd-es-master-7.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,6 @@ spec:
9595
valueFrom:
9696
tlsVarRef:
9797
enabled: Optional
98+
- name: ZONE_AWARE_ENABLED
99+
value: {{ .Values.zoneAware.enabled | quote }}
98100
{{ include "elasticsearch.common" . | nindent 2 }}

0 commit comments

Comments
 (0)