Nginx&&HAproxy实现负载均衡

Nginx&&HAproxy实现负载均衡

环境:

hostname ip os
nginx/proxy 192.168.220.130 centos7
server1 192.168.220.138 centos7
server2 192.168.220.139 centos7

Nginx 的 upstream 负载的5种方式

1)、轮询(默认)

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除。

2)、weight

指定轮询几率,weight 和访问比率成正比,用于后端服务器性能不均的情况。

3)、ip_hash

每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决 session 的问题。

4)、fair(第三方)

按后端服务器的响应时间来分配请求,响应时间短的优先分配。

**5)、url_hash(第三方) **

按访问url的hash结果来分配请求,使同样的url定向到同一个后端服务器,后端服务器为缓存时比较有效

nginx配置

下载http://nginx.org/en/download.html

[root@nginx ~]# tar zxvf nginx-1.16.1.tar.gz
[root@nginx ~]# cd nginx-1.16.1/
[root@nginx nginx-1.16.1]#./configure --prefix=/usr/local/nginx  --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module  --with-http_mp4_module
......
Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
......
--with-http_dav_module 启用ngx_http_dav_module支持(增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法)默认情况下为关闭,需编译开启
--with-http_stub_status_module 启用ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态)
--with-http_addition_module 启用ngx_http_addition_module支持(作为一个输出过滤器,支持不完全缓冲,分部分响应请求)
--with-http_sub_module 启用ngx_http_sub_module支持(允许用一些其他文本替换nginx响应中的一些文本)
--with-http_flv_module 启用ngx_http_flv_module支持(提供寻求内存使用基于时间的偏移量文件)
--with-http_mp4_module  启用对mp4文件支持(提供寻求内存使用基于时间的偏移量文件)

[root@nginx nginx-1.16.1]# make && make install
添加nginx用户
[root@nginx nginx-1.16.1]# useradd -u 8000 -s /sbin/nologin nginx
修改配置文件
[root@nginx nginx-1.16.1]# vim /usr/local/nginx/conf/nginx.conf
user  nginx nginx;

location / {中添加如下内容

 if ($request_uri ~* \.html$){
              proxy_pass http://htmlservers;
         }
 if ($request_uri ~* \.php$){
              proxy_pass http://phpservers;
         }   
              proxy_pass http://picservers;
最后再添加
  upstream  htmlservers {   #定义负载均衡服务器组名称
         server 192.168.220.138:80;   
         server 192.168.220.139:80;
 }
 upstream  phpservers{
         server 192.168.220.138:80;   
         server 192.168.220.139:80
 }
 upstream  picservers {
         server 192.168.220.138:80;   
         server 192.168.220.139:80
 }
检查下配置文件语法
[root@nginx ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
启动nginx
[root@nginx ~]# /usr/local/nginx/sbin/nginx 
[root@nginx ~]# netstat -tunlp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      9238/nginx: master 

Web服务器配置

[root@server1 ~]# yum install httpd php -y
[root@server2 ~]# yum install httpd php -y
[root@server1 ~]# echo server1 >/var/www/html/index.html
[root@server2 ~]# echo server2 >/var/www/html/index.html
[root@server1 ~]# cat /var/www/html/test.php 
<?php
phpinfo();
?>
[root@server2 ~]# cat /var/www/html/test.php 
<?php
phpinfo();
?>
再随意上传一张picture
[root@server1 ~]# ls /var/www/html/docker.png 
/var/www/html/docker.png
[root@server2 ~]# ls /var/www/html/docker.png 
/var/www/html/docker.png
[root@server1 ~]# systemctl restart httpd.service 
[root@server2 ~]# systemctl restart httpd.service 
测试
[root@nginx ~]# for i in `seq 1 5`;do curl 192.168.220.130 ;done;
server1
server2
server1
server2
server1

HAproxy实现负载均衡

HAProxy概述:

HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。根据官方数据,其最高极限支持10G的并发。

HAProxy特别适用于那些负载特大的web站点, 这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。

其支持从4层至7层的网络交换,即覆盖所有的TCP协议。就是说,Haproxy 甚至还支持 Mysql的均衡负载。

相同点:在功能上,proxy通过反向代理方式实现 WEB均衡负载。和 Nginx,ApacheProxy,lighttpd,Cheroke 等一样。

不同点:Haproxy 并不是 web 服务器。以上提到所有带反向代理均衡负载的产品,都清一色是 WEB 服务器。简单说,就是他们能处理解析页面的。而Haproxy 仅仅是一款的用于均衡负载的应用代理。其自身并不能提供web服务。

但其配置简单,拥有非常不错的服务器健康检查功能还有专门的系统状态监控页面,当其代理的后端服务器出现故障, HAProxy会自动将该服务器摘除,故障恢复后再自动将该服务器加入

HAproxy配置

[root@proxy ~]# yum install haproxy -y
[root@proxy ~]# cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak
[root@proxy ~]# cat  /etc/haproxy/haproxy.cfg.bak | grep -v ^$ | grep -v ^# > /etc/haproxy/haproxy.cfg
修改配置
[root@proxy ~]# vim /etc/haproxy/haproxy.cfg
 1 global
  2     # to have these messages end up in /var/log/haproxy.log you will
  3     # need to:
  4     #
  5     # 1) configure syslog to accept network log events.  This is done
  6     #    by adding the '-r' option to the SYSLOGD_OPTIONS in
  7     #    /etc/sysconfig/syslog
  8     #
  9     # 2) configure local2 events to go to the /var/log/haproxy.log
 10     #   file. A line like the following can be added to
 11     #   /etc/sysconfig/syslog
 12     #
 13     #    local2.*                       /var/log/haproxy.log
 14     #
 15     log         127.0.0.1 local2
 16     chroot      /var/lib/haproxy
 17     pidfile     /var/run/haproxy.pid
 18     maxconn     4000
 19     user        haproxy
 20     group       haproxy
 21     daemon
 22     # turn on stats unix socket
 23     stats socket /var/lib/haproxy/stats
 24 defaults
 25     mode                    http
 26     log                     global
 27     option                  httplog
 30     option forwardfor       except 127.0.0.0/8
 31     option                  redispatch
 32     retries                 3
 33     timeout http-request    10s		
 34     timeout queue           1m
 35     timeout connect         10s		#连接超时时间
 36     timeout client          1m		#客户端连接超时时间
 37     timeout server          1m		#服务器连接超时时间
 38     timeout http-keep-alive 10s
 39     timeout check           10s
 40     maxconn                 3000
 41     stats uri /haproxy-stats	 #haproxy 监控页面的访问地址
 42 frontend  main *:80	
 43     acl url_static       path_beg       -i /static /images /javascript /stylesh    eets
 44     acl url_static       path_end       -i .jpg .gif .png .css .js
 45     use_backend static          if url_static
 46     default_backend             http	#默认后端,名字自定义,要和下面对应
 47 backend static
 48     balance     roundrobin		#负载方式,轮询
 49     server      static 127.0.0.1:4331 check	
 50 backend http		#和上方对应
 51     balance     roundrobin
 52     server  s1 192.168.220.138:80 check		#服务器ip
 53     server  s2 192.168.220.139:80 check

测试

[root@proxy ~]# for i in `seq 1 5`; do curl 192.168.220.130; done 
server1
server2
server1
server2
server1
通过http://192.168.220.130/haproxy-stats访问监控页面
发布了65 篇原创文章 · 获赞 48 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/DoloresOOO/article/details/100518241