欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

DockerCompose搭建Zookeeper集群

程序员文章站 2022-03-07 22:42:55
...

一、前提条件

以下所有操作会使用docker-compose来进行服务编排,前提需要安装Docker,安装教程如下:

Docker安装教程(视频+图文)

二、安装Docker Compose

兼容性请参考官方:https://docs.docker.com/compose/compose-file/

更多Compose File请参考官方:https://docs.docker.com/compose/compose-file/

下载最新稳定版的Docker Compose

[[email protected] ~]$ sudo curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /opt/moudle/docker-compose

添加可执行权限

[[email protected] ~]$ sudo chmod +x /opt/moudle/docker-compose

做一个软链接:

[[email protected] ~]$ sudo ln -s /opt/moudle/docker-compose /usr/bin/docp

三、拉取镜像并编写docker-compose.yml

[[email protected] ~]$ sudo docker pull zookeeper:3.5.7

创建docker-compose.yml文件:

文件下载地址:https://github.com/xiaokangxxs/notebook/tree/master/docs/BigData/Docker/yaml

# 指定 compose 文件的版本
version: "3.8"
# 指定服务
services:
        zoo1:
                # 指定镜像名称或镜像ID
                image: 4e6f2f153ac8
                # 给容器设置标签
                labels:
                	- "desc=This is 3 node zookeeper-cluster"
                    - "author=xiaokang"
                    - "date=2020-04-21 19:20:11"
                # 容器将始终重新启动
                restart: always
                # 主机名
                hostname: zoo1
                # 特权模式(能使用systemctl)
                privileged: true
                # 指定容器的名称 (等同于 docker run --name 的作用)
                container_name: zoo1
                networks:
                    xiaokang_net:
                        ipv4_address: 172.24.11.5
                ports:
                        - 2181:2181
                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
        zoo2:
                image: 4e6f2f153ac8
                restart: always
                hostname: zoo2
                privileged: true
                container_name: zoo2
                networks:
                    xiaokang_net:
                        ipv4_address: 172.24.11.6
                ports:
                        - 2182:2181
                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
        zoo3:
                image: 4e6f2f153ac8
                restart: always
                hostname: zoo3
                privileged: true
                container_name: zoo3
                networks:
                    xiaokang_net:
                        ipv4_address: 172.24.11.7
                ports:
                        - 2183:2181
                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
networks:
  xiaokang_net:
    ipam:
      driver: default
      config:
        - subnet: "172.24.11.0/24"

四、验证

[[email protected] docker_teach]$ sudo docp exec zoo1 ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower

[[email protected] docker_teach]$ sudo docp exec zoo2 ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower

[[email protected] docker_teach]$ sudo docp exec zoo3 ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: leader