zoukankan      html  css  js  c++  java
  • 轻量级集群管理软件-ClusterShell

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

    clustershell 简称clush 优点 :

    安装方便。一条指令就能轻松安装
    配置方便。很多集群管理软件都需要在所有的服务器上都安装软件,而且还要进行很多的连接操作,clustershell就相当的方便了,仅仅需要所有机器能够ssh无密码登录即可,然后只在一台服务器上安装clustershell即可
    使用方便 clustershell的命令相对来说非常简单,只有一两个指令以及三四个参数需要记

    实验环境 : CentOS7 

    192.168.94.11 clustershell

    192.168.94.22 host1

    192.168.94.33 host2

    安装clustershell 可以用yum ,也可以用源码安装

    [root@clustershell ~]# yum -y install clustershell

    做主机名映射

    [root@clustershell ~]# vim /etc/hosts
    192.168.94.11 clustershell                                                                   
    192.168.94.22 host1
    192.168.94.33 host2

    配置ssh密钥对登录 实现免密登录

    写一个简单批量分发密钥且免交互的脚本 , 首先需要下载sshpass

    [root@clustershell ~]# yum -y install sshpass
    用法:sshpass [-f | -d | -p | -e] [-hV]命令参数
    -f filename从文件中获取密码
    -d number使用number作为获取密码的文件描述符
    -p password提供密码作为参数(安全性差)
    -e密码作为env-var“SSHPASS”传递
    没有参数 - 密码将从标准输入中获取
    -h显示帮助(此屏幕)
    -V打印版本信息
    最多应使用-f,-d,-p或-e中的一个
    [root@clustershell ~]# vim sshkey.sh
    #!/bin/bash
    . /etc/rc.d/init.d/functions
    # 创建密钥
    rm ~/.ssh/id_rsa* -f
    ssh-keygen -t rsa -f ~/.ssh/id_rsa -N "" -q
    # 分发公钥
    for ip in 22 33  
    do
    sshpass -f ~/.sshpass ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.94.$ip -o StrictHostKeyCh
    if [ $? -eq 0  ];then
        action  "copy-id 192.168.94.$ip"  /bin/true
    else
        action  "copy-id 192.168.94.$ip"  /bin/false
    fi
    echo ""
    done

    给脚本添加执行权限 脚本里使用的是密码文件 位置和名字自定义 , 因为是密码文件 ,权限600

    [root@clustershell ~]# chmod +x sshkey.sh
    [root@clustershell ~]# chmod 600 ~/.sshpass 
    [root@clustershell ~]# ll ~/.sshpass
    -rw-------. 1 root root 11 9月   8 16:31 /root/.sshpass

    执行脚本测试并且做主机名映射 

    开始使用clustershell进行批量管理

    配置clush:
    在/etc/clustershell目录下,手动创建groups文件

    [root@clustershell ~]# vim /etc/clustershell/groups
    all: host[1,2]
    web: host1
    db: host2   
    
    # groups文件中的all组对应是必须要配置的,clush 有 -a 这个参数,主机间用空格分离

    clush管理命令常用参数:
    -g 后面指定设置的组
    -a 表示所有的组
    -w 后面跟主机节点,多个主机中间用逗号隔开
    -x 表示去掉某个节点进行操作。后面跟主机节点,多个主机中间用逗号隔开
    -X 表示去掉某个组进行操作,多个组之间用逗号隔开
    -b 相同输出结果合并

    [root@clustershell ~]# clush -g db date
    host2: Host key verification failed.
    clush: host2: exited with exit code 255
    
    # 原因是需要交互 , 这里使用的是主机名来连接的 , 所以上面的脚本需要做修改 

     把之前的IP改为主机名即可

     执行脚本

    [root@clustershell ~]# clush -g db date
    host2: 2018年 09月 08日 星期六 20:04:26 CST
    [root@clustershell ~]# clush -g web date
    host1: 2018年 09月 08日 星期六 20:04:55 CST
    [root@clustershell ~]# clush -a date
    host1: 2018年 09月 08日 星期六 20:05:00 CST
    host2: 2018年 09月 08日 星期六 20:05:00 CST
    # 支持管道和重定向等操作
    [root@clustershell ~]# clush -a 'echo I Love Wife > /tmp/1314'
    [root@clustershell ~]# clush -a  cat /tmp/1314
    host2: I Love Wife
    host1: I Love Wife
    [root@clustershell ~]# clush -w host1 ifconfig ens33|awk -F '[ :]+' 'NR==2{print $3}'
    192.168.94.22
    # 加上-b选项可以是相同结果合并输出
    [root@clustershell ~]# clush -ab date
    ---------------
    host[1-2] (2)
    ---------------
    2018年 09月 08日 星期六 20:13:35 CST
    [root@clustershell ~]# clush -ab hostname
    ---------------
    host1
    ---------------
    host1
    ---------------
    host2
    ---------------
    host2
    [root@clustershell ~]# clush -ab cat /etc/centos-release
    ---------------
    host[1-2] (2)
    ---------------
    CentOS Linux release 7.5.1804 (Core)

    clush也可以进行文件和目录的分发:

    --copy 表示从本地拷贝文件或目录到远程集群节点上,等于-c
    --rcopy 表示从远程集群节点上拷贝文件或目录到本机上
    --dest 前面表示本地要复制的文件或目录路径,后面表示远程的存放路径

    [root@clustershell ~]# cat mingming 
    I Love Wife
    [root@clustershell ~]# clush -g web -c /root/mingming --dest /root
    [root@clustershell ~]# clush -g web cat /root/mingming 
    host1: I Love Wife
    [root@clustershell ~]# clush -w host2 -c /root/mingming --dest=/root/
    [root@clustershell ~]# clush -w host2 cat /root/mingming 
    host2: I Love Wife
    # 拷贝本地目录到远程节点上
    [root@clustershell ~]# clush -g web ls -l /root/
    host1: 总用量 14708
    host1: -rw-------.  1 root root     1257 5月  24 03:40 anaconda-ks.cfg
    host1: -rwxr-xr-x.  1 root root     2236 6月   4 22:55 DNS.sh
    host1: -rw-r--r--.  1 root root 15039314 8月  14 07:43 master.zip
    host1: -rw-r--r--   1 root root       12 9月   8 20:33 mingming
    host1: drwxr-xr-x   2 root root        6 9月   8 20:44 mywife
    host1: drwxr-xr-x. 10 root root     4096 8月  14 07:54 vim
    host1: drwxr-xr-x.  9 root root     4096 8月  13 17:07 vim-master
    # 拷贝远程节点的目录或文件到本地
    [root@clustershell ~]# cd mywife/
    [root@clustershell mywife]# pwd
    /root/mywife
    [root@clustershell mywife]# ls
    [root@clustershell mywife]# clush -a --rcopy /root/mingming --dest ./
    [root@clustershell mywife]# ls
    mingming.host1  mingming.host2
    # 远程拷贝文件到本地后, 后缀会加上节点的主机名

    --user=username,这个表示使用clush命令操作时,登陆ssh时使用的用户

    比如我用本机的root帐号管理host1节点的damowang用户

    [root@clustershell mywife]# sshpass -f ~/.sshpass ssh-copy-id -i ~/.ssh/id_rsa.pub damowang@host1
    [root@clustershell mywife]# clush --user=damowang -w host1 hostname
    host1: host1
    # --user参数要紧跟clush后面 
    [root@clustershell mywife]# clush --user=damowang -w host1 whoami
    host1: damowang

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

    进行文件传输时 ,需要加 -o -P2222(大写P)
    进行批量执行操作命令时,需要加 -o -p2222(小写p)

    [root@clustershell mywife]# clush -w host1 -o -P2222 -c ./mingming.host1 --dest /root/
    [root@clustershell mywife]# clush -w host1 -o -p2222  ls /root/
    host1: anaconda-ks.cfg
    host1: DNS.sh
    host1: master.zip
    host1: mingming
    host1: mingming.host1
    host1: mywife
    host1: vim
    host1: vim-master
  • 相关阅读:
    【基础算法】最长连续不重复子序列
    【python----发轫之始】【简单生活小助手】
    【python----发轫之始】【基础知识总结】
    【转】C#接口知识
    SaltStack漏洞导致的挖矿排查思路
    CVE-2020-2551复现过程
    远程调试docker构建的weblogic
    GOF23-工厂模式
    GOF23-单列模式
    VU TPS QPS RT 计算公式
  • 原文地址:https://www.cnblogs.com/bigdevilking/p/9610433.html
Copyright © 2011-2022 走看看