rsync同步工具,编写xsync脚本
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
~
编写脚本:
if [ $
then
echo Not Enough Arguement!
exit;
fi
for host in server81 server82 server83
do
echo ==================== $host ====================
for file in $@
do
if [ -e $file ]
then
pdir=$(cd -P $(dirname $file); pwd)
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登录时需要输入密码,再次访问时即可免密码登录
集群服务器,批操作脚本
- zookeeper 脚本:
#!/bin/bash
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
- Kafka 脚本
#!/bin/bash
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 ===============
ssh $host "source /etc/profile;$1"
done