zookeeper的安装和集群搭建
搭建zoopeeper
1.上传软件包
[root@dr1 ~]# ls
apache-zookeeper-3.5.5-bin.tar.gz jdk-8u171-linux-x64.tar.gz
2.配置host
[root@dr1 ~]# vim /etc/hosts
192.168.1.20 dr1
192.168.1.30 dr2
192.168.1.40 dr3
3.部署jdk
3.1.解压jdk
[root@dr1 ~]# tar -zxf jdk-8u171-linux-x64.tar.gz -C /usr/local/
3.2.配置profile
[root@dr1 ~]# vim /etc/profile 在最后添加内容:
JAVA_HOME=/usr/local/jdk1.8.0_171
PATH=
J
A
V
A
H
O
M
E
/
b
i
n
:
JAVA_HOME/bin:
JAVAHOME/bin:PATH
CLASSPATH=
J
A
V
A
H
O
M
E
/
j
r
e
/
l
i
b
/
e
x
t
:
JAVA_HOME/jre/lib/ext:
JAVAHOME/jre/lib/ext:JAVA_HOME/lib/tools.jar
export PATH JAVA_HOME CLASSPATH
[root@dr1 ~]# source /etc/profile #使环境变量生效
3.3.查看jave环境
[root@dr1 ~]# java -version
4.部署zoopeeper(独立模式)
4.1.解压zoopeeper包
[root@dr1 ~]# tar -zxf apache-zookeeper-3.5.5-bin.tar.gz -C /usr/local/
4.2.创建目录
[root@dr1 ~]# mkdir -p /data/zk/{data,datalog}
注:data目录是内存数据库快照存放目录,datalog目录是事务日志存放目录。
4.3.生成配置文件
[root@dr1 ~]# cd /usr/local/apache-zookeeper-3.5.5-bin/conf/
[root@dr1 conf]# ls
configuration.xsl log4j.properties zoo_sample.cfg
注:log4j.properties是一个属性文件,zoo_sample.cfg是一个主配置配件
[root@dr1 conf]# cp zoo_sample.cfg zoo.cfg
4.4.修改主配置文件
[root@dr1 conf]# vim zoo.cfg
dataDir=/data/zk/data #修改这行的路径
dataDir=/data/zk/datalog #添加此行
4.5.添加path的环境变量
这里必须是修改配置文件添加path环境变量,不然启动报错
[root@dr1 conf]# vim /etc/profile
export ZOOKEEPER_HOME=/usr/local/apache-zookeeper-3.5.5-bin
export PATH=
Z
O
O
K
E
E
P
E
R
H
O
M
E
/
b
i
n
:
ZOOKEEPER_HOME/bin:
ZOOKEEPERHOME/bin:PATH
[root@dr1 conf]# source /etc/profile #使变量生效
[root@dr1 conf]# echo $ZOOKEEPER_HOME #也可以这样查看
/usr/local/apache-zookeeper-3.5.5-bin
4.6.启动zoopeeper
[root@d2 conf]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/apache-zookeeper-3.5.5-bin/bin/…/conf/zoo.cfg
Starting zookeeper … STARTED
[root@dr1 conf]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/apache-zookeeper-3.5.5-bin/bin/…/conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: standalone
注:standalone是单机的意思
4.7.验证
[root@dr1 conf]# jps
12386 QuorumPeerMain
12457 Jps
或者
[root@dr1 conf]# ps -ef | grep zoopeeper0
4.8.启动客户端
[root@dr1 conf]# zkCli.sh
或者
[root@dr1 conf]# zkCli.sh -server dr1:2181
4.9.使用命令
[zk: dr1:2181(CONNECTED) 0] ls / #查看
[zookeeper]
[zk: dr1:2181(CONNECTED) 1] create /aa #创建aa
Created /aa
[zk: dr1:2181(CONNECTED) 3] create /aa/bb #在aa下创建一个bb
Created /aa/bb
[zk: dr1:2181(CONNECTED) 4] ls /aa #查看aa下的内容
[bb]
[zk: dr1:2181(CONNECTED) 2] ls / #查看/
[aa, zookeeper]
[zk: dr1:2181(CONNECTED) 8] set /aa a #创建/aa下的字符串
[zk: dr1:2181(CONNECTED) 9] get /aa #查看
a
[zk: dr1:2181(CONNECTED) 23] delete /aa/bb #删除/aa/bb
[zk: dr1:2181(CONNECTED) 24] ls /aa #查看/aa
[]
[zk: dr1:2181(CONNECTED) 25] delete /aa #删除/aa
[zk: dr1:2181(CONNECTED) 26] ls /
[zookeeper]/
5.Zoopeeper搭建集群
5.1.集群拓扑
主机名 IP地址 系统环境
dr1 192.168.1.20 Centos7.5/zoopeeper3.5.5
dr2 192.168.1.30 Centos7.5/zoopeeper3.5.5
dr3 192.168.1.40 Centos7.5/zoopeeper3.5.5
5.2.配置host
所有点配置host
[root@dr1 ~]# vim /etc/hosts
192.168.1.20 dr1
192.168.1.30 dr2
192.168.1.40 dr3
5.3.修改配置文件
[root@dr3 ~]# vim /usr/local/apache-zookeeper-3.5.5-bin/conf/zoo.cfg #最后添加一下内容
server.1=dr1:2888:3888
server.2=dr2:2888:3888
server.3=dr3:2888:3888
5.4.拷贝zoopeeper
[root@dr1 conf]# scp -r /usr/local/apache-zookeeper-3.5.5-bin/ dr2:/usr/local/
[root@dr1 conf]# scp -r /usr/local/apache-zookeeper-3.5.5-bin/ dr3:/usr/local/2.4 拷贝jdk
5.5.拷贝jdk
[root@dr1 conf]# scp -r /usr/local/jdk1.8.0_171 dr2:/usr/local/
[root@dr1 conf]# scp -r /usr/local/jdk1.8.0_171 dr3:/usr/local/
2.6 拷贝profile文件,设置环境变量
[root@dr1 conf]# scp /etc/profile dr2:/etc/
[root@dr1 conf]# scp /etc/profile dr3:/etc/
5.6.profile生效
[root@dr2 ~]# source /etc/profile
[root@dr2 ~]# java -version
java version “1.8.0_171”
Java™ SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot™ 64-Bit Server VM (build 25.171-b11, mixed mode)
[root@dr2 ~]# mkdir -p /data/zk/{data,datalog}
[root@dr3 ~]# source /etc/profile
[root@dr3 ~]# java -version #查看java环境
java version “1.8.0_171”
Java™ SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot™ 64-Bit Server VM (build 25.171-b11, mixed mode)
2.8 创建目录
[root@dr2 ~]# mkdir -p /data/zk/{data,datalog}
[root@dr3 ~]# mkdir -p /data/zk/{data,datalog}
2.9 写入节点标记
[root@dr1 conf]# echo 1 > /data/zk/data/myid
[root@dr2 ~]# echo 2 >/data/zk/data/myid
[root@dr3 ~]# echo 3 > /data/zk/data/myid
2.10 启动zoopeeper集群
[root@dr1 ~]# zkServer.sh restart
[root@dr2 ~]# zkServer.sh restart
[root@dr3 ~]# zkServer.sh restart
2.11 查看集群服务状态
[root@dr1 ~]# zkServer.sh status
[root@dr2 ~]# zkServer.sh status
[root@dr3~]# zkServer.sh status
可以看到3个节点。一个leader,2个follower
2.12 服务器连接测试
两种方式连接
1)[root@dr1 ~]# zkCli.sh #本机连接
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 1] create /aa
Created /aa
[zk: localhost:2181(CONNECTED) 3] ls /
[aa, zookeeper]
或者
2)[root@dr1 ~]# zkCli.sh -server 192.168.1.20:2181,192.168.1.30:2181,192.168.1.40:2181 #三台同时连接
[zk: 192.168.1.20:2181,192.168.1.30:2181,192.168.1.40:2181(CONNECTED) 0] ls /
[aa, zookeeper]
[zk: 192.168.1.20:2181,192.168.1.30:2181,192.168.1.40:2181(CONNECTED) 2] set /aa a1
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/aa
[zk: 192.168.1.20:2181,192.168.1.30:2181,192.168.1.40:2181(CONNECTED) 3] get /aa
a1
2.11 故障测试
关掉leader
[root@dr1 ~]# zkServer.sh stop
查看dr2、dr3
重启dr1
[root@dr1 ~]# zkServer.sh start
[root@dr1 ~]# zkServer.sh status
在dr3写入数据
[root@dr3 ~]# zkCli.sh
[zk: localhost:2181(CONNECTED) 0] create /bb
Created /bb
[zk: localhost:2181(CONNECTED) 1] ls /
[aa, bb, zookeeper]
在dr1,dr2查看
[root@dr1 ~]# zkCli.sh
[zk: localhost:2181(CONNECTED) 0] ls /
[aa, bb, zookeeper]
[root@dr2 ~]# zkCli.sh
[zk: localhost:2181(CONNECTED) 0] ls /
[aa, bb, zookeeper]
2.配置observer模式
三台同时添加
peerType=observer
加:observer,如:server.x=host:n:n:observer
[root@dr1 ~]# vim /usr/local/apache-zookeeper-3.5.5-bin/conf/zoo.cfg
[root@dr2 ~]# vim /usr/local/apache-zookeeper-3.5.5-bin/conf/zoo.cfg
[root@dr3 ~]# vim /usr/local/apache-zookeeper-3.5.5-bin/conf/zoo.cfg
peerType=observer
server.1=dr1:2888:3888:observer
server.2=dr2:2888:3888
server.3=dr3:2888:3888
[root@dr1 ~]# zkServer.sh stop
[root@dr1 ~]# zkServer.sh status
[root@dr1 ~]# zkServer.sh restart
[root@dr1 ~]# zkServer.sh status
[root@dr2 ~]# zkServer.sh stop
[root@dr2 ~]# zkServer.sh status
[root@dr2 ~]# zkServer.sh restart
[root@dr2 ~]# zkServer.sh status
[root@dr3 ~]# zkServer.sh stop
[root@dr3 ~]# zkServer.sh status
[root@dr3 ~]# zkServer.sh restart
[root@dr3 ~]# zkServer.sh status