很是简单的一种高度算法,就是按顺序把请求依次发送给后端的服务器,它无论后端服务器的处理速度和响应时间怎样。当后端服务器性能不一致时,用这种调度算法就不合适过了。html
比第一种算法多了一个权重的设置,权重越高的服务器被分配到的请求就越多,这样后端服务器被分配到的请求就越多,这样后端服务器性能不一致时,就能够给配置低的服务器较小的权重。linux
这种算法会根据各真实服务器上的链接数来决定把新的请求分配给谁,链接数少说明服务器是空闲的,这样把新的请求分配到空闲服务器上才更加合理。nginx
在最小链接高度的基础上再增长一个权重设置,这样就能够人为地去控制哪些服务上多分配请求,哪些少分配请求。web
这种算法简称LBLC,是针对请求报文的目标IP地址的负载均衡调度,目前主要用于Cache集群系统,由于在Cache集群客户请求报文的目标IP地址是变化的。算法的设计目标是在服务器的负载基本平衡的状况下,将相同目标IP地址的请求调度到同一台服务器,来提升各台服务器的访问局部性和主存Cache命中率。 算法
该算法简称LBLCR,也是针对目标IP地址的负载均衡,它与LBLC算法的不一样之处是:它要维护从一个目标IP地址到一组服务器的映射,而LBLC算法是维护从一个目标IP地址到一台服务器的映射。LBLCR算法先根据请求的目标IP地址找出该目标IP地址对应的服务器组,按“最小链接”原则从该服务器组中选出一台服务器,若服务器没有超载,则将请求发送到该服务器;若服务器超载,则按“最小链接”原则从整个集群中选出一台服务器,将该服务器加入到服务器组中,将请求发送到该服务器。同时,当该服务器组有一段时间没有被修改,将最忙的服务器从服务器组中删除,以下降复制的程度。 shell
该算法也是针对目标IP地址的负载均衡的,但它是一种静态映射算法,经过一个散列(hash)函数将一个目标IP地址映射到一台服务器。目标地址散列调度算法先根据请求的目标IP地址,做为散列键(hash key)从静态分配的散列表找出对应的服务器,若该服务器是可用的且未超载,将请求发送到该服务器,不然返回空。apache
该算法正好与目标地址散列调度算法相反,它根据请求的源IP地址,做为散列键从静态分配的散列表找出对应的服务器,若该服务器是可用的且未超载,将请求发送到该服务器,不然返回空。它的算法流程与目标地址散列调度算法的基本类似,只不过将请求的目标IP地址换成请求的源IP地址。 vim
[root@localhost ~]# hostnamectl set-hostname gary-tao-03 [root@localhost ~]# bash ##进入子shell,更改为功 [root@gary-tao-03 ~]#
[root@localhost ~]# iptables -nvL //查看 [root@localhost ~]# systemctl stop firewalld //关闭 [root@gary-tao ~]# systemctl disable firewalld //关闭开机启动 Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
[root@gary ~]# yum install -y iptables-services #因为要调用epel国外资源安装会很慢,能够临时取消epel源的调用方式安装,操做以下 #把/etc/yum.repos.d/目录下epel.repo 改一下名字 [root@gary ~]# cd /etc/yum.repos.d/ [root@gary yum.repos.d]# ls CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo nginx.repo [root@gary yum.repos.d]# mv epel.repo epel.repo.1 #查看安装包 [root@gary yum.repos.d]# yum list |grep iptables-service iptables-services.x86_64 1.4.21-18.2.el7_4 @updates [root@gary ~]# yum install -y iptables-services #查看一个包都安装了哪些文件 [root@gary ~]# rpm -ql iptables-services /etc/sysconfig/ip6tables /etc/sysconfig/iptables /usr/lib/systemd/system/ip6tables.service /usr/lib/systemd/system/iptables.service /usr/libexec/initscripts/legacy-actions/ip6tables /usr/libexec/initscripts/legacy-actions/ip6tables/panic /usr/libexec/initscripts/legacy-actions/ip6tables/save /usr/libexec/initscripts/legacy-actions/iptables /usr/libexec/initscripts/legacy-actions/iptables/panic /usr/libexec/initscripts/legacy-actions/iptables/save /usr/libexec/iptables /usr/libexec/iptables/ip6tables.init /usr/libexec/iptables/iptables.init #启动iptables.service [root@gary ~]# systemctl start iptables [root@gary ~]# systemctl enable iptables Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service. #开启的目的是为了调用一个空的规则 [root@localhost ~]# iptables -F [root@localhost ~]# service iptables save iptables: Saving firewall rules to /etc/sysconfig/iptables:[ 肯定 ] #关闭SELinux [root@localhost ~]# getenforce #查看selinux Enforcing [root@localhost ~]# setenforce 0 #临时关闭 [root@gary-tao ~]# getenforce #已关闭状态 Disabled [root@gary ~]# getenforce #临时关闭状态 Permissive [root@localhost ~]# vi /etc/selinux/config #永久关闭,修改配置文件,把Enforcing改为Disabled,重启生效。 #rs1与rs2的网关都要是dir机器的内网ip [root@garytao-03 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 172.16.111.100 0.0.0.0 UG 100 0 0 ens33 172.16.111.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
安装LVS核心工具ipvsdam后端
[root@garytao-01 ~]# yum install -y ipvsadm //安装LVS的核心工具,只须要在dir机器安装 #假设若是报错没有可用软件包 ipvsadm,按以下方式安装: wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz tar zxf ipvsadm-1.26.tar.gz cd ipvsadm-1.26 rpm -qa | grep kernel-devel(默认已经安装) (安装所需依赖包 yum install popt-static kernel-devel make gcc openssl-devel lftplibnl* popt* openssl-devel lftplibnl* popt* libnl* libpopt* gcc*) make && make install 检测是否安装成功: [root@gary-tao ipvsadm-1.26]# ipvsadm IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn [root@gary-tao ipvsadm-1.26]# lsmod |grep ip_vs ip_vs 141092 0 nf_conntrack 111302 1 ip_vs libcrc32c 12644 2 xfs,ip_vs
在dir上编写一个脚本:centos
[root@gary-tao ~]# 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 echo 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/ens38/send_redirects # director 设置nat防火墙 iptables -t nat -F iptables -t nat -X iptables -t nat -A POSTROUTING -s 172.16.111.0/24 -j MASQUERADE # director设置ipvsadm IPVSADM='/usr/sbin/ipvsadm' $IPVSADM -C $IPVSADM -A -t 172.16.13.129:80 -s wlc -p 3 $IPVSADM -a -t 172.16.13.129:80 -r 172.16.111.110:80 -m -w 1 $IPVSADM -a -t 172.16.13.129:80 -r 172.16.111.120:80 -m -w 1 [root@gary-tao ~]# sh /usr/local/sbin/lvs_nat.sh //执行脚本
这里改了下第二台rs的nginx配置,第三台没有改。
[root@gary ~]# curl localhost <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> [root@gary ~]# vi /usr/share/nginx/html/index.html [root@gary ~]# curl localhost garytao02.
[root@garytao-01 ~]# curl 172.16.13.129 garytao02. [root@garytao-01 ~]# curl 172.16.13.129 garytao02. [root@garytao-01 ~]# curl 172.16.13.129 garytao02. [root@garytao-01 ~]# curl 172.16.13.129 garytao02. [root@garytao-01 ~]# curl 172.16.13.129 garytao02. #链接访问屡次,一直请求到rs1上。这是由于在前面的脚本中有设置有-p参数,理论上有300秒内会一直请求到rs1上。 #更改脚本的时间值,把wlc -p 3参数删除了,改为rr [root@garytao-01 ~]# cat /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 echo 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/ens38/send_redirects # director 设置nat防火墙 iptables -t nat -F iptables -t nat -X iptables -t nat -A POSTROUTING -s 172.16.111.0/24 -j MASQUERADE # director设置ipvsadm IPVSADM='/usr/sbin/ipvsadm' $IPVSADM -C $IPVSADM -A -t 172.16.13.129:80 -s rr $IPVSADM -a -t 172.16.13.129:80 -r 172.16.111.110:80 -m -w 1 $IPVSADM -a -t 172.16.13.129:80 -r 172.16.111.120:80 -m -w 1 #再次测试,作到了均衡访问 [root@garytao-01 ~]# curl 172.16.13.129 garytao02. [root@garytao-01 ~]# curl 172.16.13.129 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> [root@garytao-01 ~]# curl 172.16.13.129 garytao02. [root@garytao-01 ~]# curl 172.16.13.129 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>