一.LVS ipvsadm 命令的使用
1.安装LVS管理软件
[root@lvs ~]# yum install -y ipvsadm
ipvsadm
命令是Linux Virtual Server(LVS)在应用层的管理命令,用于管理LVS的配置和策略规则。以下是对ipvsadm
命令语法的详细解析:
命令/选项 | 描述 | 示例 |
---|---|---|
-A 或 --add-service | 添加一个新的虚拟服务 | ipvsadm -A -t 192.168.1.100:80 -s rr |
-D 或 --delete-service | 删除一个虚拟服务 | ipvsadm -D -t 192.168.1.100:80 |
-E 或 --edit-service | 编辑一个现有的虚拟服务 | 通常需要具体参数,但直接编辑较为复杂,建议使用删除后重新添加 |
-R 或 --restore | 从标准输入中恢复策略规则 | ipvsadm -R < rules.txt (假设rules.txt包含策略规则) |
-S 或 --save | 保存策略规则到标准输出 | ipvsadm -S > rules.txt (将规则保存到rules.txt) |
-a 或 --add-destination | 向虚拟服务中添加一个真实服务器 | ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.101:8080 -m -w 1 |
-d 或 --delete-destination | 从虚拟服务中删除一个真实服务器 | ipvsadm -d -t 192.168.1.100:80 -r 192.168.1.101:8080 |
-e 或 --edit-destination | 编辑虚拟服务中的一个真实服务器 | 通常需要具体参数,但直接编辑较为复杂,建议使用删除后重新添加 |
-L 或 --list | 列出所有虚拟服务和真实服务器 | ipvsadm -L -n |
-Z 或 --zero | 将所有统计计数器清零 | ipvsadm -Z |
-n 或 --numeric | 以数字格式输出地址和端口 | ipvsadm -L -n |
-t | 指定TCP协议的虚拟服务地址和端口号 | -t 192.168.1.100:80 |
-u | 指定UDP协议的虚拟服务地址和端口号 | -u 192.168.1.100:53 |
-f | 使用防火墙标记(Firewall Mark)来指定服务 | -f 123 (假设123是iptables的mark值) |
-s | 指定调度算法,如rr(轮询)、wrr(加权轮询)等 | -s rr |
-p | 设置持久连接的超时时间(秒) | -p 300 |
-M | 设置网络掩码,用于持久连接的粒度控制 | -M 255.255.255.0 |
-r | 指定真实服务器的IP地址和端口号 | -r 192.168.1.101:8080 |
-g | 指定LVS的工作模式为直接路由(DR)模式 | -g |
-i | 指定LVS的工作模式为隧道(TUN)模式 | -i |
-m | 指定LVS的工作模式为网络地址转换(NAT)模式 | -m |
-w | 指定真实服务器的权重 | -w 1 |
--set | 设置TCP、TCPFIN、UDP连接的超时时间 | --set tcp 3600 tcpfin 120 udp 600 |
--start-daemon | 启动连接同步守护进程 | --start-daemon master |
--stop-daemon | 停止连接同步守护进程 | --stop-daemon master |
-h 或 --help | 显示帮助信息 | ipvsadm -h |
二.LVS NAT 模式实践
1.准备三台虚拟机
#新环境记得关闭防火墙和selinux。
角色 | 主机名 | 网络模式 | 外网 IP 地址 | 内网 IP 地址 | 网关 |
---|---|---|---|---|---|
director lb | lvs | nat 模式/仅主机 | 192.168.73.128 | 192.168.239.254 | 无需设置网关 |
real server1 | web1 | 仅主机模式 | 192.168.239.100 | 192.168.239.254 | |
real server2 | web2 | 仅主机模式 | 192.168.239.200 | 192.168.239.254 |
2.web服务器安装nginx
[root@web1 ~]# yum install -y epel-release.noarch
[root@web1 ~]# yum insatll -y nginx
[root@web1 ~]# echo 100 > /usr/share/nginx/html/index.html
[root@web1 ~]# systemctl start nginx
[root@web2 ~]# yum install -y epel-release.noarch
[root@web2 ~]# yum insatll -y nginx
[root@web2 ~]# echo 200 > /usr/share/nginx/html/index.html
[root@web2 ~]# systemctl start nginx
3.配置仅主机模式的网卡
#配置lvs的网卡,网关可以删掉
[root@lvs ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens36
DEVICE=ens36 #记得改网卡名称
BOOTPROTO=static #静态ip
ONBOOT=yes
IPADDR=192.168.239.254 #这里要和web端的网卡对应上
NETMASK=255.255.255.0
DNS1=8.8.8.8
#配置web1网卡
[root@web1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
DEVICE=ens33
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.239.200
NETMASK=255.255.255.0
GATEWAY=192.168.239.254 #网关要和lvs的ip对应上
DNS1=8.8.8.8
#配置web2网卡
[root@web2 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
DEVICE=ens33
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.239.100
NETMASK=255.255.255.0
GATEWAY=192.168.239.254 #和lvs端ip对应上
DNS1=8.8.8.8
4.配置lvs
#安装ipvsadm
[root@lvs ~]# yum install -y ipvsadm
#创建 ipvsadm 配置文件
[root@lvs ~]# touch /etc/sysconfig/ipvsadm
#开启内核转发
[root@lvs ~]# sysctl -w net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1
[root@lvs ~]# sysctl -p
[root@lvs ~]# echo net.ipv4.ip_forward = 1 >> /etc/sysctl.conf #持久化设置
#启动 ipvsadm 并验证
[root@lvs ~]# systemctl start ipvsadm
#创建 ipvsadm 规则
[root@lvs ~]# ipvsadm -C #新环境可以—C清空所有规则
[root@lvs ~]# ipvsadm -A -t 192.168.73.128:80 -s rr #rr表示轮询算法
[root@lvs ~]# ipvsadm -a -t 192.168.73.128:80 -r 192.168.239.100 -m #-m表示nat
[root@lvs ~]# ipvsadm -a -t 192.168.73.128:80 -r 192.168.239.200 -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.73.128:80 rr
-> 192.168.239.100:80 Masq 1 0 7
-> 192.168.239.200:80 Masq 1 0 7
# Forward: 转发方式,当前是地址伪装转发
# Masq 转发"可能是指“MASQUERADE转发”,在iptables中,MASQUERADE是一种地址伪装(IP伪装)的方式,它会自动使用出口IP接口的IP地址来转发数据包。
# Weight:轮询权重
# ActiveConn:是活动连接数,也就是tcp连接状态的ESTABLISHED;
# InActConn: 指非活跃连接数,将处于 TCP ESTABLISH 状态以外的连接都称为不活跃连接。例如处于 SYN_RECV 状态的连接,处于 TIME_WAIT 状态的连接等。
#统计自该条转发规则生效以来的包
[root@lvs ~]# ipvsadm -L --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes
-> RemoteAddress:Port
TCP lvs:http 16 95 56 6232 6356
-> 192.168.239.100:http 8 49 28 3206 3178
-> 192.168.239.200:http 8 46 28 3026 3178
# Conns (connections scheduled) : 已经转发过的连接数
# InPkts (incoming packets) :入包个数
# OutPkts (outgoing packets) : 出包个数
# InBytes (incoming bytes) :入流量(字节)
# OutBytes (outgoing bytes) : 出流量(字节)
# 显示速率信息
[root@lvs ~]# ipvsadm -L --rate
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port CPS InPPS OutPPS InBPS OutBPS
-> RemoteAddress:Port
TCP lvs:http 0 0 0 0 0
-> 192.168.239.100:http 0 0 0 0 0
-> 192.168.239.200:http 0 0 0 0 0
# CPS (current connection rate) :每秒连接数
# InPPS (current in packet rate) : 每秒的入包个数
# OutPPS (current out packet rate): 每秒的出包个数
# InBPS (current in byte rate) : 每秒入流量(字节)
# OutBPS (current out byte rate) 每秒入流量(字节)
[root@lvs ~]# curl 192.168.73.128:80
100
[root@lvs ~]# curl 192.168.73.128:80
200
[root@lvs ~]# for i in $(seq 10); do curl 192.168.152.166:80;done
100
200
100
200
100
200
100
200
100
200