~LVS的nat模式+ipvsadm命令的使用+lvs的dr模式实现+lvs火墙标记

lvs简介

LVS:Linux Virtual Server,负载调度器,内核集成,章文嵩,阿里的四层SLB(Server LoadBalance)是基 于LVS+keepalived实现

lvs集群的类型

lvs-nat: 修改请求报文的目标IP,多目标IP的DNAT

lvs-dr: 操纵封装新的MAC地址 lvs-tun: 在原请求IP报文之外新加一个IP首部

lvs-fullnat: 修改请求报文的源和目标IP

一、lvs的nat模式

lvs是nat模式

复制代码
[root@lvs ~]# cat /etc/NetworkManager/system-connections/eth1.connection 
[connection]
id=eth1
type=ethernet
interface-name=eth1
 
[ipv4]
address1=192.168.0.100/24
method=manual

克隆两个名为webserver1和webserver2,且为仅主机模式

webserver1

复制代码
[root@webserver1 ~]# cat /etc/NetworkManager/system-connections/eth0.connection 
[connection]
id=eth0
type=ethernet
interface-name=eth0
 
[ipv4]
address1=192.168.0.10/24,192.168.0.100
method=manual

webserver2

复制代码
[root@webserver2 ~]# cat /etc/NetworkManager/system-connections/eth0.connection 
[connection]
id=eth0
type=ethernet
interface-name=eth0
 
[ipv4]
address1=192.168.0.20/24,192.168.0.100
method=manual

网关设不设不影响实验

搭建nat模式

别忘了下载web服务

在webserver里配置

复制代码
echo webserver1-192.168.0.10 > /var/www/html/index.html       
 
echo webserver1-192.168.0.20 > /var/www/html/index.html         

再下载ipvsadm

ipvsadm命令 核心功能: 集群服务管理:增、删、改 集群服务的RS管理:增、删、改 查看

接着配置规则

复制代码
ipvsadm -A -t 172.25.254.100:80 -s rr
ipvsadm -a -t 172.25.254.100:80 -r 192.168.0.10 -m
ipvsadm -a -t 172.25.254.100:80 -r 192.168.0.20 -m
ipvsadm -Ln
watch -n 1 ipvsadm -Ln --rate

测试结果

二、 ipvsadm命令的使用

命令参数

复制代码
管理集群服务
ipvsadm -A|E -t(tcp)|u(udp)|f(防护墙标签) \
service-address(集群地址) \
[-s scheduler(调度算法)] \
[-p [timeout]] \
[-M netmask] \
[--pepersistence_engine] \
[-b sched-flags]
ipvsadm -D -t|u|f service-address 删除
ipvsadm --C 清空
ipvsadm --R 重载
ipvsadm -S [-n] 保存
管理集群中的real server
ipvsadm -a|e -t|u|f service-address -r server-address [-g | -i| -m](工作模式) [-w 
weight](权重)
ipvsadm -d -t|u|f service-address -r server-address 删除RS
ipvsadm -L|l [options] 查看rs
ipvsadm -Z [-t|u|f service-address] 清楚计数器

三、lvs的dr模式实现

DR:Direct Routing,直接路由,LVS默认模式,应用最广泛,通过为请求报文重新封装一个MAC首部进行 转发,源MAC是DIP所在的接口的MAC,目标MAC是某挑选出的RS的RIP所在接口的MAC地址;源 IP/PORT,以及目标IP/PORT均保持不变

lvs主机

复制代码
[root@lvs ~]# cat /etc/NetworkManager/system-connections/eth0.connection 
[connection]
id=eth0
type=ethernet
interface-name=eth0
 
[ipv4]
address1=192.168.0.50/24,192.168.0.100
method=manual

webserver1

复制代码
[root@server1 ~]# cat /etc/NetworkManager/system-connections/eth0.connection
[connection]
id=eth0
type=ethernet
interface-name=eth0
 
[ipv4]
address1=192.168.0.10/24,192.168.0.100
method=manual

webserver2

复制代码
[root@server2 ~]# cat /etc/NetworkManager/system-connections/eth0.connection 
[connection]
id=eth0
type=ethernet
interface-name=eth0
 
[ipv4]
address1=192.168.0.20/24,192.168.0.100
method=manual

路由器主机(router)

复制代码
[root@router ~]# cat /etc/NetworkManager/system-connections/eth0.connection 
[connection]
id=eth0
type=ethernet
interface-name=eth0
 
[ipv4]
address1=172.25.254.100/24,172.25.254.2
dns=114.114.114.114;
method=manual

[root@router ~]# cat /etc/NetworkManager/system-connections/eth1.connection 
[connection]
id=eth1
type=ethernet
interface-name=eth1
 
[ipv4]
address1=192.168.0.100/24
method=manual

客户端nat模式(client)

复制代码
[root@client ~]# cat /etc/NetworkManager/system-connections/eth0.connection 
[connection]
id=eth0
type=ethernet
interface-name=eth0
 
[ipv4]
address1=172.25.254.200/24,172.25.254.100
method=manual

之后打开内核路由功能

rs主机中vip不对外响应

在lvs中配置策略

复制代码
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore 
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce 
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce 
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore 

lvs设定:

复制代码
ipvsadm -A -t 192.168.0.200:80 -s wrr
ipvsadm -a -t 192.168.0.200:80 -r 192.168.0.10:80 -g -w 1
ipvsadm -a -t 192.168.0.200:80 -r 192.168.0.20:80 -g -w 2

别忘了启用IP转发

复制代码
vim /etc/sysctl.conf           
net.ipv4.ip_forward = 1     
 
ipvsadm -p

在lvs主机和rs主机添加vip

测试效果

复制代码
[root@client ~]# for N in {1..10};
do 
curl 192.168.0.200;
done
RS2 server - 192.168.0.20
RS1 server - 192.168.0.10
RS2 server - 192.168.0.20
RS2 server - 192.168.0.20
RS1 server - 192.168.0.10
RS2 server - 192.168.0.20
RS2 server - 192.168.0.20
RS1 server - 192.168.0.10
RS2 server - 192.168.0.20
RS2 server - 192.168.0.20

四、lvs火墙标记

复制代码
]# yum install mod_ssl -y
]# systemctl restart httpd

[root@lvs ~]# ipvsadm -A -t 192.168.0.100:80 -s rr
[root@lvs ~]# ipvsadm -A -t 192.168.0.100:443 -s rr
[root@lvs ~]# ipvsadm -a -t 192.168.0.100:80 -r 192.168.0.101:80 -g
[root@lvs ~]# ipvsadm -a -t 192.168.0.100:80 -r 192.168.0.102:80 -g
[root@lvs ~]# ipvsadm -a -t 192.168.0.100:443 -r 192.168.0.102:80 -g
[root@lvs ~]# ipvsadm -a -t 192.168.0.100:443 -r 192.168.0.101:80 -g

测试

复制代码
[root@node10 ~]# curl http://192.168.0.100;curl -k https://192.168.0.100
RS1 server - 192.168.0.101
RS1 server - 192.168.0.101
相关推荐
Johny_Zhao1 小时前
Docker + CentOS 部署 Zookeeper 集群 + Kubernetes Operator 自动化运维方案
linux·网络安全·docker·信息安全·zookeeper·kubernetes·云计算·系统运维
小毛驴8502 小时前
Linux 后台启动java jar 程序 nohup java -jar
java·linux·jar
好好学习啊天天向上3 小时前
世上最全:ubuntu 上及天河超算上源码编译llvm遇到的坑,cmake,ninja完整过程
linux·运维·ubuntu·自动性能优化
tan180°4 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
典学长编程4 小时前
Linux操作系统从入门到精通!第二天(命令行)
linux·运维·chrome
wuk9985 小时前
基于MATLAB编制的锂离子电池伪二维模型
linux·windows·github
snoopyfly~7 小时前
Ubuntu 24.04 LTS 服务器配置:安装 JDK、Nginx、Redis。
java·服务器·ubuntu
独行soc7 小时前
#渗透测试#批量漏洞挖掘#HSC Mailinspector 任意文件读取漏洞(CVE-2024-34470)
linux·科技·安全·网络安全·面试·渗透测试
BD_Marathon8 小时前
Ubuntu下Tomcat的配置
linux·ubuntu·tomcat
饥饿的半导体8 小时前
Linux快速入门
linux·运维