很多時候大家不同的容器想要不用啟動引數這個時候不妨使用指令碼封裝來實現,官方也很推薦這個做法,這裡介紹下如何使用指令碼傳參快速啟動一個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 : nginxs@126.com # # 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 initLimit=10 syncLimit=5 dataDir=${ZK_DATA} clientPort=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 <nginxs@126.com> #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安裝
使用技巧和推薦