如果有一个服务器集群,要保证每台服务器上的环境一样,要自动部署相同的软件
用脚本来实现:
1 设置免密登录 ssh-copy-id server 把本地主机的公钥复制到远程主机的authorized_keys文件上
2发送软件安装脚本到远程服务器
3运行脚本
boot.sh
#!/bin/bash
SERVERS="mini2 mini3"
PASSWORD=hadoop
BASE_SERVER=192.168.180.100
auto_ssh_copy_id() {
expect -c "set timeout -1;
spawn ssh-copy-id $1;
expect {
*(yes/no)* {send -- yes\r;exp_continue;}
*assword:* {send -- $2\r;exp_continue;}
eof {exit 0;}
}";
}
ssh_copy_id_to_all() {
for SERVER in $SERVERS
do
auto_ssh_copy_id $SERVER $PASSWORD
done
}
yum install -y expect;
ssh_copy_id_to_all
for SERVER in $SERVERS
do
ssh root@$SERVER yum install -y openssh-clients
scp install_everyone.sh root@$SERVER:/root
ssh root@$SERVER /root/install_everyone.sh
done
安装脚本内容:
1安装wget yum -y install wget
2在指定地址下载软件 wget baseserver/soft
3安装软件
install_everyone.sh
#!/bin/bash
BASE_SERVER=192.168.180.100
yum install -y wget
wget $BASE_SERVER/soft/jdk-7u45-linux-x64.tar.gz
tar -zxvf jdk-7u45-linux-x64.tar.gz -C /usr/local
cat >> /etc/profile << EOF
export JAVA_HOME=/usr/local/jdk1.7.0_45
export PATH=\$PATH:\$JAVA_HOME/bin
EOF