在 Linux 多节点安装配置 Apache Zookeeper 分布式集群

规划:

三台物理服务器就形成了(法定人数)。对于高可用性集群,您可以使用高于3的任何奇数。例如,如果设置5台服务器,则集群可以处理两个故障节点等。

物理服务器需要开启的端口 2888 3888 和 2181 上有入站连接。如果启用了 IPtables 或 Firewall,请确保启用指定的端口,因为zookeeper 需要通过这些端口进行通信。

OS:Centos 7.4 x64
Zookeeper-3.4.10

在本教程中,我们将在以下3台服务器部署zookeeper分布式群集:

10.10.204.63
10.10.204.64
10.10.204.65

先决条件:

在安装 Zookeeper 之前,你应该在系统中安装并配置好 JDKOracle Java8,这将与Zookeeper配合使用。

Linux JAVA JDK JRE 环境变量安装与配置

步骤1: 在各个实例上安装Zookeeper。

 下载Zookeeper
 # cd /tmp
 # wget //apache.fayea.com/zookeeper/stable/zookeeper-3.4.10.tar.gz
 解压它
 # tar zxvf zookeeper-3.4.10.tar.gz
 移动 Zookeeper 到 /usr/local/
 # mv zookeeper-3.4.10 /usr/local/
 创建软连接
 # ln -s /usr/local/zookeeper-3.4.10 /usr/local/zookeeper
 拷贝配置文件
 # cp /usr/local/zookeeper/conf/zoo_sample.cfg /usr/local/zookeeper/conf/zoo.cfg
 创建数据及日志存放目录
 # mkdir -p /usr/local/zookeeper/data
 # mkdir -p /usr/local/zookeeper/logs
 新建用户
 # groupadd zookeeper
 # useradd -g zookeeper -s /sbin/nologin zookeeper
 赋予Zookeeper目录权限
 # chown -R zookeeper:zookeeper /usr/local/zookeeper-3.4.10 /usr/local/zookeeper
 # chmod +755 /usr/local/zookeeper-3.4.10

步骤2:修改配置文件。

 # vim /usr/local/zookeeper/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.
#
# //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

修改为:

#服务器之间或客户端与服务器之间维持心跳的时间间隔,每隔tickTime时间就会发送一个心跳。
tickTime=2000
#这个配置项是用来配置Zookeeper接受客户端(这里所说的客户端不是用户连接Zookeeper 服务器的客户端,而是Zookeeper服务器集群中连接到Leader的Follower 服务器)初始化连接时最长 能忍受多少个心跳时间间隔数。当已经超过 10 个心跳的时间(也就是 tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 10*2000=20 秒。
initLimit=10
#这个配置项标识Leader与Follower之间发送消息,请求和应答时间长度,最长不能超过多少个tickTime的时间长度,总的时间长度就是5*2000=10秒(适用于3.4以上版本)。
syncLimit=5
#这个参数和上面的参数搭配使用,这个参数指定了需要保留的文件数目。默认是保留3个。
autopurge.snapRetainCount=3
#这个参数指定了清理频率,单位是小时,需要填写一个1或更大的整数,默认是0,表示不开启自己清理功能(适用于3.4以上版本)。
autopurge.purgeInterval=1
maxClientCnxns=60
#修改数据目录(可以是任意目录)。
dataDir=/usr/local/zookeeper/data
#新增日志目录(可以是任意目录)。
dataLogDir=/usr/local/zookeeper/logs
#Zookeeper服务器监听的端口,以接受客户端的访问请求。
clientPort=2181
#新增以下内容。
server.1=10.10.204.63:2888:3888
server.2=10.10.204.64:2888:3888
server.3=10.10.204.65:2888:3888

步骤3:分别在各个Zookeeper实例中创建myid文件。

 # echo "1" >> /usr/local/zookeeper/data/myid
 # echo "2" >> /usr/local/zookeeper/data/myid
 # echo "3" >> /usr/local/zookeeper/data/myid

步骤4:添加系统变量。

编辑:/etc/profile  文件,添加以下内容:

 export ZOOKEEPER_HOME=/usr/local/zookeeper/
 export PATH=$ZOOKEEPER_HOME/bin:$PATH

执行以下命令使其系统变量永久生效:

 # source /etc/profile

步骤5:创建系统单元文件。

在 /usr/lib/systemd/system 目录下创建  zookeeper.service  ,并填写如下内容:

[Unit]
 Description=zookeeper.service
 After=network.target

[Service]
 Type=forking
 Environment=ZOO_LOG_DIR=/usr/local/zookeeper/
 ExecStart=/usr/local/zookeeper/bin/zkServer.sh start
 ExecStop=/usr/local/zookeeper/bin/zkServer.sh stop
 ExecReload=/usr/local/zookeeper/bin/zkServer.sh restart
 Restart=always
 User=zookeeper
 Group=zookeeper

[Install]
 WantedBy=multi-user.target

步骤6:启动Zookeeper。

重新加载配置信息:systemctl daemon-reload
启动zookeeper服务:systemctl start zookeeper.service
关闭zookeeper服务:systemctl stop zookeeper.service
查看进程状态及日志:systemctl status zookeeper.service
开机自启动:systemctl enable zookeeper.service
关闭自启动:systemctl disable zookeeper.service

步骤7:放行 2888、3888、2181 端口。

 # firewall-cmd --permanent --zone=public --add-port=2888/tcp
 # firewall-cmd --permanent --zone=public --add-port=3888/tcp
 # firewall-cmd --permanent --zone=public --add-port=2181/tcp

重载防火墙:

 # firewall-cmd --reload

步骤8:查看Zookeeper状态

分别检测3台服务器的运行状态是否正常。

查看 10.10.204.63 节点;

 [root@10-10-204-63 ~]# /usr/local/zookeeper/bin/zkServer.sh status
 ZooKeeper JMX enabled by default
 Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: leader

查看 10.10.204.64 节点;

 [root@10-10-204-64 ~]# /usr/local/zookeeper/bin/zkServer.sh status
 ZooKeeper JMX enabled by default
 Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: follower

查看 10.10.204.65 节点;

 [root@10-10-204-65 ~]# /usr/local/zookeeper/bin/zkServer.sh status
 ZooKeeper JMX enabled by default
 Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
 Mode: follower

服务器中ZooKeeper分别扮演者不同的角色,1台将处于 leader(领导)地位,另外2台将处于 follower (追随者)。如果您获得相同的结果,那么你已经正确地安装配置好了ZooKeeper集群服务器。

步骤9:在3台物理服务器中的任意一台使用客户端连接。

客户端连接信息如下所示:

 [root@10-10-204-63 ~]# /usr/local/zookeeper/bin/zkCli.sh -server 10.10.204.64:2181
 Connecting to 10.10.204.64:2181
 2017-08-13 20:30:11,816 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
 2017-08-13 20:30:11,863 [myid:] - INFO [main:Environment@100] - Client environment:host.name=103-28-204-63.10.10.204.63
 2017-08-13 20:30:11,863 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_144
 2017-08-13 20:30:11,875 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
 2017-08-13 20:30:11,883 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/java/jdk1.8.0_144/jre
 2017-08-13 20:30:11,883 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/usr/local/zookeeper/bin/../build/classes:/usr/local/zookeeper/bin/../build/lib/*.jar:/usr/local/zookeeper/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zookeeper/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zookeeper/bin/../lib/netty-3.10.5.Final.jar:/usr/local/zookeeper/bin/../lib/log4j-1.2.16.jar:/usr/local/zookeeper/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper/bin/../zookeeper-3.4.10.jar:/usr/local/zookeeper/bin/../src/java/lib/*.jar:/usr/local/zookeeper/bin/../conf:.:/usr/java/jdk1.8.0_144/jre/lib/rt.jar:/usr/java/jdk1.8.0_144/lib/dt.jar:/usr/java/jdk1.8.0_144/lib/tools.jar:/usr/java/jdk1.8.0_144/jre/lib
 2017-08-13 20:30:11,884 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
 2017-08-13 20:30:11,884 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
 2017-08-13 20:30:11,884 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=
 2017-08-13 20:30:11,884 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
 2017-08-13 20:30:11,884 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
 2017-08-13 20:30:11,885 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-514.21.2.el7.x86_64
 2017-08-13 20:30:11,885 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
 2017-08-13 20:30:11,885 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
 2017-08-13 20:30:11,885 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/root
 2017-08-13 20:30:11,893 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=10.10.204.64:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@69d0a921
 Welcome to ZooKeeper!
 2017-08-13 20:30:12,103 [myid:] - INFO [main-SendThread(10.10.204.64:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server 10.10.204.64/10.10.204.64:2181. Will not attempt to authenticate using SASL (unknown error)
 JLine support is enabled
 2017-08-13 20:30:12,768 [myid:] - INFO [main-SendThread(10.10.204.64:2181):ClientCnxn$SendThread@876] - Socket connection established to 10.10.204.64/10.10.204.64:2181, initiating session
 2017-08-13 20:30:12,935 [myid:] - INFO [main-SendThread(10.10.204.64:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server 10.10.204.64/10.10.204.64:2181, sessionid = 0x15dda7deb6c0000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
 [zk: 10.10.204.64:2181(CONNECTED) 2] create /renwoledb 'renwole' #创建数据节点
 Created /renwoledb
 [zk: 10.10.204.64:2181(CONNECTED) 3] get /renwoledb #调出节点数据
 renwole
 cZxid = 0x500000002
 ctime = Sun Aug 13 21:19:24 CST 2017
 mZxid = 0x500000002
 mtime = Sun Aug 13 21:19:24 CST 2017
 pZxid = 0x500000002
 cversion = 0
 dataVersion = 0
 aclVersion = 0
 ephemeralOwner = 0x0
 dataLength = 7
 numChildren = 0

整个zookeeper集群到此已经搭建并测试完成。如果 leader 节点出现故障,其他 follower (追随者)会投票选择新的 leader ,所以这就是我们想要的 Zookeeper 分布式集群。

转载请注明本文地址。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

Captcha Code