linux 之 LVS调度
一、 前言
[root@centos7 ~]#grep -i -C 10 ipvs /boot/config-4.18.0-147.el8.x86_64
三、LVS集群中的术语
四、简述lvs四种集群特点及使用场景
1)NAT转发模型
1. 客户端发起请求到load balancer的虚拟ip
2. load banlancer把客户请的目标地址改写为其中一个real server的,源地址改成不变。
3. realserver接受请求,并返回给load banlancer响应
4. load banlancer接受到响应,修改目标地址为不变,源地址改成自己的。
5. 客户端接受loader banlancer的响应
注意:
如果客户端和realserver在同一个网段,不会执行nat转换,realserver直接返回响应,客户端也会拒收此报文。
2)TUNNEL转发模型
1. 客户端发起请求到load balancer的虚拟ip
2. load banlancer把客户的请求包包裹,然后转发给其中的一个real server。
3. realserver接受请求,解包。得到客户端发来的原始包。
4. realserver处理,把结果通过vip直接返回给客户端。
5. 客户端接受real server的响应。
注意:
a. load balancer和realserver 直接通过ip tunnel技术重新封装、解包
b. load balancer和 realserver 使用相同的vip
c. load balnacer和realserver可以不再同一个网络
3)DR模型
1. 客户端发起请求到load balancer的虚拟ip
2. load banlancer把客户发送的包,修改源mac地址为vip的,目的mac地址为realserver的,然后发送给realserver
3. realserver接受请求,并处理,然后把结果通过vip直接返回给客户端。
4. 客户端接受real server的响应。
注意
a. load balancer和 realserver 使用相同的vip
b. load balancer和realserver必须在同一个网络,因为load balancer需要知道realserver的mac地址。
4)LVS的FULLNAT模式
实战部分
实战一、实现LVS-NAT模式
1.网络拓扑图如下
根据网络拓扑我们准备4台机器 客户端:192.168.1.6 LVS服务器:eth1:192.168.1.8 eth0 10.0.0.8 两台RealServer RS1: 10.0.0.7 RS2:10.0.0.8
从左到右配置细节如下
1.客户端配置如下
TYPE="Ethernet" BOOTPROTO="static" IPADDR=192.168.1.6 DEVICE="eth0" ONBOOT="yes"
2.lvs服务器配置
网络配置
eth1
TYPE="Ethernet" BOOTPROTO="static" IPADDR=10.0.0.8 GATEWAY=10.0.0.2 DNS=223.6.6.6 PREFIX=24 DEVICE="eth0" ONBOOT="yes"
TYPE="Ethernet" BOOTPROTO="static" IPADDR=192.168.1.8 PREFIX=24 DEVICE="eth1" ONBOOT="yes"
开启路由转发功能
vim /etc/sysctl.conf net.ipv4.ip_forward = 1 sysctl -p
LVS配置
1.yum install ipvsadm -y
[root@chujiapeng network-scripts]# rpm -ql ipvsadm
/etc/sysconfig/ipvsadm-config
/usr/lib/systemd/system/ipvsadm.service
/usr/sbin/ipvsadm
/usr/sbin/ipvsadm-restore
/usr/sbin/ipvsadm-save
/usr/share/doc/ipvsadm-1.27
/usr/share/doc/ipvsadm-1.27/README
/usr/share/man/man8/ipvsadm-restore.8.gz
/usr/share/man/man8/ipvsadm-save.8.gz
/usr/share/man/man8/ipvsadm.8.gz
配置lvs集群
[root@lvs ~]#ipvsadm -A -t 192.168.1.8:80 -s rr [root@lvs ~]#ipvsadm -a -t 192.168.1.8:80 -r 10.0.0.7 -m [root@lvs ~]#ipvsadm -a -t 192.168.1.8:80 -r 10.0.0.17 -m [root@LVS ~]#ipvsadm -Ln
RS服务器配置
RS1:10.0.0.7
TYPE="Ethernet" BOOTPROTO="static" IPADDR=10.0.0.7 PREFIX=24 DNS1=223.6.6.6 GATEWAY=10.0.0.8 NAME="eth0" DEVICE="eth0" ONBOOT="yes"
RS2:10.0.0.17
TYPE="Ethernet"
BOOTPROTO="static"
IPADDR=10.0.0.17
PREFIX=24
DNS1=223.6.6.6
GATEWAY=10.0.0.8
PEERDNS="yes"
NAME="eth0"
DEVICE="eth0"
ONBOOT="yes"
配置http服务
[root@chujiapeng network-scripts]# yum install httpd;systemctl start httpd
编写测试脚本进行测试
[root@chujiapeng network-scripts]# while true ; do curl http://192.168.1.8/;sleep 1;done
到此测试完成
实战二、描述LVS-DR工作原理,并配置实现。
LVS-DR模式单网段案例
利用别名,快速的改网卡配置脚本 echo "alias eth='cd /etc/sysconfig/network-scripts/'" >>/root/.bashrc; echo "alias netr='systemctl restart network'">>/root/.bashrc; . /root/.bashrc;
1.根据拓扑图从左到右以此配置,首先配置客户端
TYPE="Ethernet" BOOTPROTO="static" IPADDR=192.168.0.6 GATEWAY=192.168.0.200 PREFIX=24 DEVICE="eth0" ONBOOT="yes"
2.路由器配置
eth0
TYPE="Ethernet" BOOTPROTO="static" IPADDR=10.0.0.200 PREFIX=24 DEVICE="eth0" ONBOOT="yes"
==================================================
eth1
TYPE="Ethernet"
BOOTPROTO="static"
IPADDR=192.168.0.200
PREFIX=24
DEVICE="eth1"
ONBOOT="yes
开启路由转发功能 [root@chujiapeng network-scripts]# cat /etc/sysctl.conf net.ipv4.ip_forward = 1
sysctl -p
测试路由器
3.RS配置
RS1 TYPE="Ethernet" BOOTPROTO="static" IPADDR=10.0.0.7 PREFIX=24 DNS1=223.6.6.6 GATEWAY=10.0.0.200 NAME="eth0" DEVICE="eth0" ONBOOT="yes" RS2 TYPE="Ethernet" BOOTPROTO="static" IPADDR=10.0.0.17 PREFIX=24 DNS1=223.6.6.6 GATEWAY=10.0.0.200 NAME="eth0" DEVICE="eth0" ONBOOT="yes" 安装httpd服务器 [root@rs1 ~]#yum -y install httpd [root@rs1 ~]#systemctl enable --now httpd [root@rs1 ~]#hostname -I > /var/www/html/index.html
[root@chujiapeng network-scripts]# curl 10.0.0.17
10.0.0.17
TYPE="Ethernet" BOOTPROTO="static" IPADDR=10.0.0.8 PREFIX=24 GATEWAY=10.0.0.200 DEVICE="eth0" ONBOOT="yes"
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore [root@chujiapeng network-scripts]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore [root@chujiapeng network-scripts]# #echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce [root@chujiapeng network-scripts]# #echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore [root@chujiapeng network-scripts]# #echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce [root@chujiapeng network-scripts]# ifconfig lo:1 10.0.0.100/32 [root@chujiapeng network-scripts]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 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 inet 10.0.0.100/0 scope global lo:1 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:c8:b7:0d brd ff:ff:ff:ff:ff:ff inet 10.0.0.7/24 brd 10.0.0.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fec8:b70d/64 scope link valid_lft forever preferred_lft forever 3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000 link/ether 52:54:00:fc:86:38 brd ff:ff:ff:ff:ff:ff inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0 valid_lft forever preferred_lft forever 4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000 link/ether 52:54:00:fc:86:38 brd ff:ff:ff:ff:ff:ff
同样配置rs2
[root@chujiapeng network-scripts]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@chujiapeng network-scripts]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@chujiapeng network-scripts]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@chujiapeng network-scripts]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@chujiapeng network-scripts]# ifconfig lo:1 10.0.0.100/32
[root@chujiapeng network-scripts]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
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
inet 10.0.0.100/0 scope global lo:1
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:58:e8:6f brd ff:ff:ff:ff:ff:ff
inet 10.0.0.17/24 brd 10.0.0.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe58:e86f/64 scope link
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
link/ether 52:54:00:fc:86:38 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
link/ether 52:54:00:fc:86:38 brd ff:ff:ff:ff:ff:ff
[root@chujiapeng network-scripts]#
1.配置vip
ifconfig lo:1 10.0.0.100/32
2.安装软件
[root@chujiapeng network-scripts]# yum install ipvsadm -y
3.集群配置
[root@chujiapeng network-scripts]# ipvsadm -A -t 10.0.0.100:80 -s rr
[root@chujiapeng network-scripts]# ipvsadm -a -t 10.0.0.100:80 -r 10.0.0.7:80 -g
[root@chujiapeng network-scripts]# ipvsadm -a -t 10.0.0.100:80 -r 10.0.0.17:80 -g
[root@chujiapeng network-scripts]# 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.100:80 rr
-> 10.0.0.7:80 Route 1 0 0
-> 10.0.0.17:80 Route 1 0 0
编写测试脚本进行测试
while true ; do curl http://10.0.0.100/;sleep 1;done
数据传输分析 过程
第一个包
第二个包
第三个包
第四个包
第五个包
第6个包
第7个包
第8个包
到此整个请求过程的包 解析完毕
实战三、实现LVS+Keepalived高可用。
实验架构图
环境快速构建脚本 echo "alias netr='systemctl restart network'" >>~/.bashrc echo "alias yum='yum install -y'" >>~/.bashrc echo "alias eth='cd /etc/sysconfig/network-scripts/'" >>~/.bashrc . ~/.bashrc
环境规划
第一个实验,实现LVS集群VIP漂移
两台lvs服务器实现基于key 验证
配置好之前抓包测试多播地址
keepalived 脚本配置,实现脚本通知功能
#!/bin/bash # contact='root@localhost' notify() { mailsubject="$(hostname) to be $1, vip floating" mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1" echo "$mailbody" | mail -s "$mailsubject" $contact } case $1 in master) notify master ;; backup) notify backup ;; fault) notify fault ;; *) echo "Usage: $(basename $0) {master|backup|fault}" exit 1 ;; esac
! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id ka1 vrrp_mcast_group4 224.100.100.100 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 66 priority 80 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.37.100/24 dev eth0 label eth0:1 } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" }
下图是实现效果
实现lvs的独立日志功能
配置日志
邮件通知邮箱演示
配置新浪邮箱一直没成功,最后改成qq邮箱
测试
双主实现
实现keepalived 的lvs 功能
把37 服务断开 检查 切换
到此 实验结束,实现了lvs的高可用
posted on 2021-04-17 14:05 jiapengchu 阅读(206) 评论(0) 编辑 收藏 举报