您的位置:首页 > 其它

自动化脚本腾讯云配置集群(一)挂载磁盘

2017-07-19 18:23 525 查看
具体挂载步骤见网址:https://www.qcloud.com/document/product/213/2974

构建自动化脚本需要 linux 上的服务器交互工具 expect, 简要用法和原理了解见 http://blog.csdn.net/leexide/article/details/17485451

配置集群的思路

挂载master机器上的硬盘,再通过expect在master上操作挂载slaves机器上的硬盘,且不需要登录slaves节点。

master 挂载硬盘

笔者的服务器使用的Ubuntu系统,所以首先设置root用户的密码,然后从当前用户登录到root用户。

sudo passwd root
su - root


挂载磁盘,基本步骤同腾讯云官网给出的步骤一致 https://www.qcloud.com/document/product/213/2974, 但是笔者在使用习惯上,不愿意采用官网上将磁盘挂载在其他的文件夹下,习惯将磁盘直接挂在/home目录下。所以笔者的挂载步骤稍有不同:

fdisk /dev/vdb  # "n", "p", 回车, 回车, 回车, "wq"
fdisk -l    # 查看磁盘
mkfs.ext3 /dev/vdb1     # 格式化磁盘
cp -rf /home/ ./        # 将HOME下的文件移出保存,方便硬盘挂载在home目录下
mount /dev/vdb1 /home/  # 挂载硬盘
cp -rf ./home/* /home/  # 将移出的文件数据移回来
chown -R ubuntu:ubuntu /home/ubuntu #移回来的文件权限是root,所以需要更换回原来的权限
echo '/dev/vdb1 /home ext3 defaults 0 0' >> /etc/fstab # 重启读入的磁盘挂载信息


slaves 自动化挂载硬盘

首先确保已经通过root用户安装好expect软件,运行以下脚本时,可借助interact实现断点调试。注意,为了安全起见,读者可以先在一个从节点上调试脚本expect.sh效果,再批量运行。

#!/usr/bin/expect
# 此脚本的运行方式
# expect expect_disk.sh 10.186.2.84 ubuntu 123456

# 1.设置登录节点IP,用户名和密码
# 两种方式定义,字符串定义和参数传递,参数传递方式见上
set timeout 5
#set host "10.186.2.84"
#set username "ubuntu"
#set password "123456"

set host [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]

# 2. 远程登录节点的root用户
spawn ssh $username@$host
expect {
"Are you sure you want to continue connecting (yes/no)?" {send "yes\r"; exp_continue}
"*password*" {send "$password\r"}
}

expect "*$"
send "sudo passwd root\r"
expect "*password*" {send "123456\r"}
expect "*password*" {send "123456\r"}

expect "*$"
send "su - root\r"
expect "Password:" {send "123456\r"}

# 3. 自动化挂载磁盘
expect "*#"
send "fdisk /dev/vdb\r"
expect "*(m for help):" {send "n\r"}
expect "*default p*" {send "p\r"}
expect "*default*" {send "\r"}
expect "*default 2048*" {send "\r"}
expect "Last sector*"  {send "\r"}
expect "*m for help*" {send "wq\r"}

expect "*#"
send "fdisk -l\r"

expect "*#"
send "mkfs.ext3 /dev/vdb1\r"

expect "*#"
send "cp -rf /home/ ./\r"

expect "*#"
send "mount /dev/vdb1 /home/\r"

expect "*#"
send "cp -rf ./home/* /home/\r"

expect "*#"
send "chown -R ubuntu:ubuntu /home/ubuntu\r"

expect "*#"
send "echo '/dev/vdb1 /home ext3 defaults 0 0' >> /etc/fstab\r"

expect "*#"
send "exit\r"

expect eof


从节点批量挂载硬盘

创建文件slavenodes,保存从节点在该文件中,如图所示



执行disk.sh脚本

#!bin/bash
nodes=($(awk "{print \$1}" slavenodes))

for node in ${nodes[*]}
do
expect expect_disk.sh $node ubuntu 123456
done


自动化创建用户

在服务器中分用户共享资源很常见,所以管理员一般会为使用者创建相应的用户账号,通过以下脚本也能实现自动化批量创建脚本。

expect_login.sh

#!/usr/bin/expect
# expect expect_login.sh 10.186.2.84 ubuntu 123456
set timeout 1
#set host "10.186.2.84"
#set username "ubuntu"
#set password "123456"
set host [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]

spawn ssh $username@$host
expect {
"Are you sure you want to continue connecting (yes/no)?" {send "yes\r"; exp_continue}
"*password*" {send "$password\r"}
}

# interact

expect "*$"
send "sudo adduser TenXun\r"
expect "*password*" {send "$password\r"}
expect "*password*" {send "$password\r"}
expect "*[]*" {send "\r"}
expect "*[]*" {send "\r"}
expect "*[]*" {send "\r"}
expect "*[]*" {send "\r"}
expect "*[]*" {send "\r"}
expect "*information correct*" {send "\r"}

expect eof


仿照disk.sh编写脚本批量执行expect_login.sh

#!bin/bash
nodes=($(awk "{print \$1}" slavenodes))

for node in ${nodes[*]}
do
expect expect_login.sh $node ubuntu 123456
done
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: