yum install -y rsync
hostname处理:
vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.0.81 server81
192.168.0.82 server82
192.168.0.83 server83
~
编写脚本:
#!/bin/bash
#1. 判断参数个数
if [ $# -lt 1 ]
then
echo Not Enough Arguement!
exit;
fi
#2. 遍历集群所有机器, 修改为自己的主机名
#for host in managerhd.bigdata masterhd.bigdata workerhd.bigdata
for host in server81 server82 server83
do
echo ==================== $host ====================
#3. 遍历所有目录,挨个发送
for file in $@
do
#4. 判断文件是否存在
if [ -e $file ]
then
#5. 获取父目录
pdir=$(cd -P $(dirname $file); pwd)
#6. 获取当前文件的名称
fname=$(basename $file)
ssh $host "mkdir -p $pdir"
rsync -av $pdir/$fname $host:$pdir
else
echo $file does not exists!
fi
done
done
设置免登录
cd ~/.ssh
ssh-keygen -t rsa
输入该命令后会有提示,一直回车即可
如果提示 【-bash: cd: .ssh: 没有那个文件或目录】 直接 ssh-keygen -t rsa 生成密钥就行
server82节点的公钥拷贝
[root@server82 .ssh]# ssh-copy-id -i server82
server83 节点的公钥拷贝
[root@server83 .ssh]# ssh-copy-id -i server83
免密码登录已经设定完成,注意第一次ssh登录时需要输入密码,再次访问时即可免密码登录
#!/bin/bash
# zookeeper集群命令
if [ $# == 0 ]
then
echo " Not Arguement! | start | stop | status"
exit;
fi
case $1 in
"start")
for i in server81 server82 server83
do
echo ------------- zookeeper $i start ------------
ssh $i "source /etc/profile;/opt/components/zookeeper/zookeeper-3.8.0/bin/zkServer.sh start /opt/components/zookeeper/zookeeper-3.8.0/conf/zoo.cfg"
done
;;
"stop")
for i in server81 server82 server83
do
echo ------------- zookeeper $i stop ------------
ssh $i "source /etc/profile;/opt/components/zookeeper/zookeeper-3.8.0/bin/zkServer.sh stop "
done
;;
"status")
for i in server81 server82 server83
do
echo ------------- zookeeper $i status ------------
ssh $i "source /etc/profile;/opt/components/zookeeper/zookeeper-3.8.0/bin/zkServer.sh status "
done
;;
esac
#!/bin/bash
# kafka集群命令
if [ $# == 0 ]
then
echo " Not Arguement! | start | stop | status "
exit;
fi
case $1 in
"start")
for i in server81 server82 server83
do
echo ------------- kafka $i start ------------
ssh $i "source /etc/profile;/opt/components/kafka/kafka_2.13-3.2.1/bin/kafka-server-start.sh -daemon /opt/components/kafka/conf/server.properties"
done
;;
"stop")
for i in server81 server82 server83
do
echo ------------- kafka $i stop ------------
ssh $i "source /etc/profile;/opt/components/kafka/kafka_2.13-3.2.1/bin/kafka-server-stop.sh "
done
;;
"status")
for i in server81 server82 server83
do
echo ------------- kafka $i status ------------
ssh $i "source /etc/profile;jps | grep Kafka "
done
;;
esac
#!/bin/bash
if [ $# == 0 ]
then
echo " Not Arguement! "
exit;
fi
for host in server81 server82 server83
do
echo =============== $host ===============
#在shell脚本写的ssh到 其他节点的时候 默认时不加载配置文件。 linux并不能去找到java中的jps命令。解决可以 先source一下
ssh $host "source /etc/profile;$1"
done