Docker搭建Zookeeper集群
程序员文章站
2024-01-15 15:30:37
...
创建网络并配置ip
众所周知bridge模式是Docker默认的网络设置,此模式会为每一个容器分配Network Namespace、设置IP等,并将一个主机上的Docker容器连接到一个虚拟网桥上。
docker network create --driver bridge --subnet 120.25.0.0/16 zookeeper_net
查看网卡信息
docker network inspect zookeeper_net
搭建zookeeper集群
这里使用docker-compose搭建,就不用一个一个容器去创建了
创建docker-compose.yml文件,输入一下内容:
version: '2'
services:
zoo1:
image: zookeeper:3.4 # 镜像名称
restart: always # 当发生错误时自动重启
hostname: zoo1
container_name: zoo1
privileged: true
ports: # 端口
- 2181:2181
volumes: # 挂载数据卷
- ./zoo1/data:/data
- ./zoo1/datalog:/datalog
environment:
TZ: Asia/Shanghai
ZOO_MY_ID: 1 # 节点ID
ZOO_PORT: 2181 # zookeeper端口号
ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888 # zookeeper节点列表
networks:
default:
ipv4_address: 120.25.0.2
zoo2:
image: zookeeper:3.4
restart: always
hostname: zoo2
container_name: zoo2
privileged: true
ports:
- 2182:2181
volumes:
- ./zoo2/data:/data
- ./zoo2/datalog:/datalog
environment:
TZ: Asia/Shanghai
ZOO_MY_ID: 2
ZOO_PORT: 2181
ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
networks:
default:
ipv4_address: 120.25.0.3
zoo3:
image: zookeeper:3.4
restart: always
hostname: zoo3
container_name: zoo3
privileged: true
ports:
- 2183:2181
volumes:
- ./zoo3/data:/data
- ./zoo3/datalog:/datalog
environment:
TZ: Asia/Shanghai
ZOO_MY_ID: 3
ZOO_PORT: 2181
ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
networks:
default:
ipv4_address: 120.25.0.4
networks:
default:
external:
name: zookeeper_net
查看容器
docker ps
验证是否搭建成功
进入容器,输入./bin/zkServer.sh status
docker exec -it 容器id /bin/bash
说明该容器是follower,接下来可以用相同方法查看其它容器。
测试连接
进入本机zookeeeper bin目录下,执行./zkCli.sh -server 127.0.0.1:容器映射端口
至此Zookeeper集群搭建成功