配置realserver1和realserver2
[root@web01 ~]# cat /etc/init.d/realserver #!/bin/bash # description: Config realserver lo and apply noarp #Written by :NetSeek http://www.linuxtone.org SNS_VIP=192.168.1.225 SNS_VIP1=192.168.1.226 . /etc/rc.d/init.d/functions case "$1" in start) ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP ifconfig lo:1 $SNS_VIP1 netmask 255.255.255.255 broadcast $SNS_VIP1 /sbin/route add -host $SNS_VIP dev lo:0 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce sysctl -p >/dev/null 2>&1 echo "RealServer Start OK" ;; stop) ifconfig lo:0 down
ifconfig lo:1 down route del $SNS_VIP >/dev/null 2>&1 echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce echo "RealServer Stoped" ;; *) echo "Usage: $0 {start|stop}" exit 1 esac exit 0
安装并配置keepalived(两台负载均衡调度主机上都要安装)
Keep1安装配置。
[root@localhost ~]# yum -y install keepalived [root@localhost ~]# cd /etc/keepalived/ [root@localhost keepalived]# cp keepalived.conf keepalived.conf.bak [root@localhost keepalived]# echo "" > keepalived.conf
[root@localhost keepalived]# cat keepalived.conf vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 31 priority 100 advert_int 1 authentication { auth_type PASS auth_pass abcd } virtual_ipaddress { 192.168.1.225 } } vrrp_instance VI_2 { state BACKUP interface ens33 virtual_router_id 41 priority 99 advert_int 1 authentication { auth_type PASS auth_pass abcd } virtual_ipaddress { 192.168.1.226 } } virtual_server 192.168.1.225 80 { delay_loop 6 lb_algo wrr //LVS算法 lb_kind DR //调度类型 protocol TCP real_server 192.168.1.223 80 { weight 1 inhibit_on_failure TCP_CHECK { connect_timeout 3 nb_get_retry 2 delay_before_retry 1 connect_port 80 } } real_server 192.168.1.220 80 { weight 1 inhibit_on_failure TCP_CHECK { connect_timeout 3 nb_get_retry 2 delay_before_retry 1 connect_port 80 } } } virtual_server 192.168.1.226 80 { delay_loop 6 lb_algo wrr lb_kind DR protocol TCP real_server 192.168.1.223 80 { weight 1 inhibit_on_failure TCP_CHECK { connect_timeout 3 nb_get_retry 2 delay_before_retry 1 connect_port 80 } } real_server 192.168.1.220 80 { weight 1 inhibit_on_failure TCP_CHECK { connect_timeout 3 nb_get_retry 2 delay_before_retry 1 connect_port 80 } } } [root@localhost keepalived]# yum -y install ipvsadm [root@localhost keepalived]# systemctl start keepalived [root@localhost keepalived]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.1.225:80 wrr -> 192.168.1.220:80 Route 1 0 0 -> 192.168.1.223:80 Route 1 0 0 TCP 192.168.1.226:80 wrr -> 192.168.1.220:80 Route 1 0 0 -> 192.168.1.223:80 Route 1 0 0
[root@localhost keepalived]# ip add show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:f0:61:cb brd ff:ff:ff:ff:ff:ff
inet 192.168.1.221/24 brd 192.168.1.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.1.225/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 2409:8a0c:48:2db0:12b8:d62:fbef:d2d6/64 scope global noprefixroute dynamic
valid_lft 86398sec preferred_lft 14398sec
inet6 fe80::c8e4:7436:3263:f906/64 scope link
valid_lft forever preferred_lft forever
[root@localhost keepalived]#
Keep2上的安装配置。
[root@localhost keepalived]# cat keepalived.conf vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 31 priority 99 advert_int 1 authentication { auth_type PASS auth_pass abcd } virtual_ipaddress { 192.168.1.225 } } vrrp_instance VI_2 { state MASTER interface ens33 virtual_router_id 41 priority 100 advert_int 1 authentication { auth_type PASS auth_pass abcd } virtual_ipaddress { 192.168.1.226 } } virtual_server 192.168.1.225 80 { delay_loop 6 lb_algo wrr lb_kind DR protocol TCP real_server 192.168.1.223 80 { weight 1 inhibit_on_failure TCP_CHECK { connect_timeout 3 nb_get_retry 2 delay_before_retry 1 connect_port 80 } } real_server 192.168.1.220 80 { weight 1 inhibit_on_failure TCP_CHECK { connect_timeout 3 nb_get_retry 2 delay_before_retry 1 connect_port 80 } } } virtual_server 192.168.1.226 80 { delay_loop 6 lb_algo wrr lb_kind DR protocol TCP real_server 192.168.1.223 80 { weight 1 inhibit_on_failure TCP_CHECK { connect_timeout 3 nb_get_retry 2 delay_before_retry 1 connect_port 80 } } real_server 192.168.1.220 80 { weight 1 inhibit_on_failure TCP_CHECK { connect_timeout 3 nb_get_retry 2 delay_before_retry 1 connect_port 80 } } }
[root@localhost keepalived]# yum -y install ipvsadm [root@localhost keepalived]# systemctl start keepalived [root@localhost keepalived]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.1.225:80 wrr -> 192.168.1.220:80 Route 1 0 0 -> 192.168.1.223:80 Route 1 0 0 TCP 192.168.1.226:80 wrr -> 192.168.1.220:80 Route 1 0 0 -> 192.168.1.223:80 Route 1 0 0 [root@localhost keepalived]# ip add show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:21:e3:49 brd ff:ff:ff:ff:ff:ff inet 192.168.1.222/24 brd 192.168.1.255 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.1.226/32 scope global ens33 valid_lft forever preferred_lft forever inet6 2409:8a0c:48:2db0:b0b4:9430:1a68:fa8c/64 scope global noprefixroute dynamic valid_lft 86253sec preferred_lft 14253sec inet6 fe80::1592:73e7:370e:7e72/64 scope link valid_lft forever preferred_lft forever
测试