目录
日常学习中发现,部署集群服务时需要每个节点都上传相同源码包或者相同命令,很无聊,本质上是首先通过ssh认证,遍历各节点hostname通过ssh远程操作。
一、文件同步分发工具xsync
1、配置主机名映射一致
[root@k8s-master2 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.10.10.132 k8s-master2
172.10.10.133 k8s-lb1
172.10.10.134 k8s-lb2
2、为了方便集群管理,配置ssh信任证书.可以直接免密码登录其他节点
[root@master ~]# ssh-keygen一直回车即可 [root@master ~]# ssh-copy-id [email protected] [root@master ~]# ssh-copy-id [email protected]
3、所有节点安装sync服务
yum install rsync -y
4、创建文件同步脚本及授权
[root@k8s-master2 ~]# touch /usr/local/bin/xsync
[root@k8s-master2 ~]# chmod 777 /usr/local/bin/xsync
[root@k8s-master2 ~]# cat /usr/local/bin/xsync
#!/bin/bash
#1 获取输入参数个数,如果没有参数,直接退出
pcount=$#
if [ $pcount -lt 1 ]
then
echo Not Enough Arguement!
exit;
fi
#2. 遍历集群所有机器
for host in k8s-lb1 k8s-lb2
do
echo ==================== $host ====================
#3. 遍历所有目录,挨个发送
for file in $@
do
#4 判断文件是否存在
if [ -e $file ]
then
#5. 获取父目录
pdir=$(cd -P $(dirname $file); pwd)
echo pdir=$pdir
#6. 获取当前文件的名称
fname=$(basename $file)
echo fname=$fname
#7. 通过ssh执行命令:在$host主机上递归创建文件夹(如果存在该文件夹)
ssh $host "mkdir -p $pdir"
#8. 远程同步文件至$host主机的$USER用户的$pdir文件夹下
rsync -av $pdir/$fname $USER@$host:$pdir
else
echo $file does not exists!
fi
done
done
5、测试文件同步分发成功
[root@k8s-master2 ~]# touch test && xsync test
==================== k8s-lb1 ====================
pdir=/root
fname=test
sending incremental file list
testsent 85 bytes received 35 bytes 240.00 bytes/sec
total size is 0 speedup is 0.00
==================== k8s-lb2 ====================
pdir=/root
fname=test
sending incremental file list
testsent 85 bytes received 35 bytes 240.00 bytes/sec
total size is 0 speedup is 0.00
二、命令同步执行工具xcall
1、创建命令同步执行脚本及授权
[root@k8s-master2 bin]# pwd
/usr/local/bin
[root@k8s-master2 bin]# ll xcall
-rwxrwxrwx 1 root root 296 2月 5 16:31 xcall
[root@k8s-master2 bin]# cat xcall
#!/bin/sh
pcount=$# 无输出命令则退出
if((pcount==0));then
echo no args...;
exit;
fi
echo ==================master:172.10.10.132==================
$* 如果时$@只判断一个字符,例如ls -l就不能识别了
for((host=133; host<=134; host++)); do
echo ==================agent:172.10.10.$host==================
ssh 172.10.10.$host $* 遍历节点
done
2、测试命令同步执行成功
[root@k8s-master2 bin]# xcall ls -l
==================master:172.10.10.132==================
总用量 18816
-rwxr-xr-x 1 root root 10376657 3月 30 2016 cfssl
-rwxr-xr-x 1 root root 6595195 3月 30 2016 cfssl-certinfo
-rwxr-xr-x 1 root root 2277873 3月 30 2016 cfssljson
-rwxrwxrwx 1 root root 296 2月 5 16:31 xcall
-rwxrwxrwx 1 root root 894 2月 5 16:06 xsync
==================agent:172.10.10.133==================
总用量 8680
-rw-------. 1 root root 961 1月 26 22:41 anaconda-ks.cfg
-rw-r--r-- 1 root root 6595195 3月 30 2016 cfssl-certinfo_linux-amd64
-rw-r--r-- 1 root root 2277873 3月 30 2016 cfssljson_linux-amd64
drwxr-xr-x 5 root root 4096 1月 28 23:24 k8s
==================agent:172.10.10.134==================
总用量 42904
-rw-------. 1 root root 961 1月 26 22:41 anaconda-ks.cfg
-rw-r--r-- 1 root root 6595195 3月 30 2016 cfssl-certinfo_linux-amd64
-rw-r--r-- 1 root root 2277873 3月 30 2016 cfssljson_linux-amd64
drwxr-xr-x 5 root root 4096 1月 28 23:24 k8s
-rw-r--r-- 1 root root 35042811 7月 22 2019 zookeeper-3.4.10.tar.gz