本文介绍如何3台linux机器搭建zookeeper集群,并启动
- 环境
- 安装
- 设置主机名映射
- 把zk包上传到linux并解压
- 增加环境变量
- 修改zk配置文件
- 在配置的dataDir目录中创建myid文件
- 将配置好的zk文件夹分发到另外两台机器上
- 根据配置需要创建dataDir目录并添加myid文件
- 启动每台机器
- zkServer.sh其它命令
- 部署错误处理
- Error contacting service. It is probably not running.
环境
3台Centos7、3台机器之前能够ping通、jdk、zookeeper-3.4.9
注本教程默认jdk已经安装好了,可用java、javac、java -version查看是否安装好
安装
设置主机名映射
三台机器之前互相映射主机名,之后编写集群信息时就可以用主机名代替ip
vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.31.201 mym
192.168.31.202 mini2
192.168.31.203 mini3
把zk包上传到linux并解压
先上传到mym机器上进行操作 解压
tar -zvxf zookeeper-3.4.9.tar.gz
更名到/usr/local目录
mv zookeeper-3.4.9 /usr/local/zookeeper
此时可在/usr/local中看到zookeeper目录
增加环境变量
vi /etc/profile
增加如下内容
export ZOOKEEPER_HOME=/usr/local/zookeeper
export PATH=$PATH:$ZOOKEEPER_HOME/bin
source /etc/profile
修改zk配置文件
cd /usr/local/zookeeper/conf/
进入到配置目录
查看该目录内容:
[root@mym conf]# ls
configuration.xsl log4j.properties zoo_sample.cfg
需要把zoo_sample.cfg复制一份名为zoo.conf的文件作为配置文件
cp zoo_sample.cfg zoo.cfg
修改zoo.cfg:vi 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
- 修改zk存储数据的目录(dataDir项)
自定义设置数据目录,该目录如果不存在需要手动创建
mkdir zkDataFile
dataDir=/usr/local/zkDataFile
- 配置集群信息
添加如下:
其中server.1表示三台中唯一的key,mym表示主机名(若没有映射主机名可直接写ip),2888是心跳检测端口,3888是选举端口
# cluster info
server.1=mym:2888:3888
server.2=mini2:2888:3888
server.3=mini3:2888:3888
在配置的dataDir目录中创建myid文件
不同机器上根据配置的==server.id==来设置内容
cd /usr/local/zkDataFile
echo 1 > myid
将配置好的zk文件夹分发到另外两台机器上
scp -r /usr/local/zookeeper/ root@mini2:/usr/local/
scp -r /usr/local/zookeeper/ root@mini3:/usr/local/
此时可以看到另两台机器上`/usr/local/`目录中已经有了`zookeeper`目录
根据配置需要创建dataDir目录并添加myid文件
由于个人把dataDir文件放到了zookeeper目录之外,故另外两台机器的dataDir目录仍然需要手动创建,并创建myid文件
`mini2机器`
mkdir /usr/local/zkDataFile
echo 2 > /usr/local/zkDataFile/myid
`mini3机器`
mkdir /usr/local/zkDataFile
echo 3 > /usr/local/zkDataFile/myid
启动每台机器
和一般启动方式一样,进入到bin目录中,找到启动sh脚本进行启动
cd /usr/local/zookeeper/bin
`bin`目录中内容
-rwxr-xr-x. 1 1001 1001 232 Aug 23 2016 README.txt
-rwxr-xr-x. 1 1001 1001 1937 Aug 23 2016 zkCleanup.sh 清除数据
-rwxr-xr-x. 1 1001 1001 1032 Aug 23 2016 zkCli.cmd windows客户端
-rwxr-xr-x. 1 1001 1001 1534 Aug 23 2016 zkCli.sh linux客户端
-rwxr-xr-x. 1 1001 1001 1579 Aug 23 2016 zkEnv.cmd
-rwxr-xr-x. 1 1001 1001 2696 Aug 23 2016 zkEnv.sh
-rwxr-xr-x. 1 1001 1001 1065 Aug 23 2016 zkServer.cmd windows服务端
-rwxr-xr-x. 1 1001 1001 6773 Aug 23 2016 zkServer.sh linux服务端
启动
./zkServer.sh start
启动结果
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
则说明启动成功。
zkServer.sh其它命令
- 启动ZK服务:
./zkServer.sh start
- 查看ZK服务状态:
./zkServer.sh status
- 停止ZK服务:
./zkServer.sh stop
- 重启ZK服务:
./zkServer.sh restart
那么我们就查看zk的状态,结果如下
mym机器
[root@mym bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: follower
说明机器mym是从节点
mini3机器
[root@mini3 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: leader
说明机器mini3是主节点
部署错误处理
Error contacting service. It is probably not running.
使用
./zkServer.sh status
命令查看状态是报错
[root@localhost bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
查看是否zk成功启动
[root@localhost bin]# jps
4128 QuorumPeerMain
4210 Jps
QuorumPeerMain是zk的入口类,说明已经启动成功。
那么查看运行日志,运行日志在bin目录下,这个位置也可以在zoo.cfg中进行配置,这里查看日志。注:本错误出现在mini2机器上
tail -Fn 200 zookeeper.out
输出如下错误
2018-06-21 18:45:09,638 [myid:2] - INFO [QuorumPeer[myid=2]/0:0:0:0:0:0:0:0:2181:FastLeaderElection@852] - Notification time out: 60000
2018-06-21 18:46:14,651 [myid:2] - WARN [QuorumPeer[myid=2]/0:0:0:0:0:0:0:0:2181:QuorumCnxManager@400] - Cannot open channel to 1 at election address mym/192.168.216.201:3888
java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:381)
at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectAll(QuorumCnxManager.java:426)
at org.apache.zookeeper.server.quorum.FastLeaderElection.lookForLeader(FastLeaderElection.java:843)
at org.apache.zookeeper.server.quorum.QuorumPeer.run(QuorumPeer.java:822)
2018-06-21 18:46:14,652 [myid:2] - INFO [QuorumPeer[myid=2]/0:0:0:0:0:0:0:0:2181:QuorumPeer$QuorumServer@149] - Resolved hostname: mym to address: mym/192.168.216.201
可知,mini2机器上配置的mym机器映射ip为192.168.216.201
但是不能通信。那么我们去看下mym机器的ip
在mym机器上:ifconfig
可知mym机器的ip是192.168.31.201
。说明mini2机器的主机映射名配置错了。
解决
修改主机名映射vi /etc/hosts
[root@localhost bin]# vi /etc/hosts
192.168.216.201 mym
192.168.216.202 mini2
192.168.216.203 mini3
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
- 把
192.168.216.201
修改为192.168.31.201
。 - 把
192.168.216.201
修改为192.168.31.201
。
重启zk即可