前置环境:
实例1 | 实例2 | 实例3 | |
---|---|---|---|
IP | 192.168.11.11 | 192.168.11.12 | 192.168.11.13 |
OS | centos7.9 | centos7.9 | centos7.9 |
zk | zookeeper-3.8.0 | zookeeper-3.8.0 | zookeeper-3.8.0 |
java | JDK8 | JDK8 | JDK8 |
安装zookeeper集群依赖java环境,请在三台机器上先自行安装JDK。
如果java环境已就绪,下面开始安装zk集群。
# 工作目录
mkdir -p /usr/local/zookeeper
# 日志存储目录
mkdir -p /usr/local/zookeeper/dataDir
在三台实例上重复上述操作
# 下载
cd /usr/local/zookeeper
wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.8.0/apache-zookeeper-3.8.0-bin.tar.gz
# 解压
tar -zxvf apache-zookeeper-3.8.0-bin.tar.gz -C /usr/local/zookeeper/
在三台实例上重复上述操作
三台实例分别操作
#实例1
echo "1" > /usr/local/zookeeper/dataDir/myid
#实例2
echo "2" > /usr/local/zookeeper/dataDir/myid
#实例3
echo "3" > /usr/local/zookeeper/dataDir/myid
myid文件后续zoo.cfg中使用。
cd /usr/local/zookeeper/apache-zookeeper-3.8.0-bin/conf
mv zoo_sample.cfg zoo.cfg
vim zoo.cfg
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/usr/local/zookeeper/dataDir
server.1=192.168.11.11:2888:3888
server.2=192.168.11.12:2888:3888
server.3=192.168.11.13:2888:3888
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
maxClientCnxns=60
在三台实例上重复上述操作
也可以在一台机器修改zoo.cfg文件,scp到另外两台机器,命令参考如下:
scp zoo.cfg [email protected]:/usr/local/zookeeper/apache-zookeeper-3.8.0-bin/conf/zoo.cfg
systemctl stop firewalld
systemctl disable firewalld
若需要关闭防火墙,在三台实例上重复上述操作
cd /usr/lib/systemd/system/
#创建zookeeper服务文件
vim zookeeper.service
[Unit]
Description=zookeeper.service
After=network.target
[Service]
Type=forking
Environment="PATH=/usr/local/jdk1.8.0_311/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
User=root
Group=root
ExecStart=/usr/local/zookeeper/apache-zookeeper-3.8.0-bin/bin/zkServer.sh start
ExecStop=/usr/local/zookeeper/apache-zookeeper-3.8.0-bin/bin/zkServer.sh stop
Restart=on-failure
[Install]
WantedBy=multi-user.target
刷新systemctl
systemctl daemon-reload
设置zookeeper服务开机自启
systemctl enable zookeeper
启动zookeeper
systemctl start zookeeper
在三台实例上重复上述操作
关闭zookeeper
systemctl stop zookeeper
重启zookeeper
systemctl restart zookeeper
查看zookeeper实例状态
systemctl status zookeeper
实例1、实例2、实例3分别查看集群状态
/usr/local/zookeeper/apache-zookeeper-3.8.0-bin/bin/zkServer.sh status
查询结果如下:
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/apache-zookeeper-3.8.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/zookeeper/apache-zookeeper-3.8.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: leader
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/apache-zookeeper-3.8.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
以上,zookeeper集群搭建完成。