Error message here!

Hide Error message here!

忘记密码?

Error message here!

请输入正确邮箱

Hide Error message here!

密码丢失?请输入您的电子邮件地址。您将收到一个重设密码链接。

Error message here!

返回登录

Close

shell脚本一键部署lvs+keepalived

千机伞 2019-09-28 19:50:00 阅读数:771 评论数:0 点赞数:0 收藏数:0

环境

两个调度器dr1、dr2,两台真实机rs1、rs2。两台真实机安装httpd,并编辑主页内容用于验证

vip="192.168.132.250"
dr1="192.168.132.120"
dr2="192.168.132.121"
rs1="192.168.132.131"
rs2="192.168.132.134"

配置

#/bin/bash
vip="192.168.132.250"
dr1="192.168.132.120"
dr2="192.168.132.121"
rs1="192.168.132.131"
rs2="192.168.132.134"
#dr1配置keepalived
yum install -y keepalived
cat > /etc/keepalived/keepalived.conf <<EOF
global_defs {
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 
priority 
advert_int 
authentication {
auth_type PASS
auth_pass 
}
virtual_ipaddress {
$vip/
}
}
virtual_server $vip  {
delay_loop 
lb_algo rr
lb_kind DR
protocol TCP
real_server $rs1  {
weight 
TCP_CHECK {
connect_timeout 
nb_get_retry 
delay_before_retry 
}
}
real_server $rs2  {
weight 
TCP_CHECK {
connect_timeout 
nb_get_retry 
delay_before_retry 
}
}
}
EOF
systemctl restart keepalived
yum install -y ipvsadm sshpass
sed -i 35c"StrictHostKeyChecking no" /etc/ssh/ssh_config
setenforce 
systemctl stop firewalld
#dr1复制keepalived文件到dr2
sshpass -p root ssh $dr2 "yum install -y keepalived"
sshpass -p root scp /etc/keepalived/keepalived.conf $dr2:/etc/keepalived/keepalived.conf
#dr2配置keepalived
dr2_cmd="
setenforce ;
systemctl stop firewalld;
sed -i 's/MASTER/BACKUP/g' /etc/keepalived/keepalived.conf;
sed -i 's/100/90/g' /etc/keepalived/keepalived.conf;
systemctl restart keepalived;
"
sshpass -p root ssh $dr2 "$dr2_cmd"
#rs1,rs2配置lvs
cmd="
setenforce ;
firewall-cmd --add-port=/tcp;
ifconfig lo: $vip broadcast $vip netmask 255.255.255.255 up;
route add -host $vip dev lo:;
echo "" > /proc/sys/net/ipv4/conf/lo/arp_ignore;
echo "" > /proc/sys/net/ipv4/conf/lo/arp_announce;
echo "" > /proc/sys/net/ipv4/conf/all/arp_ignore;
echo "" > /proc/sys/net/ipv4/conf/all/arp_announce;
service httpd restart
"
sshpass -p root ssh $rs1 "$cmd"
sshpass -p root ssh $rs2 "$cmd"

 

版权声明
本文为[千机伞]所创,转载请带上原文链接,感谢
https://www.cnblogs.com/qianjisan66/p/11604578.html