轻量级的集群管理软件clustershell操作

1、 ClusterShell简介

在运维实战中,如果有若干台数据库服务器,想对这些服务器进行同等动作,比如查看它们当前的即时负载情况,查看它们的主机名,分发文件等等,这个时候该怎么办?一个个登陆服务器去操作,太傻帽了!写个shell去执行,浪费时间~~

这种情况下,如果集群数量不多的话,选择一个轻量级的集群管理软件就显得非常有必要了。ClusterShell就是这样一种小的集群管理工具,原理是利用ssh,可以说是Linux系统下非常好用的运维利器!
选择了clustershell这个软件(也简称clush),原因如下:

1)安装方便。一条指令就能轻松安装。

2)配置方便。很多集群管理软件都需要在所有的服务器上都安装软件,而且还要进行很多的连接操作,clustershell就相当的方便了,仅仅需要所有机器能够ssh无密码登录即可,然后只在一台服务器上安装clustershell即可。

3)使用方便。clustershell的命令相对来说非常简单,只有一两个指令以及三四个参数需要记。

2、clustershell安装

2.1 环境

服务器名 IP 备注
vm9 192.168.26.9 主控
vm10 192.168.26.10 备用
vm11 192.168.26.11 节点11
vm12 192.168.26.12 节点12
vm13 192.168.26.13 节点13

配置主控节点到所有其他服务器的root免密登录


[root@vms9 /]# ssh-keygen -t rsa  ##生成密钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:vJ39nD+3D1TXDn46Ad6GCvD/oColTH+l4rpJB4x/Y9g root@vms9
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|                .|
|      .     . . +|
|   o.  +  .. = +.|
|  .oo.  So  o * o|
|   .o++ o= + o + |
|    ++Eo. * . +  |
|   ..=.. . o o =.|
|    ++...   . +oB|
+----[SHA256]-----+


[root@vms9 .ssh]# ssh-copy-id -i id_rsa.pub root@192.168.26.10 ## 发送公钥给其他服务器,依次发送给所有服务器,完成主控到其他服务器的root免密登录
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa.pub"
The authenticity of host '192.168.26.10 (192.168.26.10)' can't be established.
ECDSA key fingerprint is SHA256:K0Hj9hsgMc/f4iCOeBspHkvx71sIGaMplYXlc0NdZW4.
ECDSA key fingerprint is MD5:af:5b:2a:ee:a2:5a:97:e2:56:43:4c:d1:ff:46:c0:22.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.26.10's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.26.10'"
and check to make sure that only the key(s) you wanted were added.


2.2 安装clustershell


[root@vms9 .ssh]# yum install -y clustershell 在主控节点安装clustershell


2.3 配置好主机映射


[root@vms9 clustershell]# cat /etc/hosts ##做好主机映射关系
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.26.9 vm9
192.168.26.10 vm10
192.168.26.11 vm11
192.168.26.12 vm12
192.168.26.13 vm13


3、clustershell 配置文件以及常用命令

3.1 配置clustershell 分组文件

手动生成/etc/clustershell/groups文件,配置cluster分组

需要注意的是all 是必须配置的,clush 有 -a 这个参数,主机间用空格分离。


[root@vms9 clustershell]# cat /etc/clustershell/groups
all:vm9 vm10 vm11 vm12 vm13
control:vm9 vm10
node:vm11 vm12 vm13
gene:vm10 vm11 vm12 vm13

##支持正则,或者可以写作如下方式

[root@vms9 clustershell]# cat /etc/clustershell/groups
all:vm[9-13]
control:vm[9-10]
node:vm[11-13]
gene:vm[10-13]


3.2 clush常用命令

clush命令:

clush -a 全部 等于 clush -g all

clush -g 指定组

clush -w 操作主机名字,多个主机之间用逗号隔开

clush -g 组名 -c --dest 文件群发 (-c等于--copy)

clush -g 组名 --rcopy 远程文件或者文件夹 --dest 本地目的目录 复制远程服务器上的文件、文件夹至本地

clush -x 表示去掉某个节点进行操作。后面跟主机节点,多个主机中间用逗号隔开

clush -X 表示去掉某个组进行操作,多个组之间用逗号隔开

clush -b 相同输出结果合并

--user=username,这个表示使用clush命令操作时,登陆ssh时使用的用户。该参数需要紧跟在clush命令之后

clush是基于ssh和scp命令进行封装的一个工具,默认的ssh端口如果不是22,那么在执行clush命令的时候需要指明端口:

1)进行文件传输时, 需要加 -o -P57891 即大写P

2)进行直接访问(批量执行操作命令)时, 需要加 -o -p57891 即小写P

注意:clush 是不支持环境变量的$PATH

注意: clush操作远程机器,执行动作要放在双引号或单引号内进行

3.3 clush测试

测试批量执行命令


[root@vms9 clustershell]# clush -g gene uptime | sort -u  ## 批量查看启动时间
vm10:  18:00:47 up 50 min,  1 user,  load average: 0.00, 0.01, 0.02
vm11:  18:00:47 up 50 min,  1 user,  load average: 0.00, 0.01, 0.05
vm12:  18:00:47 up 50 min,  1 user,  load average: 0.00, 0.01, 0.03
vm13:  18:00:47 up 50 min,  1 user,  load average: 0.00, 0.01, 0.03

[root@vms9 clustershell]# clush -g gene "echo abcdfa >> /root/yjtest20210713.txt" ##批量写文件
[root@vms9 clustershell]# clush -g gene "cat /root/yjtest20210713.txt"  ## 批量查看文件         
vm10: abcdfa                                                                    
vm12: abcdfa                                                                     
vm11: abcdfa                                                                     
vm13: abcdfa  

测试批量复制文件


[root@vms9 clustershell]# touch /root/yjtest20210713001.txt
[root@vms9 clustershell]# clush -g gene -c /root/yjtest20210713001.txt --dest /root/ ## 批量复制文件
[root@vms9 clustershell]# clush -g gene "ls -al /root/yjtest20210713001.txt" | sort -u
vm10: -rw-r--r-- 1 root root 0 7月  13 18:06 /root/yjtest20210713001.txt
vm11: -rw-r--r-- 1 root root 0 7月  13 18:06 /root/yjtest20210713001.txt
vm12: -rw-r--r-- 1 root root 0 7月  13 18:06 /root/yjtest20210713001.txt
vm13: -rw-r--r-- 1 root root 0 7月  13 18:06 /root/yjtest20210713001.txt                                                                   

测试能否批量覆盖文件


[root@vms9 clustershell]# cat /root/yjtest20210713001.txt
test20210711
123456
612345
[root@vms9 clustershell]# clush -g gene -c /root/yjtest20210713001.txt --dest /root/
[root@vms9 clustershell]# clush -g gene "cat /root/yjtest20210713001.txt" | sort -u ##经查看确认可以批量覆盖文件
vm10: 123456
vm10: 612345
vm10: test20210711
vm11: 123456
vm11: 612345
vm11: test20210711
vm12: 123456
vm12: 612345
vm12: test20210711
vm13: 123456
vm13: 612345
vm13: test20210711

测试批量复制文件夹


[root@vms9 ~]# mkdir yjtest
[root@vms9 ~]# touch ./yjtest/1.txt ./yjtest/2.txt
[root@vms9 ~]# cd yjtest/
[root@vms9 yjtest]# ls
1.txt  2.txt

[root@vms9 yjtest]# clush -g gene "ls -al /root/yjtest" | sort -u
vm10: drwxr-xr-x  2 root root  32 7月  13 18:20 .
vm10: dr-xr-x---. 5 root root 277 7月  13 18:20 ..
vm10: -rw-r--r--  1 root root   0 7月  13 18:20 1.txt
vm10: -rw-r--r--  1 root root   0 7月  13 18:20 2.txt
vm10: 总用量 0
vm11: drwxr-xr-x  2 root root  32 7月  13 18:20 .
vm11: dr-xr-x---. 5 root root 277 7月  13 18:20 ..
vm11: -rw-r--r--  1 root root   0 7月  13 18:20 1.txt
vm11: -rw-r--r--  1 root root   0 7月  13 18:20 2.txt
vm11: 总用量 0
vm12: drwxr-xr-x  2 root root  32 7月  13 18:20 .
vm12: dr-xr-x---. 5 root root 277 7月  13 18:20 ..
vm12: -rw-r--r--  1 root root   0 7月  13 18:20 1.txt
vm12: -rw-r--r--  1 root root   0 7月  13 18:20 2.txt
vm12: 总用量 0
vm13: drwxr-xr-x  2 root root  32 7月  13 18:20 .
vm13: dr-xr-x---. 5 root root 277 7月  13 18:20 ..
vm13: -rw-r--r--  1 root root   0 7月  13 18:20 1.txt
vm13: -rw-r--r--  1 root root   0 7月  13 18:20 2.txt
vm13: 总用量 0


测试批量远程拷贝文件



[root@vms9 ~]# clush -g gene --rcopy /etc/shadow --dest /root/yjtest/
[root@vms9 ~]# cd /root/yjtest/
[root@vms9 yjtest]# ls
1.txt  2.txt  shadow.vm10  shadow.vm11  shadow.vm12  shadow.vm13
[root@vms9 yjtest]# ll  ## 从远程服务器取得的文件都会带有一个主机名称的后缀
总用量 16
-rw-r--r-- 1 root root   0 7月  13 18:15 1.txt
-rw-r--r-- 1 root root   0 7月  13 18:15 2.txt
---------- 1 root root 710 7月  13 22:46 shadow.vm10
---------- 1 root root 710 7月  13 22:46 shadow.vm11
---------- 1 root root 710 7月  13 22:46 shadow.vm12
---------- 1 root root 710 7月  13 22:46 shadow.vm13


测试使用其他用户批量执行


[root@vms9 yjtest]# clush -g gene "useradd slview && echo slview | passwd --stdin slview" ##批量创建测试用户
vm12: 更改用户 slview 的密码 。
vm12: passwd:所有的身份验证令牌已经成功更新。
vm13: 更改用户 slview 的密码 。
vm13: passwd:所有的身份验证令牌已经成功更新。
vm11: 更改用户 slview 的密码 。
vm11: passwd:所有的身份验证令牌已经成功更新。
vm10: 更改用户 slview 的密码 。
vm10: passwd:所有的身份验证令牌已经成功更新。

[root@vms9 yjtest]# ssh-copy-id -i /root/.ssh/id_rsa.pub slview@vm10 ##依次创建主控服务器到各节点服务器的免密登录
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
slview@vm10's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'slview@vm10'"
and check to make sure that only the key(s) you wanted were added.

[root@vms9 yjtest]# clush --user=slview -g gene  hostname ##使用账号slview来测试各节点服务器的批量命令下发
vm10: vms10
vm13: vms13
vm12: vms12
vm11: vms11


原文地址:https://www.cnblogs.com/yj411511/p/15008896.html