shell 脚本实战笔记(6)--集群环境配置检测

 

1). 背景:
  集群部署的时候, 需要一致的配置和环境设置. 对于虚拟机集群, 可以借助镜像拷贝, 复制和还原集群机器. 对与物理机集群而言, 则不一样, 如果机器一多, 多人去操作和配置, 对于成熟精干的团队还好, 对于不熟悉环境的小团队, 由于水平的参差不齐, 往往会导致不一致的环境. 因此无论如何, 写脚本进行自动化的配置和环境校验总是最佳实践.


2). 假设应用场景:
*) 系统环境:
安装CDH5, 集群规模为16台机器, 每台机器16CPU, 内存16G, 2块SATA盘共500G, 操作系统为Centos 6.4.
集群机器, ip范围为192.168.1.101~192.168.1.116.

*) 基本要求
安装CDH5时, 需要满足以下基本要求
#) 需要配置每台机器的/etc/hosts文件, 使得每台机器拥有集群所有机器的域名
#) 需要关闭防火墙, 并禁止开启启动
#) 需要配置本地yum源
#) 磁盘分区尽量满足/mnt/disk{N}的形式
#) 机器时间基本同步

3). 具体实施
*) 配置集群的ssh无登录登录
选择一台中控机(跳板机), 或者集群的某台机器, 作为自动化脚本的发起端, 作为系统管理员, 采用root用户.
#) 本地创建RSA密钥, 产生RSA公钥/私钥对
mkdir -p ~/.ssh
cd ~/.ssh
ssh-keygen -t rsa -P ''
默认生成id_rsa(私钥), id_rsa.pub(公钥)文件

#) 编辑脚本
ssh-copy-id -i id_rsa root@<target_ip>

#! /bin/bash
username="root"
server_ips=(
  "192.168.1.101" "192.168.1.102" "192.168.1.103" "192.168.1.104" "192.168.1.105"
  "192.168.1.106" "192.168.1.107" "192.168.1.108" "192.168.1.109" "192.168.1.110" 
  "192.168.1.111" "192.168.1.112" "192.168.1.113" "192.168.1.114" "192.168.1.115"
  "192.168.1.116" 
)

for (( i = 0; i < ${#server_ips[*]}; i++ )); do
  ssh-copy-id -i ~/.ssh/id_rsa $username@${server_ips[i]}
done

执行, 当然这步还是痛苦的, 需要手动输入16次密码(16台机器).

#) 开启RSA验证
编辑/etc/ssh/sshd_conf

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

GSSAPIAuthentication no
UseDNS no

前三项, 用于开启RSA服务, 后两项用于解决初始连接SSH响应慢的问题

#) 重启ssh服务
service sshd restart

*) 编写自动化脚本

#! /bin/bash

username="root"

server_ips=(
  "192.168.1.101" "192.168.1.102" "192.168.1.103" "192.168.1.104" "192.168.1.105"
  "192.168.1.106" "192.168.1.107" "192.168.1.108" "192.168.1.109" "192.168.1.110" 
  "192.168.1.111" "192.168.1.112" "192.168.1.113" "192.168.1.114" "192.168.1.115"
  "192.168.1.116" 
)

# description:
#   在各个节点上, 执行命令, 并把执行结果汇总到一个文件中, 便于对比
# params:
#   $1 => command, 要执行的命令
#   $2 => filename, 要保存输出结果的文件, 用于结果对比
execute_all_servers() {
  ssh_command=$1
  result_file=$2
  echo "start execute..." > $result_file
  for (( i = 0; i < ${#server_ips[*]}; i++ )); do
    echo "server_ip: ${server_ips[i]}, execute command: '$ssh_command'" >> $result_file
    ssh $username@${server_ips[i]} "$ssh_command" >> $result_file
    echo "=================================" >> $result_file
  done
}

#) 检测/etc/hosts文件
execute_all_servers "cat /etc/hosts" "check_hosts_result.log"

#) 磁盘分区和挂载检测
execute_all_servers "df -h ; fdisk -l" "check_fdisk_result.log"

#) 防火墙关闭检测
execute_all_servers "service iptables status" "check_iptable_result.log"

#) 防火墙关闭命令
execute_all_servers "service iptables stop ; chkconfig --levels 235 iptables off" "stop_iptables_result.log"

其他需要加的环境检测和环境配置, 皆可采用类似的方式去实现, 这并非完美, 只是提供了一种解决思路

 

 

 

posted on 2014-06-27 13:07  mumuxinfei  阅读(752)  评论(0编辑  收藏  举报

导航