今天搭建Zookeeper集羣時,遇到了一些問題,好頭疼的說,不過,也經過本身動手實踐理解了許多,總的來講,實踐出真知,當真不錯。下面說下集羣的搭建吧,但願可以幫助你們。vim
1、環境:虛擬機CentOs7系統,完整的環境,請確認已安裝JDK,Zookeeper,可經過克隆已配置好的虛擬機環境,以下:網絡
具體的克隆步驟自行百度,很簡單。注意集羣部署的節點至少爲3個,更多時爲奇數個。這次開啓3個節點進行集羣部署。dom
2、網絡配置:默認的NAT鏈接,可保證虛擬機之間的鏈接oop
(1)修改主機名測試
vim /etc/sysconfig/network
修改後內容爲:this
# Created by anaconda NETWORKING=yes HOSTNAME=slave01
其餘兩個虛擬機一樣如此設置,分別命名爲:slave02,slave03。scala
(2)修改主機IP地址rest
vim /etc/sysconfig/network-scripts/ifcfg-lo
該文件名稱可能不盡相同,但前綴一致,修改後內容爲:code
DEVICE=lo IPADDR=虛擬機IP NETMASK=255.255.255.0 NETWORK=網絡地址 # If you're having problems with gated making 127.0.0.0/8 a martian, # you can change this to something else (255.255.255.255, for example) BROADCAST=127.255.255.255 ONBOOT=yes NAME=loopback
根據本身虛擬機的IP進行設置,查看IP的命令:ifconfig,其餘兩個虛擬機一樣如此設置,文件裏的參數表示爲(若無,可忽略),設置完成後,執行命令 service network restart: server
DEVICE=eth0 //指出設備名稱 BOOTPROT=static //啓動類型 dhcp|static,使用橋接模式,必須是static HWADDR=00:06:5B:FE:DF:7C //硬件Mac地址 IPADDR=192.168.0.2 //IP地址 NETMASK=255.255.255.0 //子網掩碼 NETWORK=192.168.0.0 //網絡地址 GATEWAY=192.168.0.1 //網關地址 ONBOOT=yes //是否啓動應用 TYPE=Ethernet //網絡類型
(3)設置主機名與IP地址映射
vim /etc/hosts
修改後內容爲:
#127.0.0.1 slave01 localhost.localdomain localhost ::1 localhost 虛擬機IP1 slave01 虛擬機IP2 slave02 虛擬機IP3 slave03
注意,將/etc/hosts中的127.0.1.1這一行註釋掉,後面啓動可能會拒絕鏈接的錯誤。其餘兩個虛擬機一樣如此設置,注意映射的主機名和IP不要弄亂了。
3、網絡連通測試
前面全部的配置完成後,重啓這三個虛擬機,使主機名生效,而後分別在三個虛擬機上測試連通性:
#在slave01上測試 ping slave02 ping slave03 #在slave02上測試 ping slave01 ping slave03 #在slave03上測試 ping slave01 ping slave02
測試以上命令,若經過,則表示三臺虛擬機之間互通。固然也能夠測試下與外網的連通性:
ping www.baidu.com
4、Zookeeper集羣搭建
(1)環境配置,省略,請配置好須要的環境
(2)Zookeeper配置文件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=/usr/local/zookeeper/data # the port at which the clients will connect clientPort=2181 server.0=slave01:2888:3888 server.1=slave02:2888:3888 server.2=slave03:2888:3888
注意參數的意義,在此很少說了。注意,dataDir的路徑,修改完配置文件後,務必在該路徑下生成設置的文件夾,如上data文件夾,同時,server.0、server.一、server.2和data文件夾下的myid文件中的值對應,該myid本身建立並添加對應的值,分別爲:0,1,2。
(3)完成配置後,進行集羣測試
#在slave01上啓動Zookeeper ./zkServer.sh start #在slave02上啓動Zookeeper ./zkServer.sh start #在slave03上啓動Zookeeper ./zkServer.sh start
啓動後顯示爲:
[hadoop@slave01 bin]$ ./zkServer.sh start ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Starting zookeeper ... STARTED
執行命令 jps 可查看是否啓動成功,有該進程存在:
[hadoop@slave01 bin]$ jps 5370 Jps 5342 QuorumPeerMain
當三臺虛擬機都成功啓動後,分別在各自虛擬機上執行以下命令:
[hadoop@slave01 bin]$ ./zkServer.sh status ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Mode: follower
[hadoop@slave02 bin]$ ./zkServer.sh status ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Mode: leader
[hadoop@slave03 bin]$ ./zkServer.sh status ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Mode: follower
啓動和查看status時,注意防火牆是否關閉,CentOs7默認爲firewall:
查看防火牆狀態:firewall-cmd –-state 關閉防火牆:systemctl stop firewalld.service
至此,Zookeeper集羣已經部署成功了(另外,最好使用Hadoop建立的用戶進行操做,root用戶未測試,你們能夠試一試)。