自动化脚本腾讯云配置集群(一)挂载磁盘
2017-07-19 18:23
525 查看
具体挂载步骤见网址:https://www.qcloud.com/document/product/213/2974
构建自动化脚本需要 linux 上的服务器交互工具 expect, 简要用法和原理了解见 http://blog.csdn.net/leexide/article/details/17485451
挂载磁盘,基本步骤同腾讯云官网给出的步骤一致 https://www.qcloud.com/document/product/213/2974, 但是笔者在使用习惯上,不愿意采用官网上将磁盘挂载在其他的文件夹下,习惯将磁盘直接挂在/home目录下。所以笔者的挂载步骤稍有不同:
从节点批量挂载硬盘
创建文件slavenodes,保存从节点在该文件中,如图所示
执行disk.sh脚本
expect_login.sh
仿照disk.sh编写脚本批量执行expect_login.sh
构建自动化脚本需要 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
相关文章推荐
- 自动化脚本腾讯云配置集群(三)批量修改host
- 自动化脚本腾讯云配置集群(四)自动化安装从节点的JDK+Hadoop
- 自动化脚本腾讯云配置集群(二)大集群SSH免密登录
- Hadoop实战4:MapR分布式集群的安装配置及shell自动化脚本
- hadoop集群安装与配置--hadoop自动化安装脚本
- 1虚拟机下U盘或磁盘挂载,Ubuntu下的网络配置,图形化界面和命令界面之间的切换,软件源配置
- 使用shell脚本自动化配置工作环境
- jenkins上配置robotframeworkride自动化脚本任务
- yum源配置以及开机自动挂载shell脚本
- CentOS 或 Fedora 下自动化安装mono和jexus的脚本(Linux下的Asp.net运行环境配置)
- 自动挂载磁盘脚本
- 腾讯云和腾讯云 挂载新的云磁盘
- crontab定时启动脚本,自动化配置
- 配置文件fstab读取挂载磁盘详解
- Storm之——自动化集群配置工具Puppet的类和模块
- [笔记] CentOS 7 部署 Kubernetes 集群自动化脚本部分
- crontab定时启动脚本,自动化配置
- 多磁盘自动分区自动挂载脚本
- sentinel redis 集群部署+zabbix监控配置+性能测试+多实例自动配置脚本
- Centos 集群配置SSH免登陆脚本