本次搭建集群,Zookeeper节点个数(奇数)为3个。默认对外提供服务的端口号2181 ,集群内部3个节点之间通信默认使用 2888:3888。
节点 IP 如下(虚拟机):

  • 192.168.10.150
  • 192.168.10.151
  • 192.168.10.152

然后在三台服务器分别安装zookeeper

进入安装位置

cd /usr/local/soft/zookeeper

下载 zookeeper

wget https://dlcdn.apache.org/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz

如果出现以下错误

ERROR: cannot verify dlcdn.apache.org's certificate, issued by ‘/C=US/O=Let's Encrypt/CN=R3’:
  Issued certificate has expired.
To connect to dlcdn.apache.org insecurely, use `--no-check-certificate'.

则加上 --no-check-certificate

wget --no-check-certificate https://dlcdn.apache.org/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz

解压

tar -zxvf apache-zookeeper-3.7.0-bin.tar.gz 

进入 zookeeper 解压文件

cd apache-zookeeper-3.7.0-bin/conf/

修改配置文件

 #zookeeper服务端启动时候会根据这个名字来加载
 vim zoo.cfg

在 zoo.cfg 末端添加如下配置

initLimit=10
syncLimit=5
dataDir=/usr/local/soft/zookeeper/apache-zookeeper-3.7.0-bin/data
clientPort=2181
server.1=192.168.10.150:2888:3888
server.2=192.168.10.151:2888:3888
server.3=192.168.10.152:2888:3888

创建数据存储目录

mkdir -p /usr/local/soft/zookeeper/apache-zookeeper-3.7.0-bin/data

创建myid文件, id 与 zoo.cfg 中的序号对应

#在192.168.10.150机器上执行
echo 1 > /usr/local/soft/zookeeper/apache-zookeeper-3.7.0-bin/data/myid
#在192.168.10.151机器上执行
echo 2 > /usr/local/soft/zookeeper/apache-zookeeper-3.7.0-bin/data/myid
#在192.168.10.152机器上执行
echo 3 > /usr/local/soft/zookeeper/apache-zookeeper-3.7.0-bin/data/myid

配置环境变量

vim /etc/profile

在最后加上

export ZK_HOME=/usr/local/soft/zookeeper/apache-zookeeper-3.7.0-bin
export PATH=$ZK_HOME/bin:$PATH

强制生效

source /etc/profile

分别进入bin目录启动

cd /usr/local/soft/zookeeper/apache-zookeeper-3.7.0-bin/bin/
zkServer.sh start

出现以下则表示成功

Starting zookeeper ... STARTED

如果出现未安装jdk的错误

Error: JAVA_HOME is not set and java could not be found in PATH.

安装 jdk 后,再执行 zkServer.sh start启动

yum install -y java-1.8.0-openjdk.x86_64

通过zkServer.sh status查看状态信息,能出现Mode: follower或者Mode: leader才是真正的成功了。

# 查看状态信息
zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/soft/zookeeper/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower

如果也有以下的问题,则需要开启端口:

ZooKeeper JMX enabled by default
Using config: /usr/local/soft/zookeeper/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
myid could not be determined, will not able to locate clientPort in the server configs.
Client port found: 2181
2181. Client address: localhost. Client SSL: false.
Error contacting service. It is probably not running.
# 首先停掉服务
zkServer.sh stop
# 改用zkServer.sh start-foreground方式启动,可以看到具体错误日志
zkServer.sh start-foreground
# 接着配置防火墙
systemctl start firewalld
firewall-cmd --zone=public --add-port=2888/tcp --permanent
firewall-cmd --zone=public --add-port=3888/tcp --permanent
# 并重载入添加的端口:
firewall-cmd --reload
# 再次查询端口开放情况,确定2888和3888开放
firewall-cmd --zone=public --list-ports

如果还是有同样的错误,可以尝试以下方法(过程很痛苦,耐心检查即可):

  • 节点 ip 分别 ping 一下
  • 检查配置文件
  • 阿里云上的,可以在 zoo.cfg 把本机的 ip 配置为 0.0.0.0
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐