Skip to content

Commit 0705f87

Browse files
author
hongchunhua
committed
feat:增加故障域参数
1 parent bb57a11 commit 0705f87

File tree

8 files changed

+923
-825
lines changed

8 files changed

+923
-825
lines changed

Dockerfile

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ ARG PYTHON_VERSION=latest
44
FROM python:${PYTHON_VERSION}
55

66
WORKDIR /app
7-
COPY zksDaemon.py /app
8-
COPY init.sh /app
9-
RUN pip3 install kazoo && \
10-
chmod +x /app/init.sh
11-
12-
ENTRYPOINT ["/app/init.sh"]
7+
COPY zks_daemon.py /app
8+
RUN pip3 install kazoo
9+
ENTRYPOINT ["/usr/local/bin/python3", "/app/zks_daemon.py"]

Dockerfile_2

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
FROM zksdaemon:1.0.0
3+
4+
WORKDIR /app
5+
COPY zks_daemon.py /app/
6+
ENTRYPOINT ["/usr/local/bin/python3", "/app/zks_daemon.py"]

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ debian:
1111

1212
### 2.获取镜像
1313

14-
获取zookeeper镜像,如3.6.3
14+
获取zookeeper镜像,如3.5.6
1515
获取python镜像,如python:latest
1616

17-
docker pull zookeeper:3.6.3
17+
docker pull zookeeper:3.5.6
1818
docker pull python:latest
1919

2020
### 3.打包本地镜像

cluster.sh

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,24 @@ WORK_DIR=`pwd`
66
NODE_NUM=3
77
CLUSTER_ID=2
88
ZKS_DAEMON_IMAGE='zksdaemon:latest'
9-
ZKS_IMAGE='zookeeper:3.6.3'
9+
ZKS_IMAGE='zookeeper:latest'
1010
IS_CLEAR=0
11+
CPORT='9639'
12+
QUORUM_PORT='2888'
13+
ELECTION_PORT='3888'
14+
DAEMON_OFF=0
1115

12-
while getopts "p:n:d:z:i:hc" arg
16+
while getopts "p:n:d:z:i:hco" arg
1317
do
1418
case $arg in
1519
c)
1620
IS_CLEAR=1
1721
echo "Cleaning cluster...."
1822
;;
23+
0)
24+
DAEMON_OFF=1
25+
echo "DAEMON_OFF...."
26+
;;
1927
p)
2028
WORK_DIR=$OPTARG
2129
;;
@@ -79,18 +87,20 @@ metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetr
7987
metricsProvider.httpPort=7000
8088
metricsProvider.exportJvmInfo=true
8189
[zookeeper]=
90+
cnxTimeout=200
8291
tickTime=2000
8392
dataDir=/data
8493
dataLogDir=/datalog
8594
reconfigEnabled=true
8695
preAllocSize=16384
87-
standaloneEnabled=false" > $zksDir/conf/zoo.cfg
96+
standaloneEnabled=false
97+
dynamicConfigFile=/conf/zoo.cfg.dynamic" > $zksDir/conf/zoo.cfg
8898
#这里可以考虑少于等于3个节点时,都是participant。
8999
if [ -z $cluster ];then
90-
echo -e "server.$myid=$myip:2888:3888:participant;0.0.0.0:$CPORT" >> $zksDir/conf/zoo.cfg
100+
echo -e "server.$myid=$myip:$QUORUM_PORT:$ELECTION_PORT;0.0.0.0:$CPORT" > $zksDir/conf/zoo.cfg.dynamic
91101
else
92-
echo -e "$cluster" >> $zksDir/conf/zoo.cfg
93-
echo -e "server.$myid=$myip:2888:3888:participant;0.0.0.0:$CPORT" >> $zksDir/conf/zoo.cfg
102+
echo -e "$cluster" > $zksDir/conf/zoo.cfg.dynamic
103+
echo -e "server.$myid=$myip:$QUORUM_PORT:$ELECTION_PORT;0.0.0.0:$CPORT" >> $zksDir/conf/zoo.cfg.dynamic
94104
fi
95105

96106
echo "export ZK_SERVER_HEAP=2048
@@ -208,7 +218,7 @@ function add_zks_daemon(){
208218
fi
209219
docker container stop $CLUSTER_NAME-$myid-daemon > /dev/null 2>&1
210220
docker rm $CLUSTER_NAME-$myid-daemon> /dev/null 2>&1
211-
cmd="docker run -dit --privileged=true --name=$CLUSTER_NAME-$myid-daemon --network=host $ZKS_DAEMON_IMAGE $myid $myhost"
221+
cmd="docker run -dit --privileged=true --name=$CLUSTER_NAME-$myid-daemon --network=host $ZKS_DAEMON_IMAGE -i $myid -h $myhost"
212222
$cmd
213223
if [ $? != 0 ];then
214224
echo "run daemon docker fail, [$cmd]"
@@ -232,7 +242,7 @@ function clear_cluster()
232242
CLUSTER="zks"
233243
CLUSTER_NAME="$CLUSTER.$CLUSTER_ID"
234244
NETWORK_ADDR="172.22.$CLUSTER_ID.0"
235-
CPORT='9639'
245+
236246

237247
echo "### create zookeeper cluster ###"
238248

@@ -304,15 +314,15 @@ do
304314
echo "[add_zks_node $g_myid $node_ip $server_info] fail..."
305315
exit 1
306316
fi
307-
308-
add_zks_daemon $g_myid $node_ip:$CPORT
309-
if [ $? != 0 ];then
310-
echo "[add_zks_daemon $g_myid $node_ip:$CPORT] fail..."
311-
exit 1
317+
if [ $DAEMON_OFF == 0 ];then
318+
add_zks_daemon $g_myid $node_ip:$CPORT
319+
if [ $? != 0 ];then
320+
echo "[add_zks_daemon $g_myid $node_ip:$CPORT] fail..."
321+
exit 1
322+
fi
312323
fi
313-
314324
#server_info=`echo conf|nc $node_ip $CPORT|grep 'server\.'|grep "$CPORT"`
315-
server_info="$server_info""server.$g_myid=$node_ip:2888:3888:participant;0.0.0.0:$CPORT\n"
325+
server_info="$server_info""server.$g_myid=$node_ip:$QUORUM_PORT:$ELECTION_PORT;0.0.0.0:$CPORT\n"
316326
echo -e "update cluster: $server_info"
317327

318328
let g_myid++

init.sh

Lines changed: 0 additions & 25 deletions
This file was deleted.

testcase.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# -*- encoding: utf-8 -*-
2+
'''
3+
@File : testcase.py
4+
@Time : 2021/09/22 15:48:29
5+
@Author : hongchunhua
6+
@Contact : 需要依赖python3、kazoo组件:pip3 install kazoo
7+
@License : (C)Copyright 2020-2025
8+
'''
9+
10+
import unittest
11+
from zks_daemon import *
12+
'''
13+
测试环境搭建:
14+
详细见README
15+
16+
'''
17+
class TestZksDaemon(unittest.TestCase):
18+
MY_ID = 1
19+
MY_HOST = '172.22.2.2:9639'
20+
21+
def setUp(self):
22+
print('setUp...')
23+
24+
def tearDown(self):
25+
print('tearDown...')
26+
27+
def test_init(self):
28+
with self.assertRaises(TypeError):
29+
zks = ZksDaemon()
30+
with self.assertRaises(TypeError):
31+
zks = ZksDaemon(myid=self.MY_ID)
32+
with self.assertRaises(TypeError):
33+
zks = ZksDaemon(myhost=self.MY_HOST)
34+
35+
if __name__ == "__main__":
36+
unittest.main()
37+

0 commit comments

Comments
 (0)