Home  >  Article  >  php教程  >  在CentOS 7 上安装Zookeeper集群

在CentOS 7 上安装Zookeeper集群

WBOY
WBOYOriginal
2016-06-13 08:47:451660browse

在CentOS 7 上安装Zookeeper集群

测试机上需要安装java软件
$ rpm -qa|grep java
$ sudo yum install -y java-1.8.0-openjdk.x86_64
$ java -version
openjdk version "1.8.0_65"
OpenJDK Runtime Environment (build 1.8.0_65-b17)
OpenJDK 64-Bit Server VM (build 25.65-b01, mixed mode)

1、创建用户:bjrenrui0001~0003
sudo groupadd zookeeper
sudo useradd -g zookeeper zookeeper
echo "zookeeper@1234"|sudo passwd zookeeper —stdin

2、创建工作目录
bjrenrui0001:
sudo mkdir /yanfa/mq
sudo ln -s /yanfa/mq /mq
sudo chown -R dreamjobs.dreamjobs /mq

bjrenrui0002~0003:
sudo mkdir mq
sudo chown dreamjobs.dreamjobs mq
sudo ln -s /home/backupfile/mq /mq
sudo chown dreamjobs.dreamjobs mq

1. 单机模式(Standalone mode)
下载,解压
cd /mq
wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.7/zookeeper-3.4.7.tar.gz
tar zxvf zookeeper-3.4.7.tar.gz -C /mq
ln -s /mq/zookeeper-3.4.7 zookeeper

启动
默认就是单机模式
cd /mq/zookeeper/
cp ./conf/zoo_sample.cfg ./conf/zoo.cfg
sh ./bin/zkServer.sh start

防火墙开启2181端口

检查服务状态:
[dreamjobs@bjrenrui0001 bin]$ sh zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
Mode: standalone

使用java 客户端连接ZooKeeper
./bin/zkCli.sh -server 127.0.0.1:2181
然后就可以使用各种命令了,跟文件操作命令很类似,输入help可以看到所有命令。

关闭
sh bin/zkServer.sh stop

2 分布式模式(Replicated mode)
在生产环境中,要配置成分布式模式,才能发挥威力。
ZooKeeper集群一般被称为ZooKeeper ensemble(集成),或者 quorum(法定人数).

准备3台机器

假设有三台机器,hostname和ip对应关系是:
192.168.100.200 bjrenrui0001
192.168.100.201 bjrenrui0002
192.168.100.202 bjrenrui0003

ZooKeeper不存在明显的master/slave关系,各个节点都是服务器,leader挂了,会立马从follower中选举一个出来作为leader.
由于没有主从关系,也不用配置SSH无密码登录了,各个zk服务器是自己启动的,互相之间通过TCP端口来交换数据。

创建日志和数据目录:
bjrenrui0001~0003
cd /mq/zookeeper
mkdir -p zookeeperdatadir/{logs,data}

修改bjrenrui0001的配置文件conf/zoo.cfg:
[dreamjobs@bjrenrui0001 conf]$ grep -vE '^($|#)' zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/mq/zookeeper/zookeeperdatadir/data
dataLogDir=/mq/zookeeper/zookeeperdatadir/logs
clientPort=2181
server.1=bjrenrui0001:2888:3888
server.2=bjrenrui0002:2888:3888
server.3=bjrenrui0003:2888:3888
修改完后拷贝到 bjrenrui0002,以及 bjrenrui0003上

myid文件:
要在每台机器的dataDir下,新建一个myid文件,里面存放一个数字,用来标识当前主机
bjrenrui0001: echo "1" >> /mq/zookeeper/zookeeperdatadir/data/myid
bjrenrui0002: echo "2" >> /mq/zookeeper/zookeeperdatadir/data/myid
bjrenrui0003: echo "3" >> /mq/zookeeper/zookeeperdatadir/data/myid

每台机器上启动zookeeper服务:
bjrenrui0001: sh /mq/zookeeper/bin/zkServer.sh start
bjrenrui0002: sh /mq/zookeeper/bin/zkServer.sh start
bjrenrui0003: sh /mq/zookeeper/bin/zkServer.sh start
每台机器上停止zookeeper服务:
bjrenrui0001: sh /mq/zookeeper/bin/zkServer.sh stop
bjrenrui0002: sh /mq/zookeeper/bin/zkServer.sh stop
bjrenrui0003: sh /mq/zookeeper/bin/zkServer.sh stop
因为3个节点的启动是有顺序的,所以在陆续启动三个节点的时候,前面先启动的节点连接未启动的节点的时候会报出一些错误,可以忽略。

防火墙开启2181端口:
sudo vi /etc/sysconfig/iptables
-A INPUT -p tcp -s 192.168.100.0/24 -j ACCEPT
sudo systemctl restart iptables.service

查看状态
[dreamjobs@bjrenrui0001 conf]$ gmbjyf.sh bjyfnbserver "cat /mq/zookeeper/zookeeperdatadir/data/myid"
bjrenrui0001
1
bjrenrui0002
2
bjrenrui0003
3
$ gmbjyf.sh bjyfnbserver "sh /mq/zookeeper/bin/zkServer.sh restart"
bjrenrui0001
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
bjrenrui0002
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
bjrenrui0003
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper … STARTED

$ gmbjyf.sh bjyfnbserver "sh /mq/zookeeper/bin/zkServer.sh status"
bjrenrui0001
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
Mode: follower
bjrenrui0002
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
Mode: leader
bjrenrui0003
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
Mode: follower

使用java客户端连接ZooKeeper集群
找一台机器,解压zookeeper压缩包,不用配置,就可以使用java客户端连接ZooKeeper集群中的任意一台服务器了。
$ sh /mq/zookeeper/bin/zkCli.sh -server bjrenrui0001:2181
$ sh /mq/zookeeper/bin/zkCli.sh -server bjrenrui0002:2181
$ sh /mq/zookeeper/bin/zkCli.sh -server bjrenrui0003:2181
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn