运维之我的docker-compose快速创建zookeeper集群
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了运维之我的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
[[email protected] pkg]# cat zookeeper/docker-entrypoint.sh #!/bin/bash ####################################################### # author : baishaohua # # mail : [email protected] # # blog : new.nginxs.net # # msg : startup zookeeper cluster for docker # ####################################################### #Shell Base Env 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 "zookeeper is start" #init config echo -e ${ZK_CONF_TXT} >${ZK_CONF} #ZK_NODE_LIST from docker argv for i in ${ZK_NODE_LIST} do echo "$i" >>${ZK_CONF} done echo "${ZK_ID}" >${ZK_DATA}/myid #this shell main function ${ZK_HOME}/bin/zkServer.sh start-foreground
Dockerfile文件配置
[[email protected] pkg]# cat zookeeper/Dockerfile FROM registry.cn-hangzhou.aliyuncs.com/forker/centos7:last MAINTAINER baishaohua <[email protected]> #start up configure 下面自动把zookeeper安装包安装到/usr/local/zookeeper目录 ADD zookeeper-3.5.1-alpha.tar.gz /usr/local/ RUN mv /usr/local/zookeeper-3.5.1-alpha /usr/local/zookeeper && mkdir -p /var/lib/zookeeper/ RUN yum -y install make gcc libgcc gcc-c++ glibc-devel java-1.8.0-openjdk COPY docker-entrypoint.sh /usr/bin/ #ENV and CMD 配置容器默认启动脚本和开放2181端口 CMD ["/usr/bin/docker-entrypoint.sh"] EXPOSE 2181
重点来了我们要从docker-compose.yml传参到启动脚本
下面通过environment定义环境变量把参数传递到容器的启动脚本,完成自定义部分内容
https://code.aliyun.com/nginxs/docker/blob/master/pkg/zk_cluster/docker-compose.yml
[[email protected] pkg]# cat zk_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:4181 server.2=zk2:3181:4181 server.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:4181 server.2=zk2:3181:4181 server.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:4181 server.2=zk2:3181:4181 server.3=zk3:3181:4181
启动我的compose服务
这里可以看到其实zk1,zk2,zk3每个都是一个service,而且compose会为这个应用创建一个默认的桥接网络
[[email protected] zk_cluster]# docker-compose up -d WARNING: The Docker Engine you‘re using is running in swarm mode. Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node. To deploy your application across the swarm, use `docker stack deploy`. Creating network "zkcluster_default" with the default driver Creating zk3 Creating zk1 Creating zk2
测试zookeeper集群
[[email protected] zk_cluster]# echo "conf"|nc localhost 2181 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
本文出自 “运维之我的历程” 博客,请务必保留此出处http://nginxs.blog.51cto.com/4676810/1908591
以上是关于运维之我的docker-compose快速创建zookeeper集群的主要内容,如果未能解决你的问题,请参考以下文章