• 欢迎光临~

ZooKeeper集群部署

开发技术 开发技术 2022-05-20 次浏览

Zookeeper的介绍

  • ZooKeeper主要服务于分布式系统,可以用ZooKeeper来做:统一配置管理、统一命名服务、分布式锁、集群管理。
  • 使用分布式系统就无法避免对节点管理的问题(需要实时感知节点的状态、对节点进行统一管理等等),而由于这些问题处理起来可能相对麻烦和提高了系统的复杂性,ZooKeeper作为一个能够通用解决这些问题的中间件就应运而生了。

zookeeper的选举策略也是需要半数以上的节点同意才能当选leader,如果是偶数节点可能导致票数相同的情况,所以Zookeeper做集群最好部署3,5,7个节点。 

Zookeeper包下载地址:http://zookeeper.apache.org/releases.html#download 或 http://archive.apache.org/dist/zookeeper/

服务规划

ZooKeeper集群部署

Zookeeper部署

1.关闭防火墙和SELinux(所有设备)

systemctl stop firewalld.service

systemctl disable firewalld.service

setenforce 0               

vi /etc/selinux/config   

SELINUX=enforcing改为SELINUX=disabled

2.安装JDK

zookeeper是由java开发,所以需要有JDK 1.6以上的运行环境支持,JDK的部署可参考JDK的安装配置(Windows、Linux)

3.配置IP地址和主机名之间的映射(所有设备)

vim /etc/hosts

192.168.36.152 node1
192.168.36.153 node2
192.168.36.154 node3

4.配置主机名(所有设备)

vim /etc/sysconfig/network

network=主机名

需重启设备使配置生效:init 6

5.安装Zookeeper(192.168.36.152)

先在一台设备上部署  

解压安装包

tar -zxvf apache-zookeeper-3.8.0-bin.tar.gz -C /usr/local/

修改Zookeeper配置文件

cd /usr/local/apache-zookeeper-3.8.0-bin/conf/

cp zoo_sample.cfg zoo.cfg

vim zoo.cfg

#数据文件目录
dataDir=/usr/local/apache-zookeeper-3.8.0-bin/data  
#Zookeeper客户端连接端口	
clientPort=2181                           
#集群服务地址,2888为zookeeper集群内通讯端口,Leader监听此端口
server.1=node1:2888:3888
server.2=node2:2888:3888
server.3=node3:2888:3888
#保留3个快照
autopurge.snapRetainCount=3   
#1小时清理一下日志      
autopurge.purgeInterval=1            

mkdir /usr/local/apache-zookeeper-3.8.0-bin/data

将Zookeeper拷贝至其他节点

scp -r /usr/local/apache-zookeeper-3.8.0-bin/ root@node2:/usr/local/

scp -r /usr/local/apache-zookeeper-3.8.0-bin/ root@node3:/usr/local/

6.配置Zookeeper的id(所有设备)

cd /usr/local/apache-zookeeper-3.8.0-bin/data

vi myid

#myid对应zoo.cfg配置文件里相应的server号,node1也就会是1,node2是2,node3则是3
1

7.启动Zookeeper(所有设备)

cd /usr/local/apache-zookeeper-3.8.0-bin/bin

./zkServer.sh start

ZooKeeper JMX enabled by default
Using config: /usr/local/apache-zookeeper-3.8.0-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

三台zookeeper启动后再查看zookeeper状态,可以看到我是node2被选举为了leader

[root@node2 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/apache-zookeeper-3.8.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: leader

[root@node1 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/apache-zookeeper-3.8.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower

jps命令查看,看到QuorumPeerMain即可,QuorumPeerMain是zookeeper集群的启动类,用来加载配置启动QuorumPeer线程的

10451 QuorumPeerMain         
10622 Jps

 

程序员灯塔
转载请注明原文链接:ZooKeeper集群部署
喜欢 (0)