Haproxy+Nginx实现负载均衡

一、什么是Haproxy

HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。

HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。点击查看原文

二、负载均衡常用的调度算法

轮循(Round Robin)

这种方法会将收到的请求循环分配到服务器集群中的每台机器,即有效服务器。如果使用这种方式,所有的标记进入虚拟服务的服务器应该有相近的资源容量 以及负载形同的应用程序。如果所有的服务器有相同或者相近的性能那么选择这种方式会使服务器负载形同。基于这个前提,轮循调度是一个简单而有效的分配请求 的方式。然而对于服务器不同的情况,选择这种方式就意味着能力比较弱的服务器也会在下一轮循环中接受轮循,即使这个服务器已经不能再处理当前这个请求了。 这可能导致能力较弱的服务器超载。

最少连接数(Least Connection)

以上两种方法都没有考虑的是系统不能识别在给定的时间里保持了多少连接。因此可能发生,服务器B服务器收到的连接比服务器A少但是它已经超载,因为 服务器B上的用户打开连接持续的时间更长。这就是说连接数即服务器的负载是累加的。这种潜在的问题可以通过“最少连接数”算法来避免:传入的请求是根据每 台服务器当前所打开的连接数来分配的。即活跃连接数最少的服务器会自动接收下一个传入的请求。接本上和简单轮询的原则相同:所有拥有虚拟服务的服务器资源 容量应该相近。值得注意的是,在流量率低的配置环境中,各服务器的流量并不是相同的,会优先考虑第一台服务器。这是因为,如果所有的服务器是相同的,那么 第一个服务器优先,直到第一台服务器有连续的活跃流量,否则总是会优先选择第一台服务器。

源IP哈希(Source IP Hash)

这种方式通过生成请求源IP的哈希值,并通过这个哈希值来找到正确的真实服务器。这意味着对于同一主机来说他对应的服务器总是相同。使用这种方式,你不需要保存任何源IP。但是需要注意,这种方式可能导致服务器负载不平衡。

以上信息来自网络更多调度算法请点击该链接

三、案例环境

主机 操作系统 IP地址
主要软件

Haproxy Centos 6.5 x86_x64 192.168.25.5 haproxy-1.4.24.tar.gz
Nginx Server 1 Centos 6.5 x86_x64 192.168.25.3 nginx-1.6.0.tar.gz
Nginx Server 2 Centos 6.5 x86_x64 192.168.25.4 nginx-1.6.0.tar.gz
Client Windows 7 192.168.25.6 IE浏览器

这里服务器是托管在IDC中,公网访问使用的是防火墙NAT映射的公网IP,因此服务器端只需要配置一个内网IP即可。如果没有防火墙映射,建议在服务器端配置双网卡双IP,公网请求访问公网IP的网卡,Haproxy与各个节点间通信使用内网网卡。

四、案例实施

1、编译安装Nginx服务器

1)安装一些必要组件

[[email protected] ~]#yum -y install pcre-devel zlib-devel gcc gcc-c++ make

2)创建nginx用户,并设置为禁止登陆

[[email protected] ~]#useradd -M -s /sbin/nologin nginx

3)解压并进入到nginx安装目录

[[email protected] ~]# tar xf nginx-1.6.0.tar.gz

[[email protected] ~]# cd nginx-1.6.0

4)编译安装nginx

[[email protected] nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install

5)创建一个软连接,方便nginx的启动

[[email protected] nginx-1.6.0]# ls /usr/local/nginx/

conf  html  logs  sbin

[[email protected] nginx-1.6.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

[[email protected] nginx-1.6.0]# ll /usr/local/sbin/nginx

lrwxrwxrwx. 1 root root 27 12月  7 06:41 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx

6)检查语句是否存在错误

[[email protected] nginx-1.6.0]# 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

7)启动nginx

[[email protected] nginx-1.6.0]# nginx

8)检查nginx启动情况

[[email protected] nginx-1.6.0]# netstat -anpt | grep nginx

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      3465/nginx

9)测试nginx启动情况

[[email protected] nginx-1.6.0]# killall -s HUP nginx

平滑重启nginx(reload)等同于下个命令

[[email protected] nginx-1.6.0]#killall -s QUIT nginx //正常停止nginx (stop) 等同于下个命令

[[email protected] nginx-1.6.0]#killall -3 nginx

[[email protected] nginx-1.6.0]#killall -s USR1 nginx //用于nginx的日志切换,也就是重新打开一个日志文件,例如每天要生成一个日志文件时,可以使用这个信号来控制。

[[email protected] nginx-1.6.0]#killall -s USR2 nginx //用于平滑升级可执行程序。


10)编写nginx启动脚

 [[email protected] nginx-1.6.0]# vim /etc/init.d/nginx

#!/bin/bash

# chkconfig: 2345 99 20

# description: Nginx ServerControl Scripts shell

PROG="/usr/local/nginx/sbin/nginx"

PIDF="/usr/local/nginx/logs/nginx.pid"

case "$1" in

  start)

  if [ -f $PIDF ]; then

     echo "Nginx is running.. Start it is error"

  else

     $PROG

  fi

  ;;

  stop)

  if [ -f $PIDF ]; then

     kill -s QUIT $(cat $PIDF)

     rm -rf $PIDF

  else

     echo "Nginx is stopping .. Stop it is error"

  fi

  ;;

  restart)

     $0 stop

     $0 start

  ;;

  reload)

  if [ -f $PIDF ]; then

     kill -s HUP $(cat $PIDF)

  else

     echo "Nginx is stopping . reload it is error"

  fi

  ;;

  status)

  if [ -f $PIDF ]; then

     echo "Nginx is running"

  else

     echo "Nginx is stopping"

  fi

  ;;

  *)

  echo "Usage: $0 (start|stop|restart|reload|status)"

  exit 1

esac

exit 0


[[email protected] nginx-1.6.0]# chmod +x /etc/init.d/nginx

给脚本执行权限

[[email protected] nginx-1.6.0]# chkconfig --add nginx

添加为系统服务

[[email protected] nginx-1.6.0]# chkconfig --list nginx

nginx           0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭

[[email protected] nginx-1.6.0]# service nginx status

Nginx is running

[[email protected] nginx-1.6.0]# service nginx stop

[[email protected] nginx-1.6.0]# netstat -anpt | grep nginx

[[email protected] nginx-1.6.0]# service nginx start

[[email protected] nginx-1.6.0]# netstat -anpt | grep nginx

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      3536/nginx

[[email protected] nginx-1.6.0]# service nginx restart

[[email protected] nginx-1.6.0]# netstat -anpt | grep nginx

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      3551/nginx

测试nginx服务脚本是否可以正确使用

11)编辑nginx配置文件

[[email protected] ~]# cd /usr/local/nginx/conf/

[[email protected] conf]# vim nginx.conf

user  nginx;

worker_processes  1;

#error_log  logs/error.log;

error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

pid        logs/nginx.pid;

events {

use epoll;

worker_connections  1024;

}

http {

include       mime.types;

default_type  application/octet-stream;

log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘

‘$status $body_bytes_sent "$http_referer" ‘

‘"$http_user_agent" "$http_x_forwarded_for"‘;

#access_log  logs/access.log  main;

sendfile        on;

#tcp_nopush     on;

#keepalive_timeout  0;

keepalive_timeout  65;

#gzip  on;

server {

listen       80;

server_name  nginx1;

charset utf-8;

#access_log  logs/host.access.log  main;

location / {

root   /var/www/html;

index  index.html index.htm;

}

error_page  404              /404.html;

# redirect server error pages to the static page /50x.html

#

error_page   500 502 503 504  /50x.html;

location = /50x.html {

root   html;

}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80

#

#location ~ \.php$ {

#    proxy_pass   http://127.0.0.1;

#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#

#location ~ \.php$ {

#    root           html;

#    fastcgi_pass   127.0.0.1:9000;

#    fastcgi_index  index.php;

#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

#    include        fastcgi_params;

#}

# deny access to .htaccess files, if Apache‘s document root

# concurs with nginx‘s one

#

#location ~ /\.ht {

#    deny  all;

#}

}

# another virtual host using mix of IP-, name-, and port-based configuration

#

#server {

#    listen       8000;

#    listen       somename:8080;

#    server_name  somename  alias  another.alias;

#    location / {

#        root   html;

#        index  index.html index.htm;

#    }

#}

# HTTPS server

#

#server {

#    listen       443 ssl;

#    server_name  localhost;

#    ssl_certificate      cert.pem;

#    ssl_certificate_key  cert.key;

#    ssl_session_cache    shared:SSL:1m;

#    ssl_session_timeout  5m;

#    ssl_ciphers  HIGH:!aNULL:!MD5;

#    ssl_prefer_server_ciphers  on;

#    location / {

#        root   html;

#        index  index.html index.htm;

#    }

#}

}

将nginx的配置文件改成和上面相同即可

[[email protected] conf]# mkdir -p /var/www/html

创建网页文件根目录

[[email protected] conf]# echo "nginx1" > /var/www/html/index.html

将网页的内容写到网页的根目录下

[[email protected] conf]# cat /var/www/html/index.html

nginx1

[[email protected] conf]# service nginx restart

12)测试nginx服务器是否搭建成功

从图中可以看出两台nginx服务器已经可以正常访问,我们需要按照步(1)—(11)安装nginx server2 这里不再做过多赘述。

2、编译安装Haproxy

1)安装支持软件

[[email protected] haproxy-1.4.24]# yum -y install pcre-devel zlib-devel bzip2-devel

[[email protected] haproxy-1.4.24]# yum -y install gcc* make

2)编译并安装Haproxy

[[email protected] ~]# tar xf haproxy-1.4.24.tar.gz

[[email protected] ~]# cd haproxy-1.4.24

[[email protected] haproxy-1.4.24]# make TARGET=linux26    ←指定linux系统位数

[[email protected] haproxy-1.4.24]# make install

3)修改Haproxy配置文件

[[email protected] haproxy-1.4.24]# mkdir /etc/haproxy

[[email protected] haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/ ←将haproxy.cfg文件复制到配置文件目录。

[[email protected] haproxy-1.4.24]# vim /etc/haproxy/haproxy.cfg

# this config needs haproxy-1.1.28 or haproxy-1.2.1

global

log 127.0.0.1 local0 ←配置日志记录,local0为日志设备,默认在系统日志

log 127.0.0.1 local1 notice ←notice为日志级别,通常有24个级别

#log loghost local0 info

maxconn 4096    ←最大连接数

# chroot /usr/share/haproxy   ←禁锢目录

uid 99  ←用户UID

gid 99  ←用户GID

daemon  ←守护模式

#debug

#quiet

defaults

log global

mode http    ←工作在7层,tcp在四层

option httplog

option dontlognull

retries 3

# redispatch

maxconn 2000

contimeout 5000

clitimeout 50000

srvtimeout 50000

listen  nginx-web 0.0.0.0:80 ←定义一个nginx-web的应用

option  httpchk GET /index.html ←健康检查服务器的主页文件,方式为GET

option  persist ←强制将请求发送到已经DOWN掉的服务器

balance roundrobin ←负载均衡调度算法为轮询。source是来源

server inst1 192.168.25.3:80 check inter 2000 fall 3 ←定义在线节点

server inst2 192.168.25.4:80 check inter 2000 fall 3  ←定义备份节点

4)创建自启动脚本

[[email protected] haproxy-1.4.24]# cp examples/haproxy.init /etc/init.d/haproxy

[[email protected] haproxy-1.4.24]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy

[[email protected] haproxy-1.4.24]# /etc/init.d/haproxy start

-bash: /etc/init.d/haproxy: 权限不够

[[email protected] haproxy-1.4.24]# chmod +x /etc/init.d/ha

halt     haproxy

[[email protected] haproxy-1.4.24]# chmod +x /etc/init.d/haproxy

[[email protected] haproxy-1.4.24]# chkconfig --add /etc/init.d/haproxy

[[email protected] haproxy-1.4.24]# chkconfig haproxy on

[[email protected] haproxy-1.4.24]# chkconfig --list haproxy

haproxy         0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭

[[email protected] haproxy-1.4.24]# /etc/init.d/haproxy start

Starting haproxy:                                          [确定]

5)测试负载均衡是否搭建成功

从图中我们可以看出访问同一个ip地址两次出现了两个不同的网页,所以我们的Haproxy负载均衡就搭建完了,但是大家需要注意的是windows的浏览器会有缓存,有时候会显示的不是特别及时,下面给大家看下linux下访问的结果

从图中可以看出linux的响应很及时,到这儿我们的服务就搭完了,后续我还会跟进其他调度算法的实验步骤,本次实验中如有哪里做的有错误或不对的地方请大家及时和探讨,以免给大家造成错误指导!

时间: 2024-07-31 14:30:37

Haproxy+Nginx实现负载均衡的相关文章

haproxy/nginx+keepalived负载均衡 双机热备 邮件报警 实战及常见问题

Haproxy 做http和tcp反向代理和负载均衡keepalived 为两台 Haproxy 服务器做高可用/主备切换.nginx   为内网服务器做正向代理,如果业务需求有变化,也可以部分替代 haproxy 做 http 反向代理.如果发生主备切换,向指定邮箱发送报警邮件. 本文比较裹脚布,没耐心的就别看了. 一.两台服务器,系统 CentOS6主机名        外网IP        内网IPlbserver_01  202.1.1.101   10.1.1.11/24lbserv

使用Haproxy+nginx 搭建负载均衡集群

案例环境 本案例使用三台服务器模拟搭建一套Web集群,如下所示: 实验步骤如下: 编辑安装nginx服务器安装依赖包下载nginx-01服务器[[email protected] ~]# yum -y install gcc pcre-devel zlib-devel[[email protected] ~]# wget https://nginx.org/download/nginx-1.6.3.tar.gz[[email protected] ~]# tar zxvf nginx-1.6.3

使用nginx+Apache负载均衡及动静分离

使用nginx+Apache负载均衡及动静分离 介绍    LB负载均衡集群分两类: LVS (四层)和 nginx或haproxy (七层)    客户端都是通过访问分发器的VIP来访问网站 在七层中的网站页面有: .php .html .png .jpeg .jsp 等, 有动态页面有静态页面. 需要在应用层基于不同的应用进行分发. 一:实验拓扑图:     二:实验目标 实战:使用Apache+nginx实现动静分离的负载均衡集群 三:实验环境 主机作用分类 主机名 IP地址 安装软件 N

基于nginx的负载均衡概述与实现

前言: 前面我们提到了lvs和keepalived结合起来的高可用负载均衡,lvs根据原目ip地址及端口将其调度转发至后端 的某个主机,是一种四层的实现,因为lvs是四层的,所以不会受限于套接字或打开的文件数量.不过,如果我们想实现一些更高阶的功能,lvs就显得力不从心了,比如基于uri,cookie,header头部信息的负载均衡,此时我们就可以选择一些7层的负载均衡实现,比如nginx或haproxy等.本次我们就先来讲讲nginx的负载均衡把~ 正文: 其实,如果对lvs的各种类型和调度有

HAProxy高可用负载均衡集群部署

HAProxy高可用负载均衡集群部署 基本信息: 系统平台:VMware WorkStation 系统版本: CentOS Linux release 7.2.1511 (Core) 内核版本: 3.10.0-327.el7.x86_64 集群架构: 前端:HAProxy 1.虚拟FQDN:www.simpletime.net 2.VIP:192.168.39.1:DIP:172.16.39.50 3.调度服务器:Varnish1.Varnish2 4.调度算法:URL_Hash_Consist

haproxy高可用负载均衡

常见的做负载均衡的机制:nginx,lvs,haproxy 适合做网站调度:nginx,haproxy 适合做应用层,如mysql数据库:lvs haproxy特点:吞吐量很高 HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代 理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理.HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接.并且它的运行模式使得它可以很简单安全的整合

nginx配置负载均衡详解

一.负载均衡简介 1.什么是负载均衡 负载均衡 建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽.增加吞吐量.加强网络数据处理能力.提高网络的灵活性和可用性. 负载均衡,英文名称为Load Balance,其意思就是分摊到多个操作单元上进行执行,例如Web服务器.FTP服务器.企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务. 简单的来说.负载均衡可以减少服务器的压力,将原本一台服务器所要承受的访问量分给了多台,并提高了项目的可用性,当一台服务器挂掉

HAProxy实现高级负载均衡实战和ACL控制

 haproxy实现高级负载均衡实战 环境:随着公司业务的发展,公司负载均衡服务已经实现四层负载均衡,但业务的复杂程度提升,公司要求把mobile手机站点作为单独的服务提供,不在和pc站点一起提供服务,此时需要做7层规则负载均衡,运维总监要求,能否用一种服务同既能实现七层负载均衡,又能实现四层负载均衡,并且性能高效,配置管理容易,而且还是开源. 实验前准备: ① 两台服务器都使用yum 方式安装haproxy yum -y install haproxy ② iptables -F &&

Nginx做负载均衡时session共享问题详解

用nginx做负载均衡时,同一个IP访问同一个页面会被分配到不同的服务器上,如果session不同步的话,就会出现很多问题,比如说最常见的登录状态. 再者Nginx连接Memcached集群时,Nignx的请求从memcached服务器中根据key获得了value则直接返回value,如果没有获得到value则去MySQL中查询再返回. location / { set $memcached_key "$request_uri"; #设置请求memcached服务器的key memca