1、准备好ZooKeeper-3.4.6的安装包 ,分别放到3台机器上的/usr/local目录
2、配置hosts,详细明细如下
10.99.1.161 zkServer1-161
10.99.1.162 zkServer2-162
10.99.1.163 zkServer3-163
3、设置myid
解压zookeeper安装文件,进入zookeeper目录,创建data目录,并在data目录下面创建myid文件,写入相应的数值
10.99.1.161 1
10.99.1.162 2
10.99.1.163 3
命令如下:
echo 1 > /usr/local/zookeeper-3.4.6/data/myid
echo 2 > /usr/local/zookeeper-3.4.6/data/myid
echo 3 > /usr/local/zookeeper-3.4.6/data/myid
4、修改修改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=/usr/local/zookeeper-3.4.6/data # 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=zkServer1-161:2888:3888 server.2=zkServer2-162:2888:3888 server.3=zkServer3-163:2888:3888
复制zoo.cfg文件到另2台机器中
以上内容的配置,参照了ZooKeeper的官方文档:zookeeperStarted.html。server.X用来配置ZooKeeper集群中的各节点,并建议X的值和myid保持一致。
端口2181用于监听客户端的连接,端口2888用于Leader监听Follower的连接,而3888则用于Leader选举。
5、 启动ZooKeeper集群
bin目录下的脚本zkServer.sh用来启动ZooKeeper集群,但需要带一个start参数,如:
cd /usr/local/zookeeper-3.4.6/bin/ && ./zkServer.sh start
由于启动时,每个节点都会试图去连接其它节点,因此先启动的刚开始会连接不上其它的,导致日志中会包含错误信息,在未全启动之前,这个属正常现象。
6. 安装验证
脚本zkServer.sh不但可以用来启动ZooKeeper,还可以用来查看状态。使用方式为带一个status参数,如:
./zkServer.sh status
JMX enabled by default Using config: /usr/local/zookeeper-3.4.6/bin/../conf/zoo.cfg Mode: follower
7、基本命令,创建节点验证数据同步
./zkCli.sh -server 10.99.1.161:2181
参数“-server”中只含一个“-”,用以指定被连接的ZooKeeper节点,可以为Leader,也可以为Follower,“10.12.154.78”为Leader或Follower的IP或主机名,“2181”为ZooKeerp提供的客户端服务端口。
创建一个节点test_node,看看是否同步到集群中的另外2台机器中
[zk: 10.99.1.161:2181(CONNECTED) 0] ls [zk: 10.99.1.161:2181(CONNECTED) 1] ls / [zookeeper] [zk: 10.99.1.161:2181(CONNECTED) 2] create /test_node test_data Created /test_node [zk: 10.99.1.161:2181(CONNECTED) 3] ls / [test_node, zookeeper] [zk: 10.99.1.161:2181(CONNECTED) 4]
查看另2台机器的数据
162
[zk: 10.99.1.162:2181(CONNECTED) 0] ls / [test_node, zookeeper] [zk: 10.99.1.162:2181(CONNECTED) 1]
163
[zk: 10.99.1.163:2181(CONNECTED) 0] ls / [test_node, zookeeper] [zk: 10.99.1.163:2181(CONNECTED) 1]
数据都已经同步到另外2台机器中,验证结束,至此,zookeeper的集群安装完成!