Linux学习之路-集群及LVS24---20180213
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux学习之路-集群及LVS24---20180213相关的知识,希望对你有一定的参考价值。
一、Cluster概念
系统扩展方式:
Scale UP:向上扩展,增强
Scale Out:向外扩展,增加设备,调度分配问题,Cluster
Cluster
集群,为解决某个特定问题将多台计算机组合起来形成的单个系统
Linux Cluster类型:
LB:Load Balancing,负载均衡
HA:High Availiablity,高可用,SPOF(single Point Of failure)(单点失败)
MTBF:Mean Time Between Failure 平均无故障时间
MTTR:Mean Time To Restoration( repair)平均恢复前时间
A=MTBF/(MTBF+MTTR)
(0,1):99%, 99.5%, 99.9%, 99.99%, 99.999%, 99.9999%
HPC:High-performance computing,高性能 www.top500.org
分布式系统:
分布式存储:云盘
分布式计算:hadoop,Spark
1、Cluster分类
LB Cluster的实现
硬件
F5 Big-IP
Citrix Netscaler
A10 A10
软件
lvs:Linux Virtual Server
nginx:支持四层调度
haproxy:支持四层调度
ats:apache traffic server,yahoo捐助
perlbal:Perl 编写
pound
基于工作的协议层次划分
传输层(通用):DPORT
LVS:
nginx:stream(语法格式)
haproxy:mode tcp
应用层(专用):针对特定协议,自定义的请求模型分类
proxy server:
http:nginx, httpd, haproxy(mode http), ...
fastcgi:nginx, httpd, ...
mysql:mysql-proxy, ...(读写分离)
2、Cluster相关
会话保持:负载均衡
(1) session sticky(粘滞位):同一用户调度固定服务器
Source IP:LVS sh算法(对某一特定服务而言,弊端就是基于SNAT模式访问服务器的,对服务器造成负责较大)
Cookie
(2) session replication:每台服务器拥有全部session
session multicast cluster
(3) session server:专门的session服务器(专门存储session信息)
Memcached,Redis支持session server的软件
HA集群实现方案
keepalived:vrrp协议
ais:应用接口规范
heartbeat
cman+rgmanager(RHCS)
coresync_pacemaker
二、LVS介绍
1、LVS概念
LVS:Linux Virtual Server,负载调度器,现在已经集成内核中,章文嵩 阿里
官网:http://www.linuxvirtualserver.org/
VS: Virtual Server(虚拟服务器),负责调度
RS: Real Server,负责真正提供服务
L4:四层路由器或交换机(基于TCP、UDP端口号、基于ip地址调度)
工作原理:VS根据请求报文的目标IP和目标协议及端口将其调度转发至某RS,根据调度算法来挑选RS
iptables/netfilter:
iptables:用户空间的管理工具
netfilter:内核空间上的框架
流入:PREROUTING --> INPUT
流出:OUTPUT --> POSTROUTING
转发:PREROUTING --> FORWARD --> POSTROUTING
DNAT:目标地址转换; PREROUTING
2、LVS集群体系结构
3、lvs集群类型中的术语
VS:Virtual Server,Director Server(DS)(导演服务器)
Dispatcher(调度器),Load Balancer(负载均衡器)
RS:Real Server(lvs), upstream server(nginx)(上游服务器)
backend server(haproxy)(后端服务器)
CIP:Client IP
VIP: Virtual serve IP VS外网的IP
DIP: Director IP VS内网的IP
RIP: Real server IP
访问流程:CIP <--> VIP == DIP <--> RIP
lvs: ipvsadm/ipvs
ipvsadm:用户空间的命令行工具,规则管理器用于管理集群服务及RealServer
ipvs:工作于内核空间netfilter的INPUT钩子上的框架
三、lvs集群的类型
1、lvs集群的类型
lvs-nat:修改请求报文的目标IP,多目标IP的DNAT(端口映射)
lvs-dr:操纵封装新的MAC地址
lvs-tun:在原请求IP报文之外新加一个IP首部
lvs-fullnat:修改请求报文的源和目标IP
2、lvs-nat模式
本质是多目标IP的DNAT,通过将请求报文中的目标地址和目标端口修改为某挑出的RS的RIP和PORT实现转发
(1)RIP和DIP应在同一个IP网络,且应使用私网地址;RS的网关要指向DIP
(2)请求报文和响应报文都必须经由Director转发,Director易于成为系统瓶颈
(3)支持端口映射,可修改请求报文的目标PORT
(4)VS必须是Linux系统,RS可以是任意OS系统
#实验:NAT模型实现http负载均衡集群、NAT模型实现https负载均衡集群
注意:RS: 都要提供同一个私钥和同一个证书
#1、关闭防火墙、SElinux、VS和RS同步时间 #2、配置相关网络 #VS网络配置 [[email protected]~]#ip a #注意,生产环境中,VS服务器要配置GW 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:14:fe:3c brd ff:ff:ff:ff:ff:ff inet 192.168.1.100/24 brd 192.168.1.255 scope global eth0 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:14:fe:46 brd ff:ff:ff:ff:ff:ff inet 172.18.68.100/16 brd 172.18.255.255 scope global eth1 [[email protected]~]#route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth1 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth1 0.0.0.0 172.18.0.100 0.0.0.0 UG 0 0 0 eth1 #RS网络配置 [[email protected]~]#nmcli nmcli connection modify ens32 connection.id ens33 #修改网卡名字 [[email protected]~]#nmcli connection modify ens33 ipv4.addresses 172.18.68.103/16 ipv4.method manual [[email protected]~]#nmcli connection modify ens33 ipv4.gateway 172.18.68.100 #指定网管为VS服务器的DIP [[email protected]~]#nmcli connection up ens33 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3) [[email protected]~]#route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 172.18.68.100 0.0.0.0 UG 100 0 0 ens32 172.18.0.0 0.0.0.0 255.255.0.0 U 100 0 0 ens32 [[email protected]~]#nmcli connection show NAME UUID TYPE DEVICE ens33 c96bc909-188e-ec64-3a96-6a90982b08ad 802-3-ethernet ens32 [[email protected]~]#route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0 0.0.0.0 172.18.68.100 0.0.0.0 UG 0 0 0 eth0 #客户端网络配置 [[email protected]~]#ip a 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:06:23:22 brd ff:ff:ff:ff:ff:ff inet 192.168.1.101/24 brd 192.168.1.255 scope global ens33 [[email protected]~]#route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.100 0.0.0.0 UG 100 0 0 ens33 192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 #之间网络互相可以ping通 [[email protected]~]#ping -c 2 172.18.68.103 PING 172.18.68.103 (172.18.68.103) 56(84) bytes of data. 64 bytes from 172.18.68.103: icmp_seq=1 ttl=64 time=2.15 ms [[email protected]~]#ping -c 2 172.18.68.104 PING 172.18.68.104 (172.18.68.104) 56(84) bytes of data. 64 bytes from 172.18.68.104: icmp_seq=1 ttl=64 time=0.975 ms [[email protected]~]#ping -c 2 192.168.1.101 PING 192.168.1.101 (192.168.1.101) 56(84) bytes of data. 64 bytes from 192.168.1.101: icmp_seq=1 ttl=64 time=90.9 ms [[email protected]~]#ping -c 2 172.18.68.100 PING 172.18.68.100 (172.18.68.100) 56(84) bytes of data. 64 bytes from 172.18.68.100: icmp_seq=1 ttl=64 time=0.518 ms [[email protected]~]#ping -c 2 172.18.68.104 PING 172.18.68.104 (172.18.68.104) 56(84) bytes of data. 64 bytes from 172.18.68.104: icmp_seq=1 ttl=64 time=0.759 ms [[email protected]~]#ping -c 2 192.168.1.101 PING 192.168.1.101 (192.168.1.101) 56(84) bytes of data. 64 bytes from 192.168.1.101: icmp_seq=1 ttl=63 time=103 ms #3、VS服务器要启用ip_forward [[email protected]~]#vim /etc/sysctl.conf net.ipv4.ip_forward = 1 [[email protected]~]#sysctl -p #生效下 #4、RS服务器安装相关服务http [[email protected]~]#echo RS1 > /var/www/html/index.html [[email protected]~]#echo RS2 > /var/www/html/index.html [[email protected]~]#curl 172.18.68.103 RS1 [[email protected]~]#curl 172.18.68.104 RS2 #5、配置IPVS-NAT [[email protected]~]#ipvsadm -A -t 192.168.1.100:80 -s rr [[email protected]~]#ipvsadm -a -t 192.168.1.100:80 -r 172.18.68.103 -m [[email protected]~]#ipvsadm -a -t 192.168.1.100:80 -r 172.18.68.104 -m [[email protected]~]#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.1.100:80 rr -> 172.18.68.103:80 Masq 1 0 0 -> 172.18.68.104:80 Masq 1 0 0 [[email protected]~]#for i in {1..10} ; do curl 192.168.1.100 ; done RS1 RS2 #6、修改为WRR模式 [[email protected]~]#ipvsadm -E -t 192.168.1.100:80 -s wrr [[email protected]~]#ipvsadm -e -t 192.168.1.100:80 -r 172.18.68.103 -w 3 -m [[email protected]~]#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.1.100:80 wrr -> 172.18.68.103:80 Masq 3 0 0 -> 172.18.68.104:80 Masq 1 0 0 [[email protected]~]#for i in {1..10} ; do curl 192.168.1.100 ; done RS1 RS1 RS1 RS2 [[email protected]~]#tail /var/log/httpd/access_log 192.168.1.101 - - [04/Mar/2018:16:07:21 +0800] "GET / HTTP/1.1" 200 4 "-" "curl/7.29.0" #7、修改为WLC模式 [[email protected]~]#ipvsadm -E -t 192.168.1.100:80 -s wlc #8、启用长连接 [[email protected]~]#vim /etc/httpd/conf/httpd.conf KeepAlive On MaxKeepAliveRequests 100 KeepAliveTimeout 150 [[email protected]~]#telnet 192.168.1.100 80 Trying 192.168.1.100... Connected to 192.168.1.100. Escape character is '^]'. GET /test.html HTTP/1.1 HOST:192.168.1.101 HTTP/1.1 200 OK Date: Tue, 23 Jan 2018 21:56:19 GMT 。。。 </body> </html> GET /test.txt HTTP/1.1 HOST:192.168.1.101 HTTP/1.1 200 OK Date: Tue, 23 Jan 2018 21:56:52 GMT 。。。 </body> </html> q #9、修改RS服务器的端口号 [[email protected]~]#ipvsadm -d -t 192.168.1.100:80 -r 172.18.68.103 -m [[email protected]~]#ipvsadm -a -t 192.168.1.100:80 -r 172.18.68.103:8080 -m
#实现https服务的调度 #简单模式,先安装 mod_ssl 模块 [[email protected]~]#yum install mod_ssl [[email protected]~]#ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 :::80 :::* LISTEN 0 128 :::443 :::* [[email protected]~]#rpm -ql --scripts mod_ssl #安装这个模块,就会自签名颁发证书 postinstall scriptlet (using /bin/sh): umask 077 if [ -f /etc/pki/tls/private/localhost.key -o -f /etc/pki/tls/certs/localhost.crt ]; then exit 0 fi /usr/bin/openssl genrsa -rand /proc/apm:/proc/cpuinfo:/proc/dma:/proc/filesystems:/proc/interrupts:/proc/ioports:/proc/pci:/proc/rtc:/proc/uptime 2048 > /etc/pki/tls/private/localhost.key 2> /dev/null FQDN=`hostname` if [ "x${FQDN}" = "x" -o ${#FQDN} -gt 59 ]; then FQDN=localhost.localdomain fi cat << EOF | /usr/bin/openssl req -new -key /etc/pki/tls/private/localhost.key -x509 -sha256 -days 365 -set_serial $RANDOM -extensions v3_req -out /etc/pki/tls/certs/localhost.crt 2>/dev/null -- SomeState SomeCity SomeOrganization SomeOrganizationalUnit ${FQDN} [email protected]${FQDN} EOF /etc/httpd/conf.d/ssl.conf /etc/httpd/conf.modules.d/00-ssl.conf /usr/lib64/httpd/modules/mod_ssl.so /usr/libexec/httpd-ssl-pass-dialog /var/cache/httpd/ssl #添加新的集群 [[email protected]~]#ipvsadm -A -t 192.168.1.100:443 #不加 -s 选项,默认就是wlc算法 [[email protected]~]#ipvsadm -a -t 192.168.1.100:443 -r 172.18.68.103 -m [[email protected]~]#ipvsadm -a -t 192.168.1.100:443 -r 172.18.68.104 -m [[email protected]~]#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.1.100:80 rr -> 172.18.68.103:80 Route 1 0 0 -> 172.18.68.104:80 Masq 1 0 0 TCP 192.168.1.100:443 wlc -> 172.18.68.103:443 Masq 1 0 0 -> 172.18.68.104:443 Masq 1 0 0 [[email protected]~]#for i in {1..100} ; do curl -k https://192.168.1.100/ ; done #-k就是忽略证书检测
3、LVS-DR模式
LVS-DR:Direct Routing,直接路由,LVS默认模式,应用最广泛,通过为请求报文重新封装一个MAC首部进行转发,源MAC是DIP所在的接口的MAC,目标MAC是某挑选出的RS的RIP所在接口的MAC地址;源IP/PORT,以及目标IP/PORT均保持不变
Director和各RS都配置有VIP
(1) 确保前端路由器将目标IP为VIP的请求报文发往Director
在前端网关做静态绑定VIP和Director的MAC地址
在RS上使用arptables工具
arptables -A IN -d $VIP -j DROP
arptables -A OUT -s $VIP -j mangle --mangle-ip-s $RIP
在RS上修改内核参数以限制arp通告及应答级别
arp_announce
arp_ignore
帮助文档可以看 yum install kernel-doc
[[email protected]~]#less /usr/share/doc/kernel-doc-3.10.0/Documentation/networking/ip-sysctl.txt
(2) RS的RIP可以使用私网地址,也可以是公网地址;RIP与DIP在同一IP网络;RIP的网关不能指向DIP,以确保响应报文不会经由Director
(3) RS和Director要在同一个物理网络
(4) 请求报文要经由Director,但响应报文不经由Director,而由RS直接发往Client
(5) 不支持端口映射(端口不能修改)
(6) RS可使用大多数OS系统
arp_ignore - INTEGER 定义不同的发送回复以响应收到的解析本地目标IP地址的ARP请求的模式: 0 - (默认):回复在任何接口上配置的任何本地目标IP地址 1 - 仅当目标IP地址是入站接口上配置的本地地址时才回复 2 - 仅当目标IP地址是入站接口上配置的本地地址且同时具有发件人IP地址的部分来自此接口上相同子网 3 - 不回复使用作用域主机配置的本地地址,只回复全局和链接地址的分辨率 4-7 - 保留 8 - 不要回复所有本地地址 在{interface}接收到ARP请求时,使用来自conf / {all,interface} / arp_ignore的最大值 arp_announce - INTEGER 为接口上发送的ARP请求中的IP数据包通告本地源IP地址,定义不同的限制级别: 0 - (默认)使用在任何接口上配置的任何本地地址 1 - 尽量避免本接口不在目标子网中的本地地址。当通过此接口可达的目标主机要求ARP请求中的源IP地址成为其在接收接口上配置的逻辑网络的一部分时,此模式非常有用。当我们生成请求时,我们将检查包含目标IP的所有子网,并保留来自此子网的源地址。如果没有这样的子网,我们根据第2级的规则选择源地址。 2 - 始终使用该目标的最佳本地地址。在这种模式下,我们忽略IP数据包中的源地址,并尝试选择我们更喜欢与目标主机进行会谈的本地地址。通过在出站接口上的所有子网上查找包含目标IP地址的主IP地址来选择此类本地地址。如果找不到合适的本地地址,我们会选择出站接口或所有其他接口上的第一个本地地址,希望我们能收到我们请求的回复,有时甚至不管我们发布的源IP地址。 使用来自conf / {all,interface} / arp_announce的最大值。 限制级别的增加为从解析目标接收答案提供了更多机会,而降低级别宣布更有效的发件人信息。
#实验:
R模型实现http负载均衡集群
DR模型实现https负载均衡集群
注意:RS: 都要提供同一个私钥和同一个证书
DR模型实现mysql负载均衡集群
#1、搭建网络 #设置路由器的配置 [[email protected]~]#nmcli connection modify ens33 +ipv4.addresses 10.0.0.200/24 [[email protected]~]#ip a 2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:cb:db:c3 brd ff:ff:ff:ff:ff:ff inet 192.168.1.200/24 brd 192.168.1.255 scope global ens32 valid_lft forever preferred_lft forever inet 10.0.0.200/24 brd 10.0.0.255 scope global ens32 valid_lft forever preferred_lft forever 3: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:cb:db:cd brd ff:ff:ff:ff:ff:ff inet 172.18.68.200/16 brd 172.18.255.255 scope global ens33 valid_lft forever preferred_lft forever [[email protected]~]#route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.0.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens32 172.18.0.0 0.0.0.0 255.255.0.0 U 100 0 0 ens33 192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 ens32 客户端设置 [[email protected]~]#ip a 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:06:23:22 brd ff:ff:ff:ff:ff:ff inet 192.168.1.101/24 brd 192.168.1.255 scope global ens33 valid_lft forever preferred_lft forever [[email protected]~]#route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.200 0.0.0.0 UG 100 0 0 ens33 192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 [[email protected]~]#ping 192.168.1.200 PING 192.168.1.200 (192.168.1.200) 56(84) bytes of data. 64 bytes from 192.168.1.200: icmp_seq=1 ttl=64 time=454 ms [[email protected]~]#ping -c 2 10.0.0.200 PING 10.0.0.200 (10.0.0.200) 56(84) bytes of data. 64 bytes from 10.0.0.200: icmp_seq=1 ttl=64 time=10.6 ms [[email protected]~]#ping -c 2 172.18.68.103 PING 172.18.68.103 (172.18.68.103) 56(84) bytes of data. 64 bytes from 172.18.68.103: icmp_seq=1 ttl=63 time=7.43 ms [[email protected]~]#ping -c 2 172.18.68.104 PING 172.18.68.104 (172.18.68.104) 56(84) bytes of data. 64 bytes from 172.18.68.104: icmp_seq=1 ttl=63 time=16.4 ms #路由器设置 [[email protected]~]#sysctl -p net.ipv4.ip_forward = 1 [[email protected]~]#sysctl -a | grep ipv4.ip_forward net.ipv4.ip_forward = 1 [[email protected]~]#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 inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:cb:db:c3 brd ff:ff:ff:ff:ff:ff inet 192.168.1.200/24 brd 192.168.1.255 scope global ens32 valid_lft forever preferred_lft forever inet6 fe80::6c28:30e9:3794:f2ae/64 scope link valid_lft forever preferred_lft forever 3: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:cb:db:cd brd ff:ff:ff:ff:ff:ff inet 172.18.68.200/16 brd 172.18.255.255 scope global ens33 valid_lft forever preferred_lft forever inet 10.0.0.200/8 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::134d:608:9f85:bce3/64 scope link valid_lft forever preferred_lft forever [[email protected]~]#route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.0.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens32 172.18.0.0 0.0.0.0 255.255.0.0 U 100 0 0 ens33 192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 ens32 #修改RS、VS的VIP地址 [[email protected]~]#vim lvs_dr_vs.sh #!/bin/bash vip='10.0.0.100' iface='eth1:1' mask='255.0.0.0' port='80' rs1='172.18.68.103' rs2='172.18.68.104' scheduler='wrr' type='-g' case $1 in start) ifconfig $iface $vip netmask $mask #broadcast $vip up iptables -F ipvsadm -A -t ${vip}:${port} -s $scheduler ipvsadm -a -t ${vip}:${port} -r ${rs1} $type -w 3 ipvsadm -a -t ${vip}:${port} -r ${rs2} $type -w 1 ;; stop) ipvsadm -C ifconfig $iface down ;; *) echo "Usage $(basename $0) start|stop" exit 1 ;; esac [[email protected]~]#vim lvs_dr_rs.sh #!/bin/bash vip=10.0.0.100 mask='255.0.0.0' dev=lo:1 case $1 in start) echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce ifconfig $dev $vip netmask $mask #broadcast $vip up #route add -host $vip dev $dev ;; stop) ifconfig $dev down echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce ;; *) echo "Usage: $(basename $0) start|stop" exit 1 ;; esac [[email protected]~]#bash lvs_dr_vs.sh start [[email protected]~]#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 wrr -> 172.18.68.103:80 Route 3 0 0 -> 172.18.68.104:80 Route 1 0 0 [[email protected]~]#bash lvs_dr_rs.sh start [[email protected]~]#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/24 scope global lo:1 valid_lft forever preferred_lft forever 2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:a2:83:cb brd ff:ff:ff:ff:ff:ff inet 172.18.68.103/16 brd 172.18.255.255 scope global ens32 valid_lft forever preferred_lft forever [[email protected]~]#ping 10.0.0.100 PING 10.0.0.100 (10.0.0.100) 56(84) bytes of data. 64 bytes from 10.0.0.100: icmp_seq=1 ttl=63 time=1.35 ms [[email protected]~]#bash lvs_dr_rs.sh start [[email protected]~]#ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet 10.0.0.100/24 brd 10.0.0.255 scope global lo:1 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:d0:4d:a7 brd ff:ff:ff:ff:ff:ff inet 172.18.68.104/16 brd 172.18.255.255 scope global eth0 [[email protected]~]#for i in {1..10} ; do curl 10.0.0.100 ; done RS1 RS1 RS1 RS2 #完成http及https的多集群调度 [[email protected]~]#ipvsadm -A -t 10.0.0.100:443 -s rr [[email protected]~]#ipvsadm -a -t 10.0.0.100:443 -r 172.18.68.103:443 -g [[email protected]~]#ipvsadm -a -t 10.0.0.100:443 -r 172.18.68.104:443 -g [[email protected]~]#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 wrr -> 172.18.68.103:80 Route 3 0 0 -> 172.18.68.104:80 Route 1 0 0 TCP 10.0.0.100:443 rr -> 172.18.68.103:443 Route 1 0 0 -> 172.18.68.104:443 Route 1 0 0 [[email protected]~]#curl -k https://10.0.0.100 RS1 [[email protected]~]#curl -k https://10.0.0.100 RS2
4、lvs-tun模式
转发方式:不修改请求报文的IP首部(源IP为CIP,目标IP为VIP),而在原IP报文之外再封装一个IP首部(源IP是DIP,目标IP是RIP),将报文发往挑选出的目标RS;RS直接响应给客户端(源IP是VIP,目标IP是CIP)
(1) DIP, VIP, RIP都应该是公网地址
(2) RS的网关不能,也不可能指向DIP
(3) 请求报文要经由Director,但响应不能经由Director
(4) 不支持端口映射
(5) RS的OS须支持隧道功能
5、lvs-fullnat模式
lvs-fullnat:通过同时修改请求报文的源IP地址和目标IP地址进行转发
CIP --> DIP
VIP --> RIP
(1) VIP是公网地址,RIP和DIP是私网地址,且通常不在同一IP网络;因此,RIP的网关一般不会指向DIP
(2) RS收到的请求报文源地址是DIP,因此,只需响应给DIP;但Director还要将其发往Client
(3) 请求和响应报文都经由Director
(4) 支持端口映射
注意:此类型kernel默认不支持
6、LVS工作模式总结
VS/NAT | VS/TUN | VS/DR | |
Server | any | Tunneling(必须支持隧道模式) | Non-arp device |
Server network | private(局域网络) | LAN/WAN | LAN(不能跨网段) |
Server number | low(10-20) | High(100) | High(100) |
Server gateway | load balancer | own router | own router |
lvs-nat与lvs-fullnat:请求和响应报文都经由Director
lvs-nat:RIP的网关要指向DIP
lvs-fullnat:RIP和DIP未必在同一IP网络,但要能通信
lvs-dr与lvs-tun:请求报文要经由Director,但响应报文由RS直接发往Client
lvs-dr:通过封装新的MAC首部实现,通过MAC网络转发
lvs-tun:通过在原IP报文外封装新IP头实现转发,支持远距离通信
四、FireWall Mark模式及持久连接
1、FireWall Mark
FWM:FireWall Mark
MARK target 可用于给特定的报文打标记
--set-mark value
其中:value 为十六进制数字
借助于防火墙标记来分类报文,而后基于标记定义集群服务;可将多个不同的应用使用同一个集群服务进行调度
实现方法:
在Director主机打标记:
iptables -t mangle -A PREROUTING -d $vip -p $proto –m multiport --dports $port1,$port2,… -j MARK --set-mark NUMBER
在Director主机基于标记定义集群服务:
ipvsadm -A -f NUMBER [options]
#实验:在DR模式下完成 Mark 的LVS多集群服务
完成实验的原理就是默认情况下,系统是根据端口号及IP地址组合区分不同的集群服务的。
如果把http及https何为一个集群服务,就是修改 Mangle 表里的 MARK target 的标签(要在VS服务器上修改)。
#实验基础为上诉的DR模式的实验 #实验前LVS配置信息 [[email protected]~]#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 wrr -> 172.18.68.103:80 Route 3 0 0 -> 172.18.68.104:80 Route 1 0 0 TCP 10.0.0.100:443 rr -> 172.18.68.103:443 Route 1 0 0 -> 172.18.68.104:443 Route 1 0 0 #在VS服务器上添加mangle表的标签 [[email protected]~]#iptables -t mangle -A PREROUTING -d 10.0.0.100 -p tcp -m multiport --dports 80,443 -j MARK --set-mark 10 #10是十六进制数 [[email protected]~]#iptables -nvL -t mangle Chain PREROUTING (policy ACCEPT 46 packets, 4048 bytes) pkts bytes target prot opt in out source destination 0 0 MARK tcp -- * * 0.0.0.0/0 10.0.0.100 multiport dports 80,443 MARK set 0xa Chain INPUT (policy ACCEPT 46 packets, 4048 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 24 packets, 3744 bytes) pkts bytes target prot opt in out source destination Chain POSTROUTING (policy ACCEPT 24 packets, 3744 bytes) pkts bytes target prot opt in out source destination #然后要修改原有的ipvsadm的配置 [[email protected]~]#ipvsadm -C [[email protected]~]#ipvsadm -A -f 10 -s rr #这里的10是上面设置的十六进制的表情 [[email protected]~]#ipvsadm -a -f 10 -r 172.18.68.103 -g [[email protected]~]#ipvsadm -a -f 10 -r 172.18.68.104 -g [[email protected]~]#ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn FWM 10 rr #FWM MARK标签的模式 -> 172.18.68.103:0 Route 1 0 0 -> 172.18.68.104:0 Route 1 0 0 #测试 [[email protected]~]#for i in {1..10} ; do sleep 0.5 ; curl -k https://10.0.0.100 ; curl http://10.0.0.100 ; done RS1 RS2 #因为设置的算法是 rr ,所以统一调度的结果就是轮询 RS1 RS2
2、持久连接
session 绑定:对共享同一组RS的多个集群服务,需要统一进行绑定,lvs sh算法无法实现
持久连接( lvs persistence )模板:实现无论使用任何调度算法,在一段时间内(默认360s ),能够实现将来自同一个地址的请求始终发往同一个RS
ipvsadm -A|E -t|u|f service-address [-s scheduler] [-p [timeout]]
持久连接实现方式:
每端口持久(PPC):
每个端口对应定义为一个集群服务,每集群服务单独调度
每防火墙标记持久(PFWMC):
基于防火墙标记定义集群服务;可实现将多个端口上的应用统一调度,即所谓的port Affinity
每客户端持久(PCC):
基于0端口(表示所有服务)定义集群服务,即将客户端对所有应用的请求都调度至后端主机,必须定义为持久模式
#实验:一定时间内同一个地址的访问,调度到同一个RS服务器上,在rr的算法下实现
#实验基础就是上诉MARK实验的基础配置 [[email protected]~]#ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn FWM 10 rr -> 172.18.68.103:0 Route 1 0 0 -> 172.18.68.104:0 Route 1 0 0 #查询默认持久连接的时间 [[email protected]~]#ipvsadm -E -f 10 -s rr -p [[email protected]~]#ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn FWM 10 rr persistent 360 #注意:默认时间为360秒,官方的文档写的是300秒 -> 172.18.68.103:0 Route 1 0 0 -> 172.18.68.104:0 Route 1 0 0 #测试 [[email protected]~]#for i in {1..10} ; do sleep 0.5 ; curl -k https://10.0.0.100 ; curl http://10.0.0.100 ; done RS2 RS2 RS2
#实验:实现每端口持久(PPC)的调度
#实验基础的上述实验的基础 [[email protected]~]#ipvsadm -C [[email protected]~]#ipvsadm -A -t 10.0.0.100:80 -s rr -p [[email protected]~]#ipvsadm -a -t 10.0.0.100:80 -r 172.18.68.103 -g [[email protected]~]#ipvsadm -a -t 10.0.0.100:80 -r 172.18.68.104 -g [[email protected]~]#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 persistent 360 -> 172.18.68.103:80 Route 1 0 0 -> 172.18.68.104:80 Route 1 0 0 #测试 [[email protected]~]#for i in {1..10} ; do sleep 0.5 ; curl http://10.0.0.100 ; done RS2 RS2
#实验:实现每客户端持久(PCC)的调度(这种情况使用非常少,因为后续的所有服务都调度到同一个RS服务器上)
#实验基础的上述实验的基础 [[email protected]~]#ipvsadm -C [[email protected]~]#ipvsadm -A -t 10.0.0.100:0 -s rr -p [[email protected]~]#ipvsadm -a -t 10.0.0.100:0 -r 172.18.68.104 -g [[email protected]~]#ipvsadm -a -t 10.0.0.100:0 -r 172.18.68.103 -g [[email protected]~]#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:0 rr persistent 360 -> 172.18.68.103:0 Route 1 0 0 -> 172.18.68.104:0 Route 1 0 0 #测试 [[email protected]~]#ssh 10.0.0.100 #之后的所有服务都是调度到同一个RS服务器 The authenticity of host '10.0.0.100 (10.0.0.100)' can't be established. RSA key fingerprint is 22:a0:0f:06:72:0e:31:28:0c:d9:4f:cd:45:92:64:42. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '10.0.0.100' (RSA) to the list of known hosts. [email protected]'s password: Last login: Sun Mar 11 12:12:39 2018 from 172.18.0.1 [[email protected]~]#exit logout Connection to 10.0.0.100 closed. [[email protected]~]#curl 10.0.0.100 RS1
以上是关于Linux学习之路-集群及LVS24---20180213的主要内容,如果未能解决你的问题,请参考以下文章