虚拟IP:
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:f2:9a:71 brd ff:ff:ff:ff:ff:ff
inet 10.0.128.133/24 brd 10.0.128.255 scope global dynamic ens33
valid_lft 85733sec preferred_lft 85733sec
inet 10.0.128.145/24 scope global secondary ens33
valid_lft forever preferred_lft forever
inet6 fe80::a9b5:bf8f:70f8:8d01/64 scope link
valid_lft forever preferred_lft forever
inet6 fe80::6558:c967:a7d3:865f/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id Director1 #两边不一样。
}
vrrp_instance JIA_1 {
state MASTER #另外一台机器是BACKUP
interface ens33 #心跳网卡
virtual_router_id 51 #虚拟路由编号,主备要一致
priority 150 #优先级
advert_int 1 #检查间隔,单位秒
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.128.145/24 dev ens33 #VIP和工作端口
}
}
vrrp_instance JIA_2 {
state BACKUP #另外一台机器是BACKUP
interface ens33 #心跳网卡
virtual_router_id 60 #虚拟路由编号,主备要一致
priority 100 #优先级
advert_int 1 #检查间隔,单位秒
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.128.146/24 dev ens33 #VIP和工作端口
}
}
virtual_server 10.0.128.145 80 { #LVS 配置,VIP
delay_loop 3 #服务论询的时间间隔,#每隔3秒检查一次real_server状态
lb_algo rr #LVS 调度算法
lb_kind DR #LVS 集群模式
protocol TCP
real_server 10.0.128.211 80 {
weight 1
TCP_CHECK {
connect_timeout 3 #健康检查方式,连接超时时间
}
}
real_server 10.0.128.248 80 {
weight 1
TCP_CHECK {
connect_timeout 3
}
}
}
virtual_server 10.0.128.146 80 { #LVS 配置,VIP
delay_loop 3 #服务论询的时间间隔,#每隔3秒检查一次real_server状态
lb_algo rr #LVS 调度算法
lb_kind DR #LVS 集群模式
protocol TCP
real_server 10.0.128.211 80 {
weight 1
TCP_CHECK {
connect_timeout 3 #健康检查方式,连接超时时间
}
}
real_server 10.0.128.248 80 {
weight 1
TCP_CHECK {
connect_timeout 3
}
}
}
虚拟IP:
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:8e:08:11 brd ff:ff:ff:ff:ff:ff
inet 10.0.128.202/24 brd 10.0.128.255 scope global dynamic ens33
valid_lft 85630sec preferred_lft 85630sec
inet 10.0.128.146/24 scope global secondary ens33
valid_lft forever preferred_lft forever
inet6 fe80::f98:3607:eb6d:987c/64 scope link
valid_lft forever preferred_lft forever
inet6 fe80::a9b5:bf8f:70f8:8d01/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::6558:c967:a7d3:865f/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id Director2 #两边不一样。
}
vrrp_instance JIA_1 {
state BACKUP #另外一台机器是BACKUP
interface ens33 #心跳网卡
virtual_router_id 51 #虚拟路由编号,主备要一致
priority 100 #优先级
advert_int 1 #检查间隔,单位秒
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.128.145/24 dev ens33 #VIP和工作端口
}
}
vrrp_instance JIA_2 {
state MASTER #另外一台机器是BACKUP
interface ens33 #心跳网卡
virtual_router_id 60 #虚拟路由编号,主备要一致
priority 150 #优先级
advert_int 1 #检查间隔,单位秒
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.128.146/24 dev ens33 #VIP和工作端口
}
}
virtual_server 10.0.128.145 80 { #LVS 配置,VIP
delay_loop 3 #服务论询的时间间隔,#每隔3秒检查一次real_server状态
lb_algo rr #LVS 调度算法
lb_kind DR #LVS 集群模式
protocol TCP
real_server 10.0.128.211 80 {
weight 1
TCP_CHECK {
connect_timeout 3 #健康检查方式,连接超时时间
}
}
real_server 10.0.128.248 80 {
weight 1
TCP_CHECK {
connect_timeout 3
}
}
}
virtual_server 10.0.128.146 80 { #LVS 配置,VIP
delay_loop 3 #服务论询的时间间隔,#每隔3秒检查一次real_server状态
lb_algo rr #LVS 调度算法
lb_kind DR #LVS 集群模式
protocol TCP
real_server 10.0.128.211 80 {
weight 1
TCP_CHECK {
connect_timeout 3 #健康检查方式,连接超时时间
}
}
real_server 10.0.128.248 80 {
weight 1
TCP_CHECK {
connect_timeout 3
}
}
}
后端脚本配置虚拟ip,DR模式
[root@localhost ~]# cat realserver.sh
#!/bin/bash
VIP=192.168.40.200
RIP1=192.168.40.133
RIP2=192.168.40.134
case “$1” in
start)
echo " start LVS of RealServer"
ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
route add -host $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
;;
stop)
ifconfig lo:0 down
echo “close LVS of RealServer”
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 “Usage: $0 start|stop”
exit 1
esac
[root@localhost ~]# cat realserver-1.sh
#!/bin/bash
VIP=192.168.40.220
RIP1=192.168.40.133
RIP2=192.168.40.134
case “$1” in
start)
echo " start LVS of RealServer"
ifconfig lo:1 $VIP broadcast $VIP netmask 255.255.255.255 up
route add -host $VIP dev lo:1
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
;;
stop)
ifconfig lo:1 down
echo “close LVS of RealServer”
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 “Usage: $0 start|stop”
exit 1
esac