集群管理命令总结

前言

在进行TPC性能测试时用到两个集群管理命令:clushpssh,这俩命令都可以在多台服务器上并发执行相同命令,其中个人比较推荐 clush,因为感觉 clush 比较好用,本文来总结一下 clushpssh 的安装配置和使用。

Python

clushpssh 都是基于Python实现,由于不同的系统默认的Python版本不同,由于 Python3 和 Python2 可能由于支持度不同可能会遇到不一样的问题。 目前使用过程中两个命令对于默认安装 Python2 的系统几乎没有遇到任何问题,安装和使用都比较顺利。而对于默认安装Python3的系统安装和使用过程中可能会分别遇到不同的问题,所以本文对于Python2和Python3都进行了总结。 目前用到的系统中,Centos 7 默认安装 Python2,Centos8 和 openeuler22.03 默认安装 Python3 ,如果某些软件或者命令不支持Python3 可以选择离线编译安装 Python2,然后在基于 Python2 进行安装即可。

clush

Clush是一个非常实用的集群管理命令。通过这个命令,用户可以批量管理多台服务器,使它们执行相同的命令,例如安装软件或监测运行状况等,从而达到管理多个节点的目的。

官网

https://clustershell.readthedocs.io/

https://github.com/cea-hpc/clustershell

安装

当前最新版本 ClusterShell 依赖 PyYAML ,PyYAML 依赖 pathlib,对应版本如下:

  • python 2.7.x (2.7.5)
  • ClusterShell 1.9.2
  • PyYAML 6.0.1
  • pathlib 1.0.1
在线安装

能连外网

bash 复制代码
pip install pathlib
pip install ClusterShell
离线安装

不能连外网

先下载对应的安装包,上传到服务器

在通过下面的里面离线安装

bash 复制代码
pip install pathlib-1.0.1.tar.gz
pip install PyYAML-6.0.1.tar.gz
pip install ClusterShell-1.9.2.tar.gz

配置

配置文件路径:/usr/etc/clustershell/ (1.7版本以后) (安装时自动创建)

旧版本路径:/etc/clustershell/ (1.7版本以前,包含1.7版本)(安装时自动创建)

bash 复制代码
tree /usr/etc/clustershell/

/usr/etc/clustershell/
├── clush.conf
├── clush.conf.d
│   ├── README
│   ├── sshpass.conf.example
│   └── sudo.conf.example
├── groups.conf
├── groups.conf.d
│   ├── genders.conf.example
│   ├── README
│   ├── slurm.conf.example
│   └── xcat.conf.example
├── groups.d
│   ├── cluster.yaml.example
│   ├── local.cfg
│   └── README
└── topology.conf.example

3 directories, 13 files

默认的组配置文件:/usr/etc/clustershell/groups.d/local.cfg

java 复制代码
adm: example0
oss: example4 example5
mds: example6
io: example[4-6]
compute: example[32-159]
gpu: example[156-159]
all: example[4-6,32-159]

也可以新建 /usr/etc/clustershell/groups 配置文件,/usr/etc/clustershell/groups的优先级要比 /usr/etc/clustershell/groups.d/local.cfg 的优先级高

bash 复制代码
echo "all: 192.168.1.[1-212]" > /usr/etc/clustershell/groups

修改 ssh 端口号
clush.conf

bash 复制代码
ssh_options: -p 6233

其他配置:

1、.ssh/config

bash 复制代码
StrictHostKeyChecking no

可参考:Linux 批量添加 known_hosts

2、/etc/ssh/sshd_config 修改 MaxStartups 1000

bash 复制代码
#MaxStartups 10:30:100
MaxStartups 1000

service sshd restart

修改这个原因是因为默认值为10,当并发执行scp的数量超过10时,会存在部分失败,比如我的集群机器数量为22,超过了10,就会存在这个问题。

参考:https://www.coder.work/article/1884598

3、免密登录

简单使用

clush
bash 复制代码
clush -a pwd
# 汇总和排序
clush -a pwd | clubak -c

clush -a 进入交互式模式

bash 复制代码
clush -a
Enter 'quit' to leave this interactive mode
Working with nodes: 192.168.1.[1-22]
clush>
bash 复制代码
pwd
echo 1 > /proc/sys/vm/drop_caches
echo 2 > /proc/sys/vm/drop_caches
echo 3 > /proc/sys/vm/drop_caches

交互式模式不支持 clubak

clubak

官方文档: https://clustershell.readthedocs.io/en/latest/tools/clubak.html

-b 和 -c 参数效果一样

java 复制代码
cat file
node17: MD5 (cstest.py) = 62e23bcf2e11143d4875c9826ef6183f
node14: MD5 (cstest.py) = 62e23bcf2e11143d4875c9826ef6183f
node16: MD5 (cstest.py) = e88f238673933b08d2b36904e3a207df
node15: MD5 (cstest.py) = 62e23bcf2e11143d4875c9826ef6183f
java 复制代码
clubak -b < file
---------------
node[14-15,17] (3)
---------------
 MD5 (cstest.py) = 62e23bcf2e11143d4875c9826ef6183f
---------------
node16
---------------
 MD5 (cstest.py) = e88f238673933b08d2b36904e3a207df


clubak -c < file
---------------
node[14-15,17] (3)
---------------
 MD5 (cstest.py) = 62e23bcf2e11143d4875c9826ef6183f
---------------
node16
---------------
 MD5 (cstest.py) = e88f238673933b08d2b36904e3a207df


clubak -bL < file
node[14-15,17]:  MD5 (cstest.py) = 62e23bcf2e11143d4875c9826ef6183f
node16:  MD5 (cstest.py) = e88f238673933b08d2b36904e3a207df
scp

从 1 节点传到其他节点

bash 复制代码
scp -P 6233 192.168.1.1:~/spark-defaults.conf /opt/spark2/conf/
其他

其他参数或者功能可以自己尝试学习使用。

参数

clush
bash 复制代码
clush -h
Usage: clush [options] command

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -s GROUPSOURCE, --groupsource=GROUPSOURCE
                        optional groups.conf(5) group source to use
  -n, --nostdin         don't watch for possible input from stdin
  --groupsconf=FILE     use alternate config file for groups.conf(5)
  --conf=FILE           use alternate config file for clush.conf(5)
  -O KEY=VALUE, --option=KEY=VALUE
                        override any key=value clush.conf(5) options

  Selecting target nodes:
    -w NODES            nodes where to run the command
    -x NODES            exclude nodes from the node list
    -a, --all           run command on all nodes
    -g GROUP, --group=GROUP
                        run command on a group of nodes
    -X GROUP            exclude nodes from this group
    --hostfile=FILE, --machinefile=FILE
                        path to file containing a list of target hosts
    --topology=FILE     topology configuration file to use for tree mode
    --pick=N            pick N node(s) at random in nodeset

  Output behaviour:
    -q, --quiet         be quiet, print essential output only
    -v, --verbose       be verbose, print informative messages
    -d, --debug         output more messages for debugging purpose
    -G, --groupbase     do not display group source prefix
    -L                  disable header block and order output by nodes
    -N                  disable labeling of command line
    -P, --progress      show progress during command execution
    -b, --dshbak        gather nodes with same output
    -B                  like -b but including standard error
    -r, --regroup       fold nodeset using node groups
    -S, --maxrc         return the largest of command return codes
    --color=WHENCOLOR   whether to use ANSI colors (never, always or auto)
    --diff              show diff between gathered outputs
    --outdir=OUTDIR     output directory for stdout files (OPTIONAL)
    --errdir=ERRDIR     output directory for stderr files (OPTIONAL)

  File copying:
    -c, --copy          copy local file or directory to remote nodes
    --rcopy             copy file or directory from remote nodes
    --dest=DEST_PATH    destination file or directory on the nodes
    -p                  preserve modification times and modes

  Connection options:
    -f FANOUT, --fanout=FANOUT
                        use a specified fanout
    -l USER, --user=USER
                        execute remote command as user
    -o OPTIONS, --options=OPTIONS
                        can be used to give ssh options
    -t CONNECT_TIMEOUT, --connect_timeout=CONNECT_TIMEOUT
                        limit time to connect to a node
    -u COMMAND_TIMEOUT, --command_timeout=COMMAND_TIMEOUT
                        limit time for command to run on the node
    -m MODE, --mode=MODE
                        run mode; define MODEs in <confdir>/*.conf
    -R WORKER, --worker=WORKER
                        worker name to use for command execution ('exec',
                        'rsh', 'ssh', etc. default is 'ssh')
    --remote=REMOTE     whether to enable remote execution: in tree mode,
                        'yes' forces connections to the leaf nodes for
                        execution, 'no' establishes connections up to the leaf
                        parent nodes for execution (default is 'yes')
clubak
bash 复制代码
clubak -h
Usage: clubak [options]

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -s GROUPSOURCE, --groupsource=GROUPSOURCE
                        optional groups.conf(5) group source to use
  --groupsconf=FILE     use alternate config file for groups.conf(5)

  Output behaviour:
    -q, --quiet         be quiet, print essential output only
    -v, --verbose       be verbose, print informative messages
    -d, --debug         output more messages for debugging purpose
    -G, --groupbase     do not display group source prefix
    -L                  disable header block and order output by nodes
    -N                  disable labeling of command line
    -b, -c, --dshbak    gather nodes with same output
    -B                  like -b but including standard error
    -r, --regroup       fold nodeset using node groups
    -S SEPARATOR, --separator=SEPARATOR
                        node / line content separator string (default: ':')
    -F, --fast          faster but memory hungry mode
    -T, --tree          message tree trace mode
    --interpret-keys=INTERPRET_KEYS
                        whether to interpret keys (never, always or auto)
    --color=WHENCOLOR   whether to use ANSI colors (never, always or auto)
    --diff              show diff between gathered outputs
    --outdir=OUTDIR     output directory for stdout files (OPTIONAL)
    --errdir=ERRDIR     output directory for stderr files (OPTIONAL)

python3

python3 安装完 clush 之后,直接使用会报错

bash 复制代码
clush -a
clush: External error: Group source error (GroupResolverSourceError: <default>)

最开始不止如何解决,所以对于默认安装python3的服务器依旧选择先手动安装python2再用python2安装clush比较麻烦,后来发现异常原因是 python3 安装 clush 时自动创建的配置文件路径不对,修改一下路径即可解决。

默认安装路径:/usr/local/etc/clustershell 正确路径:/etc/clustershell 或 /usr/etc/clustershell

bash 复制代码
mv /usr/local/etc/clustershell /etc/

旧版

最开始是基于旧版(1.7.3)安装包离线安装的,这里记录一下。同时对于某些环境可能新版装着有问题也可以使用这种方式。比如对于默认安装Python3的环境,假如Python3安装不上,可以选择手动离线安装Python2,如果离线安装的没有pip或者pip使用起来有问题,可以通过 python setup.py install 的方式安装,有可能安装好的命令不会默认装到/usr/bin 或者 /usr/local/bin。我们可以通过添加软连接的形式 ,比如 python2的安装目录为 /usr/local/python27,那么clush会安装到 /usr/local/python27/bin/clush, 通过软连接 ln -s /usr/local/python27/bin/clush /usr/bin/clush,这样就可以使用了。

1.7.3 版本的安装包:https://pan.baidu.com/s/1fBD5jzlcFTwle85giNrCMw

安装方法:

bash 复制代码
## 该安装包只能使用Python2安装,Python3装完之后使用起来有问题
python2.7 setup.py install

pssh

pssh命令是一个用于在多台主机上并行执行ssh命令的工具,全称为parallel-ssh。它使用Python编写,能够方便地对多台Linux主机进行批量管理。PSSH提供了OpenSSH和相关工具的并行版本。包括pssh、pscp、prsync、pnuke和pslurp。该项目包括可以在自定义应用程序中使用的psshlib,源代码使用Python编写的。

官网

https://code.google.com/archive/p/parallel-ssh/

安装

基于 Python2 进行安装

在线安装
bash 复制代码
pip install pssh
离线安装

https://files.pythonhosted.org/packages/60/9a/8035af3a7d3d1617ae2c7c174efa4f154e5bf9c24b36b623413b38be8e4a/pssh-2.3.1.tar.gz

bash 复制代码
 pip install pssh-2.3.1.tar.gz

配置

clush 一样也需要配置:

1、.ssh/config

bash 复制代码
StrictHostKeyChecking no

可参考:Linux 批量添加 known_hosts

2、/etc/ssh/sshd_config 修改 MaxStartups 1000

bash 复制代码
#MaxStartups 10:30:100
MaxStartups 1000

service sshd restart

修改这个原因是因为默认值为10,当并发执行scp的数量超过10时,会存在部分失败,比如我的集群机器数量为22,超过了10,就会存在这个问题。

参考:https://www.coder.work/article/1884598

3、免密登录

使用示例

bash 复制代码
pssh -h /root/dkl/host_list.txt -t0 "scp -P 6233 192.168.1.1:/root/dkl/test.sh /tmp/"

host_list.txt

java 复制代码
root@192.168.1.1:6233
root@192.168.1.2:6233
root@192.168.1.3:6233
root@192.168.1.4:6233
root@192.168.1.5:6233
root@192.168.1.6:6233
root@192.168.1.7:6233
root@192.168.1.8:6233
root@192.168.1.9:6233
root@192.168.1.10:6233
root@192.168.1.11:6233
root@192.168.1.12:6233
root@192.168.1.13:6233
root@192.168.1.14:6233
root@192.168.1.15:6233
root@192.168.1.16:6233
root@192.168.1.17:6233
root@192.168.1.18:6233
root@192.168.1.19:6233
root@192.168.1.20:6233
root@192.168.1.21:6233
root@192.168.1.22:6233

pssh 只能后台运行,不会有返回值,也没有交互式模式,所以个人感觉不如clush好用。

参数

bash 复制代码
 pssh --help
Usage: pssh [OPTIONS] command [...]

Options:
  --version             show program's version number and exit
  --help                show this help message and exit
  -h HOST_FILE, --hosts=HOST_FILE
                        hosts file (each line "[user@]host[:port]")
  -H HOST_STRING, --host=HOST_STRING
                        additional host entries ("[user@]host[:port]")
  -l USER, --user=USER  username (OPTIONAL)
  -p PAR, --par=PAR     max number of parallel threads (OPTIONAL)
  -o OUTDIR, --outdir=OUTDIR
                        output directory for stdout files (OPTIONAL)
  -e ERRDIR, --errdir=ERRDIR
                        output directory for stderr files (OPTIONAL)
  -t TIMEOUT, --timeout=TIMEOUT
                        timeout (secs) (0 = no timeout) per host (OPTIONAL)
  -O OPTION, --option=OPTION
                        SSH option (OPTIONAL)
  -v, --verbose         turn on warning and diagnostic messages (OPTIONAL)
  -A, --askpass         Ask for a password (OPTIONAL)
  -x ARGS, --extra-args=ARGS
                        Extra command-line arguments, with processing for
                        spaces, quotes, and backslashes
  -X ARG, --extra-arg=ARG
                        Extra command-line argument
  -i, --inline          inline aggregated output and error for each server
  --inline-stdout       inline standard output for each server
  -I, --send-input      read from standard input and send as input to ssh
  -P, --print           print output as we get it

Example: pssh -h hosts.txt -l irb2 -o /tmp/foo uptime

Python3

Python3 安装 pssh 后有异常,不能使用,可能是因为不支持 Python3。记录一下异常解决过程

No module named 'version'
bash 复制代码
pssh
Traceback (most recent call last):
  File "/usr/local/bin/pssh", line 26, in <module>
    from psshlib.cli import common_parser, common_defaults
  File "/usr/local/lib/python3.9/site-packages/psshlib/cli.py", line 9, in <module>
    import version
ModuleNotFoundError: No module named 'version'

https://stackoverflow.com/questions/32423793/importerror-no-module-named-version

尝试安装 version

在线安装失败:

接着尝试离线安装:

下载:https://pypi.org/project/version

最新版:0.1.1 次新版 0.1.0, 0.1.1 安装有异常,所以尝试0.1.0,0.1.0 直接安装成功

0.1.1 安装异常:

bash 复制代码
ImportError: cannot import name 'izip_longest' from 'itertools' (unknown location)

0.1.0 虽然安装成功,但是使用pssh时依旧报异常:

bash 复制代码
AttributeError: module 'version' has no attribute 'VERSION'

尝试解决安装 0.1.1 遇到的异常,解决方法:解压安装包,修改 version.py ,将其中的 izip_longest改为zip_longest


安装成功 version 0.1.1 之后依旧报异常:

bash 复制代码
AttributeError: module 'version' has no attribute 'VERSION'

所以最终改为先手动离线安装Python2,然后基于Python2 安装 pssh

安装Python2

下载

https://www.python.org/downloads/release/python-275/
https://www.python.org/ftp/python/2.7.5/Python-2.7.5.tgz

安装gcc

bash 复制代码
yum install gcc -y
#yum install g++ -y

缺少 gcc 会报一下异常:

bash 复制代码
configure: error: no acceptable C compiler found in $PATH

编译安装

bash 复制代码
tar -zxvf Python-2.7.5.tgz
cd Python-2.7.5/
./configure --prefix=/usr/local/python27
make
make install

添加软连接

bash 复制代码
ln -s /usr/local/python27/bin/python /usr/bin/python2

这样就可以使用 python2 了

bash 复制代码
python2
Python 2.7.5 (default, Apr 11 2024, 17:05:23)
[GCC 10.3.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

这时是没有pip的,本文先不介绍如何安装pip,我们可以直接使用源码的方式安装pssh

bash 复制代码
tar -zxvf pssh-2.3.1.tar.gz
cd pssh-2.3.1/
python2 setup.py install
ln -s /usr/local/python27/bin/pssh /usr/bin/pssh

这样就可以使用pssh了。

假如我们开始使用Python3 pip安装了 pssh,但是忘了卸载pssh,这个时候我们离线安装Python2并且装了pssh时,我们会发现 /usr/bin/pssh文件已经存在,且使用pssh时报和Python3一样的错误,原因是因为我们使用的就是Python3的pssh。

解决方式用两个:

  1. 将 /usr/bin/pssh文件文件中的 #!/usr/bin/python3 改成 #!/usr/bin/python2
  2. 删除 /usr/bin/pssh ,ln -s /usr/local/python27/bin/pssh /usr/bin/pssh

注:有的环境的命令默认去 /usr/bin 路径查找,有的环境的命令默认去 /usr/local/bin 查找,可根据实际情况调整。

总结

本文总结了两个集群管理命令:clushpssh 的安装和使用方式,给出部分使用示例,并记录了 Python2 和 Python3 分别遇到的问题以及解决过程。

相关推荐
虾..18 小时前
Linux 软硬链接和动静态库
linux·运维·服务器
Evan芙18 小时前
Linux常见的日志服务管理的常见日志服务
linux·运维·服务器
hkhkhkhkh12320 小时前
Linux设备节点基础知识
linux·服务器·驱动开发
HZero.chen21 小时前
Linux字符串处理
linux·string
张童瑶21 小时前
Linux SSH隧道代理转发及多层转发
linux·运维·ssh
汪汪队立大功12321 小时前
什么是SELinux
linux
石小千21 小时前
Linux安装OpenProject
linux·运维
柏木乃一1 天前
进程(2)进程概念与基本操作
linux·服务器·开发语言·性能优化·shell·进程
Lime-30901 天前
制作Ubuntu 24.04-GPU服务器测试系统盘
linux·运维·ubuntu
百年渔翁_肯肯1 天前
Linux 与 Unix 的核心区别(清晰对比版)
linux·运维·unix