拉取zookeeper镜像
docker pull zookeeper
根据镜像创建docker容器
分别创建容器zookeeper、zookeeper2、zookeeper3这三个容器
docker run --privileged=true -d --name zookeeper --publish 2181:2181 -d zookeeper:latest
docker run --privileged=true -d --name zookeeper2 --publish 2182:2181 -d zookeeper:latest
docker run --privileged=true -d --name zookeeper3 --publish 2183:2181 -d zookeeper:latest
注意,此时这三个容器还处于单机状态,并没有搭建好集群,我们需要规划一下这三个容器
容器id(已自己的为准) | name | 虚拟机端口 | 容器端口 | 容器ip | myid | 职责(看启动顺序,详情见zookeeper集群leader选取机制) | |
7c............. | zookeeper | 2181 | 2181 | 172.17.0.2 | 1 | leader | |
ee................ | zookeeper2 | 2182 | 2181 | 172.17.0.3 | 2 | follower | |
ce.................... | zookeeper3 | 2183 | 2181 | 172.17.0.4 | 3 | follower |
修改这三个容器的配置文件(myid & zoo.cfg)
myid文件地址:
- 进入容器docker exec 7c /bin/bash
- cd / 回到根目录
- cd data
zoo.cfg文件地址
- 进入容器docker exec 7c /bin/bash
- cd / 回到根目录
- cd conf
在zoo.cfg文件尾部添加以下配置
server.1=172.17.0.2:2888:3888;2181
server.2=172.17.0.3:2888:3888;2181
server.3=172.17.0.4:2888:3888;2181
注意,server.后面的数字需要和你myid设置的一样,IP就是你每个容器的ip,分别加到另外两台机器上
docker 查看容器IP方法:
[root@localhost ~]# docker inspect --format='{
{.NetworkSettings.IPAddress}}' zookeeper
172.17.0.2
[root@localhost ~]# docker inspect --format='{
{.NetworkSettings.IPAddress}}' zookeeper2
172.17.0.3
[root@localhost ~]# docker inspect --format='{
{.NetworkSettings.IPAddress}}' zookeeper3
172.17.0.4
[root@localhost ~]#
我们启动一下这三个容器
首先确保这三个容器都已经安装了vim命令(若没安装,则执行apt-get update 和 apt-get install -y vim命令即可)
考虑到每次启动都需要执行N次启动命令,比较麻烦,这里直接写两个简单的启动&关闭脚本节省时间吧,脚本如下。
1.批量启动脚本zookeeperbatchstart.sh
echo "=================================================="
docker restart 7c
echo "==================zookeeper1 start================"
docker restart ee
echo "==================zookeeper2 start================"
docker restart ce
echo "==================zookeeper3 start================"
2.批量关闭脚本zookeeperbatchstop.sh
echo "=================================================="
docker stop 7c
echo "==================zookeeper1 stop ================"
docker stop ee
echo "==================zookeeper2 stop ================"
docker stop ce
echo "==================zookeeper3 stop ================"
启动后分别进入容器,如下图,可以看到,zookeeper为leader,zookeeper2和zookeeper3为follower
来源:oschina
链接:https://my.oschina.net/u/4381476/blog/4775966