當(dāng)前位置:首頁(yè) > IT技術(shù) > 系統(tǒng)服務(wù) > 正文

linux集群自動(dòng)化搭建(生成密鑰對(duì)+分發(fā)公鑰+遠(yuǎn)程批量執(zhí)行腳本)
2021-08-10 15:52:39

第一步,服務(wù)器準(zhǔn)備

這里使用docker模擬幾臺(tái)服務(wù)器,分別命名為node2,node3,node4(使用鏡像chenqionghe/ubuntu,密碼統(tǒng)一為88888888),生產(chǎn)環(huán)境為ip或host

docker run -d --name node2 -p 2223:22 chenqionghe /ubuntu
docker run -d --name node3 -p 2224:22 chenqionghe /ubuntu
docker run -d --name node4 -p 2225:22 chenqionghe /ubuntu

還得有一臺(tái)主控制服務(wù)器node1,負(fù)責(zé)操作所有的服務(wù)器節(jié)點(diǎn)

docker run -d --name node1 -p 2222:22 
--link node2:node2 
--link node3:node3 
--link node4:node4 
chenqionghe/ubuntu

初始化完成后進(jìn)入node1節(jié)點(diǎn)

ssh root@127.0.0.1 -p 2222

安裝必須軟件

apt-get install expect -y

創(chuàng)建存放腳本的目錄~/env

mkdir -p ~/env && cd ~/env

這里先模擬一個(gè)簡(jiǎn)單的安裝包scripts/install.sh,安裝vim命令

mkdir scripts 
cat > scripts/install.sh <<EOF
#!/bin/bash bash
apt-get installvim -y
EOF

創(chuàng)建機(jī)器列表配置文件,vim nodes.txt

node2
node3
node4

第二步 編寫自動(dòng)化腳本

1.無(wú)交互ssh-keygen生成密鑰對(duì)腳本,vim ssh-keygen.exp

#!/usr/bin/expect
#set enter "
"
spawn ssh-keygen
expect {
        "*(/root/.ssh/id_rsa)" {send "
";exp_continue}
        "*(empty for no passphrase)" {send "
";exp_continue}
        "*again" {send "
"}
}
expect eof

2.無(wú)交互分發(fā)公鑰腳本,vim send-sshkey.exp

#!/usr/bin/expect
if { $argc != 2 } {
 send_user "usage: expect send-sshkey.exp file host
"
 exit
}
#define var
set file [lindex $argv 0]
set host [lindex $argv 1]
set password "88888888"
spawn ssh-copy-id -i $file -p 22 root@$host
expect {
        "yes/no" {send "yes
";exp_continue}
        "*password" {send "$password
"}
}
expect eof 

3.遠(yuǎn)程批量執(zhí)行ssh腳本, vim mssh.sh

#!/bin/bash
Port=22
if [ $# -lt 1 ];then
    echo "Usage: `basename $0` command"
    exit
fi
echo $@
for v in `cat nodes.txt`
do
    ssh -t -p ${Port} root@${v} $@
    if [ $? -eq 0 ];then
        echo "執(zhí)行成功:$v"
    else
        echo "執(zhí)行失敗:$v"
    fi
done

4.自動(dòng)化部署腳本,vim auto-deploy.sh

#!/usr/bin/env bash
#機(jī)器列表
HostList=`cat nodes.txt`
#端口號(hào)
Port=22
# 1.無(wú)交互生成密鑰對(duì)
if [ ! -f ~/.ssh/id_rsa.pub ];then
    expect ssh-keygen.exp
fi
# 2.無(wú)交互分發(fā)公密鑰
for v in ${HostList}
do
    expect send-sshkey.exp ~/.ssh/id_rsa.pub ${v}
    if [ $? -eq 0 ];then
        echo "公鑰-發(fā)送成功:$v"
    else
        echo "公鑰-發(fā)送失?。?v"
    fi
done
# 3.分發(fā)腳本文件(安裝軟件包)
for v in ${HostList}
do
    scp -P ${Port} -rp ~/env/scripts root@${v}:~
    if [ $? -eq 0 ];then
        echo "scripts-發(fā)送成功:$v"
    else
        echo "scripts-發(fā)送失敗:$v"
    fi
done
# 4.使用腳本文件安裝
for v in ${HostList}
do
    ssh -t -p ${Port} root@${v} /bin/bash ~/scripts/install.sh
done

到這里所有的文件已經(jīng)創(chuàng)建完成,整個(gè)env目錄結(jié)構(gòu)如下

├── auto-deploy.sh
├── mssh.sh
├── nodes.txt
├── scripts
│   └── install.sh
├── send-sshkey.exp
└── ssh-keygen.exp

第三步 執(zhí)行自動(dòng)化腳本查看效果

sh auto-deploy.sh

執(zhí)行成功結(jié)果

spawn ssh-keygen
...
公鑰-發(fā)送成功:node2
...
公鑰-發(fā)送成功:node3
...
公鑰-發(fā)送成功:node4
install.sh 100% 40 41.4KB/s 00:00
scripts-發(fā)送成功:node2
install.sh 100% 40 45.0KB/s 00:00
scripts-發(fā)送成功:node3
install.sh 100% 40 39.9KB/s 00:00
scripts-發(fā)送成功:node4
...
Connection to node4 closed.

出現(xiàn)上面的結(jié)果代表3個(gè)node節(jié)點(diǎn)已經(jīng)初始化完成,拷貝公鑰+安裝軟件成功!

我們?cè)賮?lái)執(zhí)行一下遠(yuǎn)程命令腳本,查看所有服務(wù)器系統(tǒng)類型

sh mssh.sh "cat /etc/os-release|head -n 1"

執(zhí)行結(jié)果如下

cat /etc/os-release|head -n 1
NAME="Ubuntu"
Connection to node2 closed.
執(zhí)行成功:node2
NAME="Ubuntu"
Connection to node3 closed.
執(zhí)行成功:node3
NAME="Ubuntu"
Connection to node4 closed.
執(zhí)行成功:node4

這樣就實(shí)現(xiàn)了自動(dòng)化創(chuàng)建密鑰對(duì)+分發(fā)公鑰+所有服務(wù)器軟件環(huán)境安裝+批量遠(yuǎn)程執(zhí)行腳本mssh.sh,麻雀雖小,五臟俱全,絕對(duì)是干貨!

本文摘自 :https://blog.51cto.com/u

開通會(huì)員,享受整站包年服務(wù)立即開通 >