前言
项目准备
一、关于Zookeeper
1.ZooKeeper简介
2.ZooKeeper目录
3.ZooKeeper集群角色
4.ZooKeeper的作用
二、使用步骤
1.基础配置
改名
配置网卡(主要信息)
配置hosts文件(3个节点都要)
配置yum源(三个节点都要)
2.项目实施
搭建JDK环境(三个节点都要)
解压ZooKeeper的gz软件包(三个节点都要)
修改zookeeper配置文件(三个节点都要)
创建myid文件
启动ZooKeeper服务
3.debug
查看三个节点的zoo.cfg配置文件,主要查看server.id行
查看三个节点的myid文件是否与zoo.cfg对应
查看zookeeper.out运行日志,发现有好几个报错:连接被阻止
查看端口状态
查看防火墙
依次停止并开启zookeeper服务,并推选zookeeper1为leader
参考来源
总结
根据项目练习开始搭建ZooKeeper,希望能帮到你
本次项目目标:
镜像
Linux-CentOS7
软件
VMware Workstation 15.5 Pro
MobaXterm_Personal_22.0
软件包
zookeeper-3.4.14.tar.gz
gpmall-repo
主机配置
2vCPU/4GB内存/50GB硬盘
节点规划
(见下表)
IP | 主机名 | 节点 |
---|---|---|
172.16.51.23 | zookeeper1 | 集群节点 |
172.16.51.32 | zookeeper2 | 集群节点 |
172.16.51.41 | zookeeper3 | 集群节点 |
提示:以下是本篇文章正文内容,下面案例可供参考
ZooKeeper 是一种分布式同步协调服务与集中式组管理服务,用于管理大型场合。
[root@zookeeper3 zookeeper-3.4.14]# ls -l | grep ^d #只显示文件夹
(
drwxr-xr-x. 2 2002 2002 223 Sep 21 06:52 bin
drwxr-xr-x. 2 2002 2002 70 Sep 21 06:46 conf
drwxr-xr-x. 2 2002 2002 4096 Mar 6 2019 dist-maven
drwxr-xr-x. 4 2002 2002 4096 Mar 6 2019 lib
drwxr-xr-x. 3 2002 2002 22 Mar 6 2019 src
drwxr-xr-x. 3 2002 2002 47 Mar 6 2019 zookeeper-client
drwxr-xr-x. 12 2002 2002 4096 Mar 6 2019 zookeeper-contrib
drwxr-xr-x. 7 2002 2002 4096 Mar 6 2019 zookeeper-docs
drwxr-xr-x. 3 2002 2002 35 Mar 6 2019 zookeeper-it
drwxr-xr-x. 4 2002 2002 46 Mar 6 2019 zookeeper-jute
drwxr-xr-x. 5 2002 2002 176 Mar 6 2019 zookeeper-recipes
drwxr-xr-x. 3 2002 2002 32 Mar 6 2019 zookeeper-server
)
(图片来源于网络,侵删)
ZooKeeper主要用于分布式应用管理,他可以做到:统一配置管理、统一命名服务、分布式锁、集群管理。
zookeeper1:
[root@localhost ~]# hostnamectl set-hostname zookeeper1
[root@localhost ~]# su
su
[root@zookeeper1 ~]#
zookeeper2:
[root@localhost ~]# hostnamectl set-hostname zookeeper2
[root@localhost ~]# su
su
[root@zookeeper2 ~]#
zookeeper3:
[root@localhost ~]# hostnamectl set-hostname zookeeper3
[root@localhost ~]# su
su
[root@zookeeper3 ~]#
zookeeper1:
[root@zookeeper1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
{
BOOTPROTO=static
ONBOOT=yes
IPADDR=172.16.51.23
NETMASK=255.255.255.0
GATEWAY=172.16.51.254
DNS1=223.5.5.5 #阿里免费公共DNS
}
[root@zookeeper1 ~]# systemctl restart network #重启网卡
zookeeper2:
[root@zookeeper2 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
{
BOOTPROTO=static
ONBOOT=yes
IPADDR=172.16.51.32
NETMASK255.255.255.0
GATEWAY=172.16.51.254
DNS1=223.5.5.5
}
[root@zookeeper2 ~]# systemctl restart network
zookeeper3:
[root@zookeeper3 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
{
BOOTPROTO=static
ONBOOT=yes
IPADDR=172.16.51.41
NETMASK=255.255.255.0
GATEWAY=172.16.51.254
DNS1=223.5.5.5
}
[root@zookeeper3 ~]# systemctl restart network
zookeeper1/2/3:
[root@zookeeper1/2/3 ~]# vi /etc/hosts #修改hosts配置文件
{
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.51.23 zookeeper1
172.16.51.32 zookeeper2
172.16.51.41 zookeeper3
}
zookeeper1/2/3:
[root@zookeeper1/2/3 ~]# mv /etc/yum.repos.d/* /media/ #备份源
#将gpmall-repo包上传到虚拟机/opt/目录下#
[root@zookeeper1/2/3 ~]# vi /etc/yum.repos.d/local.repo
{
[gpmall]
name=gpmall
baseurl=file:///opt/gpmall-repo
enabled=1
gpgcheck=0
}
[root@zookeeper1/2/3 ~]# yum clean all #清除yum缓存
[root@zookeeper1/2/3 ~]# yum -y install java* #具体版本为“java-1.8.0-openjdk java-1.8.0-openjdk-devel”
(Complete! #完成安装JDK环境)
[root@zookeeper1/2/3 ~]# java -version #检查是否成功安装JDK环境
openjdk version "1.8.0_262"
OpenJDK Runtime Environment (build 1.8.0_262-b10)
OpenJDK 64-Bit Server VM (build 25.262-b10, mixed mode)
#先将zookeeper-3.4.14.tar.gz软件包上传至root用户家目录下
[root@zookeeper1/2/3 ~]# tar -zxvf zookeeper-3.4.14.tar.gz #解压.tar.gz包命令
[root@zookeeper1/2/3 ~]# cd zookeeper-3.4.14/conf/ #进入zokeeper的配置目录
[root@zookeeper1/2/3 conf]# ls
(configuration.xsl log4j.properties zoo_sample.cfg)
[root@zookeeper1/2/3 conf]# mv zoo_sample.cfg zoo.cfg #zoo.cfg是zookeeper启动文件
[root@zookeeper1/2/3 conf]# ls
(configuration.xsl log4j.properties zoo.cfg)
[root@zookeeper1/2/3 conf]# vi zoo.cfg #此处十分重要,不能出错!!!
{ :set nu #末行模式显示行数
2:tickTime=2000
5:initLimit=10
8:syncLimit=5
12:dataDir=/tmp/zookeeper
14:clientPort=2181
29:server.1=172.16.51.23:2888:3888 #29-31行上下不要有空行
30:server.2=172.16.51.32:2888:3888 #server1.2.3分别对应三个节点,请不要弄错
31:server.3=172.16.51.41:2888:3888 #2888端口用于集群内机器互相通讯,3888端口用来选举leader
}
知识点:
- tickTime :用于做心跳,是zookeeper的基本时间单位(毫秒)。
- initLimit:用于限制仲裁中的ZooKeeper服务器必须连接到领导者的时间长度。
ZooKeeper集群模式下包含多个zk进程,其中一个进程为leader,余下的进程为follower。当follower最初与leader建立连接时,它们之间会传输相当多的数据,尤其是follower的数据落后leader很多。initLimit配置follower与leader之间建立连接后进行同步的最长时间。
- syscLimit:配置follower和leader之间发送消息,请求和应答的最大时间长度。
- dataDir:存储内存数据库快照的位置,除非另有说明,否则存储数据库更新的事务日志。
- dataLogDir:用于存储日志,若不指定日志将和快照都存放到dataDir 中
- clientPort:用于侦听客户端连接的端口
server.id=host:port1:port2:其中id为一个数字,表示zk进程的id,这个id也是dataDir目录下myid文件的内容。host是该zk进程所在的IP地址,port1表示follower和leader交换消息所使用的端口,port2表示选举leader所使用的端口。
zookeeper1:
[root@zookeeper1 conf]# mkdir /tmp/zookeeper
[root@zookeeper1 conf]# vi /tmp/zookeeper/myid
{
1 #与zoo.cfg的server.1对应
}
zookeeper2:
[root@zookeeper2 conf]# mkdir /tmp/zookeeper
[root@zookeeper2 conf]# vi /tmp/zookeeper/myid
{
2 #与zoo.cfg中的server.2对应
}
zookeeper3:
[root@zookeeper3 conf]# mkdir /tmp/zookeeper
[root@zookeeper3 conf]# vi /tmp/zookeeper/myid
{
3 #与zoo.cfg的server.3对应
}
zookeeper1:
[root@zookeeper1 conf]# cd ~/zookeeper-3.4.14/bin/ #进入zookeeper目录下的bin目录
[root@zookeeper1 bin]# ls
README.txt zkCli.cmd zkEnv.cmd zkServer.cmd zkTxnLogToolkit.cmd
zkCleanup.sh zkCli.sh zkEnv.sh zkServer.sh zkTxnLogToolkit.sh
[root@zookeeper1 bin]# ./zkServer.sh start #启动服务
( #启动完成
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
)
[root@zookeeper1 bin]# ./zkServer.sh status #查看服务状态
(
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
)
zookeeper2:
[root@zookeeper2 conf]# cd ~/zookeeper-3.4.14/bin/ #进入zookeeper目录下的bin目录
[root@zookeeper2 bin]# ls
README.txt zkCli.cmd zkEnv.cmd zkServer.cmd zkTxnLogToolkit.cmd
zkCleanup.sh zkCli.sh zkEnv.sh zkServer.sh zkTxnLogToolkit.sh
[root@zookeeper2 bin]# ./zkServer.sh start #启动服务
( #启动完成
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
)
[root@zookeeper1 bin]# ./zkServer.sh status #查看服务状态
(
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: follower #角色为follower
)
zookeeper3:
[root@zookeeper3 conf]# cd ~/zookeeper-3.4.14/bin/ #进入zookeeper目录下的bin目录
[root@zookeeper3 bin]# ls
README.txt zkCli.cmd zkEnv.cmd zkServer.cmd zkTxnLogToolkit.cmd
zkCleanup.sh zkCli.sh zkEnv.sh zkServer.sh zkTxnLogToolkit.sh
[root@zookeeper3 bin]# ./zkServer.sh start #启动服务
( #启动完成
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
)
[root@zookeeper3 bin]# ./zkServer.sh status #查看服务状态
(
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: leader #角色为leader
)
发现错误:三个节点服务状态都显示异常,开始debug后
zookeeper1的原因是防火墙的问题,返回关闭防火墙。
zookeeper2与zookeeper3是因为启动服务的顺序不同,先将三个节点的服务关闭,将选举为leader的节点先开启服务,剩下两个节点依次开启,最后再查看状态,发现配置成功。
以下为具体操作:(此处我复盘了我老师的排错手法)
[root@zookeeper1/2/3 bin]# cat ~/zookeeper-3.4.14/conf/zoo.cfg
( #无误
server.1=172.16.51.23:2888:3888
server.2=172.16.51.32:2888:3888
server.3=172.16.51.41:2888:3888
)
zookeeper1:
[root@zookeeper1 bin]# cat /tmp/zookeeper/myid
1
zookeeper2:
[root@zookeeper2 bin]# cat /tmp/zookeeper/myid
2
zookeeper3:
[root@zookeeper3 bin]# cat /tmp/zookeeper/myid
3
java.net.ConnectException: Connection refused (Connection refused)
[root@zookeeper1 bin]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp6 0 0 :::2181 :::* LISTEN 2061/java
发现属于zookeeper的2181端口属于进程名是java,所以没有被占用
[root@zookeeper1 bin]# getenforce #查看SELinux状态
(
Enforcing #强制模式
)
[root@zookeeper1 bin]# systemctl status firewalld
(
Active: active (running) …… #running状态——运行中
)
#查出原因,开始关闭防火墙
[root@zookeeper1 bin]# setenforce 0 #切换SELinux模式
[root@zookeeper1 bin]# getenforce #重新检查
(
Permissive #宽容模式
)
[root@zookeeper1 bin]# systemctl stop firewalld #关闭防火墙
[root@zookeeper1 bin]# systemctl status firewalld
(
Active: inactive (dead)…… #dead状态——已关闭
)
zookeeper1:
[root@zookeeper1 bin]# ./zkServer.sh stop #停止服务
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
[root@zookeeper1 bin]# ./zkServer.sh start #重新开启服务
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@zookeeper1 bin]# ./zkServer.sh status #查看服务角色
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: leader #角色为leader
zookeeper2:
[root@zookeeper2 bin]# ./zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
[root@zookeeper2 bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@zookeeper2 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: follower #角色为follower
zookeeper3:
[root@zookeeper3 bin]# ./zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
[root@zookeeper3 bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@zookeeper3 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: follower #角色为follower
至此,zookeeper集群管理配置完成!
Zookeeper 端口说明 - 简书 (jianshu.com)
3.0 Zookeeper linux 服务端集群搭建步骤 | 菜鸟教程 (runoob.com)
Zookeeper入门看这篇就够了_java_66666的博客-CSDN博客_zookeeper
Zookeeper 概述_w3cschool
什么是ZooKeeper? - 知乎 (zhihu.com)
ZooKeeper 详解 - 知乎 (zhihu.com)
如果有人问你ZooKeeper是什么,就把这篇文章发给他 - 知乎 (zhihu.com)
这是第二篇项目练习博客,一周至少会更一次知道这个项目结束。如果你发现了文章的数据、科学性、知识点错误的话,还请指正,我会马上修改。如果你还想了解更多关于ZooKeeper的信息可以去官网Apache ZooKeeper、我提供的参考来源等。最后,感谢您的阅读。