grep -i -C 10 “ipvs” /boot/config-VERSION-RELEASE.x86_64 #查看内核是否支持IPVS
LVS命令使用
例:ipvsadm -A -t 192.168.64.7:80 -s wlc
ipvsadm -a -t 192.168.64.7:80 -r 192.168.64.107:80 -m -w 1
-L|-l –list 显示内核虚拟服务器表
-Z –zero 虚拟服务表计数器清零(清空当前的连接数量等)
NAT模式
LVS+keepalived
准备三台虚拟机
一台做LVS调度器,两台做后端服务器,四台最好,还有一台可做客户端
环境说明
HOST | OS | hostname | GATEWAY |
---|---|---|---|
192.168.217.136 | centos7 | LVS | 192.168.217.2 |
192.168.217.137 | centos7 | r1 | 192.168.217.136 |
192.168.217.138 | centos7 | r2 | 192.168.217.136 |
不懂???那就结束吧,
好吧,开始配置,
为了好辨认
192.168.217.136 改名为lvs这台机子有两个网卡,
192.168.217.137 改名为r1
192.168.217.138 改名为r2
1.增加一块外网网卡
2.配置/etc/sysconfig/network-scripts/ifcfg-
这里新添加的网卡名为ens37
配置文件
[root@lvs ~]# ifconfig
[root@lvs ~]# cp /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-ens37
[root@lvs ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens37
配置ip
[root@lvs ~]# systemctl restart network
[root@lvs ~]# ifconfig
3.安装ipvsadm
yum安装就很快
[root@lvs ~]# yum install -y ipvsadm
编译安装
wget …
tar xvf …
这里没有configure,自己解决吧、
make && make install
4.开启路由转发、关闭ICMP重定向、防火墙规则
[root@lvs ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
[root@lvs ~]# echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects
[root@lvs ~]# echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects
[root@lvs ~]# echo 0 > /proc/sys/net/ipv4/conf/eth0/send_redirects
[root@lvs ~]# echo 0 > /proc/sys/net/ipv4/conf/eth1/send_redirects
[root@lvs ~]# iptables -t nat -F
[root@lvs ~]# iptables -t nat -X
[root@lvs ~]# iptables -t nat -A POSTROUTING -s 192.168.56.0/24 -j MASQUERADE
5.配置ipvsadm
[root@lvs ~]# ipvsadm -C 清空
[root@lvs ~]# ipvsadm -A -t 192.168.124.29:80 -s rr
[root@lvs ~]# ipvsadm -a -t 192.168.124.29:80 -r 192.168.217.137:80 -m -w 1
[root@lvs ~]# ipvsadm -a -t 192.168.124.29:80 -r 192.168.217.138:80 -m -w 1
[root@lvs ~]# ipvsadm -L -n
查看
参数说明:
-A:表示增加一个虚拟服务
-a:表示增加一个real server
-t:表示TCP服务
-r:指定real server的ip地址
-s:指定调度算法
-m:选择NAT方式调度(-i tun模式、-g dr模式)
-w:指定权重
1.下载httpd
[root@r1 ~]# yum install -y httpd
[root@r1 ~]# systemctl start httpd
2.创建index.html文件
[root@r1 ~]# vim /var/www/html/index.html
写入
3.修改/etc/sysconfig/network-scripts/ifcfg-ens33中的网关
[root@r1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
修改如下
r2同理
到这基本上就完成配置了。
在lvs上操作或者在客户端
[root@lvs ~]# curl 192.168.124.29
[root@lvs ~]# curl 192.168.124.29
[root@lvs ~]# watch -n 1 ipvsadm -L -n #动态查看访问记录
测试结果
动态查看访问记录
环境就不说了,开始配置吧
1.清空ipvsadm规则
[root@lvs ~]# ipvsadm -C
2.关掉ens37
[root@lvs ~]# ifdown ens37
[root@lvs ~]# ifconfig
3.配置VIP 192.168.217.200
[root@lvs ~]# ifconfig ens33:1 192.168.217.200/24 up
[root@lvs ~]# ifconfig ens33:1
4.配置ipvsadm
[root@lvs ~]# ipvsadm -A -t 192.168.217.200:80 -s rr
[root@lvs ~]# ipvsadm -a -t 192.168.217.200:80 -r 192.168.217.137 -g -w 1
[root@lvs ~]# ipvsadm -a -t 192.168.217.200:80 -r 192.168.217.138 -g -w 1
[root@lvs ~]# ipvsadm -ln
1.修改/etc/sysconfig/network-scripts/ifcfg-ens33
[root@r1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
将网关改回来GATEWAY=192.168.217.2
[root@r1 ~]# systemctl restart network
2.在RS节点上的lo(网卡的回环接口)接口上配置vip(192.168.217.200)
[root@r1 ~]# ifconfig lo:1 192.168.217.200/32 up
[root@r1 ~]# ifconfig lo:1
3.在RS节点上抑制ARP响应 (RS1和RS2上分别执行)
[root@r1 ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@r1 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@r1 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@r1 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
r2操作同理
[root@localhost ~]# curl 192.168.217.200
hello 192.168.217.137
[root@localhost ~]# curl 192.168.217.200
hello 192.168.217.138
[root@localhost ~]# curl 192.168.217.200
hello 192.168.217.137
[root@localhost ~]# curl 192.168.217.200
hello 192.168.217.138
在现实的应用中,LVS得到了大量的部署,请参考 http://www.linuxvirtualserver.org/deployment.html
(1)在realserver上部署Nginx并配置主页
[root@rs1 ~]# yum install -y nginx
[root@rs2 ~]# yum install -y nginx
[root@rs1 ~]# echo "welcome to use RS1 192.168.217.137" > /usr/share/nginx/html/index.html
[root@rs2 ~]# echo "welcome to use RS2 192.168.217.138
" > /usr/share/nginx/html/index.html
[root@rs1 ~]# curl 192.168.217.137
welcome to use RS1 192.168.217.137
[root@rs2 ~]# curl 192.168.217.138
welcome to use RS2 192.168.217.138
(2)在rs1和rs2上编辑realserver脚本并执行,此处贴rs1脚本详情
[root@rs1 ~]# vim /etc/init.d/realserver
#!/bin/bash
SNS_VIP=192.168.217.200
/etc/init.d/functions
case "$1" in
start)
ifconfig lo:1 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
/sbin/route add -host $SNS_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
echo "RealServer Start OK"
;;
stop)
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
[root@rs1 ~]# chmod +x /etc/init.d/realserver
[root@rs1 ~]# /etc/init.d/realserver start
RealServer Start OK
(1)lvs和lvsbackup上安装Keepalived
[root@lvs ~]# yum install -y keepalived
[root@lvsbackup ~]# yum install -y keepalived
(2)编辑lb01和lb02上的keepalived.conf配置文件
[root@lvs ~]# cp /etc/keepalived/keepalived.conf{,.bak} #备份源文件
[root@lvs ~]# > /etc/keepalived/keepalived.conf #清空源文件
[root@lvs ~]# vim /etc/keepalived/keepalived.conf #编辑keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id lvs
}
vrrp_instance VI_1 {
state MASTER #指定Keepalived的角色,MASTER为主,BACKUP为备
interface ens33
virtual_router_id 55 #虚拟路由id号,主备需要一直
priority 150 #优先级,数值越大,优先级越高
advert_int 1 #检测间隔,默认为1s
authentication {
auth_type PASS #认证类型
auth_pass 1111 #认证密码
}
virtual_ipaddress {
192.168.217.200/24 #设置DR的虚拟ip,可以多设,一行一个
}
}
virtual_server 192.168.217.200 80 { #定义LVS对外提供服务的VIP为192.168.217.200和port为80
delay_loop 6 #设置健康检查时间,单位为秒
lb_algo wrr #设置负载均衡调度算法为wrr
lb_kind DR #设置LVS实现负载均衡机制为DR模式
nat_mask 255.255.255.0
persistence_timeout 20 #会话保持超时配置
protocol TCP #使用TCP协议检查realserver的状态
real_server 192.168.217.137 80 { #配置真实服务器节点和端口
weight 100 #权重
TCP_CHECK {
connect_timeout 10 #连接超时,单位为秒
nb_get_retry 3 #重试连接次数
connect_port 80 #连接端口
}
}
real_server 192.168.217.138 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
connect_port 80
}
}
}
从负载均衡服务器与主负载服务器大致相同,只是在keepalived的配置文件中需要改以下两处:
(1)将state由MASTER改为BACKUP
(2)将priority由150改为120
(3)将router_id由lvs改为lvsbackup
配置完成后,启动Keepalived
[root@lvs ~]# systemctl start keepalived
[root@lvsbackup ~]# systemctl start keepalived
[root@lvs ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.217.200:80 wrr persistent 20
-> 192.168.217.137:80 Route 100 0 0
-> 192.168.217.138:80 Route 100 0 0
[root@lvsbackup ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.217.200:80 wrr persistent 20
-> 192.168.217.137:80 Route 100 0 0
-> 192.168.217.138:80 Route 100 0 0
[root@lvs ~]# ip addr |grep 192.168.217.200 #查看lb01上是否存在VIP
inet 192.168.217.200/24 scope global secondary ens33
[root@lvsbackup ~]# ip addr |grep 192.168.217.200 #查看lb02上是否存在VIP,如果有说明存在脑裂
(1)指定请求的均衡转发:因为两个Web服务器的权重都一样,所以会依次转发给两个Web服务器
(2)Web服务器发生故障时
模拟停止192.168.217.138,暂停其Nginx服务,再进行访192.168.217.200,可以看到只会从137上获取页面
[root@r2 ~]# /etc/init.d/nginx stop
Stopping nginx: [ OK ]
从负载均衡器的状态监控上,也可以看到192.168.217.138这台服务器已经从集群中剔除,当故障修复后,再查看调度器状态信息,可以看到rs2已经重新加入集群当中
[root@lvs ~]# ipvsadm -L -n #lb01上查看调度信息,可以看到rs2已经从集群中剔除
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.217.200:80 wrr persistent 20
-> 192.168.217.137:80 Route 100 1 0
[root@r2 ~]# /etc/init.d/nginx start #重启rs2上的nginx服务
Starting nginx: [ OK ]
[root@lvs ~]# ipvsadm -L -n #可以看到rs2又重新加入到集群当中提供服务
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.217.200:80 wrr persistent 20
-> 192.168.217.137:80 Route 100 1 0
-> 192.168.217.138:80 Route 100 1 0
(3)主负载均衡服务器发生故障时,备机立即充当主机角色提供请求转发服务
模拟停止lb01上的keepalived服务,可以看到lb01上的vip漂移到了lb02上,但继续访问vip却不受影响。当主负载均衡器(lb01)服务恢复时,vip又会重新漂移到主负载均衡器上(lb01)
[root@lvs ~]# ip addr |grep 192.168.217.200
inet 192.168.217.200/24 scope global secondary ens33
[root@lvs ~]# systemctl stop keepalived
[root@lvs ~]# ip addr |grep 192.168.217.200
[root@lvsbackup ~]# ip addr |grep 192.168.217.200
inet 192.168.217.200/24 scope global secondary ens33
此时依然正常运行