輕量級集群管理軟件-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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM