文章目录
实验准备
client | 192.168.4.10 |
---|---|
lvs | 192.168.2.20/192.168.4.20 |
lvs1 | 192.168.4.30 |
web1 | 192.168.2.200/192.168.4.200 |
web2 | 192.168.2.200/192.168.4.200 |
本文2个实验(centos7)
1:lvsNAT实验
所用机器:client+lvs+web1+web2
2:lvsDR+keepalived实验
所用机器:client+lvs+lvs1+web1+web2
一、什么是lvs集群
LVS 是 Linux Virtual Server 的简写,意即 Linux虚拟服务器 ,是一个虚拟的服务器集群系统。
lvs工作模式有
NAT:网络地址转换
DR:路由模式
TUN:隧道模式
调度算法:支持的有10种,常见的4种
轮询rr:Real Server轮流提供服务
加权轮询wrr:Real Server根据权重,轮流提供服务
最少连接LC:根据Real Server的连接数数,分配请求
加权最少连接WLC:类似于wrr,给不同的服务器分配不同的权重
二、lvs+NAT实验
1.web1和web2安装httpd服务(关闭防火墙,selinux)
yum -y install httpd (2台机器)并能访问http
2.web1和web2配置网关
web1和web2同时做
因为lvs有2个地址把2.20当成网卡可以访问4.0网段的内容
[root@web1 ~]# nmcli connection modify eth1 ipv4.gateway 192.168.2.20
[root@web1 ~]# ifdown eth1; ifup eth1
[root@web1 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.2.20 0.0.0.0 UG 100 0 0 eth1
192.168.2.0 0.0.0.0 255.255.255.0 U 100 0 0 eth1
3.配置lvs
开启lvs路由转发功
[root@lvs ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@lvs ~]# sysctl -p //使配置文件生效
net.ipv4.ip_forward = 1
安装lvs
yum install -y ipvsadm
ipvsadm语法
-A:添加虚拟服务器(虚拟IP)
-E:编辑虚拟服务器
-t|u:tcp或udp协议
-s:指定调度算法,如rr/wrr/lc/wlc
-a: 创建虚拟服务器后,向虚拟服务器中加入真实服务器
-r:真实服务器地址
-w: 设置权重,默认是1
-m:指定LVS的工作模式是NAT
-g:指定LVS的工作模式是DR
lvs NET配置
[root@lvs ~]# ipvsadm -A -t 192.168.4.20:80 -s rr //创建虚拟服务器调度算法是轮询
[root@lvs ~]# ipvsadm -a -t 192.168.4.20:80 -r 192.168.2.100 -w 1 -m //向虚拟服务器添加真实服务器web1 权重为1 nat模式
[root@lvs ~]# ipvsadm -a -t 192.168.4.20:80 -r 192.168.2.200 -w 2 -m
[root@lvs ~]# 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.4.20:80 rr
-> 192.168.2.100:80 Masq 1 0 0
-> 192.168.2.200:80 Masq 2 0 0
[root@lvs ~]# systemctl stop firewalld.service 关闭防火墙
客户机访问lvs调度器
三、lvs路由模式+keepalived项目实战
LVS的DR模式,LVS只需要一块网卡(4.0网段)
VIP:虚拟地址,提供给用户访问的地址
keepalived:用于实现高可用集群
1.配置web1,web2的vip:192.168.4.40(2台web机配置一样)
[root@web1 network-scripts]# cd /etc/sysconfig/network-scripts/
[root@web1 network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@web1 network-scripts]# vim ifcfg-lo:0
DEVICE=lo:0 //lo是本地
IPADDR=192.168.4.40 //配置虚拟地址
NETMASK=255.255.255.255 //这里一定要255.255.255.255否则出错
NETWORK=192.168.4.40
BROADCAST=192.168.4.40
ONBOOT=yes
NAME=lo:0
2.在2台web服务器上修改内核参数,使得它们不响应vip的查询请求
[root@web1 network-scripts]# vim /etc/sysctl.conf
# System default settings live in /usr/lib/sysctl.d/00-system.conf.
# To override those settings, enter new settings here, or in an /etc/sysctl.d/<name>
.conf file
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
[root@web1 network-scripts]# sysctl -p //配置生效
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
3. lvs安装keepalived
yum install -y keepalived
yum -y install ipvsadm
配置文件
[root@lvs ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost //收件人地址
}
notification_email_from admin@tedu.cn //发件人地址
smtp_server 127.0.0.1 //邮件服务器地址
smtp_connect_timeout 30
router_id lvs //唯一标别ID
vrrp_iptables // 自动开启IPTABLES放行规则
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.4.40/24 // VIP地址
}
}
virtual_server 192.168.4.40 80 { // LVS虚拟服务器
delay_loop 6 //健康检查延迟6秒
lb_algo rr // 调度算法
lb_kind DR // 工作模式
persistence_timeout 50 // 50秒内相同客户端调度到相同服务器
protocol TCP
real_server 192.168.4.100 80 { # real server配置
weight 1 // 权重
TCP_CHECK { // 对real server健康检查的配置
connect_timeout 3 //连接超时时间
nb_get_retry 3 //健康检查重试次数
delay_before_retry 3 //两次检查的间隔
}
}
real_server 192.168.4.200 80 { // real server配置
weight 1 // 权重
TCP_CHECK { // 对real server健康检查的配置
connect_timeout 3 // 连接超时时间
nb_get_retry 3 //健康检查重试次数
delay_before_retry 3 //两次检查的间隔
}
}
}
启动服务查看配置
systemctl start keepalived.service
客户端访问
[root@client ~]# curl 192.168.4.40
woshiweb2
[root@client ~]# curl 192.168.4.40
woshiweb2
[root@client ~]# curl 192.168.4.40
woshiweb2
[root@client ~]# curl 192.168.4.40
woshiweb2
[root@client ~]# curl 192.168.4.40
woshiweb2
4. lvs1安装keepalived
yum install -y keepalived
yum -y install ipvsadm
配置文件
[root@lvs1~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from admin@tedu.cn
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id lvs1 //主机名
vrrp_iptables
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP //这里改备份模式
interface eth0
virtual_router_id 51
priority 80 //优先级要比lvs低
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.4.40/24
}
}
virtual_server 192.168.4.40 80{
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.4.100 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.4.200 80 {
weight 2
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
启动服务
systemctl start keepalived.service
关机lvs 客户端访问