Lvs-DR Keepalived的解決方案

NO IMAGE

參考

https://www.cnblogs.com/liaojiafa/p/6087276.html

 

 

windows real server設定相關,參考下面文章

http://www.loadbalancer.org/blog/direct-server-return-on-windows-2008-using-loopback-adpter/

四臺主機在一個區域網內,LVS_master的ip 為192.168.1.196,lvs-backup:192.168.1.197,兩臺web伺服器的ip為192.168.1.200,192.168.1.201,虛擬IP為192.168.1.198從keepalived中配置檔案中可以看出。

實驗環境為vmware虛擬出4臺linux主機網絡卡全為橋接模式。系統版本為:CentOS Linux release 7.3.1611 (Core) 

【1】安裝ipvsadm和keepalived
 

【2】配置主從LVS伺服器

a,開啟路由轉發功能

b,LVS_master keepalived配置檔案

[[email protected] keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_MASTER
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.198
}
}
virtual_server 192.168.1.198 80 {
delay_loop 6
lb_algo rr
lb_kind DR
#    persistence_timeout 50
protocol TCP
real_server 192.168.1.200 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.201 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}

c,LVS_backup 配置檔案

[[email protected] ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_BACK
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.198
}
}
virtual_server 192.168.1.198 80 {
delay_loop 6
lb_algo rr
lb_kind DR
#    persistence_timeout 50
protocol TCP
real_server 192.168.1.200 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.201 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}

d,在兩臺LVS上執行keepalived服務

e,配置兩臺WEB服務

分別在兩臺伺服器上做如下操作:

    編輯sysctl.conf檔案

[[email protected] ~]# cat /etc/sysctl.conf 
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.conf.ens33.arp_ignore = 1
net.ipv4.conf.ens33.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

重啟

[[email protected] ~]# sysctl -p
net.ipv4.conf.ens33.arp_ignore = 1
net.ipv4.conf.ens33.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

配置lo介面ip

ifconfig lo 192.168.1.198 netmask 255.255.255.255 broadcast 192.168.1.198 up

增加一條路由

 route add -host 192.168.1.198 dev lo

建議不要忘記在rc.local裡面寫入增加VIP的新增命令,省去開機後人工配置的麻煩:

[[email protected] ~]# cat /etc/rc.local 
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod  x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
ifconfig lo 192.168.1.198 netmask 255.255.255.255 broadcast 192.168.1.198 up
route add -host 192.168.1.198 dev lo