拉取镜像
docker pull zookeeper:latest
获取镜像基本信息
docker inspect zookeeper
mkdir /opt/zookeeper -p
vim /opt/zookeeper/Dockerfile
FROM zookeeper:3.5.6
EXPOSE 2181
EXPOSE 2182
EXPOSE 2183
EXPOSE 2888
EXPOSE 3888
EXPOSE 8080
单机
sudo mkdir -p /var/lib/zookeeper;sudo chmod -R 777 /var/lib/zookeeper
sudo mkdir -p /var/logs/zookeeper;sudo chmod -R 777 /var/logs/zookeeper
vim /opt/zookeeper/conf/zoo.cfg
dataDir=/var/lib/zookeeper
dataLogDir=/var/logs/zookeeper
clientPort=2181
4lw.commands.whitelist=*
tickTime=2000
initLimit=5
syncLimit=2
autopurge.snapRetainCount=3
autopurge.purgeInterval=0
maxClientCnxns=60
standaloneEnabled=true
admin.enableServer=true
vim /opt/zookeeper/docker-compose.yml
version: '3.6'
services:
zookeeper:
image: v-zookeeper
container_name: zookeeper
build:
context: .
dockerfile: Dockerfile
restart: always
ports:
- "2181:2181"
- "28080:8080"
volumes:
- "/var/lib/zookeeper:/var/lib/zookeeper"
- "/var/logs/zookeeper:/var/logs/zookeeper"
伪集群
sudo mkdir -p /var/lib/zookeeper/node1;sudo chmod -R 777 /var/lib/zookeeper/node1
sudo mkdir -p /var/log/zookeeper/node1/version-2;sudo chmod -R 777 /var/log/zookeeper/node1/version-2
mkdir vim /opt/zookeeper/node1/conf -p
vim /opt/zookeeper/node1/conf/zoo.cfg
dataDir=/data
dataLogDir=/datalog
clientPort=2181
4lw.commands.whitelist=*
tickTime=2000
initLimit=5
syncLimit=2
autopurge.snapRetainCount=3
autopurge.purgeInterval=0
maxClientCnxns=60
admin.enableServer=true
server.1=zookeeper1:2888:3888
server.2=zookeeper2:2888:3888
server.3=zookeeper3:2888:3888
quorumListenOnAllIPs=true
sudo mkdir -p /var/lib/zookeeper/node2;sudo chmod -R 777 /var/lib/zookeeper/node2
sudo mkdir -p /var/log/zookeeper/node2/version-2;sudo chmod -R 777 /var/log/zookeeper/node2/version-2
mkdir vim /opt/zookeeper/node2/conf -p
vim /opt/zookeeper/node2/conf/zoo.cfg
dataDir=/data
dataLogDir=/datalog
clientPort=2181
4lw.commands.whitelist=*
tickTime=2000
initLimit=5
syncLimit=2
autopurge.snapRetainCount=3
autopurge.purgeInterval=0
maxClientCnxns=60
admin.enableServer=true
server.1=zookeeper1:2888:3888
server.2=zookeeper2:2888:3888
server.3=zookeeper3:2888:3888
quorumListenOnAllIPs=true
ZOO_SERVERS中的zookeeper为容器名
sudo mkdir -p /var/lib/zookeeper/node3;sudo chmod -R 777 /var/lib/zookeeper/node3
sudo mkdir -p /var/log/zookeeper/node3/version-2;sudo chmod -R 777 /var/log/zookeeper/node3/version-2
mkdir vim /opt/zookeeper/node3/conf -p
vim /opt/zookeeper/node3/conf/zoo.cfg
dataDir=/data
dataLogDir=/datalog
clientPort=2181
4lw.commands.whitelist=*
tickTime=2000
initLimit=5
syncLimit=2
autopurge.snapRetainCount=3
autopurge.purgeInterval=0
maxClientCnxns=60
admin.enableServer=true
server.1=zookeeper1:2888:3888
server.2=zookeeper2:2888:3888
server.3=zookeeper3:2888:3888
quorumListenOnAllIPs=true
version: '3.6'
services:
zookeeper1:
image: v-zookeeper1
container_name: zookeeper1
build:
context: .
dockerfile: Dockerfile
hostname: zookeeper1
restart: always
ports:
- "2181:2181"
- "18081:8080"
environment:
ZOO_MY_ID: 1
volumes:
- "./node1/conf/zoo.cfg:/conf/zoo.cfg"
- "/var/lib/zookeeper/node1:/data"
- "/var/log/zookeeper/node1/version-2:/datalog/version-2"
zookeeper2:
image: v-zookeeper2
container_name: zookeeper2
build:
context: .
dockerfile: Dockerfile
hostname: zookeeper2
restart: always
ports:
- "2182:2181"
- "18082:8080"
environment:
ZOO_MY_ID: 2
volumes:
- "./node2/conf/zoo.cfg:/conf/zoo.cfg"
- "/var/lib/zookeeper/node2:/data"
- "/var/log/zookeeper/node2/version-2:/datalog/version-2"
zookeeper3:
image: v-zookeeper3
container_name: zookeeper3
build:
context: .
dockerfile: Dockerfile
hostname: zookeeper3
restart: always
ports:
- "2183:2181"
- "18083:8080"
environment:
ZOO_MY_ID: 3
volumes:
- "./node3/conf/zoo.cfg:/conf/zoo.cfg"
- "/var/lib/zookeeper/node3:/data"
- "/var/log/zookeeper/node3/version-2:/datalog/version-2"
查看状态docker exec -it zookeeper2 bash ./bin/zkServer.sh status
集群
集群模式必须设置clientPort的值
sudo mkdir -p /etc/zookeeper;sudo chmod -R 777 /etc/zookeeper
sudo mkdir -p /var/lib/zookeeper/version-2;sudo chmod -R 777 /var/lib/zookeeper/version-2
sudo mkdir -p /var/log/zookeeper/version-2;sudo chmod -R 777 /var/log/zookeeper/version-2
vim /opt/zookeeper/Dockerfile
FROM zookeeper:3.5.6
EXPOSE 2181
EXPOSE 2888
EXPOSE 3888
EXPOSE 8080
服务器1、2、3:
vim /etc/zookeeper/zoo.cfg
dataDir=/data
dataLogDir=/datalog
clientPort=2181
4lw.commands.whitelist=*
tickTime=2000
initLimit=5
syncLimit=2
autopurge.snapRetainCount=3
autopurge.purgeInterval=0
maxClientCnxns=60
admin.enableServer=true
server.1=192.168.1.101:2888:3888
server.2=192.168.1.102:2888:3888
server.3=192.168.1.103:2888:3888
quorumListenOnAllIPs=true
服务器1:
version: '3.7'
services:
zookeeper:
image: v-zookeeper
container_name: zookeeper
build:
context: .
dockerfile: Dockerfile
restart: always
privileged: true
ports:
- "2181:2181"
- "2888:2888"
- "3888:3888"
- "18080:8080"
environment:
- JAVA_OPTS=-Xms256m -Xmx256m -Xmn100m
- ZOO_MY_ID=1
volumes:
- "/etc/zookeeper/zoo.cfg:/conf/zoo.cfg"
- "/var/lib/zookeeper/version-2:/data/version-2"
- "/var/log/zookeeper/version-2:/datalog/version-2"
logging:
driver: "json-file"
options:
max-size: "200k"
max-file: "100"
服务器2:
version: '3.7'
services:
zookeeper:
image: v-zookeeper
container_name: zookeeper
build:
context: .
dockerfile: Dockerfile
restart: always
privileged: true
ports:
- "2181:2181"
- "2888:2888"
- "3888:3888"
- "18080:8080"
environment:
- JAVA_OPTS=-Xms256m -Xmx256m -Xmn100m
- ZOO_MY_ID=2
volumes:
- "/etc/zookeeper/zoo.cfg:/conf/zoo.cfg"
- "/var/lib/zookeeper/version-2:/data/version-2"
- "/var/log/zookeeper/version-2:/datalog/version-2"
logging:
driver: "json-file"
options:
max-size: "200k"
max-file: "100"
服务器3:
version: '3.7'
services:
zookeeper:
image: v-zookeeper
container_name: zookeeper
build:
context: .
dockerfile: Dockerfile
restart: always
privileged: true
ports:
- "2181:2181"
- "2888:2888"
- "3888:3888"
- "18080:8080"
environment:
- JAVA_OPTS=-Xms256m -Xmx256m -Xmn100m
- ZOO_MY_ID=3
volumes:
- "/etc/zookeeper/zoo.cfg:/conf/zoo.cfg"
- "/var/lib/zookeeper/version-2:/data/version-2"
- "/var/log/zookeeper/version-2:/datalog/version-2"
logging:
driver: "json-file"
options:
max-size: "200k"
max-file: "100"
需要配置quorumListenOnAllIPs=true,用以监听所有网卡,这个和云服务器的一些虚拟技术有关。
启动
cd /opt/zookeeper
docker-compose build
docker-compose up -d --force-recreate
查看日志
docker logs --tail="500" zookeeper
备份配置文件
docker cp zookeeper:/conf/zoo.cfg /opt/zookeeper/conf/zoo_bak.cfg
docker cp zookeeper1:/conf/ /opt/zookeeper/zookeeper1_conf_bak/
进入容器
docker exec -it zookeeper /bin/bash
检查容器状态
docker exec -it zookeeper1 /bin/bash ./bin/zkServer.sh status