18.6 负载均衡集群介绍
18.7 LVS介绍
18.8 LVS调度算法
18.9/18.10 LVS NAT模式搭建html
18.6 负载均衡集群介绍linux
主流开源软件LVS、keepalived、haproxy、nginx等nginx
其中LVS属于4层(网络OSI 7层模型),nginx属于7层,haproxy既能够认为是4层,也能够当作7层使用web
keepalived的负载均衡功能其实就是lvs,lvs是keepalived内置的算法
lvs这种4层的负载均衡是能够分发TCP协议,web服务是80端口,除了分发80端口,还有其余的端口通讯的,好比MySQL的负载均衡,就能够用LVS实现,而nginx仅仅支持http,https,mail,haproxy;haproxy也支持MySQL这种TCP负载均衡的apache
7层有限制,不过有些更高级的功能,nginx能够经过站点目录,去区分网站服务器以前,LVS4层的就不支持vim
相比较来讲,LVS这种4层的更稳定,能承受更多的请求,承载的并发量更高,而nginx这种7层的更加灵活,能实现更多的个性化需求后端
18.7 LVS介绍浏览器
LVS是由国人章文嵩开发bash
流行度不亚于apache的httpd,基于TCP/IP作的路由和转发,稳定性和效率很高
LVS最新版本基于Linux内核2.6,有好多年不更新了
LVS有三种常见的模式:NAT、DR、IP Tunnel
LVS架构中有一个核心角色叫作分发器(Load balance),它用来分发用户的请求,还有诸多处理用户请求的服务器(Real Server,简称rs)
LVS NAT模式,借助iptables的nat表来实现
用户的请求到分发器后,经过预设的iptables规则,把请求的数据包转发到后端的rs上去
rs须要设定网关为分发器的内网ip
用户请求的数据包和返回给用户的数据包所有通过分发器,因此分发器成为瓶颈
在nat模式中,只须要分发器有公网ip便可,因此比较节省公网ip资源
原理图解释:
Load Balancer,就是一个分发器;把用户的请求,分发给后端的Real Server ,Real Server这些服务器接收到请求之后,处理好用户请求之后,就从新丢回给Load Balancer;最后Load Balancer再返回给用户;
这个模式的弊端,当访问量、请求量、反馈量大的时候,Load Balancer的压力很大
LVS规模,通常规模最多支持10来台服务器,超过10台的话就会有力不从心;
nat模式这个结构,只须要有一个公网IP,其余real server服务器所有在内网就能够实现。优势,节省不少的资源
LVS IP Tunnel模式,须要有一个公共的IP配置在分发器和全部rs上,咱们把它叫作vip
客户端请求的目标IP为vip,分发器接收到请求数据包后,会对数据包作一个加工,会把目标IP改成rs的IP,这样数据包就到了rs上
rs接收数据包后,会还原原始数据包,这样目标IP为vip,由于全部rs上配置了这个vip,因此它会认为是它本身
原理图解释:
在load balancer与real server之间创建了虚拟通道,叫作 ip tunnel ;其实是更改了数据包目的IP;请求过来经过load balancer,经过在real server上配置的VIP;用户请求的时候,数据包里面包好的目的IP,当数据包到达load balancer的时候,load balancer会进行一个数据包目的IP的更改,而后发送到具体的real server上,经过lvs的本身的算法,进行实现到底传输到哪一个real server上;而后real server再解包处理,再经过一个VIP直接返回到用户,这就省略数据回到load balancer分发器的过程,这样就load balancer就没有瓶颈
LVS DR模式,也须要有一个公共的IP配置在分发器和全部rs上,也就是vip
和IP Tunnel不一样的是,它会把数据包的MAC地址修改成rs的MAC地址
rs接收数据包后,会还原原始数据包,这样目标IP为vip,由于全部rs上配置了这个vip,因此它会认为是它本身
18.8 LVS调度算法
轮询 Round-Robin 简称:rr 最简单的也是最容易理解
用户请求过来,均衡的分发到rs上
加权轮询 Weight Round-Robin 简称:wrr
带权重的轮询,能够对机器单独设置权重,对高权重的机器发送的请求会多一些
最小链接 Least-Connection 简称: lc
把请求发送到请求数量小的rs上
加权最小链接 Weight Least-Connection 简称: wlc
对请求数量小的rs,加上一个权重,使他优先
基于局部性的最小链接 Locality-Based Least Connections 简称: lblc
带复制的基于局部性最小链接 Locality-Based Least Connections with Replication 简称: lblcr
目标地址散列调度 Destination Hashing 简称:dh
源地址散列调度 Source Hashing 简称: sh
18.9/18.10 LVS NAT模式搭建
LVS NAT模式搭建
NAT模式搭建 – 准备工做
内网:133.133,设置网关为133.130
内网:133.132,设置网关为133.130
内网:133.130,外网:147.144(vmware仅主机模式)
三台机器
分发器,也叫调度器(简写为dir)
rs1
rs2
三台机器上都执行执行
systemctl stop firewalld; systemc disable firewalld
systemctl start iptables-services; iptables -F; service iptables save
NAT模式搭建
在dir上安装ipvsadm
yum install -y ipvsdam
在dir上编写脚本,vim /usr/local/sbin/lvs_nat.sh//内容以下
#! /bin/bash # director 服务器上开启路由转发功能 echo 1 > /proc/sys/net/ipv4/ip_forward # 关闭icmp的重定向 echo 0 > /proc/sys/net/ipv4/conf/all/send_redirectsecho 0 > /proc/sys/net/ipv4/conf/default/send_redirects # 注意区分网卡名字,阿铭的两个网卡分别为ens33和ens37 echo 0 > /proc/sys/net/ipv4/conf/ens33/send_redirects echo 0 > /proc/sys/net/ipv4/conf/ens37/send_redirects # director 设置nat防火墙 iptables -t nat -F iptables -t nat -X iptables -t nat -A POSTROUTING -s 192.168.133.0/24 -j MASQUERADE # director设置ipvsadm IPVSADM='/usr/sbin/ipvsadm' $IPVSADM -C$IPVSADM -A -t 192.168.147.144:80 -s wlc -p 3 $IPVSADM -a -t 192.168.147.144:80 -r 192.168.133.132:80 -m -w 1 $IPVSADM -a -t 192.168.147.144:80 -r 192.168.133.133:80 -m -w 1
NAT模式效果测试
两台rs上都安装nginx
设置两台rs的主页,作一个区分,也就是说直接curl两台rs的ip时,获得不一样的结果
浏览器里访问192.168.142.147,多访问几回看结果差别
NAT模式是经过iptables实现的,因此必须配置一些iptables规则
1.在配置前准备三台机器,一台做为分发器,也叫作调度器,简称 dir,另外两台就是real server,用来处理用户请求的服务器,rs一、rs2(克隆虚拟机步骤)
安装ifconfig命令 yum install -y net-tools
A机器IP为192.168.74.12九、B机器IP为192.168.74.130,C机器IP为192.168.74.133
PS:网关最后设置,不然包没法下载
B机器和C机器的网关必须设置成分发器(即A机器)的内网IP,若不设置成它的网关,是无法通讯的
网卡配置文件中更改(即本机器) vi /etc/sysconfig/network-scripts/ifcfg-eno16777736 更改完以后重启网络服务 systemctl restart network [root@hf-02 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.74.129 0.0.0.0 UG 1024 0 0 eno16777736 192.168.74.0 0.0.0.0 255.255.255.0 U 0 0 0 eno16777736 [root@hf-02 ~]#
关闭firewalld服务 systemctl stop firewalld 使firewalld服务再也不开机启动 systemctl disable firewalld
yum install -y iptables-services
[root@hf-01 ~]# cd /etc/yum.repos.d/ [root@hf-01 yum.repos.d]# ls CentOS7-Base-163.repo CentOS-Sources.repo epel.repo CentOS-Debuginfo.repo CentOS-Vault.repo epel-testing.repo [root@hf-01 yum.repos.d]# mv epel.repo epel.repo.1 [root@hf-01 yum.repos.d]#
机器B [root@hf-02 ~]# systemctl start iptables [root@hf-02 ~]# 机器C [root@hf-03 ~]# systemctl start iptables [root@hf-03 ~]#
机器B [root@hf-02 ~]# systemctl start iptables [root@hf-02 ~]# systemctl enable iptables ln -s '/usr/lib/systemd/system/iptables.service' '/etc/systemd/system/basic.target.wants/iptables.service' [root@hf-02 ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 71 5076 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 1 124 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT 48 packets, 4680 bytes) pkts bytes target prot opt in out source destination [root@hf-02 ~]# iptables -F //清空表的规则,以便后续实验 [root@hf-02 ~]# service iptables save iptables: Saving firewall rules to /etc/sysconfig/iptables:[ 肯定 ] [root@hf-02 ~]# 机器C同上
[root@hf-01 ~]# iptables -nvL Chain INPUT (policy ACCEPT 19 packets, 1296 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 11 packets, 1108 bytes) pkts bytes target prot opt in out source destination [root@hf-01 ~]#
setenforce 0 //临时关闭selinux getenforce //查看selinux是否关闭 为了保险起见,在配置文件中永久关闭selinux vi /etc/selinux/config SELINUX=enforcing更改成SELINUX=disabled
NAT模式搭建
[root@hf-01 ~]# yum install -y ipvsadm
[root@hf-01 ~]# vim /usr/local/sbin/lvs_nat.sh #! /bin/bash # director 服务器上开启路由转发功能 echo 1 > /proc/sys/net/ipv4/ip_forward //对内核参数修改,打开路由转发 # 关闭icmp的重定向 echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects //假装操做,否则不能转发rs的数据 echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects //假装操做,否则不能转发rs的数据 # 注意区分网卡名字,dir机器的两块网卡分别为ens33和ens37 echo 0 > /proc/sys/net/ipv4/conf/eno16777736/send_redirects echo 0 > /proc/sys/net/ipv4/conf/ens36/send_redirects # director 设置nat防火墙 iptables -t nat -F iptables -t nat -X iptables -t nat -A POSTROUTING -s 192.168.133.0/24 -j MASQUERADE //MASQUERADE实现同网段的机器去上网,路由器使用的就是这个功能 # director设置ipvsadm IPVSADM='/usr/sbin/ipvsadm' //设置一个变量,方便下面命令引用 $IPVSADM -C //清空规则 $IPVSADM -A -t 192.168.204.1:80 -s lc -p 3 //用来定义lvs 的模式;wlc,为算法,能够按需求选择 lvs 里面适合的算法 $IPVSADM -a -t 192.168.204.1:80 -r 192.168.74.131:80 -m -w 1 //小规则,-r 指定dir机器IP,-m 指定nat模式,-w指定rs权重 $IPVSADM -a -t 192.168.204.1:80 -r 192.168.74.133:80 -m -w 1 //小规则,-r 指定dir机器IP,-m 指定nat模式,-w指定rs权重
[root@hf-01 ~]# sh /usr/local/sbin/lvs_nat.sh [root@hf-01 ~]#
B机器 [root@hf-02 ~]# ps aux |grep nginx root 1102 0.0 0.0 20996 624 ? Ss 05:29 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 1107 0.0 0.3 23440 3208 ? S 05:29 0:00 nginx: worker process nobody 1108 0.0 0.3 23440 3208 ? S 05:29 0:00 nginx: worker process root 3580 0.0 0.0 112676 984 pts/1 R+ 08:30 0:00 grep --color=auto nginx [root@hf-02 ~]# C机器 [root@hf-03 ~]# ps aux |grep nginx root 821 0.0 0.0 20996 628 ? Ss 08:00 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 825 0.0 0.3 23440 3212 ? S 08:00 0:00 nginx: worker process nobody 826 0.0 0.3 23440 3212 ? S 08:00 0:00 nginx: worker process root 1851 0.0 0.0 112676 984 pts/0 R+ 08:30 0:00 grep --color=auto nginx [root@hf-03 ~]#
[root@hf-02 ~]# vim /usr/share/nginx/html/index.html [root@hf-02 ~]# curl localhost hanfeng-02
[root@hf-03 ~]# vim /usr/share/nginx/html/index.html [root@hf-03 ~]# curl localhost hanfeng-03
[root@hf-01 ~]# curl 192.168.204.1 hanfeng-02 [root@hf-01 ~]# curl 192.168.204.1 hanfeng-03
[root@hf-01 ~]# iptables -t nat -nvL Chain PREROUTING (policy ACCEPT 108 packets, 5472 bytes) pkts bytes target prot opt in out source destination Chain INPUT (policy ACCEPT 108 packets, 5472 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 MASQUERADE all -- * * 192.168.74.0/24 0.0.0.0/0 [root@hf-01 ~]#
[root@hf-01 ~]# 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.204.1:80 wlc persistent 3 -> 192.168.74.131:80 Masq 1 0 0 -> 192.168.74.133:80 Masq 1 0 0 [root@hf-01 ~]#
友情连接:http://www.apelearn.com阿铭linux