先准备好三台linux(虚拟机)。
解压安装包到我们指定的目录下。
cd /mysofttar -xvf jdk-8u131-linux-x64.gz -C /usr/local/java/jdk/
ps:这里看到了Zookeeper的安装包是因为我把Zookeeper解压之后想起来要有Java环境的。。。。
vi /etc/profile
在文件里面加入如下配置
# javaexport JAVA_HOME=/usr/local/java/jdk/jdk1.8.0_131export JRE_HOME=/usr/local/java/jdk/jdk1.8.0_131/jreexport CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/libexport PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
可以看到Zookeeper已经存在于/usr/local/目录下了
vi /etc/profile
加上
# Zookeeperexport ZOOKEEPER_HOME=/usr/local/zookeeper-3.4.10export PATH=$ZOOKEEPER_HOME/bin:$PATH
这里要说明一下,因为我是一边部署一边截图的,而这个步骤到后面貌似有点问题,所以你们就直接把Zookeeper的配置移动到Java上面去!!!
看一下Zookeeper的目录结构先
bin目录主要都是启动相关的,conf则是与配置相关的。进入conf文件夹,修改一下配置文件名字(纯粹为了方便)
cd /usr/local/cookeeper-3.4.10/confmv zoo_simple.cfg zoo.cfgvi zoo.cfg
首先 修改 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-代表端口,这个端口用来系统之间通信
找到你配的那一行,Esc,然后:q!退出,找到Zookeeper目录,新建data文件夹,并且在data文件夹下面创建一个文件,叫myid,并且在文件里写入server.X对应的X
然后
cd datavi 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】
先刷新一下环境变量
source /etc/profile
然后
zkServer.sh start
输出信息:
ZooKeeper JMX enabled by defaultUsing config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfgStarting zookeeper ... STARTED
看起来启动起来了,是否真正启动,执行以下命令
zkServer.sh status
一般来说,都会报错哈哈哈!!!
[root@localhost conf]# zkServer.sh statusZooKeeper JMX enabled by defaultUsing config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfgError contacting service. It is probably not running.
为什么呢,我想到会不会是防火墙的问题,我先看端口有没有监听,3888已经监听了,所以应该是防火墙的问题
[root@localhost conf]# netstat -tunlpActive 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 startZooKeeper JMX enabled by defaultUsing config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfgStarting zookeeper ... STARTED[root@localhost conf]# netstat -tunlpActive 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 statusZooKeeper JMX enabled by defaultUsing config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfgError contacting service. It is probably not running.[root@localhost conf]# systemctl stop firewalld[root@localhost conf]# zkServer.sh startZooKeeper JMX enabled by defaultUsing config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfgStarting zookeeper ... already running as process 12714.[root@localhost conf]# zkServer.sh statusZooKeeper JMX enabled by defaultUsing config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfgMode: follower[root@localhost conf]#
192.168.127.130
[root@localhost data]# zkServer.sh startZooKeeper JMX enabled by defaultUsing config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfgStarting zookeeper ... STARTED[root@localhost data]# netstat -tunlpActive 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 startZooKeeper JMX enabled by defaultUsing config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfgStarting zookeeper ... already running as process 9270.[root@localhost data]# zkServer.sh statusZooKeeper JMX enabled by defaultUsing config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfgMode: follower[root@localhost data]#
192.168.127.131
[root@localhost data]# zkServer.sh startZooKeeper JMX enabled by defaultUsing config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfgStarting zookeeper ... STARTED[root@localhost data]# netstat -tunlpActive 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 startZooKeeper JMX enabled by defaultUsing config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfgStarting zookeeper ... already running as process 9224.[root@localhost data]# zkServer.sh statusZooKeeper JMX enabled by defaultUsing config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfgMode: 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
到此看到三台的状态了,相关教程会陆续更新。。。可能会有点慢哦
联系客服