Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

Commit db5c992

Browse files
authored
Use the proper featured Zookeeper config generation script (#3688)
* Updates to make Zookeeper configurable through the Helm chart logic
1 parent 103cf19 commit db5c992

File tree

4 files changed

+161
-52
lines changed

4 files changed

+161
-52
lines changed

deploy/kubernetes/helm/templates/zookeeper.yaml

-4
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,6 @@ spec:
118118
value: "{{ .Values.zookeeper.serverPort }}"
119119
- name: ZK_ELECTION_PORT
120120
value: "{{ .Values.zookeeper.leaderElectionPort }}"
121-
- name: ZOOKEEPER_SERVERS
122-
{{- $replicaCount := int .Values.zkReplicas }}
123-
{{- $fullName := printf "%s-%s" .Release.Name "zookeeper" }}
124-
value: "{{ range $i, $e := until $replicaCount }}{{ $fullName }}-{{ $e }},{{ end }}"
125121
command:
126122
- sh
127123
- -c

docker/base/scripts/generate-zookeeper-config.sh

+16-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
#!/usr/bin/env bash
2-
# Copyright 2016 The Kubernetes Authors.
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
39
#
4-
# Licensed under the Apache License, Version 2.0 (the "License");
5-
# you may not use this file except in compliance with the License.
6-
# You may obtain a copy of the License at
10+
# http://www.apache.org/licenses/LICENSE-2.0
711
#
8-
# http://www.apache.org/licenses/LICENSE-2.0
9-
#
10-
# Unless required by applicable law or agreed to in writing, software
11-
# distributed under the License is distributed on an "AS IS" BASIS,
12-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
# See the License for the specific language governing permissions and
14-
# limitations under the License.
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
1518

1619
ZK_USER=${ZK_USER:-"zookeeper"}
1720
ZK_LOG_LEVEL=${ZK_LOG_LEVEL:-"INFO"}
@@ -98,6 +101,8 @@ function create_config() {
98101
echo "maxSessionTimeout=$ZK_MAX_SESSION_TIMEOUT" >> $ZK_CONFIG_FILE
99102
echo "autopurge.snapRetainCount=$ZK_SNAP_RETAIN_COUNT" >> $ZK_CONFIG_FILE
100103
echo "autopurge.purgeInteval=$ZK_PURGE_INTERVAL" >> $ZK_CONFIG_FILE
104+
echo "# Enable all four letter word commands by default" >> $ZK_CONFIG_FILE
105+
echo "4lw.commands.whitelist=*" >> $ZK_CONFIG_FILE
101106

102107
if [ $ZK_REPLICAS -gt 1 ]; then
103108
print_servers >> $ZK_CONFIG_FILE

docker/base/scripts/install-zookeeper.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ rm -rf \
4040
# copy zk scripts
4141
mkdir -p /opt/zookeeper/scripts
4242
cp /opt/heron-docker/scripts/generate-zookeeper-config.sh /opt/zookeeper/scripts/
43-
chmod +x /opt/heron-docker/scripts/generate-zookeeper-config.sh
43+
chmod +x /opt/zookeeper/scripts/generate-zookeeper-config.sh
4444
cp /opt/heron-docker/scripts/zookeeper-ruok.sh /opt/zookeeper/scripts/
45-
chmod +x /opt/heron-docker/scripts/zookeeper-ruok.sh
45+
chmod +x /opt/zookeeper/scripts/zookeeper-ruok.sh
4646
cp /opt/heron-docker/scripts/start-zookeeper.sh /opt/zookeeper/scripts/
47-
chmod +x /opt/heron-docker/scripts/start-zookeeper.sh
47+
chmod +x /opt/zookeeper/scripts/start-zookeeper.sh
4848
cp /opt/heron-docker/scripts/wait-for-zookeeper.sh /opt/zookeeper/scripts/
49-
chmod +x /opt/heron-docker/scripts/wait-for-zookeeper.sh
49+
chmod +x /opt/zookeeper/scripts/wait-for-zookeeper.sh

docker/dist/scripts/generate-zookeeper-config.sh

+141-33
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,157 @@
66
# to you under the Apache License, Version 2.0 (the
77
# "License"); you may not use this file except in compliance
88
# with the License. You may obtain a copy of the License at
9-
#
9+
#
1010
# http://www.apache.org/licenses/LICENSE-2.0
11-
#
11+
#
1212
# Unless required by applicable law or agreed to in writing,
1313
# software distributed under the License is distributed on an
1414
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1515
# KIND, either express or implied. See the License for the
1616
# specific language governing permissions and limitations
1717
# under the License.
1818

19-
# Apply env variables to config file and start the regular command
19+
ZK_USER=${ZK_USER:-"zookeeper"}
20+
ZK_LOG_LEVEL=${ZK_LOG_LEVEL:-"INFO"}
21+
ZK_DATA_DIR=${ZK_DATA_DIR:-"/var/lib/zookeeper/data"}
22+
ZK_DATA_LOG_DIR=${ZK_DATA_LOG_DIR:-"/var/lib/zookeeper/log"}
23+
ZK_LOG_DIR=${ZK_LOG_DIR:-"var/log/zookeeper"}
24+
ZK_CONF_DIR=${ZK_CONF_DIR:-"/opt/zookeeper/conf"}
25+
ZK_CLIENT_PORT=${ZK_CLIENT_PORT:-2181}
26+
ZK_SERVER_PORT=${ZK_SERVER_PORT:-2888}
27+
ZK_ELECTION_PORT=${ZK_ELECTION_PORT:-3888}
28+
ZK_TICK_TIME=${ZK_TICK_TIME:-2000}
29+
ZK_INIT_LIMIT=${ZK_INIT_LIMIT:-10}
30+
ZK_SYNC_LIMIT=${ZK_SYNC_LIMIT:-5}
31+
ZK_HEAP_SIZE=${ZK_HEAP_SIZE:-2G}
32+
ZK_MAX_CLIENT_CNXNS=${ZK_MAX_CLIENT_CNXNS:-60}
33+
ZK_MIN_SESSION_TIMEOUT=${ZK_MIN_SESSION_TIMEOUT:-$((ZK_TICK_TIME*2))}
34+
ZK_MAX_SESSION_TIMEOUT=${ZK_MAX_SESSION_TIMEOUT:-$((ZK_TICK_TIME*20))}
35+
ZK_SNAP_RETAIN_COUNT=${ZK_SNAP_RETAIN_COUNT:-3}
36+
ZK_PURGE_INTERVAL=${ZK_PURGE_INTERVAL:-0}
37+
ID_FILE="$ZK_DATA_DIR/myid"
38+
ZK_CONFIG_FILE="$ZK_CONF_DIR/zookeeper.conf"
39+
LOGGER_PROPS_FILE="$ZK_CONF_DIR/log4j.properties"
40+
JAVA_ENV_FILE="$ZK_CONF_DIR/java.env"
41+
HOST=`hostname -s`
42+
DOMAIN=`hostname -d`
2043

21-
CONF_FILE=$1
44+
function print_servers() {
45+
for (( i=1; i<=$ZK_REPLICAS; i++ ))
46+
do
47+
echo "server.$i=$NAME-$((i-1)).$DOMAIN:$ZK_SERVER_PORT:$ZK_ELECTION_PORT"
48+
done
49+
}
2250

23-
if [ $? != 0 ]; then
24-
echo "Error: Failed to apply changes to config file"
25-
exit 1
26-
fi
51+
function validate_env() {
52+
echo "Validating enviornment"
53+
if [ -z $ZK_REPLICAS ]; then
54+
echo "ZK_REPLICAS is a mandatory environment variable"
55+
exit 1
56+
fi
2757

28-
DOMAIN=`hostname -d`
58+
if [[ $HOST =~ (.*)-([0-9]+)$ ]]; then
59+
NAME=${BASH_REMATCH[1]}
60+
ORD=${BASH_REMATCH[2]}
61+
else
62+
echo "Failed to extract ordinal from hostname $HOST"
63+
exit 1
64+
fi
65+
MY_ID=$((ORD+1))
66+
echo "ZK_REPLICAS=$ZK_REPLICAS"
67+
echo "MY_ID=$MY_ID"
68+
echo "ZK_LOG_LEVEL=$ZK_LOG_LEVEL"
69+
echo "ZK_DATA_DIR=$ZK_DATA_DIR"
70+
echo "ZK_DATA_LOG_DIR=$ZK_DATA_LOG_DIR"
71+
echo "ZK_LOG_DIR=$ZK_LOG_DIR"
72+
echo "ZK_CLIENT_PORT=$ZK_CLIENT_PORT"
73+
echo "ZK_SERVER_PORT=$ZK_SERVER_PORT"
74+
echo "ZK_ELECTION_PORT=$ZK_ELECTION_PORT"
75+
echo "ZK_TICK_TIME=$ZK_TICK_TIME"
76+
echo "ZK_INIT_LIMIT=$ZK_INIT_LIMIT"
77+
echo "ZK_SYNC_LIMIT=$ZK_SYNC_LIMIT"
78+
echo "ZK_MAX_CLIENT_CNXNS=$ZK_MAX_CLIENT_CNXNS"
79+
echo "ZK_MIN_SESSION_TIMEOUT=$ZK_MIN_SESSION_TIMEOUT"
80+
echo "ZK_MAX_SESSION_TIMEOUT=$ZK_MAX_SESSION_TIMEOUT"
81+
echo "ZK_HEAP_SIZE=$ZK_HEAP_SIZE"
82+
echo "ZK_SNAP_RETAIN_COUNT=$ZK_SNAP_RETAIN_COUNT"
83+
echo "ZK_PURGE_INTERVAL=$ZK_PURGE_INTERVAL"
84+
echo "ENSEMBLE"
85+
print_servers
86+
echo "Enviorment validation successful"
87+
}
88+
89+
function create_config() {
90+
rm -f $ZK_CONFIG_FILE
91+
echo "Creating ZooKeeper configuration"
92+
echo "#This file was autogenerated by k8szk DO NOT EDIT" >> $ZK_CONFIG_FILE
93+
echo "clientPort=$ZK_CLIENT_PORT" >> $ZK_CONFIG_FILE
94+
echo "dataDir=$ZK_DATA_DIR" >> $ZK_CONFIG_FILE
95+
echo "dataLogDir=$ZK_DATA_LOG_DIR" >> $ZK_CONFIG_FILE
96+
echo "tickTime=$ZK_TICK_TIME" >> $ZK_CONFIG_FILE
97+
echo "initLimit=$ZK_INIT_LIMIT" >> $ZK_CONFIG_FILE
98+
echo "syncLimit=$ZK_SYNC_LIMIT" >> $ZK_CONFIG_FILE
99+
echo "maxClientCnxns=$ZK_MAX_CLIENT_CNXNS" >> $ZK_CONFIG_FILE
100+
echo "minSessionTimeout=$ZK_MIN_SESSION_TIMEOUT" >> $ZK_CONFIG_FILE
101+
echo "maxSessionTimeout=$ZK_MAX_SESSION_TIMEOUT" >> $ZK_CONFIG_FILE
102+
echo "autopurge.snapRetainCount=$ZK_SNAP_RETAIN_COUNT" >> $ZK_CONFIG_FILE
103+
echo "autopurge.purgeInteval=$ZK_PURGE_INTERVAL" >> $ZK_CONFIG_FILE
104+
echo "# Enable all four letter word commands by default" >> $ZK_CONFIG_FILE
105+
echo "4lw.commands.whitelist=*" >> $ZK_CONFIG_FILE
106+
107+
if [ $ZK_REPLICAS -gt 1 ]; then
108+
print_servers >> $ZK_CONFIG_FILE
109+
fi
110+
echo "Wrote ZooKeeper configuration file to $ZK_CONFIG_FILE"
111+
}
112+
113+
function create_data_dirs() {
114+
echo "Creating ZooKeeper data directories and setting permissions"
115+
if [ ! -d $ZK_DATA_DIR ]; then
116+
mkdir -p $ZK_DATA_DIR
117+
chown -R $ZK_USER:$ZK_USER $ZK_DATA_DIR
118+
fi
29119

30-
# Generate list of servers and detect the current server ID,
31-
# based on the hostname
32-
IDX=1
33-
for SERVER in $(echo $ZOOKEEPER_SERVERS | tr "," "\n")
34-
do
35-
echo "server.$IDX=$SERVER.$DOMAIN:2888:3888" >> $CONF_FILE
36-
37-
if [ "$HOSTNAME" == "$SERVER" ]; then
38-
MY_ID=$IDX
39-
echo "Current server id $MY_ID"
40-
fi
41-
42-
((IDX++))
43-
done
44-
45-
# For ZooKeeper container we need to initialize the ZK id
46-
if [ ! -z "$MY_ID" ]; then
47-
# Get ZK data dir
48-
DATA_DIR=`grep '^dataDir=' $CONF_FILE | awk -F= '{print $2}'`
49-
if [ ! -e $DATA_DIR/myid ]; then
50-
echo "Creating $DATA_DIR/myid with id = $MY_ID"
51-
mkdir -p $DATA_DIR
52-
echo $MY_ID > $DATA_DIR/myid
53-
fi
120+
if [ ! -d $ZK_DATA_LOG_DIR ]; then
121+
mkdir -p $ZK_DATA_LOG_DIR
122+
chown -R $ZK_USER:$ZK_USER $ZK_DATA_LOG_DIR
123+
fi
124+
125+
if [ ! -d $ZK_LOG_DIR ]; then
126+
mkdir -p $ZK_LOG_DIR
127+
chown -R $ZK_USER:$ZK_USER $ZK_LOG_DIR
128+
fi
129+
if [ ! -f $ID_FILE ]; then
130+
echo $MY_ID >> $ID_FILE
131+
fi
132+
echo "Created ZooKeeper data directories and set permissions in $ZK_DATA_DIR"
133+
}
134+
135+
function create_log_props () {
136+
rm -f $LOGGER_PROPS_FILE
137+
echo "Creating ZooKeeper log4j configuration"
138+
echo "zookeeper.root.logger=CONSOLE" >> $LOGGER_PROPS_FILE
139+
echo "zookeeper.console.threshold="$ZK_LOG_LEVEL >> $LOGGER_PROPS_FILE
140+
echo "log4j.rootLogger=\${zookeeper.root.logger}" >> $LOGGER_PROPS_FILE
141+
echo "log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender" >> $LOGGER_PROPS_FILE
142+
echo "log4j.appender.CONSOLE.Threshold=\${zookeeper.console.threshold}" >> $LOGGER_PROPS_FILE
143+
echo "log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout" >> $LOGGER_PROPS_FILE
144+
echo "log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n" >> $LOGGER_PROPS_FILE
145+
echo "Wrote log4j configuration to $LOGGER_PROPS_FILE"
146+
}
147+
148+
function create_java_env() {
149+
rm -f $JAVA_ENV_FILE
150+
echo "Creating JVM configuration file"
151+
echo "ZOO_LOG_DIR=$ZK_LOG_DIR" >> $JAVA_ENV_FILE
152+
echo "JVMFLAGS=\"-Xmx$ZK_HEAP_SIZE -Xms$ZK_HEAP_SIZE\"" >> $JAVA_ENV_FILE
153+
echo "Wrote JVM configuration to $JAVA_ENV_FILE"
154+
}
155+
156+
if [ "$#" -ne 1 ]; then
157+
echo "Usage: $0 <configuration-file>"
158+
exit 1
54159
fi
160+
161+
ZK_CONFIG_FILE=$1
162+
validate_env && create_config && create_log_props && create_data_dirs && create_java_env

0 commit comments

Comments
 (0)