码迷,mamicode.com
首页 > 其他好文 > 详细

自动化脚本-配置LVS(DR模式)

时间:2019-05-29 10:26:39      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:conf   manager   tor   arp   pass   文件的   建立   使用   bootproto   

一,获取所需配置的主机IP

cat get_ip.sh

  

#!/bin/bash
#将获得到的IP地址写进IP.txt
>ip.txt
#将原有的hosts信息清除
>/root/.ssh/known_hosts

#如果没有公钥,退出建立
if [ ! -f ~/.ssh/id_rsa.pub ];then
        echo "请使用ssh-kengen建立密钥!!!"
        exit
fi
yum -y install expect

for i in {2..254}
do
        {
        ip=192.168.122.$i
        #ping一次,超时1s
        ping -c1 -W1 $ip &>/dev/null
        if [ $? -eq 0 ];then
                #ping成功则记录到ip.txt中
                echo $ip >> ip.txt
                #使用expect执行下列内容,spawn负责传递公钥给ip,传递yes和密码给可能出现的询问,遇到"#"退出expect
                /usr/bin/expect <<-EOF
                set timeout 10
                spawn ssh-copy-id -i $ip
                expect {
                        "*yes/no" { send "yes\r"; exp_continue }
                        "*password:" { send "uplooking\r" }
                }
        
                expect "#"
                send "exit\r"
                expect eof
                EOF
        fi
        }&
done

wait
echo "finish..."

 二,配置RS

vim lvs_dr_realserver_init.sh

#!/bin/bash
#LVS_DR_Realserver初始化
#设置分隔符为回车
IFS=$\n
#避免sed修改文件时导致该文件的软连接失效
alias sed=sed -c --follow-symlinks
ntpserver=172.16.8.100
gw=192.168.122.1
vip=192.168.122.100

for line in `cat real_serverip.txt`
#文件格式如下real_serverip.txt
#old_ip         hostname        newip
#192.168.122.62 web10           192.168.122.10
#192.168.122.72 web20           192.168.122.20
#192.168.122.82 web30           192.168.122.30
#...
do
        {
        ip=`echo $line |awk {print $1}`
        hostname=`echo $line |awk {print $2}`
        newip=`echo $line |awk {print $3}`
        }
        #‘‘强引用,变量失效;""变量正常调用
        ssh root@$ip chkconfig NetworkManager off
        ssh root@$ip iptables -F; service iptables save
        #sed c 取代
        ssh root@$ip "sed -r -i "/^SELINUX/cSELINUX=disabled" /etc/selinux/config"
        ssh root@$ip "sed -r -i "/^HOSTNAME/cHOSTNAME=$hostname" /etc/sysconfig/network"
        ssh root@$ip "sed -r -i "/^BOOTPROTO/cBOOTPROTO=none" /etc/sysconfig/network-scripts/ifcfg-eth0"
        #sed 3a 第三行后边新增
        ssh root@$ip "sed -r -i "/3aIPADDR=$newip" /etc/sysconfig/network-scripts/ifcfg-eth0"
        ssh root@$ip "sed -r -i "/3aGATEWAY=$gw" /etc/sysconfig/network-scripts/ifcfg-eth0"
        #ssh root@$ip "wget ftp://172.16.8.100/rhel6.repo -0 /etc/yum.repos.d/rhel6.repo"
        ssh root@$ip "yum -y install lftp tree httpd"
        ssh root@$ip "chkconfig httpd on"
        ssh root@$ip "echo $hostname > /var/www/html/index.html"
        ssh root@$ip "ntpdate -b $ntpserver"
        ssh root@$ip "echo ‘echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore‘ >> /etc/rc.local"
        ssh root@$ip "echo ‘echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce‘ >> /etc/rc.local"
        ssh root@$ip "echo ‘echo ‘ip addr add dev lo‘ $vip/32 >> /etc/rc.local"
        ssh root@$ip "reboot"
        }&
done

wait
echo "所有主机初始化完成"

 二,调度器初始化

vim lvs_dr_director_init.sh

#!/bin/bash
#LVS_DR_Director初始化
#设置分隔符为回车
IFS=$\n
#避免sed修改文件时导致该文件的软连接失效
alias sed=sed -c --follow-symlinks
ntpserver=172.16.8.100
gw=192.168.122.1

for line in `cat ip.txt`
#文件格式如下ip.txt
#old_ip         hostname        newip
#192.168.122.52 director1       192.168.122.2
#192.168.122.52 director2       192.168.122.3
#...
do
        {
        ip=`echo $line |awk {print $1}`
        hostname=`echo $line |awk {print $2}`
        newip=`echo $line |awk {print $3}`
        #‘‘强引用,变量失效;""变量正常调用
        ssh root@$ip chkconfig NetworkManager off
        ssh root@$ip iptables -F; service iptables save
        #sed c 取代
        ssh root@$ip "sed -r -i "/^SELINUX/cSELINUX=disabled" /etc/selinux/config"
        ssh root@$ip "sed -r -i "/^HOSTNAME/cHOSTNAME=$hostname" /etc/sysconfig/network"
        ssh root@$ip "sed -r -i "/^BOOTPROTO/cBOOTPROTO=none" /etc/sysconfig/network-scripts/ifcfg-eth0"
        #sed 3a 第三行后边新增
        ssh root@$ip "sed -r -i "/3aIPADDR=$newip" /etc/sysconfig/network-scripts/ifcfg-eth0"
        ssh root@$ip "sed -r -i "/3aGATEWAY=$gw" /etc/sysconfig/network-scripts/ifcfg-eth0"
        #ssh root@$ip "wget ftp://172.16.8.100/rhel6.repo -0 /etc/yum.repos.d/rhel6.repo"
        ssh root@$ip "yum -y install ipvsadm keepalived"
        ssh root@$ip "ntpdate -b $ntpserver"
        ssh root@$ip "reboot"
        }&
done

wait
echo "所有主机初始化完成"

三、配置调度器

 

自动化脚本-配置LVS(DR模式)

标签:conf   manager   tor   arp   pass   文件的   建立   使用   bootproto   

原文地址:https://www.cnblogs.com/suminem/p/10695400.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!