良许Linux教程网 干货合集 Linux下安装并使用ClusterShell

Linux下安装并使用ClusterShell

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

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

image-20211117202537858

如何在 Linux 上安装 ClusterShell?

ClusterShell 包在大多数发行版的官方包管理器中都可用。因此,使用发行版包管理器工具进行安装。

对于 Fedora 系统,使用DNF 命令来安装 clustershell。

$ sudo dnf install clustershell

如果系统默认是 Python 2,这会安装 Python 2 模块和工具,可以运行以下命令安装 Python 3 开发包。

$ sudo dnf install python3-clustershell

在执行 clustershell 安装之前,请确保你已在系统上启用 EPEL 存储库。

对于 RHEL/CentOS 系统,使用 YUM 命令 来安装 clustershell。

$ sudo yum install clustershell

如果系统默认是 Python 2,这会安装 Python 2 模块和工具,可以运行以下命令安装 Python 3 开发包。

$ sudo yum install python34-clustershell

对于 openSUSE Leap 系统,使用 Zypper 命令 来安装 clustershell。

$ sudo zypper install clustershell

如果系统默认是 Python 2,这会安装 Python 2 模块和工具,可以运行以下命令安装 Python 3 开发包。

$ sudo zypper install python3-clustershell

对于 Debian/Ubuntu 系统,使用 APT-GET 命令 或 APT 命令 来安装 clustershell。

$ sudo apt install clustershell

如何在 Linux 使用 PIP 安装 ClusterShell?

可以使用 PIP 安装 ClusterShell,因为它是用 Python 编写的。

在执行 clustershell 安装之前,请确保你已在系统上启用了 Python 和 PIP。

$ sudo pip install ClusterShell

如何在 Linux 上使用 ClusterShell?

与其他实用程序(如 pssh 和dsh )相比,它是直接了当的优秀工具。它有很多选项可以在远程并行执行。

在开始使用 clustershell 之前,请确保你已启用系统上的无密码登录。

以下配置文件定义了系统范围的默认值。你不需要修改这里的任何东西。

$ cat /etc/clustershell/clush.conf

如果你想要创建一个服务器组,那也可以。默认情况下有一些示例,请根据你的要求执行相同操作。

$ cat /etc/clustershell/groups.d/local.cfg

只需按以下列格式运行 clustershell 命令即可从给定节点获取信息:

$ clush -w 192.168.1.4,192.168.1.9 cat /proc/version
192.168.1.9: Linux version 4.15.0-45-generic ([email protected]) (gcc version 7.3.0 (Ubuntu 7.3.0-16ubuntu3)) #48-Ubuntu SMP Tue Jan 29 16:28:13 UTC 2019
192.168.1.4: Linux version 3.10.0-957.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) ) #1 SMP Thu Nov 8 23:39:32 UTC 2018

选项:

-w: 你要运行该命令的节点。 你可以使用正则表达式而不是使用完整主机名和 IP:

$ clush -w 192.168.1.[4,9] uname -r
192.168.1.9: 4.15.0-45-generic
192.168.1.4: 3.10.0-957.el7.x86_64

或者,如果服务器位于同一 IP 系列中,则可以使用以下格式:

$ clush -w 192.168.1.[4-9] date
192.168.1.6: Mon Mar  4 21:08:29 IST 2019
192.168.1.7: Mon Mar  4 21:08:29 IST 2019
192.168.1.8: Mon Mar  4 21:08:29 IST 2019
192.168.1.5: Mon Mar  4 09:16:30 CST 2019
192.168.1.9: Mon Mar  4 21:08:29 IST 2019
192.168.1.4: Mon Mar  4 09:16:30 CST 2019

clustershell 允许我们以批处理模式运行命令。使用以下格式来实现此目的:

$ clush -w 192.168.1.4,192.168.1.9 -b
Enter 'quit' to leave this interactive mode
Working with nodes: 192.168.1.[4,9]
clush> hostnamectl
---------------
192.168.1.4
---------------
  Static hostname: CentOS7.2daygeek.com
        Icon name: computer-vm
          Chassis: vm
       Machine ID: 002f47b82af248f5be1d67b67e03514c
          Boot ID: f9b37a073c534dec8b236885e754cb56
   Virtualization: kvm
 Operating System: CentOS Linux 7 (Core)
      CPE OS Name: cpe:/o:centos:centos:7
           Kernel: Linux 3.10.0-957.el7.x86_64
     Architecture: x86-64
---------------
192.168.1.9
---------------
  Static hostname: Ubuntu18
        Icon name: computer-vm
          Chassis: vm
       Machine ID: 27f6c2febda84dc881f28fd145077187
          Boot ID: f176f2eb45524d4f906d12e2b5716649
   Virtualization: oracle
 Operating System: Ubuntu 18.04.2 LTS
           Kernel: Linux 4.15.0-45-generic
     Architecture: x86-64
clush> free -m
---------------
192.168.1.4
---------------
             total        used        free      shared  buff/cache   available
Mem:           1838         641         217          19         978         969
Swap:          2047           0        2047
---------------
192.168.1.9
---------------
             total        used        free      shared  buff/cache   available
Mem:           1993         352        1067           1         573        1473
Swap:          1425           0        1425
clush> w
---------------
192.168.1.4
---------------
09:21:14 up  3:21,  3 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
daygeek  :0       :0               06:02   ?xdm?   1:28   0.30s /usr/libexec/gnome-session-binary --session gnome-classic
daygeek  pts/0    :0               06:03    3:17m  0.06s  0.06s bash
daygeek  pts/1    192.168.1.6      06:03   52:26   0.10s  0.10s -bash
---------------
192.168.1.9
---------------
21:13:12 up  3:12,  1 user,  load average: 0.08, 0.03, 0.00
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
daygeek  pts/0    192.168.1.6      20:42   29:41   0.05s  0.05s -bash
clush> quit

如果要在一组节点上运行该命令,请使用以下格式:

$ clush -w @dev uptime
or
$ clush -g dev uptime
or
$ clush --group=dev uptime
192.168.1.9:  21:10:10 up  3:09,  1 user,  load average: 0.09, 0.03, 0.01
192.168.1.4:  09:18:12 up  3:18,  3 users,  load average: 0.01, 0.02, 0.05

如果要在多个节点组上运行该命令,请使用以下格式:

$ clush -w @dev,@uat uptime
or
$ clush -g dev,uat uptime
or
$ clush --group=dev,uat uptime
192.168.1.7: 07:57:19 up 59 min, 1 user, load average: 0.08, 0.03, 0.00
192.168.1.9: 20:27:20 up 1:00, 1 user, load average: 0.00, 0.00, 0.00
192.168.1.5: 08:57:21 up 59 min, 1 user, load average: 0.00, 0.01, 0.05

clustershell 允许我们将文件复制到远程计算机。将本地文件或目录复制到同一个远程节点:

$ clush -w 192.168.1.[4,9] --copy /home/daygeek/passwd-up.sh

我们可以通过运行以下命令来验证它:

$ clush -w 192.168.1.[4,9] ls -lh /home/daygeek/passwd-up.sh
192.168.1.4: -rwxr-xr-x. 1 daygeek daygeek 159 Mar 4 09:00 /home/daygeek/passwd-up.sh
192.168.1.9: -rwxr-xr-x 1 daygeek daygeek 159 Mar 4 20:52 /home/daygeek/passwd-up.sh

将本地文件或目录复制到不同位置的远程节点:

$ clush -g uat --copy /home/daygeek/passwd-up.sh --dest /tmp

我们可以通过运行以下命令来验证它:

$ clush --group=uat ls -lh /tmp/passwd-up.sh
192.168.1.7: -rwxr-xr-x. 1 daygeek daygeek 159 Mar 6 07:44 /tmp/passwd-up.sh

将文件或目录从远程节点复制到本地系统:

$ clush -w 192.168.1.7 --rcopy /home/daygeek/Documents/magi.txt --dest /tmp

我们可以通过运行以下命令来验证它:

$ ls -lh /tmp/magi.txt.192.168.1.7
-rw-r--r-- 1 daygeek daygeek 35 Mar 6 20:24 /tmp/magi.txt.192.168.1.7

以上就是良许教程网为各位朋友分享的Linux系统相关内容。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你 !

730e0cf3d7ca7bcb0a46cd7b54597c63f6246b60021c
本文由 良许Linux教程网 发布,可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码。
良许

作者: 良许

良许,世界500强企业Linux开发工程师,公众号【良许Linux】的作者,全网拥有超30W粉丝。个人标签:创业者,CSDN学院讲师,副业达人,流量玩家,摄影爱好者。
上一篇
下一篇

发表评论

联系我们

联系我们

公众号:良许Linux

在线咨询: QQ交谈

邮箱: yychuyu@163.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部