环境
10.0.0.20 主负载 10.0.0.21 负负载 10.0.0.22 web 10.0.0.23 web 10.0.0.24 web
部署LVS
10.0.0.20
[root@node1 ~]# yum -y install ipvsadm
进入到 /usr/src 目录查看结果,如果有ip_vs_rr、ip_vs,表示正常
[root@node1 ~]# cd /usr/src/ [root@node1 src]# lsmod|grep ip_vs ip_vs 136798 0 nf_conntrack 105702 1 ip_vs libcrc32c 12644 2 xfs,ip_vs
如果没有显示加载一下,在此查看
[root@node1 src]# lsmod|grep ip_vs [root@node1 src]# modprobe ip_vs [root@node1 src]# lsmod|grep ip_vs ip_vs 136798 0 nf_conntrack 105702 1 ip_vs libcrc32c 12644 2 xfs,ip_vs
添加VIP(虚拟IP),虚拟IP需和实际IP为一个网段
[root@node1 ~]# ip addr add 10.0.0.28/24 dev eth0 label eth0:0
查看虚拟IP
[root@node1 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:29:39:b8 brd ff:ff:ff:ff:ff:ff inet 10.0.0.20/24 brd 10.0.0.255 scope global eth0 valid_lft forever preferred_lft forever inet 10.0.0.28/24 scope global secondary eth0:0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe29:39b8/64 scope link valid_lft forever preferred_lft forever
清空ipvsadm记录
[root@node1 ~]# ipvsadm -C
配置ipvs,并查看
[root@node1 ~]# ipvsadm -A -t 10.0.0.28:80 -s wrr [root@node1 ~]# ipvsadm -a -t 10.0.0.28:80 -r 10.0.0.22:80 -g -w 1 [root@node1 ~]# ipvsadm -a -t 10.0.0.28:80 -r 10.0.0.23:80 -g -w 1
[root@node1 ~]# ipvsadm -a -t 10.0.0.28:80 -r 10.0.0.24:80 -g -w 1 [root@node1 ~]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 10.0.0.28:80 wrr -> 10.0.0.22:80 Route 1 0 0 -> 10.0.0.23:80 Route 1 0 0
-> 10.0.0.24:80 Route 1 0 0
加入开机启动
[root@node1 ~]# systemctl enable ipvsadm
Web
绑定虚拟IP(VIP)
[root@node2 ~]# ip addr add 10.0.0.28/32 dev lo label lo:0 每台web都要执行
抑制VIP,防止LVS和RSVIP冲突
[root@node2 ~]# echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore [root@node2 ~]# echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce [root@node2 ~]# echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore [root@node2 ~]# echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
安装keepalived
[root@node1 ~]# yum -y install keepalived
查看版本
[root@node1 ~]# keepalived -v
配置 keepalived.conf 文件
[root@node1 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id wl #设置LVS的ID,在一个集群内是唯一的,主备要一致 } vrrp_instance VI_1 { state MASTER #指定keepalived的角色,MASTER为主,BACKUP为备 interface eth0 virtual_router_id 51 #虚拟路由编号,主备要一致 priority 100 #定义优先级,数值越大,级别越高,主必须大于备 nopreempt advert_int 1 authentication { auth_type PASS auth_pass 111 } virtual_ipaddress { 10.0.0.28 #定义虚拟IP } } virtual_server 10.0.0.28 80 { #定义对外提供服务的lvs的VIP及端口 delay_loop 6 #健康检查的时间,单位为秒 lb_algo wrr 负载调度算法 lb_kind DR 实现负载的机制 nat_mask 255.255.255.0 persistence_timeout 0 protocol TCP real_server 10.0.0.22 80 { weight 1 #配置节点的权值,数值越大权位越高 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 10.0.0.23 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 10.0.0.24 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
重启keepalived
[root@node1 ~]# systemctl restart keepalived
重启后可以通过网页访问虚拟IP进行测试
配置备负载(10.0.0.21)
安装ipvsadm
[root@node2 ~]# yum -y install ipvsadm
进入到 /usr/src 目录查看结果,如果有ip_vs_rr、ip_vs,表示正常
[root@node2 ~]# cd /usr/src/ [root@node1 src]# lsmod|grep ip_vs ip_vs 136798 0 nf_conntrack 105702 1 ip_vs libcrc32c 12644 2 xfs,ip_vs
如果没有显示加载一下,在此查看
[root@node2 src]# lsmod|grep ip_vs [root@node2 src]# modprobe ip_vs [root@node2 src]# lsmod|grep ip_vs ip_vs 136798 0 nf_conntrack 105702 1 ip_vs libcrc32c 12644 2 xfs,ip_vs
添加VIP(虚拟IP),虚拟IP需和实际IP为一个网段
[root@node2 ~]# ip addr add 10.0.0.28/24 dev eth0 label eth0:0
查看虚拟IP
[root@node2 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:29:39:b8 brd ff:ff:ff:ff:ff:ff inet 10.0.0.21/24 brd 10.0.0.255 scope global eth0 valid_lft forever preferred_lft forever inet 10.0.0.28/24 scope global secondary eth0:0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe29:39b8/64 scope link valid_lft forever preferred_lft forever
清空ipvsadm记录
[root@node2 ~]# ipvsadm -C
配置ipvs,并查看
[root@node2 ~]# ipvsadm -A -t 10.0.0.28:80 -s wrr [root@node2 ~]# ipvsadm -a -t 10.0.0.28:80 -r 10.0.0.22:80 -g -w 1 [root@node2 ~]# ipvsadm -a -t 10.0.0.28:80 -r 10.0.0.23:80 -g -w 1
[root@node2 ~]# ipvsadm -a -t 10.0.0.28:80 -r 10.0.0.24:80 -g -w 1 [root@node2 ~]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 10.0.0.28:80 wrr -> 10.0.0.22:80 Route 1 0 0 -> 10.0.0.23:80 Route 1 0 0
-> 10.0.0.24:80 Route 1 0 0
加入开机启动
[root@node1 ~]# systemctl enable ipvsadm
抑制VIP,防止LVS和RSVIP冲突
[root@node2 ~]# echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
[root@node2 ~]# echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
[root@node2 ~]# echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
[root@node2 ~]# echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
安装keepalived
[root@node1 ~]# yum -y install keepalived
查看版本
[root@node1 ~]# keepalived -v
加入开机启动
[root@node2 ~]# systemctl enable keepalived
配置 keepalived.conf 文件
[root@node1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id wl
}
vrrp_instance VI_1 {
state BACKER
interface eth0
virtual_router_id 51
priority 90
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 111
}
virtual_ipaddress {
10.0.0.28
}
}
virtual_server 10.0.0.28 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 0
protocol TCP
real_server 10.0.0.22 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 10.0.0.23 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 10.0.0.24 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
重启keepalived
[root@node1 ~]# systemctl restart keepalived
搭好以后可以先用10.0.0.20 主机测试,测试无误后将10.0.0.20主机宕掉,再用10.0.0.21主机测试