searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

Clustershell批量工具的简单使用

2024-05-29 09:08:33
6
0

简介
ClusterShell是一个事件驱动的开源Python库,旨在在服务器农场或大型Linux
ClusterShell是一个事件驱动的开源Python库,旨在在服务器农场或大型Linux集群上并行运行本地或远程命令。它会处理在HPC集群上遇到的常见问题,例如对节点组进行操作、使用优化执行算法运行分布式命令,以及收集结果并合并相同的输出,或检索返回码。ClusterShell利用已安装在您系统上的现有远程shell工具,如SSH。ClusterShell的主要目标是通过为开发人员提供轻量但可扩展的Python API来改善高性能集群的管理。它还提供了clush、clubak和cluset/nodeset等方便的命令行工具,使传统的shell脚本能够受益于一些库的特性。

当前环境Centos7,我们只需要在一台主机上操作即可完成对集群机子的所有配置

192.168.91.5 hadoop01  # 主控机
192.168.91.6 hadoop02  # 受控机
192.168.91.7 hadoop03  # 受控机

免密码登录

主机使用ssh登录1,2,3节点必须是​免密码登录方式​,并且不能是首次登录,因为Clustershell不会自动输入交互命令,例如第一次登录主机会有下面提示。

ssh root@192.168.91.5

The authenticity of host 'hadoop01 (192.168.91.5)' can't be established.
ECDSA key fingerprint is d4:5d:d6:e6:bc:70:86:1b:42:32:aa:6b:86:a6:34:d4.
Are you sure you want to continue connecting (yes/no)?

生成密钥配置免密

ssh-keygen -t rsa
回车
回车

# 配置免密
ssh-copy-id 192.168.91.6
ssh-copy-id 192.168.91.7
ssh-copy-id 192.168.91.5
# 或者
scp ~/.ssh/id_rsa.pub 192.168.91.6:/root/.ssh/authorized_keys
scp ~/.ssh/id_rsa.pub 192.168.91.5:/root/.ssh/authorized_keys
scp ~/.ssh/id_rsa.pub 192.168.91.7:/root/.ssh/authorized_keys

配置ClusterShell

添加组

vim /etc/clustershell/groups
# 配置组
ah: 192.168.0.[20-22]

可自行定义分组如:ah

通过这个分组可连接其中3个节点,如果此三个节点对应的IP地扯没有映射有连接失败,可设置/etc/hosts对应关系

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.91.5 hadoop01
192.168.91.6 hadoop02
192.168.91.7 hadoop03

clush命令几个重要的参数

# 命令行介绍
ClusterShell是通过一条命令行clush来完成操作的。我们只需要记住以下几个参数就可以了:

-g 后面指定设置的组
-a 表示所有的组
-w 后面跟主机节点,多个主机中间用逗号隔开
-x 表示去掉某个节点进行操作。后面跟主机节点,多个主机中间用逗号隔开
-X 表示去掉某个组进行操作,多个组之间用逗号隔开
-b 相同输出结果合并
-c 群发文件
-rcopy 从远程节点复制文件或目录
-dest = DEST_PATH节点上的目标文件或目录 
-l  以用户身份执行远程命令
-h 帮助

注意:# 需管理节点对其他节点ssh免密才可以使用clustershell

如批量查询时间

[root@hadoop01 ~]# clush -b -g ah date
---------------
192.168.91.[5-7] (3)
---------------
2024年 05月 29日 星期三 10:25:08 CST

批量创建文件

[root@hadoop01 ~]# clush -g ah "touch /tmp/tmp.txt"
[root@hadoop01 ~]# clush -b -g ah "ls /tmp/tmp.txt"
---------------
192.168.91.[5-7] (3)
---------------
/tmp/tmp.txt

批量群发文件

[root@hadoop01 ~]# touch hadoop01.txt
[root@hadoop01 ~]# clush -g ah -c hadoop01.txt --dest /tmp
[root@hadoop01 ~]# clush -g ah ls -lrt /tmp/hadoop01.txt
192.168.91.5: -rw-r--r-- 1 root root 0 5月  29 10:21 /tmp/hadoop01.txt
192.168.91.7: -rw-r--r-- 1 root root 0 5月  29 10:21 /tmp/hadoop01.txt
192.168.91.6: -rw-r--r-- 1 root root 0 5月  29 10:21 /tmp/hadoop01.txt

将目标文件cp到管理机

[root@hadoop01 ~]# clush -g ah --rcopy /tmp/hadoop01.txt --dest /root/
[root@hadoop01 ~]# ls -lrt /root/hadoop01.txt*
-rw-r--r-- 1 root root 0 5月  29 10:23 /root/hadoop01.txt.192.168.91.5
-rw-r--r-- 1 root root 0 5月  29 10:23 /root/hadoop01.txt.192.168.91.7
-rw-r--r-- 1 root root 0 5月  29 10:23 /root/hadoop01.txt.192.168.91.6

0条评论
0 / 1000