初学Nginx实现Web负载均衡

keepalived:是一个备份方案里面的一个关键的软件,主要是主监控从,一旦宕机立刻接管,

virtual IP:相当于一个权杖,是对外真正提供服务的IP,当nginx1是主,nginx2是副,那么只要主挂了,副就能够直接将主导权抢过来,负责通信业务

对于session方式,用户信息存放在服务器端,那么就直接搞一台session服务器,所有服务器就去session服务器上去读取用户信息,那么session同步解决了

web2到mysql中加缓存可以避免频繁的从mysql中读取相同的数据,加一个缓存层直接提升了速度,(注意缓存是直接存在内存中的,提高了IO速度)

后面的Web服务不管是什么系统(Nginx Apache),只要内容一样就可以了,但是一样的方便维护

Nginx 的负载均衡模块目前支持 4 种调度算法, 下面进行分别介绍,其中后两项属于第 三方的调度方法:

轮询poll(默认)。 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端某台 服务器宕机,故障系统被自动剔除, 使用户访问不受影响。 
Weight指定轮询权值, Weight 值越大, 分配到的访问机率越高, 主要用于后端每 个服务器性能不均的情况下。 就是权重值,可以设置
ip_hash。 每个请求按访问 IP 的 hash 结果分配,这样来自同一个 IP 的访客固定访 问一个后端服务器, 有效解决了动态网页存在的 session 共享问题。 
fair。 比上面两个更加智能的负载均衡算法。 此种算法可以依据页面大小和加载时 间长短智能地进行负载均衡, 也就是根据后端服务器响应时间来分配请求,响应时间短的优先分配。 Nginx 本身是不支持 fair 的,如果需要使用这种调度算法,必须下载 Nginx的upstream_fair 模块。
url_hash。按访问 url 的 hash 结果来分配请求,使每个 url 定向到同一个后端服务器, 可以进一步提高后端缓存服务器的效率。 Nginx 本身是不支持 url_hash 的,如果需要使用这种调度算法,必须安装 Nginx 的 hash 软件包。

ip_hash在背后还是需要轮询的,只是在轮询的基础上做了识别

注意 当负载调度算法为 ip_hash 时,后端服务器在负载均衡调度中的状态不能是 weight 和 backup。

没有指定的就是默认的,就是轮询


做负载均衡服务器:

  1. <span class="pln" style="margin-top: 0px; color: #48484c;">upstream myserver </span><span class="pun" style="color: #93a1a1;">{</span>
  2. <span class="com" style="margin-top: 0px; color: #93a1a1;"># server 172.16.60.32:80 weight=2 max_fails=3 fail_timeout=20s; 权重</span>
  3. <span class="com" style="margin-top: 0px; color: #93a1a1;"># server 172.16.60.31:80 weight=1 max_fails=3 fail_timeout=20s;</span>
  4. <span class="pln" style="margin-top: 0px; color: #48484c;">server </span><span class="lit" style="color: #195f91;">172.16</span><span class="pun" style="color: #93a1a1;">.</span><span class="lit" style="color: #195f91;">60.32</span><span class="pun" style="color: #93a1a1;">:</span><span class="lit" style="color: #195f91;">80</span><span class="pun" style="color: #93a1a1;">;</span>
  5. <span class="pln" style="margin-top: 0px; color: #48484c;">server </span><span class="lit" style="color: #195f91;">172.16</span><span class="pun" style="color: #93a1a1;">.</span><span class="lit" style="color: #195f91;">60.31</span><span class="pun" style="color: #93a1a1;">:</span><span class="lit" style="color: #195f91;">80</span><span class="pun" style="color: #93a1a1;">;</span>
  6. <span class="pln" style="margin-top: 0px; color: #48484c;">server </span><span class="lit" style="color: #195f91;">172.16</span><span class="pun" style="color: #93a1a1;">.</span><span class="lit" style="color: #195f91;">60.52</span><span class="pun" style="color: #93a1a1;">:</span><span class="lit" style="color: #195f91;">80</span><span class="pun" style="color: #93a1a1;">;</span>
  7. <span class="pun" style="color: #93a1a1;">}</span>
  8. <span class="pln" style="margin-top: 0px; color: #48484c;">server </span><span class="pun" style="color: #93a1a1;">{</span>
  9. <span class="pln" style="margin-top: 0px; color: #48484c;">listen </span><span class="lit" style="color: #195f91;">80</span><span class="pun" style="color: #93a1a1;">;</span>
  10. <span class="pln" style="margin-top: 0px; color: #48484c;">server_name localhost</span><span class="pun" style="color: #93a1a1;">;</span>
  11. <span class="com" style="color: #93a1a1;">#charset koi8-r;</span>
  12. <span class="com" style="color: #93a1a1;">#access_log logs/host.access.log main;</span>
  13. <span class="pln" style="margin-top: 0px; color: #48484c;">location </span><span class="pun" style="color: #93a1a1;">/</span><span class="pun" style="color: #93a1a1;">{</span>
  14. <span class="pln" style="margin-top: 0px; color: #48484c;">root html</span><span class="pun" style="color: #93a1a1;">;</span>
  15. <span class="pln" style="margin-top: 0px; color: #48484c;">index index</span><span class="pun" style="color: #93a1a1;">.</span><span class="pln" style="color: #48484c;">html index</span><span class="pun" style="color: #93a1a1;">.</span><span class="pln" style="color: #48484c;">htm</span><span class="pun" style="color: #93a1a1;">;</span>
  16. <span class="pln" style="margin-top: 0px; color: #48484c;">proxy_pass http</span><span class="pun" style="color: #93a1a1;">:</span><span class="com" style="color: #93a1a1;">//myserver; ##注意这里,就是引用上面定义的proxy,定义了就要引用</span>
  17. <span class="pln" style="margin-top: 0px; color: #48484c;">proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header</span><span class="pun" style="color: #93a1a1;">;</span>
  18. <span class="pln" style="margin-top: 0px; color: #48484c;">include </span><span class="pun" style="color: #93a1a1;">/</span><span class="pln" style="color: #48484c;">opt</span><span class="pun" style="color: #93a1a1;">/</span><span class="pln" style="color: #48484c;">nginx</span><span class="pun" style="color: #93a1a1;">/</span><span class="pln" style="color: #48484c;">conf</span><span class="pun" style="color: #93a1a1;">/</span><span class="pln" style="color: #48484c;">proxy</span><span class="pun" style="color: #93a1a1;">.</span><span class="pln" style="color: #48484c;">conf</span><span class="pun" style="color: #93a1a1;">;</span>
  19. <span class="pun" style="color: #93a1a1;">}</span>
  20. <span class="com" style="color: #93a1a1;">#error_page 404 /404.html;</span>
  21. <span class="com" style="color: #93a1a1;"># redirect server error pages to the static page /50x.html</span>
  22. <span class="com" style="color: #93a1a1;">#</span>
  23. <span class="pln" style="margin-top: 0px; color: #48484c;">error_page </span><span class="lit" style="color: #195f91;">500</span><span class="lit" style="color: #195f91;">502</span><span class="lit" style="color: #195f91;">503</span><span class="lit" style="color: #195f91;">504</span><span class="pun" style="color: #93a1a1;">/</span><span class="lit" style="color: #195f91;">50x</span><span class="pun" style="color: #93a1a1;">.</span><span class="pln" style="color: #48484c;">html</span><span class="pun" style="color: #93a1a1;">;</span>
  24. <span class="pln" style="margin-top: 0px; color: #48484c;">location </span><span class="pun" style="color: #93a1a1;">=</span><span class="pun" style="color: #93a1a1;">/</span><span class="lit" style="color: #195f91;">50x</span><span class="pun" style="color: #93a1a1;">.</span><span class="pln" style="color: #48484c;">html </span><span class="pun" style="color: #93a1a1;">{</span>
  25. <span class="pln" style="margin-top: 0px; color: #48484c;">root html</span><span class="pun" style="color: #93a1a1;">;</span>
  26. <span class="pun" style="color: #93a1a1;">}</span>

需要创建/opt/nginx/conf/proxy.conf文件:

  1. <span class="pln" style="margin-top: 0px; color: #48484c;">proxy_redirect off</span><span class="pun" style="color: #93a1a1;">;</span>
  2. <span class="pln" style="margin-top: 0px; color: #48484c;">proxy_set_header </span><span class="typ" style="color: teal;">Host</span><span class="pln" style="color: #48484c;"> $host</span><span class="pun" style="color: #93a1a1;">;</span>
  3. <span class="pln" style="margin-top: 0px; color: #48484c;">proxy_set_header X</span><span class="pun" style="color: #93a1a1;">-</span><span class="typ" style="color: teal;">Real</span><span class="pun" style="color: #93a1a1;">-</span><span class="pln" style="color: #48484c;">IP $remote_addr</span><span class="pun" style="color: #93a1a1;">;</span>
  4. <span class="pln" style="margin-top: 0px; color: #48484c;">proxy_set_header X</span><span class="pun" style="color: #93a1a1;">-</span><span class="typ" style="color: teal;">Forwarded</span><span class="pun" style="color: #93a1a1;">-</span><span class="typ" style="color: teal;">For</span><span class="pln" style="color: #48484c;"> $proxy_add_x_forwarded_for</span><span class="pun" style="color: #93a1a1;">;</span>
  5. <span class="pln" style="margin-top: 0px; color: #48484c;">proxy_connect_timeout </span><span class="lit" style="color: #195f91;">90</span><span class="pun" style="color: #93a1a1;">;</span>
  6. <span class="pln" style="margin-top: 0px; color: #48484c;">proxy_send_timeout </span><span class="lit" style="color: #195f91;">90</span><span class="pun" style="color: #93a1a1;">;</span>
  7. <span class="pln" style="margin-top: 0px; color: #48484c;">proxy_read_timeout </span><span class="lit" style="color: #195f91;">90</span><span class="pun" style="color: #93a1a1;">;</span>
  8. <span class="pln" style="margin-top: 0px; color: #48484c;">proxy_buffer_size </span><span class="lit" style="color: #195f91;">4k</span><span class="pun" style="color: #93a1a1;">;</span>
  9. <span class="pln" style="margin-top: 0px; color: #48484c;">proxy_buffers </span><span class="lit" style="color: #195f91;">4</span><span class="lit" style="color: #195f91;">32k</span><span class="pun" style="color: #93a1a1;">;</span>
  10. <span class="pln" style="margin-top: 0px; color: #48484c;">proxy_busy_buffers_size </span><span class="lit" style="color: #195f91;">64k</span><span class="pun" style="color: #93a1a1;">;</span>
  11. <span class="pln" style="margin-top: 0px; color: #48484c;">proxy_temp_file_write_size </span><span class="lit" style="color: #195f91;">64k</span><span class="pun" style="color: #93a1a1;">;</span>

三种算法区别: 
轮询

  1. <span class="pln" style="margin-top: 0px; color: #48484c;">upstream myserver </span><span class="pun" style="color: #93a1a1;">{</span>
  2. <span class="pln" style="margin-top: 0px; color: #48484c;">server </span><span class="lit" style="color: #195f91;">172.16</span><span class="pun" style="color: #93a1a1;">.</span><span class="lit" style="color: #195f91;">60.32</span><span class="pun" style="color: #93a1a1;">:</span><span class="lit" style="color: #195f91;">80</span><span class="pun" style="color: #93a1a1;">;</span>
  3. <span class="pln" style="margin-top: 0px; color: #48484c;">server </span><span class="lit" style="color: #195f91;">172.16</span><span class="pun" style="color: #93a1a1;">.</span><span class="lit" style="color: #195f91;">60.31</span><span class="pun" style="color: #93a1a1;">:</span><span class="lit" style="color: #195f91;">80</span><span class="pun" style="color: #93a1a1;">;</span>
  4. <span class="pln" style="margin-top: 0px; color: #48484c;">server </span><span class="lit" style="color: #195f91;">172.16</span><span class="pun" style="color: #93a1a1;">.</span><span class="lit" style="color: #195f91;">60.52</span><span class="pun" style="color: #93a1a1;">:</span><span class="lit" style="color: #195f91;">80</span><span class="pun" style="color: #93a1a1;">;</span>
  5. <span class="pun" style="color: #93a1a1;">}</span>

权重:

  1. <span class="pln" style="margin-top: 0px; color: #48484c;">upstream myserver </span><span class="pun" style="color: #93a1a1;">{</span>
  2. <span class="pln" style="margin-top: 0px; color: #48484c;">server </span><span class="lit" style="color: #195f91;">172.16</span><span class="pun" style="color: #93a1a1;">.</span><span class="lit" style="color: #195f91;">60.32</span><span class="pun" style="color: #93a1a1;">:</span><span class="lit" style="color: #195f91;">80</span><span class="pln" style="color: #48484c;"> weight</span><span class="pun" style="color: #93a1a1;">=</span><span class="lit" style="color: #195f91;">2</span><span class="pln" style="color: #48484c;"> max_fails</span><span class="pun" style="color: #93a1a1;">=</span><span class="lit" style="color: #195f91;">3</span><span class="pln" style="color: #48484c;"> fail_timeout</span><span class="pun" style="color: #93a1a1;">=</span><span class="lit" style="color: #195f91;">20s</span><span class="pun" style="color: #93a1a1;">;</span>
  3. <span class="pln" style="margin-top: 0px; color: #48484c;">server </span><span class="lit" style="color: #195f91;">172.16</span><span class="pun" style="color: #93a1a1;">.</span><span class="lit" style="color: #195f91;">60.31</span><span class="pun" style="color: #93a1a1;">:</span><span class="lit" style="color: #195f91;">80</span><span class="pln" style="color: #48484c;"> weight</span><span class="pun" style="color: #93a1a1;">=</span><span class="lit" style="color: #195f91;">1</span><span class="pln" style="color: #48484c;"> max_fails</span><span class="pun" style="color: #93a1a1;">=</span><span class="lit" style="color: #195f91;">3</span><span class="pln" style="color: #48484c;"> fail_timeout</span><span class="pun" style="color: #93a1a1;">=</span><span class="lit" style="color: #195f91;">20s</span><span class="pun" style="color: #93a1a1;">;</span>
  4. <span class="pln" style="margin-top: 0px; color: #48484c;">server </span><span class="lit" style="color: #195f91;">172.16</span><span class="pun" style="color: #93a1a1;">.</span><span class="lit" style="color: #195f91;">60.52</span><span class="pun" style="color: #93a1a1;">:</span><span class="lit" style="color: #195f91;">80</span><span class="pln" style="color: #48484c;"> weight</span><span class="pun" style="color: #93a1a1;">=</span><span class="lit" style="color: #195f91;">3</span><span class="pln" style="color: #48484c;"> max_fails</span><span class="pun" style="color: #93a1a1;">=</span><span class="lit" style="color: #195f91;">3</span><span class="pln" style="color: #48484c;"> fail_timeout</span><span class="pun" style="color: #93a1a1;">=</span><span class="lit" style="color: #195f91;">20s</span><span class="pun" style="color: #93a1a1;">;</span>
  5. <span class="pun" style="color: #93a1a1;">}</span>

IP hash:

  1. <span class="pln" style="margin-top: 0px; color: #48484c;">upstream myserver </span><span class="pun" style="color: #93a1a1;">{</span>
  2. <span class="pln" style="margin-top: 0px; color: #48484c;">ip_hash</span><span class="pun" style="color: #93a1a1;">;</span>
  3. <span class="pln" style="margin-top: 0px; color: #48484c;">server </span><span class="lit" style="color: #195f91;">172.16</span><span class="pun" style="color: #93a1a1;">.</span><span class="lit" style="color: #195f91;">60.32</span><span class="pun" style="color: #93a1a1;">:</span><span class="lit" style="color: #195f91;">80</span><span class="pun" style="color: #93a1a1;">;</span>
  4. <span class="pln" style="margin-top: 0px; color: #48484c;">server </span><span class="lit" style="color: #195f91;">172.16</span><span class="pun" style="color: #93a1a1;">.</span><span class="lit" style="color: #195f91;">60.52</span><span class="pun" style="color: #93a1a1;">:</span><span class="lit" style="color: #195f91;">80</span><span class="pun" style="color: #93a1a1;">;</span>
  5. <span class="pln" style="margin-top: 0px; color: #48484c;">server </span><span class="lit" style="color: #195f91;">172.16</span><span class="pun" style="color: #93a1a1;">.</span><span class="lit" style="color: #195f91;">60.31</span><span class="pun" style="color: #93a1a1;">:</span><span class="lit" style="color: #195f91;">80</span><span class="pun" style="color: #93a1a1;">;</span>
  6. <span class="pun" style="color: #93a1a1;">}</span>

假如提示80端口占用就使用fuser -k 80/tcp杀掉占用80端口的程序

nginx集群实验总结; 
1,明确拓扑结构,明确IP地址架构 
2,确定real_server确定是可用的 
3,确定real_server和集群服务器上selinux和seLinux不影响服务 
4,看好了自己启用的nginx的目录是自己所配置的,不要改错了文件夹, 
5,定义了负载均衡器名称要调用,一旦访问80端口就要转到负载均衡器上面,前面的http://表明转发的是http协议,是固定语法,是 
6,注意括号问题 
7,看英文报错误

那么问题来了: 
数据从客户机 –>负载均衡—>real_server 的来回?从封装角度理解 
数据经过负载均衡服务器之后,源IP和目的IP是否会发生改变?


答案:数据包经过nginx均衡服务器后,目的端口和目的IP地址和mac地址变为real_server的,源IP和端口和mac变为nginx服务器的 
nginx是负责的代理的工作,完全代理,所以外面的客户机是根本就不知道里面real_server的真实地址的

高性能:大量的并发,同时处理大量的请求和数据–>集群解决方案 
高可用:备份—>备份


keepalived:

keepalived是什么?

keepalived是集群管理中保证集群高可用的一个服务软件,其功能类似于heartbeat,用来防止单点故障。heartbeat(心跳)

keepalived工作原理

keepalived是以VRRP协议为实现基础的,VRRP全称Virtual Router Redundancy Protocol,即虚拟路由冗余协议。 
虚拟路由冗余协议,可以认为是实现路由器高可用的协议,即将N台提供相同功能的路由器组成一个路由器组,这个组里面有一个master和多个backup,master上面有一个对外提供服务的vip(��路由器所在局域网内其他机器的默认路由为该vip),master会发组播,当backup收不到vrrp包时就认为master宕掉了,这时就需要根据VRRP的优先级来选举一个backup当master。这样的话就可以保证路由器的高可用了。 
keepalived主要有三个模块,分别是core、check和vrrp。core模块为keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析。check负责健康检查,包括常见的各种检查方式。vrrp模块是来实现VRRP协议的。

keepalived的配置文件

keepalived只有一个配置文件keepalived.conf,里面主要包括以下几个配置区域,分别是global_defs、static_ipaddress、static_routes、vrrp_script、vrrp_instance和virtual_server。

global_defs区域

主要是配置故障发生时的通知对象以及机器标识


什么是四层负载均衡,什么是七层负载均衡?

四层:传输层,IP+port 
七层:应用层,当然也能够用四层进行负载均衡

为什么四层的负载均衡快?各自的优缺点

四层负载均衡只修改IP包头的IP地址和端口,只相当于个路由器的功能,外面能看真实服务器的IP地址,不做tcp连接,只是做个转发,所以说四层要快,大并发 
七层负载均衡是代理TCP连接,外面看不到里面的真实情况,我们学的nginx里面的就是七层,两面都要建立tcp连接,所以对性能的要求会更高

四层:lvs–>是中国人章文嵩搞的,现在在淘宝网,现在已经嵌入到Linux内核中去了, 美国人的F5也能够实现四层负载均衡,
七层:nginx,HAproxy,等

lvs也是需要keepalived做高可用的

集群:cluster

更多Nginx负载均衡配置相关教程见以下内容

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

相关推荐