搭建 Zookeeper-3.4.10 集群

先准备好三台linux(虚拟机)。

1. 先把Java环境配好。我CentOS7 + jdk1.8.0_131

1.1 先把jdk上传到系统里面(如果不会的话去百度),我在系统的根目录建了一个文件夹mysoft,用来存放需要的安装包,比如jdk、Zookeeper

1.2 然后cd 到/usr/local/下,建立一个文件夹用来存放jdk

复制代码
cd /usr/local/

mkdir java

cd java/

mkdir jdk
复制代码

1.3 然后定位到我们安装包的目录。

 解压安装包到我们指定的目录下。

cd /mysoft

tar -xvf jdk-8u131-linux-x64.gz -C /usr/local/java/jdk/

 

 

 ps:这里看到了Zookeeper的安装包是因为我把Zookeeper解压之后想起来要有Java环境的。。。。

 1.4 配置环境变量

vi /etc/profile

 

 在文件里面加入如下配置

# java
export JAVA_HOME=/usr/local/java/jdk/jdk1.8.0_131
export JRE_HOME=/usr/local/java/jdk/jdk1.8.0_131/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH

 

 1.5 最后刷新配置,查看Java是否安装成功

 

2. 三台机器,配置Zookeeper 

2.1 定位到安装包文件夹,然后执行解压(所有操作三台机器一样)

可以看到Zookeeper已经存在于/usr/local/目录下了

2.2 配置一下环境变量(此步骤的目的是:每次启动服务就不需要定位到Zookeeper的bin目录了)

复制代码
vi /etc/profile

加上

# Zookeeper export ZOOKEEPER_HOME
=/usr/local/zookeeper-3.4.10 export PATH=$ZOOKEEPER_HOME/bin:$PATH
复制代码

 

这里要说明一下,因为我是一边部署一边截图的,而这个步骤到后面貌似有点问题,所以你们就直接把Zookeeper的配置移动到Java上面去!!!

 

2.3 配置Zookeeper的配置文件

看一下Zookeeper的目录结构先

bin目录主要都是启动相关的,conf则是与配置相关的。进入conf文件夹,修改一下配置文件名字(纯粹为了方便)

cd /usr/local/cookeeper-3.4.10/conf

mv zoo_simple.cfg zoo.cfg

vi zoo.cfg

 

 

 2.4 具体配置

首先 修改 dataDir,顾名思义就是【数据目录】了,修改成我们自定义的即可。

然后看下面的三个server:

(1)为什么是三个,因为Zookeeper喜欢奇数不喜欢偶数。

(2)三行server解释

官方解释

The entries of the form server.X list the servers that make up the ZooKeeper service. When the server starts up, it knows which server it is by looking for the file myid in the data directory. That file has the contains the server number, in ASCII.

Finally, note the two port numbers after each server name: " 2888" and "3888". Peers use the former port to connect to other peers. Such a connection is necessary so that peers can communicate, for example, to agree upon the order of updates. More specifically, a ZooKeeper server uses this port to connect followers to the leader. When a new leader arises, a follower opens a TCP connection to the leader using this port. Because the default leader election also uses TCP, we currently require another port for leader election. This is the second port in the server entry.

蹩脚翻译

表单server.X的条目列出构成ZooKeeper服务的服务器。当服务器启动时,它通过查找数据目录中的文件myid来知道它是哪个服务器 。该文件包含服务器编号,以ASCII格式显示。

最后,请注意每个服务器名称后面的两个端口号:“2888”和“3888”。对等体使用前端口连接到其他对等体。这样的连接是必要的,使得对等体可以进行通信,例如,以商定更新的顺序。更具体地说,一个ZooKeeper服务器使用这个端口来连接追随者到领导者。当新的领导者出现时,追随者使用此端口打开与领导者的TCP连接。因为默认领导选举也使用TCP,所以我们目前需要另外一个端口进行领导选举。这是服务器条目中的第二个端口。

大概意思

server.X=A:B:C

X-代表服务器编号

A-代表ip

B和C-代表端口,这个端口用来系统之间通信

2.5 查看配置文件,根据dataDir进行X的配置【这里三台服务器不一样!!!】

找到你配的那一行,Esc,然后:q!退出,找到Zookeeper目录,新建data文件夹,并且在data文件夹下面创建一个文件,叫myid,并且在文件里写入server.X对应的X

然后

复制代码
cd data

vi myid


#之后会产生一个新文件,直接在里面写X即可

#比如我配置的三个server,当前服务器的ip是多少,myid里面写的X就是server.X=ip:2888:3888 中ip所对应的X

server.0=192.168.127.129:2888:3888【192.168.127.129服务器上面的myid填写0】
 server.1=192.168.127.130:2888:3888【192.168.127.130服务器上面的myid填写1】
 server.2=192.168.127.131:2888:3888【192.168.127.131服务器上面的myid填写2】
复制代码

 

2.6 最后启动服务器

先刷新一下环境变量

source /etc/profile

 

然后

zkServer.sh start

 

输出信息:
ZooKeeper JMX enabled by default Using config:
/usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg Starting zookeeper ... STARTED

 

看起来启动起来了,是否真正启动,执行以下命令

zkServer.sh status

 

一般来说,都会报错哈哈哈!!!

[root@localhost conf]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.

 

为什么呢,我想到会不会是防火墙的问题,我先看端口有没有监听,3888已经监听了,所以应该是防火墙的问题

复制代码
[root@localhost conf]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      2729/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1464/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1467/cupsd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2611/master         
tcp6       0      0 :::38952                :::*                    LISTEN      12714/java          
tcp6       0      0 192.168.127.129:3888    :::*                    LISTEN      12714/java          
tcp6       0      0 :::22                   :::*                    LISTEN      1464/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1467/cupsd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      2611/master         
tcp6       0      0 :::2181                 :::*                    LISTEN      12714/java          
复制代码

 

关闭防火墙

systemctl stop firewalld

之后果然好了起来,以下是三台服务器的启动结果。

192.168.127.129

复制代码
[root@localhost conf]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@localhost conf]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      2729/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1464/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1467/cupsd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2611/master         
tcp6       0      0 :::38952                :::*                    LISTEN      12714/java          
tcp6       0      0 192.168.127.129:3888    :::*                    LISTEN      12714/java          
tcp6       0      0 :::22                   :::*                    LISTEN      1464/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1467/cupsd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      2611/master         
tcp6       0      0 :::2181                 :::*                    LISTEN      12714/java          
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           768/avahi-daemon: r 
udp        0      0 127.0.0.1:323           0.0.0.0:*                           793/chronyd         
udp        0      0 0.0.0.0:33617           0.0.0.0:*                           1247/dhclient       
udp        0      0 0.0.0.0:50192           0.0.0.0:*                           768/avahi-daemon: r 
udp        0      0 192.168.122.1:53        0.0.0.0:*                           2729/dnsmasq        
udp        0      0 0.0.0.0:67              0.0.0.0:*                           2729/dnsmasq        
udp        0      0 0.0.0.0:68              0.0.0.0:*                           1247/dhclient       
udp6       0      0 ::1:323                 :::*                                793/chronyd         
udp6       0      0 :::63891                :::*                                1247/dhclient       
[root@localhost conf]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
[root@localhost conf]# systemctl stop firewalld
[root@localhost conf]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... already running as process 12714.
[root@localhost conf]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower
[root@localhost conf]# 
复制代码

 

192.168.127.130

复制代码
[root@localhost data]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@localhost data]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      2715/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1463/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1462/cupsd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2393/master         
tcp6       0      0 :::52456                :::*                    LISTEN      9270/java           
tcp6       0      0 192.168.127.130:3888    :::*                    LISTEN      9270/java           
tcp6       0      0 :::22                   :::*                    LISTEN      1463/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1462/cupsd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      2393/master         
tcp6       0      0 :::2181                 :::*                    LISTEN      9270/java           
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           772/avahi-daemon: r 
udp        0      0 127.0.0.1:323           0.0.0.0:*                           821/chronyd         
udp        0      0 0.0.0.0:60370           0.0.0.0:*                           772/avahi-daemon: r 
udp        0      0 192.168.122.1:53        0.0.0.0:*                           2715/dnsmasq        
udp        0      0 0.0.0.0:67              0.0.0.0:*                           2715/dnsmasq        
udp        0      0 0.0.0.0:68              0.0.0.0:*                           1247/dhclient       
udp        0      0 0.0.0.0:5765            0.0.0.0:*                           1247/dhclient       
udp6       0      0 :::47268                :::*                                1247/dhclient       
udp6       0      0 ::1:323                 :::*                                821/chronyd         
[root@localhost data]# systemctl stop firewalld
[root@localhost data]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... already running as process 9270.
[root@localhost data]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower
[root@localhost data]# 
复制代码

 

192.168.127.131

复制代码
[root@localhost data]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@localhost data]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      2738/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1464/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1468/cupsd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2614/master         
tcp6       0      0 :::34092                :::*                    LISTEN      9224/java           
tcp6       0      0 192.168.127.131:3888    :::*                    LISTEN      9224/java           
tcp6       0      0 :::22                   :::*                    LISTEN      1464/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1468/cupsd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      2614/master         
tcp6       0      0 :::2181                 :::*                    LISTEN      9224/java           
udp        0      0 0.0.0.0:39568           0.0.0.0:*                           1249/dhclient       
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           819/avahi-daemon: r 
udp        0      0 0.0.0.0:53561           0.0.0.0:*                           819/avahi-daemon: r 
udp        0      0 127.0.0.1:323           0.0.0.0:*                           825/chronyd         
udp        0      0 192.168.122.1:53        0.0.0.0:*                           2738/dnsmasq        
udp        0      0 0.0.0.0:67              0.0.0.0:*                           2738/dnsmasq        
udp        0      0 0.0.0.0:68              0.0.0.0:*                           1249/dhclient       
udp6       0      0 ::1:323                 :::*                                825/chronyd         
udp6       0      0 :::7584                 :::*                                1249/dhclient       
[root@localhost data]# systemctl stop firewalld
[root@localhost data]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... already running as process 9224.
[root@localhost data]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: leader
[root@localhost data]# 
复制代码

 

其实也可以查看启动过程

zkServer.sh start-foreground

 

关于防火墙关闭的问题,应该是不安全的,你可以吧2888,3888端口添加进开放端口中,我还没来得及试试

firewall-cmd --zone=public --add-port=2888/tcp --permanent
firewall-cmd --zone=public --add-port=3888/tcp --permanent

 

 

到此看到三台的状态了,相关教程会陆续更新。。。可能会有点慢哦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值