参考http://kaibinyuan.blog.51cto.com/7304008/1616454
参考http://luchunli.blog.51cto.com/2368057/1681841
1.安装zookeeper单机模式
配置java环境
[root@zookeeper ~]# wget https://www.reucon.com/cdn/java/jdk-8u45-linux-x64.tar.gz [root@zookeeper ~]# tar xf jdk-8u45-linux-x64.tar.gz [root@zookeeper ~]# cp -r jdk1.8.0_45 /usr/local/ [root@zookeeper ~]# cat /etc/profile.d/java.sh JAVA_HOME=/usr/local/jdk1.8.0_45 JAVA_BIN=/usr/local/jdk1.8.0_45/bin JRE_HOME=/usr/local/jdk1.8.0_45/jre PATH=$PATH://usr/local/jdk1.8.0_45/bin:/usr/local/jdk1.8.0_45/jre/bin CLASSPATH=/usr/local/jdk1.8.0_45/jre/lib:/usr/local/jdk1.8.0_45/lib:/usr/local/jdk1.8.0_45/jre/lib/charsets.jar [root@zookeeper ~]# source /etc/profile.d/java.sh [root@zookeeper ~]# java -version java version "1.8.0_45" Java(TM) SE Runtime Environment (build 1.8.0_45-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
下载软件包
[root@zookeeper ~]# wget http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz
安装
[root@zookeeper ~]# tar xf zookeeper-3.4.6.tar.gz -C /usr/local/ [root@zookeeper ~]# ln -s /usr/local/zookeeper-3.4.6/ /usr/local/zookeeper [root@zookeeper conf]# pwd /usr/local/zookeeper/conf [root@zookeeper conf]# cp zoo_sample.cfg zoo.cfg #修改配置文件 [root@zookeeper conf]# egrep -v "^#|^$" /usr/local/zookeeper/conf/zoo.cfg tickTime=2000 initLimit=10 syncLimit=5 dataDir=/data/zookeeper/build clientPort=2181 dataLogDir=/data/zookeeper/logs #tickTime: 这个时间是作为Zookeeper服务器之间或客户端与服务器之间维持心跳的时间间隔, 也就是每个tickTime时间就会发送一个心跳 #dataDir: 顾名思义就是Zookeeper保存数据的目录, 默认情况下, Zookeeper将写数据的日志也保存在这个目录里 #clientPort: 这个端口就是客户端连接Zookeeper服务器的端口, Zookeeper会监听这个端口, 接受客户端的访问请求
启动zookeeper
[root@zookeeper ~]# /usr/local/zookeeper/bin/zkServer.sh start JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Starting zookeeper ... STARTED #查看结果 [root@zookeeper ~]# netstat -tunlp |grep 2181 tcp 0 0 :::2181 :::* LISTEN 1444/java
2.安装zookeeper集群
zookeeper-1 192.168.3.19
zookeeper-2 192.168.3.20
zookeeper-1操作:
#停止zookeeper服务 [root@zookeeper ~]# /usr/local/zookeeper/bin/zkServer.sh stop JMX enabled by default Using config: /usr/local/zookee #修改配置文件/usr/local/zookeeper/conf/zoo.cfg [root@zookeeper ~]# egrep -v "^$|^#" /usr/local/zookeeper/conf/zoo.cfg tickTime=2000 initLimit=10 syncLimit=5 dataDir=/data/zookeeper/build clientPort=2181 dataLogDir=/data/zookeeper/logs server.1 = 192.168.3.19:2888:3888 server.2 = 192.168.3.20:2888:3888 #配置myid [root@zookeeper ~]# cat /data/zookeeper/build/myid 1
zookeeper-2需要安装java和zookeeper,过程请看上文
#配置文件内容如下 [root@zookeeper-2 conf]# egrep -v "^$|^#" /usr/local/zookeeper/conf/zoo.cfg tickTime=2000 initLimit=10 syncLimit=5 dataDir=/data/zookeeper/build clientPort=2181 dataLogDir=/data/zookeeper/logs server.1 = 192.168.3.19:2888:3888 server.2 = 192.168.3.20:2888:3888 #启动服务 [root@zookeeper-2 ~]# /usr/local/zookeeper/bin/zkServer.sh start JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Starting zookeeper ... STARTED
最后验证集群状态
#zookeeper-1 [root@zookeeper ~]# /usr/local/zookeeper/bin/zkServer.sh status JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Mode: leader #zookeeper-2 [root@zookeeper-2 ~]# /usr/local/zookeeper/bin/zkServer.sh status JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Mode: follower
通过客户端脚本验证
[root@zookeeper ~]# /usr/local/zookeeper/bin/zkCli.sh [zk: localhost:2181(CONNECTED) 2] ls / [zookeeper] # 第一次部署Zookeeper集群,默认创建名为/zookeeper的节点 [zk: localhost:2181(CONNECTED) 3] ls /zookeeper [quota] # 读取znode数据 [zk: localhost:2181(CONNECTED) 5] get /zookeeper/quota cZxid = 0x0 ctime = Thu Jan 01 08:00:00 CST 1970 mZxid = 0x0 mtime = Thu Jan 01 08:00:00 CST 1970 pZxid = 0x0 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 0 numChildren = 0 # 创建znode [zk: localhost:2181(CONNECTED) 6] create /zk-book 123 Created /zk-book [zk: localhost:2181(CONNECTED) 7] ls / [zk-book, zookeeper] [zk: localhost:2181(CONNECTED) 8] get /zk-book 123 cZxid = 0x300000004 ctime = Tue Aug 11 14:46:48 CST 2015 mZxid = 0x300000004 mtime = Tue Aug 11 14:46:48 CST 2015 pZxid = 0x300000004 cversion = 0 dataVersion = 0 # version的版本为0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 3 numChildren = 0 # 更新znode的值 [zk: localhost:2181(CONNECTED) 9] set /zk-book 456 cZxid = 0x300000004 ctime = Tue Aug 11 14:46:48 CST 2015 mZxid = 0x300000005 mtime = Tue Aug 11 14:47:45 CST 2015 pZxid = 0x300000004 cversion = 0 dataVersion = 1 # 更新后version的版本为1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 3 numChildren = 0 #在zookeeper-2上查看结果 [root@zookeeper-2 ~]# /usr/local/zookeeper/bin/zkCli.sh [zk: localhost:2181(CONNECTED) 1] get /zk-book 456 cZxid = 0x300000004 ctime = Tue Aug 11 14:46:48 CST 2015 mZxid = 0x300000005 mtime = Tue Aug 11 14:47:45 CST 2015 pZxid = 0x300000004 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 3 numChildren = 0 #在zookeeper-2上删除/zk-book [zk: localhost:2181(CONNECTED) 2] delete /zk-book [zk: localhost:2181(CONNECTED) 3] ls / [zookeeper] #在zookeeper-1上查看结果 [zk: localhost:2181(CONNECTED) 10] ls / [zookeeper]
本文出自 “ly36843运维” 博客,请务必保留此出处http://ly36843.blog.51cto.com/3120113/1683533
原文地址:http://ly36843.blog.51cto.com/3120113/1683533