1准备奇数台机子3,5,7
我准备的是三台
192.168.58.81 zookeeper-1
192.168.58.82 zookeeper-2
192.168.58.83 zookeeper-3
下载jdk 把他配置环境变量并检查是否是环境变量 echo $JAVA_HOME
cd /opt/software
wget http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz
tar -xzvf apache-zookeeper-3.6.2-bin.tar.gz -C /usr/
mv /usr/apache-zookeeper-3.6.2-bin/ /usr/zookeeper
cd /usr/zookeeper/
vim /etc/profile
source /etc/profile
echo $ZK_HOME
cp conf/zoo_sample.cfg conf/zoo.cfg
vim conf/zoo.cfg
最后一行加
server.1=zookeeper-1:2888:3888
server.2=zookeeper-2:2888:3888
server.3=zookeeper-3:2888:3888
保存退出
mkdir /usr/zookeeper/data
vi /etc/hosts
最后一行添加
192.168.58.81 zookeeper-1
192.168.58.82 zookeeper-2
192.168.58.83 zookeeper-3
保存退出
vim /usr/zookeeper/data/myid
添加数字
1
保存退出
2 准备发射
#生成公钥私钥
ssh-keygen -t rsa
回车回车回车
cd /root/
ls .ssh
能看到 id_rsa id_rsa.pub
将你的 SSH 公钥添加到名为 zookeeper-1 的远程服务器上
ssh-copy-id zookeeper-1
yes
ssh-copy-id zookeeper-2
yes
ssh-copy-id zookeeper-3
yes
scp -r /usr/zookeeper/ zookeeper-2:/usr/
scp -r /usr/zookeeper/ zookeeper-3:/usr/
scp /etc/hosts zookeeper-2:/etc/
scp /etc/hosts zookeeper-3:/etc/
scp /etc/profile zookeeper-2:/etc/
scp /etc/profile zookeeper-3:/etc/
3 发射完毕
改zookeeper-2里面的配置
source /etc/profile 让刚发射的环境变量文件生效
vim /usr/zookeeper/data/myid 从zookeeper-1发射过来的刚才配的数字是1改称2
改zookeeper-3里面的配置
source /etc/profile
vim /usr/zookeeper/data/myid 从zookeeper-1发射过来的刚才配的数字是1改称3
再zookeeper-1 zookeeper-2 zookeeper-3 启动zookeeper
zkServer.sh start zkServer.sh start zkServer.sh start
zkServer.sh status zkServer.sh status zkServer.sh status
按顺序启动1,2,3 可以看到zookeeper-2是leader ,1,3 follower
---------------------------------------------------------------------------------------------------------------------------------
4 脚本
一台一台启动太麻烦搞个zookeeper启动关闭脚本
mkdir shell && cd shell && vim zookeeper-start-stop.sh
#!/bin/bash
##获取当前主机运行的zk进程的数量
# zookeeper主机名字不带后面的数字
zkpsnum=`ps -ef | grep zookeeper-root-server-zookeeper | grep -v grep|wc -l`
#判断进程数量
if [ $zkpsnum == 0 ];then
#打印提示
echo 'zk集群开始启动!'
#使用for循环启动各个服务器
for i in {1..3};do
#远程登录服务器执行命令source /etc/profile;让环境变量生效
ssh zookeeper-$i "source /etc/profile;zkServer.sh start"
done
echo 'zk集群启动完毕!'
echo 'zk集群状态如下:'
#使用for循环查看状态
for i in {1..3};do
#远程登录服务器执行命令source /etc/profile;让环境变量生效
ssh zookeeper-$i "source /etc/profile;zkServer.sh status"
done
else
echo 'zk集群开始关闭!!!!!!!!!!!!!!!'
#使用for循环查看状态
for i in {1..3};do
#远程登录服务器执行命令source /etc/profile;让环境变量生效
ssh zookeeper-$i "source /etc/profile;zkServer.sh stop"
done
echo 'zk集群关闭完毕!'
fi
chmod +x zookeeper-start-stop.sh
标签:profile,source,zookeeper,etc,sh,集群,usr,搭建 From: https://blog.csdn.net/m0_74744788/article/details/141548299