运维之我的docker-compose快速创建zookeeper集群

时间:2017-03-27 12:29 来源:网管之家整理 字体:[ ] 评论:

运维之我的docker-compose快速创建zookeeper集群。很多时候大家不同的容器想要不用启动参数这个时候不妨使用脚本封装来实现,官方也很推荐这个做法,这里介绍下如何使用脚本传参快速启动一个zookeeper集群。

zookeeper集群大家知道有部分通用配置,和一些动态配置如果用一个简单的zookeeper实例无法正常启动集群,所以我们的节点列表和节点ID是需要动态传入的所以下面的脚本的${ZK_ID}和${ZK_LIST}是需要你使用compose的配置传入的。

zookeeper启动脚本

https://code.aliyun.com/nginxs/docker/blob/master/pkg/zookeeper/docker-entrypoint.sh

[root@test-nodepkg]#catzookeeper/docker-entrypoint.sh
#!/bin/bash
#######################################################
#author:baishaohua#
#mail:nginxs@126.com
#
#blog:new.nginxs.net
#
#msg:startupzookeeperclusterfordocker#
#######################################################

#ShellBaseEnv
ZK_HOME="/usr/local/zookeeper"
ZK_CONF="${ZK_HOME}/conf/zoo.cfg"
ZK_DATA="/var/lib/zookeeper"
ZK_CONF_TXT="tickTime=2000\ninitLimit=10\nsyncLimit=5\ndataDir=${ZK_DATA}\nclientPort=2181"

echo"zookeeperisstart"
#initconfig
echo-e${ZK_CONF_TXT}>${ZK_CONF}

#ZK_NODE_LISTfromdockerargv

foriin
${ZK_NODE_LIST}
do
echo"$i">>${ZK_CONF}
done
echo"${ZK_ID}">${ZK_DATA}/myid


#thisshellmainfunction
${ZK_HOME}/bin/zkServer.shstart-foreground

Dockerfile文件配置

[root@test-node3pkg]#catzookeeper/Dockerfile
FROMregistry.cn-hangzhou.aliyuncs.com/forker/centos7:last
MAINTAINERbaishaohua

#startupconfigure下面自动把zookeeper安装包安装到/usr/local/zookeeper目录
ADDzookeeper-3.5.1-alpha.tar.gz/usr/local/
RUNmv/usr/local/zookeeper-3.5.1-alpha/usr/local/zookeeper&&mkdir-p/var/lib/zookeeper/
RUNyum-yinstallmakegcclibgccgcc-c++glibc-develjava-1.8.0-openjdk
COPYdocker-entrypoint.sh/usr/bin/

#ENVandCMD配置容器默认启动脚本和开放2181端口
CMD["/usr/bin/docker-entrypoint.sh"]
EXPOSE2181

重点来了我们要从docker-compose.yml传参到启动脚本

下面通过environment定义环境变量把参数传递到容器的启动脚本,完成自定义部分内容

https://code.aliyun.com/nginxs/docker/blob/master/pkg/zk_cluster/docker-compose.yml

[root@test-nodepkg]#catzk_cluster/docker-compose.yml
version:'3'
services:
zk1:
image:registry.cn-hangzhou.aliyuncs.com/forker/zookeeper
restart:always
container_name:zk1
ports:
-"2181:2181"
environment:
ZK_ID:1
ZK_NODE_LIST:server.1=zk1:3181:4181server.2=zk2:3181:4181server.3=zk3:3181:4181

zk2:
image:registry.cn-hangzhou.aliyuncs.com/forker/zookeeper
restart:always
container_name:zk2
ports:
-"2182:2181"
environment:
ZK_ID:2
ZK_NODE_LIST:server.1=zk1:3181:4181server.2=zk2:3181:4181server.3=zk3:3181:4181

zk3:
image:registry.cn-hangzhou.aliyuncs.com/forker/zookeeper
restart:always
container_name:zk3
ports:
-"2183:2181"
environment:
ZK_ID:3
ZK_NODE_LIST:server.1=zk1:3181:4181server.2=zk2:3181:4181server.3=zk3:3181:4181

启动我的compose服务

这里可以看到其实zk1,zk2,zk3每个都是一个service,而且compose会为这个应用创建一个默认的桥接网络

[root@test-node3zk_cluster]#docker-composeup-d
WARNING:TheDockerEngineyou'reusingisrunninginswarmmode.

Composedoesnotuseswarmmodetodeployservicestomultiplenodesinaswarm.Allcontainerswillbescheduledonthecurrentnode.

Todeployyourapplicationacrosstheswarm,use`dockerstackdeploy`.

Creatingnetwork"zkcluster_default"withthedefaultdriver
Creatingzk3
Creatingzk1
Creatingzk2

测试zookeeper集群

[root@test-nodezk_cluster]#echo"conf"|nclocalhost2181
clientPort=2181
secureClientPort=-1
dataDir=/var/lib/zookeeper/version-2
dataDirSize=67109393
dataLogDir=/var/lib/zookeeper/version-2
dataLogSize=67109393
tickTime=2000
maxClientCnxns=60
minSessionTimeout=4000
maxSessionTimeout=40000
serverId=1
initLimit=10
syncLimit=5
electionAlg=3
electionPort=4181
quorumPort=3181
peerType=0
membership:
server.1=zk1:3181:4181:participant
server.2=zk2:3181:4181:participant
server.3=zk3:3181:4181:participant
version=100000000
顶一下(0) 踩一下(0)
Top_arrow