拉取镜像
docker pull zookeeper:latest
获取镜像基本信息
docker inspect zookeeper
mkdir /opt/zookeeper -p
vim /opt/zookeeper/Dockerfile
FROM alpine FROM zookeeper:latest EXPOSE 2181 EXPOSE 2888 EXPOSE 3888 EXPOSE 8080
单机
vim /opt/zookeeper/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 standaloneEnabled=true admin.enableServer=true
vim /opt/zookeeper/docker-compose.yml
version: ‘3.6‘ services: zookeeper: image: zookeeper:latest restart: always container_name: zookeeper ports: - "2181:2181" - "2888:2888" - "3888:3888" - "28080:8080" volumes: - "./data:/data" - "./datalog:/datalog" - "./logs:/logs"
伪集群
集群模式必须设置clientPort的值
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=192.168.1.101:2881:3881 server.2=192.168.1.101:2882:3882 server.3=192.168.1.101:2883:3883
mkdir vim /opt/zookeeper/node2/conf -p
vim /opt/zookeeper/node2/conf/zoo.cfg
dataDir=/data dataLogDir=/datalog clientPort=2182 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:2881:3881 server.2=192.168.1.101:2882:3882 server.3=192.168.1.101:2883:3883
mkdir vim /opt/zookeeper/node3/conf -p
vim /opt/zookeeper/node3/conf/zoo.cfg
dataDir=/data dataLogDir=/datalog clientPort=2183 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:2881:3881 server.2=192.168.1.101:2882:3882 server.3=192.168.1.101:2883:3883
vim /opt/zookeeper/docker-compose.yml
version: ‘3.6‘ services: zookeeper1: image: zookeeper:latest restart: always container_name: zookeeper1 network_mode: "host" ports: - "2181:2181" - "2881:2881" - "3881:3881" - "8081:8080" user: root privileged: true environment: ZOO_MY_ID: 1 ZOO_SERVERS: server.1=192.168.1.101:2881:3881 server.2=192.168.1.101:2882:3882 server.3=192.168.1.101:2883:3883 volumes: - "./node1/conf/zoo.cfg:/conf/zoo.cfg" - "./node1/data/:/data/" - "./node1/datalog/:/datalog/" - "./node1/logs/:/logs/" zookeeper2: image: zookeeper:latest restart: always container_name: zookeeper2 network_mode: "host" ports: - "2182:2182" - "2882:2882" - "3882:3882" - "8082:8080" user: root privileged: true environment: ZOO_MY_ID: 2 ZOO_SERVERS: server.1=192.168.1.101:2881:3881 server.2=192.168.1.101:2882:3882 server.3=192.168.1.101:2883:3883 volumes: - "./node2/conf/zoo.cfg:/conf/zoo.cfg" - "./node2/data/:/data/" - "./node2/datalog/:/datalog/" - "./node2/logs/:/logs/" zookeeper3: image: zookeeper:latest restart: always container_name: zookeeper3 network_mode: "host" ports: - "2183:2183" - "2883:2883" - "3883:3883" - "8083:8080" user: root privileged: true environment: ZOO_MY_ID: 3 ZOO_SERVERS: server.1=192.168.1.101:2881:3881 server.2=192.168.1.101:2882:3882 server.3=192.168.1.101:2883:3883 volumes: - "./node3/conf/zoo.cfg:/conf/zoo.cfg" - "./node3/data/:/data/" - "./node3/datalog/:/datalog/" - "./node3/logs/:/logs/"
集群
集群模式必须设置clientPort的值
服务器1、2、3:
vim /opt/zookeeper/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=192.168.1.101:2888:3888 server.2=192.168.1.102:2888:3888 server.3=192.168.1.103:2888:3888
服务器1:
vim /opt/zookeeper/docker-compose.yml
version: ‘3.6‘ services: zookeeper: image: zookeeper:latest restart: always container_name: zookeeper network_mode: "host" ports: - "2181:2181" - "2888:2888" - "3888:3888" - "28080:8080" environment: ZOO_MY_ID: 1 ZOO_SERVERS: server.1=192.168.1.101:2888:3888 server.2=192.168.1.102:2888:3888 server.3=192.168.1.103:2888:3888 volumes: - "./conf/zoo.cfg:/conf/zoo.cfg" - "./data:/data" - "./datalog:/datalog" - "./logs:/logs"
服务器2:
vim /opt/zookeeper/docker-compose.yml
version: ‘3.6‘ services: zookeeper: image: zookeeper:latest restart: always container_name: zookeeper network_mode: "host" ports: - "2181:2181" - "2888:2888" - "3888:3888" - "28080:8080" environment: ZOO_MY_ID: 2 ZOO_SERVERS: server.1=192.168.1.101:2888:3888 server.2=192.168.1.102:2888:3888 server.3=192.168.1.103:2888:3888 volumes: - "./conf/zoo.cfg:/conf/zoo.cfg" - "./data:/data" - "./datalog:/datalog" - "./logs:/logs"
服务器3:
vim /opt/zookeeper/docker-compose.yml
version: ‘3.6‘ services: zookeeper: image: zookeeper:latest restart: always container_name: zookeeper network_mode: "host" ports: - "2181:2181" - "2888:2888" - "3888:3888" - "28080:8080" environment: ZOO_MY_ID: 3 ZOO_SERVERS: server.1=192.168.1.101:2888:3888 server.2=192.168.1.102:2888:3888 server.3=192.168.1.103:2888:3888 volumes: - "./conf/zoo.cfg:/conf/zoo.cfg" - "./data:/data" - "./datalog:/datalog" - "./logs:/logs"
ZOO_SERVERS中的zookeeper为容器名
启动
cd /opt/zookeeper
docker-compose build
docker-compose up -d --force-recreate
docker-compose down
docker-compose restart
查看日志
docker logs --tail="500" zookeeper
备份配置文件
docker cp zookeeper:/conf/zoo.cfg /opt/zookeeper/conf/zoo_bak.cfg
进入容器
docker exec -it zookeeper /bin/bash
检查容器状态
docker exec -it zookeeper1 /bin/bash ./bin/zkServer.sh status
原文地址:https://www.cnblogs.com/wintersoft/p/11128484.html