Docker部署Zookeeper集群 | Eddie'Blog
Docker部署Zookeeper集群

Docker部署Zookeeper集群

eddie 402 2021-03-23

@[toc]

目录

一、单容器环境搭建

1.1.1 拉取Zookeeper镜像

[root@k8s-master1 ~]# docker pull zookeeper
Using default tag: latest
^[latest: Pulling from library/zookeeper
a076a628af6f: Pull complete 
943d8acaac04: Pull complete 
b9998d19c116: Pull complete 
6f3bacbd30c2: Pull complete 
5bb61178ce00: Pull complete 
b2cff6714614: Pull complete 
fb3ca86b025f: Pull complete 
c4f5b9c9ba49: Pull complete 
Digest: sha256:74747e50e76474074b997525046869bbe194ed73f719d880ceac71f4a2e8a2e6
Status: Downloaded newer image for zookeeper:latest

1.1.2 启动容器

[root@k8s-master1 ~]# docker run --name t_zk1 -d zookeeper   
35f5f39f0c249511eca1a80122f1ea162e7b8a35fa6ad05a5908dabf462f2c57

1.1.3 查看容器启动日志

docker logs t_zk1

1.1.4 进入容器

docker exec -it t_zk1 bash

1.1.5 安装Vim

apt-get update -y && apt-get install vim -y

1.1.6 编辑*.cfg

vim /conf/zoo.cfg

4lw.commands.whitelist=*

exit

zoo.cfg 最底部添加 " 4lw.commands.whitelist=* " 为了查看运行状态

1.1.7 重启容器与查看容器IP地址

[root@k8s-master1 ~]# docker restart t_zk1 && docker inspect t_zk1 | grep IPAddress 
t_zk1
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.6",
                    "IPAddress": "172.17.0.6",

1.1.8 通过获取容器IP地址

[root@k8s-master1 ~]# echo stat | nc 172.17.0.6 2181
Zookeeper version: 3.6.2--803c7f1a12f85978cb049af5e4ef23bd8b688715, built on 09/04/2020 12:44 GMT
Clients:
 /172.17.0.1:36388[0](queued=0,recved=1,sent=0)

Latency min/avg/max: 0/0.0/0
Received: 1
Sent: 0
Connections: 1
Outstanding: 0
Zxid: 0x0
Mode: standalone
Node count: 5

nc命令用于设置路由器: 如果没有就额外安装 "yum install -y nc"

二、升级集群环境搭建

2.1.1 重覆(一、单容器环境搭建)流程走两次, 分别创建两个容器

[root@k8s-master1 ~]# docker ps | grep zk
edb499903bba        zookeeper                     "/docker-entrypoin..."   7 minutes ago       Up 7 minutes           2181/tcp, 2888/tcp, 3888/tcp, 8080/tcp                                                       t_zk3
ac26d747d2a1        zookeeper                     "/docker-entrypoin..."   7 minutes ago       Up 7 minutes           2181/tcp, 2888/tcp, 3888/tcp, 8080/tcp                                                       t_zk2
35f5f39f0c24        zookeeper                     "/docker-entrypoin..."   26 minutes ago      Up 14 minutes          2181/tcp, 2888/tcp, 3888/tcp, 8080/tcp                                                       t_zk1

容器对应IP 地址:
t_zk1: "172.17.0.6"
t_zk2: "172.17.0.7"
t_zk3: "172.17.0.8"

2.1.2 分别进入t_zk1、t_zk2、t_zk3, 编辑 zoo.cfg 文件

vim /conf/zoo.cfg

dataDir=/data
dataLogDir=/datalog
tickTime=2000
initLimit=5
syncLimit=2
autopurge.snapRetainCount=3
autopurge.purgeInterval=0
maxClientCnxns=60
standaloneEnabled=true
admin.enableServer=true
#server.1=localhost:2888:3888;2181    ## 屏蔽
4lw.commands.whitelist=*
clientPort=2181   ## 新增
server.1=172.17.0.6:2888:3888 ## 新增
server.2=172.17.0.7:2888:3888 ## 新增
server.3=172.17.0.8:2888:3888 ## 新增

2888:zookeeper之间通信的端口 3888:zookeeper之间投票选举的端口

2.1.3 分别进入t_zk1、t_zk2、t_zk3, 编辑 zoo.cfg 文件

vim /data/myid 
1

t_zk2、t_zk3同上配置,t_zk2的myid是2,t_zk3的myid是3

2.1.4 重启所有容器

docker restart t_zk1 t_zk2 t_zk3

2.1.5 查看集群是否部署成功

[root@k8s-master1 ~]# echo stat | nc 172.17.0.6 2181  
Zookeeper version: 3.6.2--803c7f1a12f85978cb049af5e4ef23bd8b688715, built on 09/04/2020 12:44 GMT
Clients:
 /172.17.0.1:47102[0](queued=0,recved=1,sent=0)

Latency min/avg/max: 0/0.0/0
Received: 1
Sent: 0
Connections: 1
Outstanding: 0
Zxid: 0x0
Mode: follower
Node count: 5

[root@k8s-master1 ~]# echo stat | nc 172.17.0.7 2181
Zookeeper version: 3.6.2--803c7f1a12f85978cb049af5e4ef23bd8b688715, built on 09/04/2020 12:44 GMT
Clients:
 /172.17.0.1:59276[0](queued=0,recved=1,sent=0)

Latency min/avg/max: 0/0.0/0
Received: 1
Sent: 0
Connections: 1
Outstanding: 0
Zxid: 0x200000000
Mode: leader
Node count: 5
Proposal sizes last/min/max: -1/-1/-1


[root@k8s-master1 ~]# echo stat | nc 172.17.0.8 2181
Zookeeper version: 3.6.2--803c7f1a12f85978cb049af5e4ef23bd8b688715, built on 09/04/2020 12:44 GMT
Clients:
 /172.17.0.1:43118[0](queued=0,recved=1,sent=0)

Latency min/avg/max: 0/0.0/0
Received: 1
Sent: 0
Connections: 1
Outstanding: 0
Zxid: 0x200000000
Mode: follower
Node count: 5

留意每个字节的Mode字段, t_zk2 是 leader 其余是 follower

三、调正容器端口

由于之前docker run时候没有指定好宿主机与容器映射端口, 导致外部调用API失败。

3.1.1 获取容器id

[root@k8s-master1 ~]# docker ps | grep t_zk | awk '{ print $1 }'
edb499903bba
ac26d747d2a1
35f5f39f0c24

3.1.2 停止t_zk{1,2,3}容器

docker stop t_zk1 t_zk2 t_zk3

3.1.3 停止Docker所有服务

systemctl stop docker

3.1.4 进入宿主中的容器目录,根据上面提示容器id查找

如果之前没有端口映射, 应该有这样的一段:
"PortBindings":{}

增加一个映射, 这样写:
"PortBindings":{"2181/tcp":[{"HostIp":"","HostPort":"2181"}]}

前一个数字是容器端口, 后一个是宿主机端口.
而修改现有端口映射更简单, 把端口号改掉就行.
这里以zk1为例,其他两个分别修改为2181->2182,2181->2183

t_zk1 - edb499903bba

修改前

[root@k8s-master1 ~]# vim /var/lib/docker/containers/edb499903bba5a046b9e6d25faa5b065d06ac2bc408e567aa40c861544401909/hostconfig.json 
{"Binds":null,"ContainerIDFile":"","LogConfig":{"Type":"json-file","Config":{}},"NetworkMode":"default","PortBindings":{},"RestartPolicy":{"Name":"no","MaximumRetryCount":0},"AutoRemove":false,"VolumeDriver":"","VolumesFrom"
:null,"CapAdd":null,"CapDrop":null,"Dns":[],"DnsOptions":[],"DnsSearch":[],"ExtraHosts":null,"GroupAdd":null,"IpcMode":"","Cgroup":"","Links":[],"OomScoreAdj":0,"PidMode":"","Privileged":false,"PublishAllPorts":false,"Readon
lyRootfs":false,"SecurityOpt":null,"UTSMode":"","UsernsMode":"","ShmSize":67108864,"Runtime":"runc","ConsoleSize":[0,0],"Isolation":"","CpuShares":0,"Memory":0,"NanoCpus":0,"CgroupParent":"","BlkioWeight":0,"BlkioWeightDevic
e":null,"BlkioDeviceReadBps":null,"BlkioDeviceWriteBps":null,"BlkioDeviceReadIOps":null,"BlkioDeviceWriteIOps":null,"CpuPeriod":0,"CpuQuota":0,"CpuRealtimePeriod":0,"CpuRealtimeRuntime":0,"CpusetCpus":"","CpusetMems":"","Dev
  es":[],"DiskQuota":0,"KernelMemory":0,"MemoryReservation":0,"MemorySwap":0,"MemorySwappiness":-1,"OomKillDisable":false,"PidsLimit":0,"Ulimits":null,"CpuCount":0,"CpuPercent":0,"IOMaximumIOps":0,"IOMaximumBandwidth":0}

修改后

[root@k8s-master1 ~]# vim /var/lib/docker/containers/ac26d747d2a1182e419df3016ffd911f15689a4e972b0428aa8f45b0cc610d62/hostconfig.json 
{"Binds":null,"ContainerIDFile":"","LogConfig":{"Type":"json-file","Config":{}},"NetworkMode":"default","PortBindings":{"2181/tcp":[{"HostIp":"","HostPort":"2182"}]},"RestartPolicy":{"Name":"no","MaximumRetryCount":0},"AutoR
emove":false,"VolumeDriver":"","VolumesFrom":null,"CapAdd":null,"CapDrop":null,"Dns":[],"DnsOptions":[],"DnsSearch":[],"ExtraHosts":null,"GroupAdd":null,"IpcMode":"","Cgroup":"","Links":[],"OomScoreAdj":0,"PidMode":"","Privi
leged":false,"PublishAllPorts":false,"ReadonlyRootfs":false,"SecurityOpt":null,"UTSMode":"","UsernsMode":"","ShmSize":67108864,"Runtime":"runc","ConsoleSize":[0,0],"Isolation":"","CpuShares":0,"Memory":0,"NanoCpus":0,"Cgroup
Parent":"","BlkioWeight":0,"BlkioWeightDevice":null,"BlkioDeviceReadBps":null,"BlkioDeviceWriteBps":null,"BlkioDeviceReadIOps":null,"BlkioDeviceWriteIOps":null,"CpuPeriod":0,"CpuQuota":0,"CpuRealtimePeriod":0,"CpuRealtimeRun
  me":0,"CpusetCpus":"","CpusetMems":"","Devices":[],"DiskQuota":0,"KernelMemory":0,"MemoryReservation":0,"MemorySwap":0,"MemorySwappiness":-1,"OomKillDisable":false,"PidsLimit":0,"Ulimits":null,"CpuCount":0,"CpuPercent":0,"
IOMaximumIOps":0,"IOMaximumBandwidth":0}

剩余两个容器同样方法修改

3.1.5 启动Docker与服务

systemctl start docker && docker start t_zk1 t_zk2 t_zk3

3.1.6 对比端口是否新增完成

[root@k8s-master1 ~]# docker ps | grep t_zk
edb499903bba        zookeeper                     "/docker-entrypoin..."   About an hour ago   Up 8 minutes           2181/tcp, 2888/tcp, 3888/tcp, 8080/tcp                                                       t_zk3
ac26d747d2a1        zookeeper                     "/docker-entrypoin..."   About an hour ago   Up 8 minutes           2181/tcp, 2888/tcp, 3888/tcp, 8080/tcp                                                       t_zk2
35f5f39f0c24        zookeeper                     "/docker-entrypoin..."   About an hour ago   Up 8 minutes           2181/tcp, 2888/tcp, 3888/tcp, 8080/tcp                                                       t_zk1


[root@k8s-master1 ~]# docker ps | grep t_zk
edb499903bba        zookeeper                     "/docker-entrypoin..."   About an hour ago   Up 6 minutes             2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp, 8080/tcp                                         t_zk3
ac26d747d2a1        zookeeper                     "/docker-entrypoin..."   About an hour ago   Up 6 minutes             2888/tcp, 3888/tcp, 8080/tcp, 0.0.0.0:2182->2181/tcp                                         t_zk2
35f5f39f0c24        zookeeper                     "/docker-entrypoin..."   About an hour ago   Up 6 minutes             2888/tcp, 3888/tcp, 8080/tcp, 0.0.0.0:2183->2181/tcp                                         t_zk1

四、原生安装方式(单机版)

4.1.1 下载Zookeeper最新版本

4.1.2 配置文件

  • 打开文件夹/usr/local/apache-zookeeper-3.6.2-bin/conf
  • 将zoo_sample.cfg 复制一份命名为zoo.cfg

4.1.3 Start Zookeeper

  • 打开文件夹/usr/local/apache-zookeeper-3.6.2-bin/bin
  • Mac执行命令行执行 ./zkServer.sh start
  • Windows同学执行对应的cmd文件

启动完成以后出现这个就代表着启动成功

ZooKeeper JMX enabled by default
Using config: /usr/local/apache-zookeeper-3.5.5/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

4.1.4 验证

./zkCli.sh

最后打印出一堆东西最后一行是
WatchedEvent state:SyncConnected type:None path:null

4.1.5 停止

./zkServer.sh stop

我的CSDN博客


# Docker # Zookeeper