《云计算》Zookeeper基础:Zookeeper安装详解

1 案例1:Zookeeper安装
1.1 问题

本案例要求:

搭建Zookeeper集群并查看各服务器的角色
停止Leader并查看各服务器的角色

1.2 步骤

实现此案例需要按照如下步骤进行。
2 步骤一:安装Zookeeper

1)编辑/etc/hosts ,所有集群主机可以相互 ping 通(在nn01上面配置,同步到node1,node2,node3)

[root@nn01 hadoop]# vim /etc/hosts
192.168.1.21  nn01
192.168.1.22  node1
192.168.1.23  node2
192.168.1.24  node3
192.168.1.25  node4
[root@nn01 hadoop]# for i in {22..24}  \
do    \
scp /etc/hosts 192.168.1.$i:/etc/    \
done        //同步配置
hosts       100%  253   639.2KB/s   00:00    
hosts       100%  253   497.7KB/s   00:00    
hosts       100%  253   662.2KB/s   00:00    

2)安装 java-1.8.0-openjdk-devel,由于之前的hadoop上面已经安装过,这里不再安装,若是新机器要安装

3)zookeeper 解压拷贝到 /usr/local/zookeeper

[root@nn01 ~]# tar -xf zookeeper-3.4.10.tar.gz 
[root@nn01 ~]# mv zookeeper-3.4.10 /usr/local/zookeeper

4)配置文件改名,并在最后添加配置

[root@nn01 ~]# cd /usr/local/zookeeper/conf/
[root@nn01 conf]# ls
configuration.xsl  log4j.properties  zoo_sample.cfg
[root@nn01 conf]# mv zoo_sample.cfg  zoo.cfg
[root@nn01 conf]# chown root.root zoo.cfg
[root@nn01 conf]# vim zoo.cfg
server.1=node1:2888:3888
server.2=node2:2888:3888
server.3=node3:2888:3888
server.4=nn01:2888:3888:observer

5)拷贝 /usr/local/zookeeper 到其他集群主机

[root@nn01 conf]# for i in {22..24}; do rsync -aSH --delete /usr/local/zookeeper/ 192.168.1.$i:/usr/local/zookeeper  -e 'ssh' & done
[4] 4956
[5] 4957
[6] 4958

6)创建 mkdir /tmp/zookeeper,每一台都要

[root@nn01 conf]# mkdir /tmp/zookeeper
[root@nn01 conf]# ssh node1 mkdir /tmp/zookeeper
[root@nn01 conf]# ssh node2 mkdir /tmp/zookeeper
[root@nn01 conf]# ssh node3 mkdir /tmp/zookeeper

7)创建 myid 文件,id 必须与配置文件里主机名对应的 server.(id) 一致

[root@nn01 conf]# echo 4 >/tmp/zookeeper/myid
[root@nn01 conf]# ssh node1 'echo 1 >/tmp/zookeeper/myid'
[root@nn01 conf]# ssh node2 'echo 2 >/tmp/zookeeper/myid'
[root@nn01 conf]# ssh node3 'echo 3 >/tmp/zookeeper/myid'

8)启动服务,单启动一台无法查看状态,需要启动全部集群以后才能查看状态,每一台上面都要手工启动(以nn01为例子)

[root@nn01 conf]# /usr/local/zookeeper/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

注意:刚启动zookeeper查看状态的时候报错,启动的数量要保证半数以上,这时再去看就成功了

9)查看状态

[root@nn01 conf]# /usr/local/zookeeper/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: observe
[root@nn01 conf]# /usr/local/zookeeper/bin/zkServer.sh stop  
//关闭之后查看状态其他服务器的角色
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
[root@nn01 conf]# yum -y install telnet
[root@nn01 conf]# telnet node3 2181 
Trying 192.168.1.24...
Connected to node3.
Escape character is '^]'.
ruok        //发送
imokConnection closed by foreign host.        //imok回应的结果

10)利用 api 查看状态(nn01上面操作)

[root@nn01 conf]# /usr/local/zookeeper/bin/zkServer.sh start
[root@nn01 conf]# vim api.sh
#!/bin/bash
function getstatus(){
    exec 9<>/dev/tcp/$1/2181 2>/dev/null
    echo stat >&9
    MODE=$(cat <&9 |grep -Po "(?<=Mode:).*")
    exec 9<&-
    echo ${MODE:-NULL}
}
for i in node{1..3} nn01;do
    echo -ne "${i}\t"
    getstatus ${i}
done
[root@nn01 conf]# chmod 755 api.sh
[root@nn01 conf]# ./api.sh 
node1    follower
node2    leader
node3    follower 
nn01    observer

你可能感兴趣的:(架构师基础)