一、準備
1、安裝三臺虛擬機。(可以用克隆的方式)
2、安裝jdk
由於zookeeper依賴於jdk環境,因此首先三臺虛擬機都需要安裝jdk。
參考:Linux Centos7.5 安裝配置Oracle jdk1.8
至此,三臺虛擬機的jdk環境安裝成功。
3、安裝zookeeper(ps:我是安裝在/usr/local目錄下,當然你可以隨意)
ps:這裏先安裝在其中一個虛擬機中即可,我是先安裝在192.168.160.128上。
1) 下載安裝包
wget https://www-us.apache.org/dist/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz
2)解壓
tar -zxvf zookeeper-3.4.10.tar.gz
3)修改zoo.cfg
cd /usr/local/zookeeper-3.4.10/conf
cp zoo_sample.cfg zoo.cfg
vim zoo.cfg
編輯內容如下(完整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-3.4.10/dataDir
dataLogDir=/usr/local/zookeeper-3.4.10/dataLogDir
# 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
server.1=192.168.160.128:2188:2888
server.2=192.168.160.129:2188:2888
server.3=192.168.160.130:2188:2888
4)添加環境變量
vim /etc/profile #編輯配置文件
export JAVA_HOME="/opt/jdk1.8.0_191"
export CLASSPATH=".:$JAVA_HOME/lib"
export MAVEN_HOME="/usr/local/apache-maven-3.6.0"
export ZOOKEEPER_HOME="/usr/local/zookeeper-3.4.10"
export NGINX_HOME="/usr/local/nginx"
export PATH="$JAVA_HOME/bin:$MAVEN_HOME/bin:$ZOOKEEPER_HOME/bin:$NGINX_HOME/sbin:$PATH"
source /etc/profile #讓配置文件生效
【注:】
1、/etc/profile 配置文件一定要配置對,否則生效之後,會導致很多命令找不到
2、如果配置文件你配置錯了,並且讓其生效了,導致很多原本生效的命令提示找不到的話,可以在命令行中執行:
export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/X11R6/bin,然後趕緊去/etc/profile將配置修改正確。
5)將192.168.160.128虛擬機上安裝的 zookeeper-3.4.10拷貝到其它兩臺虛擬機中。
scp -r zookeeper-3.4.10/ [email protected]:/usr/local
scp -r zookeeper-3.4.10/ [email protected]:/usr/local
如圖:
6) 拷貝完成後,可以發現在192.168.160.129與192.168.160.130兩臺虛擬機中/usr/local中已經出現zookeeper-3.4.10目錄了。然後將其它兩臺的/etc/profile的zookeeper環境變量添加進去,然後source /etc/profile 讓其生效。(生效後,可以用Java或者zookeeper的命令檢查下,環境變量有沒有配置錯誤)
7)修改各個服務器中的myid
echo "2">myid #在129中執行
echo "3">myid #在130中執行
8)注意:
啓動192.168.160.128zookeeper時,啓動未報錯,查看狀態報錯了,如圖:
查看日誌:
cat zookeeper.out
查看三臺服務器的防火牆,發現防火牆都是關閉的,因此我這裏跟防火牆肯定是沒有關係的。但是三臺服務器防火牆都需要關閉,或者你爲了安全性,去三臺服務器單獨開放你zookeeper使用的端口號。
附:防火牆的常用命令
systemctl stop firewalld.service (關閉防火牆)
systemctl disable firewalld.service (禁用防火牆)
systemctl status firewalld.service (查看防火牆狀態)
這時候報錯我覺得是因爲另外兩臺服務器的zookeeper必須啓動一臺,zookeeper集羣中啓動的zookeeper服務必須大於1/2,在啓動zookeeper之前,建議先使用命令netstat -nap查看各個服務器中有沒有zookeeper中用到的端口被佔用了,有的話就kill -9 pid(pid:進程號)
ps:./zkServer.sh start-foreground 這個命令可以啓動zookeeper並查看日誌
我自己試了一下,如果zookeeper集羣中啓動的zookeeper服務必須小於1/2,會提示如下圖:
首先用命令./zkServer.sh start 啓動最少兩臺服務器的zookeeper服務,然後再用./zkServer.sh status去查看。
下面是我在百度找了一堆解決辦法,但是並沒有解決我的問題:
1、/etc/hosts文件,將第一行註釋掉,但是我這裏並沒有註釋掉,一樣可以成功,因此我感覺跟這個沒有關係。
2、在zoo.cfg中添加quorumListenOnAllIPs=true,我的zoo.cfg完整如下,並沒有添加這一行,也照樣沒有問題。