LVS-DR模型Shell脚本-实践

LVS-学习目录

1、DS脚本

cat << 'EOF_END' > lvs_dr.sh
#!/usr/bin/bash
VIP=192.168.87.200
RS1=192.168.87.129
RS2=192.168.87.130
PORT=80
DEV=ens33:1
case $1 in
start)
cat >/etc/sysconfig/network-scripts/ifcfg-${DEV}<<-EOF
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
NAME=${DEV}
DEVICE=${DEV}
ONBOOT=yes
IPADDR=${VIP}
PREFIX=24
EOF
  ifup ${DEV}
  # 配置LVS规则
  ipvsadm -C
  ipvsadm -A -t ${VIP}:${PORT} -s rr
  ipvsadm -a -t ${VIP}:${PORT} -r ${RS1}:${PORT} -g
  ipvsadm -a -t ${VIP}:${PORT} -r ${RS2}:${PORT} -g
;;
stop)
  ifdown ${DEV}
  rm -f /etc/sysconfig/network-scripts/ifcfg-${DEV}
  ipvsadm -C
;;
*)
  echo "Usage: sh $0 { start | stop }"
;;
esac
EOF_END

2、RS脚本

cat << 'EOF_END' > lvs_rs.sh
#!/usr/bin/bash
VIP=192.168.87.200
DEV=lo:0
case $1 in start)
  echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
  echo "1" >/proc/sys/net/ipv4/conf/default/arp_ignore
  echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
  echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
  echo "2" >/proc/sys/net/ipv4/conf/default/arp_announce
  echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
cat >/etc/sysconfig/network-scripts/ifcfg-${DEV} <<-EOF
DEVICE=lo:0
IPADDR=${VIP}
NETMASK=255.255.255.255
ONBOOT=yes
NAME=loopback
EOF
  ifup ${DEV}
  systemctl start httpd
;;
stop)
  echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
  echo "0" >/proc/sys/net/ipv4/conf/default/arp_ignore
  echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
  echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
  echo "0" >/proc/sys/net/ipv4/conf/default/arp_announce
  echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
  ifdown ${DEV}
  rm -f /etc/sysconfig/network-scripts/ifcfg-${DEV}
  systemctl stop httpd
;;
*)
  echo "Usage: sh $0 { start | stop }"
;;
esac
EOF_END

3、执行脚本

lvs ~]# sh lvs_dr.sh start
rs-01 ~]# sh lvs_rs.sh start
rs-02 ~]# sh lvs_rs.sh start

4、访问测试

[root@route ~]# curl 192.168.87.200
rs-01
[root@route ~]# curl 192.168.87.200
rs-02

 

posted @ 2023-05-05 11:09  小粉优化大师  阅读(14)  评论(0编辑  收藏  举报