zookeeper(二):linux centos下安装zookeeper(单机和集群)

时间:2023-03-09 08:08:49
zookeeper(二):linux centos下安装zookeeper(单机和集群)

下载

http://zookeeper.apache.org/releases.html

解压

tar –zxvf zookeeper-3.4.6.tar.gz

解压文件到"/usr/local/zookeeper-3.4.6".

复制conf目录下的zoo_sample.cfg,并命名为zoo.cfg

修改zoo.cfg配置文件

# The number of milliseconds of each tick
tickTime=
# The number of ticks that the initial
# synchronization phase can take
initLimit=
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/datatmp/zookeeper/data
dataLogDir=/datatmp/zookeeper/logs
# the port at which the clients will connect
clientPort=
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=
#
# 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=
# Purge task interval in hours
# Set to "" to disable auto purge feature
#autopurge.purgeInterval= #, are election port
server.=192.168.93.121::

配置详解

1.2888端口号是zookeeper服务之间通信的端口

2.888端口是zookeeper与其他应用程序通信的端口

3.initLimit:这个配置项是用来配置Zookeeper接受客户端(这里所说的客户端不是用户连接Zookeeper服务器的客户端,而是Zookeeper服务器集群中连接到 Leader 的 Follower 服务器)初始化连接时最长能忍受多少个心跳时间间隔数。当已经超过 10 个心跳的时间(也就是 tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 5*2000=10 秒。

4.syncLimit:这个配置项标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 2*2000=4 秒。

5.server.A=B:C:D:其中 A 是一个数字,表示这个是第几号服务器;B 是这个服务器的 ip 地址;C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于 B 都是一样,所以不同的 Zookeeper 实例通信端口号不能一样,所以要给它们分配不同的端口号。

创建dataDir参数指定的目录

配置文件zoo.cfg里的dataDir,我这里指的是“ /datatmp/zookeeper/data”,并在目录下创建文件,命名为“myid”。

编辑“myid”文件

在对应的IP的机器上输入对应的编号。如在zookeeper上,“myid”文件内容就是1。由于本次只在单点上进行安装配置,所以只有一个server.1。若还有其他服务器,比如地址为192.168.1.102,则在zoo.cfg文件中还需加入server.2=192.168.1.102:2888:3888。那么myid文件在192.168.1.102服务器上的内容就是2。至此,如果是多服务器配置,就需要将zookeeper-3.4.3目录拷贝到其他服务器,然后按照上述的方法修改myid。

在/etc/profile文件中设置PATH

修改profile文件:

sudo vi /etc/profile
export ZOOKEEPER_HOME=/home/hadooptest/zookeeper-3.4.3
export PATH=$ZOOKEEPER_HOME/bin:$PATH
source /etc/profile

在防火墙中打开要用到的端口2181 2888 3888

sudo chkconfig iptables on
service iptables start

编辑 /etc/sysconfig/iptables

vi /etc/sysconfig/iptables

增加以下内容:

-A INPUT -m state --state NEW -m tcp -p tcp --dport  -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport -j ACCEPT

重启防火墙

service iptables restart

检查防火墙状态

service iptables status

OH YEAH!!! 安装完毕!

启动

进入bin目录

./zkServer.sh start

或者

sh zkServer.sh start

输入jps命令查看进程

1573 QuorumPeerMain
1654 Jps

其中,QuorumPeerMain是zookeeper进程,启动正常。

查看状态

还是bin目录下

./zkServer.sh status

或者

sh zkServer.sh status
-
JMX enabled by default
Using config: /usr/local/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: standalone

4、启动客户端脚本:zookeeper-3.4.3/bin/zkCli.sh -server zookeeper:2181

5、停止zookeeper进程:

./zkServer.sh stop

或者

sh zkServer.sh stop

集群搭建

和单机的区别不大,就是配置文件zoo.cfg里把所有的机器都加上

server.=192.168.93.121::
server.=192.168.93.122::
server.=192.168.93.123::
// 如果有更多,就都加上

但是,记得集群里的server总数最好是单数,不要是双数,具体官方有解释

官方解释

The entries of the form server.X list the servers that make up the ZooKeeper service. When the server starts up, it knows which server it is by looking for the file myid in the data directory. That file has the contains the server number, in ASCII.

Finally, note the two port numbers after each server name: " 2888" and "3888". Peers use the former port to connect to other peers. Such a connection is necessary so that peers can communicate, for example, to agree upon the order of updates. More specifically, a ZooKeeper server uses this port to connect followers to the leader. When a new leader arises, a follower opens a TCP connection to the leader using this port. Because the default leader election also uses TCP, we currently require another port for leader election. This is the second port in the server entry.

我的翻译

表单server.X的条目列出构成ZooKeeper服务的服务器。当服务器启动时,它通过查找数据目录中的文件myid来知道它是哪个服务器 。该文件包含服务器编号,以ASCII格式显示。

最后,请注意每个服务器名称后面的两个端口号:“2888”和“3888”。对等体使用前端口连接到其他对等体。这样的连接是必要的,使得对等体可以进行通信,例如,以商定更新的顺序。更具体地说,一个ZooKeeper服务器使用这个端口来连接追随者到领导者。当新的领导者出现时,追随者使用此端口打开与领导者的TCP连接。因为默认领导选举也使用TCP,所以我们目前需要另外一个端口进行领导选举。这是服务器条目中的第二个端口。

然后就是不要忘记各个Server的dataDir目录里的myid文件的数字也要对应上。

最后分别启动各个server就OK了。

121

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

122

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

123

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

对比单机模式启动的Mode: standalone,可以发现这边分别是Mode: leaderMode: follower,说明集群环境已经好了。

最后注意

有的时候服务器重启,导致开放的2181端口失效,需要重启下iptables,不然访问zookeeper会出现如下错误

错误 Will not attempt to authenticate using SASL (unknown error)