-
準備一個文件夾並進入此文件夾, 例如docker
mkdir -p ~/docker-app/zookeeper && cd ~/docker-app/zookeeper
vim -
準備三個文件夾並寫入zookeeper的myidbash
mkdir -p zoo1/data && echo 1 > zoo1/data/myid
mkdir -p zoo2/data && echo 2 > zoo2/data/myid
mkdir -p zoo3/data && echo 3 > zoo3/data/myid
網絡 -
建立一個docker-compose.yml文件並編輯app
vim docker-compose.yml
學習文件內容以下:rest
version: '3.7' networks: zk_cluster: name: zk_cluster #爲集羣建立一個網絡 driver: bridge #集羣中的服務(3個節點) services: zoo1: image: zookeeper #所使用的鏡像 restart: always #容器異常時是否重啓 container_name: zoo1 #容器名稱 ports: #與宿主機的端口映射 - 2181:2181 - 8001:8080 # zookeeper的配置 environment: ZOO_MY_ID: 1 ZOO_SERVERS: server.1=0.0.0.0:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181 #容器目錄映射,此處使用了相對路徑 volumes: - ./zoo1/data:/data - ./zoo1/datalog:/datalog #所使用的網絡 networks: - zk_cluster zoo2: image: zookeeper restart: always container_name: zoo2 ports: - 2182:2181 - 8002:8080 environment: ZOO_MY_ID: 2 ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=0.0.0.0:2888:3888;2181 server.3=zoo3:2888:3888;2181 volumes: - ./zoo2/data:/data - ./zoo2/datalog:/datalog networks: - zk_cluster zoo3: image: zookeeper restart: always container_name: zoo3 ports: - 2183:2181 - 8003:8080 environment: ZOO_MY_ID: 3 ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=0.0.0.0:2888:3888;2181 volumes: - ./zoo3/data:/data - ./zoo3/datalog:/datalog networks: - zk_cluster
-
使用docker-compose命令運行code
docker-compose --project-directory $PWD up -d
server -
附:一鍵建立docker zookeeper集羣並執行的腳本(該方案僅供參考和學習,生產環境需仔細斟酌,合理修改配置參數)io
#! /bin/bash mkdir -p zookeeper && cd zookeeper DIR=$PWD echo "cd $DIR" echo "create docker compose config file" CONF_YML='docker-compose.yml' touch $CONF_YML echo "version: '3.7'" > $CONF_YML NET='zk_cluster' echo 'networks:' >> $CONF_YML echo " $NET:" >> $CONF_YML echo " name: $NET" >> $CONF_YML echo " driver: bridge" >> $CONF_YML #節點個數,能夠修改此值建立任意數量的容器節點 let CLUSTER_COUNT=3 IMAGE=zookeeper echo "services:" >> $CONF_YML for i in `seq 1 $CLUSTER_COUNT` do echo " zoo$i:" >> $CONF_YML echo " image: $IMAGE" >> $CONF_YML echo " restart: always" >> $CONF_YML echo " container_name: zoo$i" >> $CONF_YML echo " ports:" >> $CONF_YML echo " - $[2180 + $i]:2181" >> $CONF_YML echo " - $[8000 + $i]:8080" >> $CONF_YML echo " environment:" >> $CONF_YML echo " ZOO_MY_ID: $i" >> $CONF_YML zk_srv_conf='' for j in `seq 1 $CLUSTER_COUNT` do if test $i != $j; then zk_srv_conf+="server.$j=zoo$j:2888:3888;2181" else zk_srv_conf+="server.$j=0.0.0.0:2888:3888;2181" fi if test $j != $CLUSTER_COUNT; then zk_srv_conf+=' '; fi done echo " ZOO_SERVERS: $zk_srv_conf" >> $CONF_YML echo " volumes:" >> $CONF_YML echo " - ./zoo$i/data:/data" >> $CONF_YML echo " - ./zoo$i/datalog:/datalog" >> $CONF_YML echo " networks:" >> $CONF_YML echo " - $NET" >> $CONF_YML mkdir -p zoo$i/data echo $i > zoo$i/data/myid done # docker pull zookeeper docker-compose --project-directory $DIR up -d