1. 程式人生 > >批量部署 自動化之 - [pssh](轉)

批量部署 自動化之 - [pssh](轉)

時間 provide 分發 登錄 並發 check ast 可選 git clone

並行執行命令工具簡介

作為運維工程師來講,機器數量到一定級別的時候,批量運維和管理機器就是一件費神
的事情,還好有很多可以批量並行執行命令的工具,比如 pssh , python fabric
taobao 有在pssh基礎之上改造的pgm. 這幾個工具都可以幫助我們批量運行命令。當然
隨著 puppet, ansible等工具的流行這些並行工具變的弱化了,不過依然還是很有用,今天我們來講述一下 pssh 的使用方式

python並行執行命令工具

之前在阿裏工作的時候,並行工具是pgm , 目前可以選擇的工具如下

fabric pssh pgm 

pssh 官方介紹

PSSH provides parallel versions of OpenSSH and related tools.

Included are pssh, pscp, prsync, pnuke, and pslurp. The project includes psshlib which can be used within custom applications.
PSSH is supported on Python 2.4 and greater (including Python 3.1 and greater). It was originally written and maintained by Brent N. Chun. Due to his busy schedule, Brent handed over maintenance to Andrew McNabb in October 2009.

pssh 安裝部署

下載 parallel-ssh 並安裝

git clone https://github.com/ruiaylin/pssh.git

安裝 install

cd pssh/
python setup.py install 

配置待批量管理的服務器列表

host configuration like this : pssh_config

192.168.102.81:10000
192.168.8.183:10000

pssh 也可以配置成為不同的 group ,可以根據不同的組做對應的操作,比如說不同的集群,不同的角色都是可以的。後面有簡單的測試。

hostgroups configuration like this :

[email protected]virtual-machine:~/batch# cat /etc/pssh/hostgroups 
master: 192.168.19.132,192.168.19.135
slave: 192.168.19.134 

為了管理方便,需要打通管理機器(有些公司叫做跳板機,也有叫做堡壘機)到各個主
機的信任通道,這樣會避免每次ssh操作都需要輸入密碼,機器多的時候會真的瘋掉的

打通信任通道 :

cd 
mkdir .ssh 
ssh-keygen -t dsa
cd .ssh ; ll
[[email protected] .ssh]# ll
total 32
-rw-------  1 root root  1588 Nov 19 14:29 authorized_keys 
-rw-------  1 root root   668 Sep 11 16:15 id_dsa
-rw-r--r--  1 root root   602 Sep 11 16:15 id_dsa.pub
-rw-r--r--. 1 root root 14490 Nov 13 14:58 known_hosts
#然後將 id_dsa.pub 文件內容 copy 到 各個主機的 
/home/youruser/.ssh/authorized_keys 文件中 , 打通完畢, 如果
操作完成,之後仍然無法直接ssh 登錄,問題可能出在 authorized_keys 該文件的屬性上面。 一般設置為700

examples :

pssh to execute command

pssh options

OPTIONS
   -h host_file   # -h + 包含IP的文件名
      --hosts host_file
   -H     [[email protected]]host[:port]  # -H + <span style="font-family: Arial, Helvetica, sans-serif;">[用戶@]主機IP[:端口]   [  ]內的是可選參數 ,若有多個主機,用" "引起來,主機之間用空格分開</span>
       --host [[email protected]]host[:port]
   -H     "[[email protected]]host[:port] [ [[email protected]]host[:port ] ... ]"  
   <span style="white-space:pre">   </span>   --host "[[email protected]]host[:port] [ [[email protected]]host[:port ] ... ]"
   -l user   # -l + 用戶名(用於連接遠程主機的用戶名)
       --user user
   -p parallelism   # -p + 並發數
       --par parallelism
   -t timeout   # -t + 超時秒數
       --timeout timeout
   -o outdir   # -o + 輸出目錄  說明:會在該目錄下創建  <span style="font-family: Arial, Helvetica, sans-serif;">[用戶@]主機IP[:端口]</span><span style="font-family: Arial, Helvetica, sans-serif;">  格式的文件名,用於保存輸出結果</span>
       --outdir outdir
   -e errdir   # -e + 錯誤輸出目錄  
      --errdir errdir
   -x args  # -x + ssh連接時可提供的參數 ,例: -x "-o 
   StrictHostKeyChecking=no" 表示跳過ssh鏈接時詢問yes/no 
       --extra-args args  
   -X arg
       --extra-arg arg 
   -O options   # -O + SSH配置文件中的選項  可以出現多個 -O 選項
       --options options
   -A
       --askpass
   -i    # -i 參數用於將輸出結果直接顯示在當前終端
       --inline
       --inline-stdout
   -v  # -v 參數用於顯示ssh連接時的錯誤信息
      --verbose
   -I
       --send-input
          Read input and send to each ssh process.  Since ssh allows a command script to be sent on standard input, the -I option may be used in lieu of the command argument.
   -P  # -P 參數用於當主機連接上之後,輸出執行結果,先輸出執行結果,
            再顯示連接 的主機信息.
     --print

執行命令 , 並check

#創建幾個目錄
pssh -h pssh_config -l root -i mkdir -p  /root/works/{script,tmp,log} 
#check 剛才創建的結果
[[email protected] works]#  pssh -h pssh_config -l root -i ls  /root/works/   
    [1] 14:12:24 [SUCCESS] 192.168.102.81:10000
    log
    script
    tmp
    [2] 14:12:24 [SUCCESS] 192.168.8.183:10000
    log
    script
    tmp

多條命令要用分好分割

pssh -h pssh_config -l root -i cd  /root/works/  ; ls 
#執行結果
[[email protected] works]# pssh -h pssh_config -l root -i cd  /root/works/  ; ls 
[1] 14:13:33 [SUCCESS] 192.168.8.183:10000
log
script
tmp
[2] 14:13:33 [SUCCESS] 192.168.102.81:10000
log
script
tmp 
# 關閉selinux
pssh -h servers.txt -l root -P "sed -i ‘/SELINUX=enforcing/s/SELINUX=enforcing/SELINUX=disabled/‘/etc/sysconfig/selinux"

pscp 集中分到文件到 主機列表的機器

將文件 collect-mysql.py 分發到機器列表對應目錄
pscp -h pssh_config  collect-mysql.py   /root/works/tmp/
#check 執行結果
pssh -h pssh_config -l root -i cd  /root/works/tmp ; ls 
[1] 14:18:09 [SUCCESS] 192.168.102.81:10000
collect-mysql.py
[2] 14:18:09 [SUCCESS] 192.168.8.183:10000
collect-mysql.py

如果是包含文件夾 ,請使用 如下命令
 pscp -h pssh_config  -l root  -r  /root/bin/*   /root/bin/  

slurp copy文件到管理機器

pslurp -L /root/works/testlurp/ -h ../pssh_config  /root/works/tmp/collect-mysql.py  mysql.py
#
[1] 14:53:55 [SUCCESS] 192.168.102.81:10000
[2] 14:53:55 [SUCCESS] 192.168.8.183:10000
# check the result 
    [[email protected] testlurp]# cd /root/works/testlurp/ ; ls * 
    192.168.102.81:
    mysql.py        
    192.168.8.183:
    mysql.py 

pnuke

The pnuke command is useful when you want to kill a bunch of processes on a set of machines. For example, suppose you’ve got a bunch of java processes running on three nodes that you’d like to nuke (let’s use the three machines from the pssh example). Here you would do the following:

 # pnuke -h ips.txt -l irb2 java
 Success on 128.112.152.122:22
 Success on 18.31.0.190:22
 Success on 128.232.103.201:22

hostgroup 單獨測試

配置 /etc/pssh/hostgroups

[email protected]virtual-machine:~/batch# cat /etc/pssh/hostgroups 
master: 192.168.19.132,192.168.19.135
slave: 192.168.19.134

pssh :

[email protected]virtual-machine:/etc/pssh# pssh -g master  -i hostname  
[1] 14:20:06 [SUCCESS] 192.168.19.132
mytestdb02
[2] 14:20:06 [SUCCESS] 192.168.19.135
mytestdb01
[email protected]-virtual-machine:/etc/pssh# pssh -g master  -i ifconfig |grep inet | grep -v  127  
[1] 14:20:35 [SUCCESS] 192.168.19.135
          inet addr:192.168.19.135  Bcast:192.168.19.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe2a:d6db/64 Scope:Link
          inet6 addr: ::1/128 Scope:Host
[2] 14:20:35 [SUCCESS] 192.168.19.132
          inet addr:192.168.19.132  Bcast:192.168.19.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe78:dfd8/64 Scope:Link
          inet6 addr: ::1/128 Scope:Host
[email protected]-virtual-machine:/etc/pssh# 
[email protected]-virtual-machine:/etc/pssh# 
[email protected]-virtual-machine:/etc/pssh# pssh -g slave   -i hostname          
[1] 14:20:50 [SUCCESS] 192.168.19.134
mytaskdb
[email protected]-virtual-machine:/etc/pssh# 

pscp :

本地create 兩個file 用於測試:

[email protected]virtual-machine:~/batch# ls file1 file2 
file1  file2
[email protected]-virtual-machine:~/batch# cat file1 file2 
test master 
test slave 
[email protected]-virtual-machine:~/batch#

執行

[email protected]virtual-machine:~/batch# pscp -g slave  file2  /root/bin/filetest
[1] 14:22:28 [SUCCESS] 192.168.19.134
[email protected]-virtual-machine:~/batch# pscp -g master   file1  /root/bin/filetest      
[1] 14:22:36 [SUCCESS] 192.168.19.132
[2] 14:22:36 [SUCCESS] 192.168.19.135
[email protected]-virtual-machine:~/batch# 

結果

[email protected]virtual-machine:~/batch# pssh -g master -i cat /root/bin/filetest  
[1] 14:23:02 [SUCCESS] 192.168.19.132
test master 
[2] 14:23:02 [SUCCESS] 192.168.19.135
test master 
[email protected]-virtual-machine:~/batch# pssh -g slave  -i cat /root/bin/filetest        
[1] 14:23:10 [SUCCESS] 192.168.19.134
test slave 
[email protected]-virtual-machine:~/batch#

總結

pssh 是基於python的一個batch 管理主機的工具, 現在也有 python 的 fabric 模塊,也可以完成這類似的工具, 後面有時間可以總結一下。

批量部署 自動化之 - [pssh](轉)