zookeeper集群配置与启动——实战

时间:2024-06-19 20:33:56

1,准备:

A:三台linxu服务器:

10.112.29.177

10.112.29.172

10.112.29.174

命令 hostname 得到每台机器的 hostname

vm-10-112-29-177

vm-10-112-29-172

vm-10-112-29-174

分别创建

/opt/zookeeper/server1/data

/opt/zookeeper/server1/dataLog

/opt/zookeeper/server2/data

/opt/zookeeper/server2/dataLog

/opt/zookeeper/server3/data

/opt/zookeeper/server3/dataLog

B:下载zookeeper

版本:zookeeper-3.4.8.tar.gz

解压 tar -zxvf zookeeper-3.4.8.tar.gz

zookeeper-3.4.8

C:下载jdk

版本: jdk-7u80-linux-x64.gz

解压 tar -zxvf jdk-7u80-linux-x64.gz

jdk1.7.0_80

D:配置java环境变量

/etc/profile 文件中加入:
export JAVA_HOME=/usr/java/jdk1.7.0_80
export PATH=$PATH:/usr/java/jdk1.7.0_80/bin

export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

source /etc/profile  //执行生效
查看jdk版本 java -version

java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)

2,zookeeper集群配置
A:修改配置文件
重命名 conf/zoo_sample.cfg 配置文件
mv zoo_sample.cfg 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=/opt/zookeeper/server1/data
dataLogDir=/opt/zookeeper/server1/dataLog

server.1=vm-10-112-29-177:2888:3888
server.2=vm-10-112-29-172:2888:3888
server.3=vm-10-112-29-174:2888:3888

# 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

另外两台机器一样配置:

dataDir=/opt/zookeeper/server2/data
dataLogDir=/opt/zookeeper/server2/dataLog

dataDir=/opt/zookeeper/server3/data
dataLogDir=/opt/zookeeper/server3/dataLog

tickTime:zookeeper中使用的基本时间单位, 毫秒值。
initLimit:这个配置项是用来配置 Zookeeper 接受客户端(这里所说的客户端不是用户连接 Zookeeper 服务器的客户端,而是 Zookeeper 服务器集群中连接到 Leader 的 Follower 服务器)初始化连接时最长能忍受多少个 tickTime 时间间隔数。这里设置为5表名最长容忍时间为 5 2000 10 秒。
syncLimit:这个配置标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 2 2000 4 秒。
dataDir 和 dataLogDir 看配置就知道干吗的了,不用解释。
clientPort:监听client连接的端口号,这里说的client就是连接到Zookeeper的代码程序。
server.{myid}={ip}:{leader服务器交换信息的端口}:{当leader服务器挂了后, 选举leader的端口}
maxClientCnxns:对于一个客户端的连接数限制,默认是60,这在大部分时候是足够了。但是在我们实际使用中发现,在测试环境经常超过这个数,经过调查发现有的团队将几十个应用全部部署到一台机器上,以方便测试,于是这个数字就超过了。
修改zoo.cfg非常简单,然后还需要创建myid文件:
cd server1
echo 1 > myid
然后拷贝server1为server2和server3,并修改其中的zoo.cfg配置,当然也要修改myid的内容为23
下面给出3个server的zoo.cfg 内容:
# server1
tickTime=2000
initLimit=5
syncLimit=2
dataDir=/opt/zookeeper/server1/data
dataLogDir=/opt/zookeeper/server1/dataLog
clientPort=2181
server.1=vm-10-112-29-177:2888:3888
server.2=vm-10-112-29-172:2888:3888
server.3=vm-10-112-29-174:2888:3888

3,启动

进入 /usr/bigdata/zookeeper-3.4.8/bin 目录,使用 ./zkServer.sh start 启动zk服务。(你也可以使用 ./zkServer.sh start myzoo.cfg 指定配置文件启动,这在自动化运维的时候很有用)
使用 tail -f zookeeper.out 查看日志。
 
这个时候可能会报错。
2016-08-06 12:55:48,758 [myid:] - INFO  [main:QuorumPeerConfig@103] - Reading configuration from: /usr/bigdata/zookeeper-3.4.8/bin/../conf/zoo.cfg
2016-08-06 12:55:48,783 [myid:] - WARN [main:QuorumPeer$QuorumServer@155] - Failed to resolve address: vm-10-112-29-174
java.net.UnknownHostException: vm-10-112-29-174: Name or service not known
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:901)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1295)
at java.net.InetAddress.getAllByName0(InetAddress.java:1248)
at java.net.InetAddress.getAllByName(InetAddress.java:1164)
at java.net.InetAddress.getAllByName(InetAddress.java:1098)
at java.net.InetAddress.getByName(InetAddress.java:1048)
at org.apache.zookeeper.server.quorum.QuorumPeer$QuorumServer.recreateSocketAddresses(QuorumPeer.java:148)
at org.apache.zookeeper.server.quorum.QuorumPeer$QuorumServer.<init>(QuorumPeer.java:133)
at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parseProperties(QuorumPeerConfig.java:207)
at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerConfig.java:119)
at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:101)
at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:78)
2016-08-06 12:55:48,790 [myid:] - WARN [main:QuorumPeer$QuorumServer@155] - Failed to resolve address: vm-10-112-29-172
java.net.UnknownHostException: vm-10-112-29-172: Name or service not known
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:901)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1295)
at java.net.InetAddress.getAllByName0(InetAddress.java:1248)
at java.net.InetAddress.getAllByName(InetAddress.java:1164)
at java.net.InetAddress.getAllByName(InetAddress.java:1098)
at java.net.InetAddress.getByName(InetAddress.java:1048)
at org.apache.zookeeper.server.quorum.QuorumPeer$QuorumServer.recreateSocketAddresses(QuorumPeer.java:148)
at org.apache.zookeeper.server.quorum.QuorumPeer$QuorumServer.<init>(QuorumPeer.java:133)
at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parseProperties(QuorumPeerConfig.java:207)
at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerConfig.java:119)
at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:101)
at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:78)
2016-08-06 12:55:48,791 [myid:] - INFO [main:QuorumPeer$QuorumServer@149] - Resolved hostname: vm-10-112-29-177 to address: vm-10-112-29-177/127.0.0.1
2016-08-06 12:55:48,791 [myid:] - INFO [main:QuorumPeerConfig@331] - Defaulting to majority quorums
2016-08-06 12:55:48,795 [myid:1] - INFO [main:DatadirCleanupManager@78] - autopurge.snapRetainCount set to 3
2016-08-06 12:55:48,795 [myid:1] - INFO [main:DatadirCleanupManager@79] - autopurge.purgeInterval set to 0
2016-08-06 12:55:48,795 [myid:1] - INFO [main:DatadirCleanupManager@101] - Purge task is not scheduled.
2016-08-06 12:55:48,810 [myid:1] - INFO [main:QuorumPeerMain@127] - Starting quorum peer
2016-08-06 12:55:48,820 [myid:1] - INFO [main:NIOServerCnxnFactory@89] - binding to port 0.0.0.0/0.0.0.0:2181
2016-08-06 12:55:48,820 [myid:1] - ERROR [main:QuorumPeerMain@89] - Unexpected exception, exiting abnormally
java.net.BindException: Address already in use
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:463)
at sun.nio.ch.Net.bind(Net.java:455)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:67)
at org.apache.zookeeper.server.NIOServerCnxnFactory.configure(NIOServerCnxnFactory.java:90)
at org.apache.zookeeper.server.quorum.QuorumPeerMain.runFromConfig(QuorumPeerMain.java:130)
at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:111)
at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:78)

  

修改hosts 文件 增加ip主机对应关系:

10.112.29.174 vm-10-112-29-174
10.112.29.177 vm-10-112-29-177
10.112.29.172 vm-10-112-29-172

注意:zookeeper启动时根据hostname找主机ip,127.0.0.1  vm-10-112-29-174  则找到的是127.0.0.1  ,去掉127.0.0.1  对应 vm-10-112-29-174  配置就可以正常启动集群了。