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

Posted suminem

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自动化脚本-配置LVS(DR模式)相关的知识,希望对你有一定的参考价值。

一,获取所需配置的主机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 [email protected]$ip chkconfig NetworkManager off
        ssh [email protected]$ip iptables -F; service iptables save
        #sed c 取代
        ssh [email protected]$ip "sed -r -i "/^SELINUX/cSELINUX=disabled" /etc/selinux/config"
        ssh [email protected]$ip "sed -r -i "/^HOSTNAME/cHOSTNAME=$hostname" /etc/sysconfig/network"
        ssh [email protected]$ip "sed -r -i "/^BOOTPROTO/cBOOTPROTO=none" /etc/sysconfig/network-scripts/ifcfg-eth0"
        #sed 3a 第三行后边新增
        ssh [email protected]$ip "sed -r -i "/3aIPADDR=$newip" /etc/sysconfig/network-scripts/ifcfg-eth0"
        ssh [email protected]$ip "sed -r -i "/3aGATEWAY=$gw" /etc/sysconfig/network-scripts/ifcfg-eth0"
        #ssh [email protected]$ip "wget ftp://172.16.8.100/rhel6.repo -0 /etc/yum.repos.d/rhel6.repo"
        ssh [email protected]$ip "yum -y install lftp tree httpd"
        ssh [email protected]$ip "chkconfig httpd on"
        ssh [email protected]$ip "echo $hostname > /var/www/html/index.html"
        ssh [email protected]$ip "ntpdate -b $ntpserver"
        ssh [email protected]$ip "echo ‘echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore‘ >> /etc/rc.local"
        ssh [email protected]$ip "echo ‘echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce‘ >> /etc/rc.local"
        ssh [email protected]$ip "echo ‘echo ‘ip addr add dev lo‘ $vip/32 >> /etc/rc.local"
        ssh [email protected]$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 [email protected]$ip chkconfig NetworkManager off
        ssh [email protected]$ip iptables -F; service iptables save
        #sed c 取代
        ssh [email protected]$ip "sed -r -i "/^SELINUX/cSELINUX=disabled" /etc/selinux/config"
        ssh [email protected]$ip "sed -r -i "/^HOSTNAME/cHOSTNAME=$hostname" /etc/sysconfig/network"
        ssh [email protected]$ip "sed -r -i "/^BOOTPROTO/cBOOTPROTO=none" /etc/sysconfig/network-scripts/ifcfg-eth0"
        #sed 3a 第三行后边新增
        ssh [email protected]$ip "sed -r -i "/3aIPADDR=$newip" /etc/sysconfig/network-scripts/ifcfg-eth0"
        ssh [email protected]$ip "sed -r -i "/3aGATEWAY=$gw" /etc/sysconfig/network-scripts/ifcfg-eth0"
        #ssh [email protected]$ip "wget ftp://172.16.8.100/rhel6.repo -0 /etc/yum.repos.d/rhel6.repo"
        ssh [email protected]$ip "yum -y install ipvsadm keepalived"
        ssh [email protected]$ip "ntpdate -b $ntpserver"
        ssh [email protected]$ip "reboot"
        }&
done

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

三、配置调度器

 

以上是关于自动化脚本-配置LVS(DR模式)的主要内容,如果未能解决你的问题,请参考以下文章

lvs中dr模式配置脚本

LVS DR模式搭建keepalived+LVS

18.11 LVS DR模式搭建 18.12 keepalived + LVS

LVS负载均衡群集架构:DR模式群集部署

LVS

LVS DR模式搭建keepalived lvs