#!/bin/bash
#write:blacksnow
echo "Welcome use keepalived+LVS(DR) script!!"
read -p "Keepalived_master_地址: " KL_master
read -p "Keepalived_backup地址: " KL_backup
read -p "报警邮件名(eg:[email protected]): " EM_to
read -p "邮件的发送名(eg:[email protected]): " EM_from
read -p "监测网络的接口(eg:eth0): " interface
read -p "虚拟VIP地址: " vir_VIP
read -p "虚拟访问端口(default 80): " port
port=${port:-80}
read -p "负载调度算法(rr/wrr): " lb_algo
#read -p "负载均衡的机制(NAT/TUN/DR): " lb_kind
lb_kind=‘DR‘
read -p "后端web-1地址: " real1_IP
read -p "后端web-2地址: " real2_IP
NULL="/dev/null"
real1_weight=1
real2_weight=1
####################################################
#调度器Keepalived+lvs(DR)
####################################################
#
#ssh ${KL_master} yum -y install keepalived ipvsadm >${NULL}
#ssh ${KL_backup} yum -y install keepalived ipvsadm >${NULL}
#
####################################################
#Keepalived+lvs(DR)MASTER配置
####################################################
cat <<EOE | ssh ${KL_master} "cat -> /etc/keepalived/keepalived.conf"
! Configuration File for keepalived
global_defs {
notification_email {
${EM_to}
}
notification_email_from ${EM_from}
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface ${interface}
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
${vir_VIP}
}
}
virtual_server ${vir_VIP} ${port} {
delay_loop 6
lb_algo ${lb_algo}
lb_kind ${lb_kind}
persistence_timeout 50
protocol TCP
real_server ${real1_IP} ${port} {
weight ${real1_weight}
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server ${real2_IP} ${port} {
weight ${real2_weight}
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
EOE
####################################################
#Keepalived+lvs(DR)BACKUP配置
####################################################
cat <<EOE | ssh ${KL_backup} "cat -> /etc/keepalived/keepalived.conf"
! Configuration File for keepalived
global_defs {
notification_email {
${EM_to}
}
notification_email_from ${EM_from}
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface ${interface}
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
${vir_VIP}
}
}
virtual_server ${vir_VIP} ${port} {
delay_loop 6
lb_algo ${lb_algo}
lb_kind ${lb_kind}
persistence_timeout 50
protocol TCP
real_server ${real1_IP} ${port} {
weight ${real1_weight}
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server ${real2_IP} ${port} {
weight ${real2_weight}
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
EOE
####################################################
#Realserver 修改ARP内核参数和绑定VIP地址
####################################################
for IP in ${real1_IP} ${real2_IP}
do
cat <<EOE |ssh ${IP} "cat - > /etc/sysconfig/network-scripts/ifcfg-lo:0"
DEVICE=lo:0
IPADDR=${vir_VIP}
NETMASK=255.255.255.255
NETWORK=${vir_VIP}
BROADCAST=${vir_VIP}
ONBOOT=yes
NAME=lo:0
EOE
ssh ${IP} ifup lo:0
cat <<EOE |ssh ${IP} "cat - > /etc/sysctl.conf"
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
EOE
ssh ${IP} sysctl -p > ${NULL}
done
####################################################
#开启keepalived
####################################################
ssh ${KL_master} "systemctl restart keepalived"
ssh ${KL_master} "systemctl enable keepalived"
ssh ${KL_backup} "systemctl restart keepalived"
ssh ${KL_backup} "systemctl enable keepalived"
echo -e "\033[36mSuccessful configure!!\033[0m"
原文地址:http://blog.51cto.com/13762620/2131606