一、ZooKeeper相关概念简介:

      ZooKeeper是一个基于Java开发的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。ZooKeeper的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效、功能稳定的系统提供给用户。ZooKeeper集群中节点个数要为奇数个(>=3),若集群中leader节点挂掉,剩余节点个数在半数以上时,会推举新的leader节点,继续对外提供服务。

二、集群部署前准备(3个节点都需要执行)

1、演示环境:

IP

主机名

系统

JDK版本

ZooKeeper版本

192.168.0.200

server1

CentOS 7.9

jdk-8u401-linux-x64.rpm

apache-zookeeper-3.9.1-bin.tar.gz

192.168.0.201

server2

192.168.0.202

server3

JDK下载地址:https://www.oracle.com/java/technologies/downloads/archive/

ZooKeeper下载地址:https://archive.apache.org/dist/zookeeper/

说明:apache-zookeeper-3.9.1.tar.gz是源码包,需要编译,不能直接使用

2、配置主机名:

# hostnamectl set-hostname serverX
# vim /etc/hostname --> serverX
# vim /etc/hosts
192.168.0.200 server1
192.168.0.201 server2
192.168.0.202 server3
# logout
# hostname

CentOS 7.9部署ZooKeeper集群_三节点集群

3、关闭防火墙:

# systemctl stop firewalld
# firewall-cmd --state

CentOS 7.9部署ZooKeeper集群_三节点集群_02

# systemctl disable firewalld
# systemctl status firewalld

4、关闭SELinux:

# vim /etc/selinux/config
SELINUX=disabled
# reboot
# sestatus

CentOS 7.9部署ZooKeeper集群_三节点集群_03

说明:如果服务器不能重启,可使用# setenforce 0命令临时关闭SELinux

5、服务器时间同步

6、修改openfile:

# ulimit -a

CentOS 7.9部署ZooKeeper集群_zookeeper_04

# vim /etc/security/limits.conf
* soft nofile 102431
* hard nofile 102431
* soft nproc 102431
* hard nproc 102431
# logout
# ulimit -n

CentOS 7.9部署ZooKeeper集群_三节点集群_05

7、修改sysctl.conf:

# vim /etc/sysctl.conf
vm.max_map_count=655360
# sysctl -p

CentOS 7.9部署ZooKeeper集群_zookeeper_06

8、配置JDK环境:

# rpm -ivh jdk-8u401-linux-x64.rpm

CentOS 7.9部署ZooKeeper集群_zookeeper_07

# java -version

CentOS 7.9部署ZooKeeper集群_zookeeper_08

三、部署3节点ZooKeeper集群(3个节点都需要执行)

1、   安装ZooKeeper:

# tar -xf apache-zookeeper-3.9.1-bin.tar.gz -C /usr/local
# cd /usr/local
# ln -sv apache-zookeeper-3.9.1-bin zookeeper
# cd zookeeper

2、创建数据目录:

# mkdir -pv data

3、创建日志目录:

# mkdir -pv logs

4、配置ZooKeeper环境变量:

# vim /etc/profile.d/zookeeper.sh
export PATH=/usr/local/zookeeper/bin:$PATH
# . /etc/profile.d/zookeeper.sh

5、修改配置文件:

# cd /usr/local/zookeeper/conf
# cp zoo_sample.cfg zoo.cfg
# vim zoo.cfg

修改或新增如下代码:

参数

含义

tickTime=2000

通信心跳时间,ZooKeeper服务器与客户端心跳时间间隔,单位为毫秒

initLimit=10

leader节点和follower节点初始连接时能容忍的最多心跳数(tickTime的数量),此处表示10*2000ms

syncLimit=5

leader节点和follower节点之间同步通信的超时时间,此处表示如果超过5*2000ms,leader认为follower节点挂掉,并从服务器列表中删除此follwer节点

dataDir=/usr/local/zookeeper/data

指定ZooKeeper的数据存放目录

clientPort=2181

客户端(应用程序)连接ZooKeeper服务器的端口,ZooKeeper会监听此端口接收客户端的访问请求

dataLogDir=/usr/local/zookeeper/logs

指定ZooKeeper的日志存放目录

server.1=192.168.0.200:2888:3888

server.2=192.168.0.201:2888:3888

server.3=192.168.0.202:2888:3888

server.X:指定ZooKeeper集群中的每个节点,X是节点的唯一标识符,从1开始递增

2888:集群节点间的通信端口,只有leader节点才会监听此端口

3888:选举leader节点的端口

6、创建myid文件:

server1:

# echo 1 > /usr/local/zookeeper/data/myid

server2:

# echo 2 > /usr/local/zookeeper/data/myid

server3:

# echo 3 > /usr/local/zookeeper/data/myid

说明:文件myid中只有一行内容,且内容为该节点对应的server.X中的标识符

7、配置ZooKeeper开机自启:

# vim /etc/init.d/zookeeper

#!/bin/bash
#chkconfig: 2345 20 90
#description: ZooKeeper Service
Control Script
ZK_HOME='/usr/local/zookeeper'
case $1 in
    start)
        echo "----------ZooKeeper启动----------"
        $ZK_HOME/bin/zkServer.sh start
        ;;
    stop)
        echo "----------ZooKeeper停止-----------"
        $ZK_HOME/bin/zkServer.sh stop
        ;;
    restart)
        echo "----------ZooKeeper重启------------"
        $ZK_HOME/bin/zkServer.sh restart
        ;;
    status)
        echo "----------ZooKeeper状态------------"
        $ZK_HOME/bin/zkServer.sh status
        ;;
    *)
        echo "Usage: $0 {start | stop | restart | status}"
        ;;
esac

# chmod +x /etc/init.d/zookeeper
# chkconfig --add zookeeper
# chkconfig --list zookeeper

8、启动ZooKeeper:

# service zookeeper start

CentOS 7.9部署ZooKeeper集群_三节点集群_09

说明:在启动之前,确保每个节点的dataDir目录(/usr/local/zookeeper/data)是空的

四、集群验证(3个节点都需要执行)

1、查看集群状态:

server1:

# service zookeeper status

CentOS 7.9部署ZooKeeper集群_zookeeper_10

server2:

# service zookeeper status

CentOS 7.9部署ZooKeeper集群_zookeeper_11

server3:

# service zookeeper status

CentOS 7.9部署ZooKeeper集群_三节点集群_12

2、查看监听端口:

server1:

# ss -tunlp | grep '2181\|2888\|3888'

CentOS 7.9部署ZooKeeper集群_zookeeper_13

server2:

# ss -tunlp | grep '2181\|2888\|3888'

CentOS 7.9部署ZooKeeper集群_zookeeper_14

server3:

# ss -tunlp | grep '2181\|2888\|3888'

CentOS 7.9部署ZooKeeper集群_三节点集群_15

3、使用ZooKeeper提供的客户端工具zkCli.sh测试:

server1:

# zkCli.sh -server server1:2181

创建/myNode节点,数据为Hello World:create /myNode "Hello World"

CentOS 7.9部署ZooKeeper集群_三节点集群_16

server2:

# zkCli.sh -server server2:2181

获取/myNode节点数据:get /myNode

CentOS 7.9部署ZooKeeper集群_三节点集群_17

修改/myNode节点数据为Hello ZooKeeper:set /myNode "Hello ZooKeeper"

CentOS 7.9部署ZooKeeper集群_zookeeper_18

server3:

# zkCli.sh -server server3:2181

获取/myNode节点数据:get /myNode

CentOS 7.9部署ZooKeeper集群_三节点集群_19

删除/myNode节点:delete /myNode

CentOS 7.9部署ZooKeeper集群_三节点集群_20

server1:

# zkCli.sh -server server1:2181

获取/myNode节点数据:get /myNode

CentOS 7.9部署ZooKeeper集群_三节点集群_21

4、使用jps命令显示当前所有Java进程的PID:

server1:

# jps

CentOS 7.9部署ZooKeeper集群_三节点集群_22

server2:

# jps

CentOS 7.9部署ZooKeeper集群_三节点集群_23

server3:

# jps

CentOS 7.9部署ZooKeeper集群_zookeeper_24

说明:QuorumPeerMain进程代表当前节点ZooKeeper已经启动成功

5、查看/usr/local/zookeeper/logs目录下.out格式的日志