很多时候大家不同的容器想要不用启动参数这个时候不妨使用脚本封装来实现,官方也很推荐这个做法,这里介绍下如何使用脚本传参快速启动一个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-node 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文件配置
[root@test-node3 pkg]# cat zookeeper/Dockerfile FROM registry.cn-hangzhou.aliyuncs.com/forker/centos7:last MAINTAINER baishaohua#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
[root@test-node pkg]# cat zk_cluster/docker-compose.yml version: '3' services: zk1: p_w_picpath: 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: p_w_picpath: 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: p_w_picpath: 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会为这个应用创建一个默认的桥接网络
[root@test-node3 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集群
[root@test-node 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
下面是一些我写的其它帮助文档:
0. docker安装
阿里云加速你的docker
运行你第一个镜像实例-docker容器
docker命令参数
docker仓库使用和镜像提交
docker镜像构建构建及参数详解
docker镜像的导入导出
docker的网络
docker网络的测试示例
docker数据卷
docker的api和python sdk
单机多容器的编排-compose
docker-swarm集群的管理-swarm
docker-swarm集群管理详解
docker-swarm集群中删除节点和服务
docker自建仓库Registry
使用技巧和推荐
docker集群web页面管理工具
不要再给你的docker安装ssh server
compose快速创建zookeeper集群