若是集羣數量很少的話,選擇一個輕量級的集羣管理軟件就顯得很是有必要了。ClusterShell就是這樣一種小的集羣管理工具,原理是利用ssh,能夠說是Linux系統下很是好用的運維工具 web
clustershell 簡稱clush 優勢 :shell
安裝方便。一條指令就能輕鬆安裝
配置方便。不少集羣管理軟件都須要在全部的服務器上都安裝軟件,並且還要進行不少的鏈接操做,clustershell就至關的方便了,僅僅須要全部機器可以ssh無密碼登陸便可,而後只在一臺服務器上安裝clustershell便可
使用方便 clustershell的命令相對來講很是簡單,只有一兩個指令以及三四個參數須要記vim
實驗環境 : CentOS7 centos
192.168.94.11 clustershell安全
192.168.94.22 host1bash
192.168.94.33 host2服務器
安裝clustershell 能夠用yum ,也能夠用源碼安裝運維
[root@clustershell ~]# yum -y install clustershell
作主機名映射ssh
[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