【Zookeeper】Re03 集群搭建
我这里采用的是模拟真实情况:
部署三台虚拟机Centos7
192.168.242.101 192.168.242.102 192.168.242.103
每台机器都安装Zookeeper且一致:
/usr/local/apache-zookeeper-3.7.0-bin/
要三台机器上的Zookeeper相互通信,设置比较简单
1、找到ZK配置的dataDir路径,配置myid文件,内容即服务编号
[root@localhost ~]# cat /usr/local/apache-zookeeper-3.7.0-bin/conf/zoo.cfg # The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=/tmp/zookeeper # the port at which the clients will connect clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1 ## Metrics Providers # # https://prometheus.io Metrics Exporter #metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider #metricsProvider.httpPort=7000 #metricsProvider.exportJvmInfo=true server.1 = 192.168.242.101:2182:3888 server.2 = 192.168.242.102:2182:3888 server.3 = 192.168.242.103:2182:3888 [root@localhost ~]#
zoo.cfg默认是找zoo_sample.cfg复制粘贴的
可以看到dataDir默认是配置在 /tmp/zookeeper
然后配置myid文件:
[root@192.168.242.101 ~]# echo 1 > /tmp/zookeeper/myid [root@192.168.242.102 ~]# echo 2 > /tmp/zookeeper/myid [root@192.168.242.103 ~]# echo 3 > /tmp/zookeeper/myid
然后回到zoo.cfg,把所有Zookeeper都注册一遍:
vim /usr/local/apache-zookeeper-3.7.0-bin/conf/zoo.cfg
格式: server.[myid编号] = ZK服务IP:ZK服务端口:选举端口
在最底下空行处追加
server.1 = 192.168.242.101:2182:3888 server.2 = 192.168.242.102:2182:3888 server.3 = 192.168.242.103:2182:3888
这里要注意,不能再使用单例模式的2181端口,否则ZK报错启动失败,端口已被占用
选举端口在黑马的教程中是使用3881端口,尚硅谷则使用3888,应该是都可以使用的
下一步是开放防火墙端口,也可以直接选择关闭防火墙:
systemctl start firewalld firewall-cmd --zone=public --add-port=2182/tcp --permanent firewall-cmd --zone=public --add-port=3888/tcp --permanent firewall-cmd --reload
然后依次启动Zookeeper:
/usr/local/apache-zookeeper-3.7.0-bin/bin/zkServer.sh start
启动之后检查一下各个ZK的状态是否正常
1号机ZK:
[root@localhost ~]# /usr/local/apache-zookeeper-3.7.0-bin/bin/zkServer.sh status /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Client SSL: false. Mode: leader [root@localhost ~]#
2号机ZK:
[root@localhost ~]# /usr/local/apache-zookeeper-3.7.0-bin/bin/zkServer.sh status /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Client SSL: false. Mode: follower [root@localhost ~]#
3号机ZK:
[root@localhost ~]# /usr/local/apache-zookeeper-3.7.0-bin/bin/zkServer.sh status /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Client SSL: false. Mode: follower [root@localhost ~]#
可以看到模式发生变化了,选举机制将1号机ZK任命为Leader,2和3号机ZK则任命为Follower
说明集群部署成功
这里补充一个尚硅谷的集群控制脚本,非常实用
# 集群控制脚本 vim /usr/bin/zk-cluster ###################################################################### #!/bin/bash case $1 in "start"){ for i in 192.168.242.131 192.168.242.132 192.168.242.133 do echo ---------- zookeeper $i 启动 ------------ ssh $i "/opt/module/apache-zookeeper-3.7.0/bin/zkServer.sh start" done };; "stop"){ for i in 192.168.242.131 192.168.242.132 192.168.242.133 do echo ---------- zookeeper $i 停止 ------------ ssh $i "/opt/module/apache-zookeeper-3.7.0/bin/zkServer.sh stop" done };; "status"){ for i in 192.168.242.131 192.168.242.132 192.168.242.133 do echo ---------- zookeeper $i 状态 ------------ ssh $i "/opt/module/apache-zookeeper-3.7.0/bin/zkServer.sh status" done };; esac ###################################################################### # 可执行权限赋予 chmod +x /usr/bin/zk-cluster # 集群操作 zk-cluster start zk-cluster stop zk-cluster status