我這里采用的是模擬真實情況:
部署三台虛擬機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
說明集群部署成功