ITPub博客

首页 > 应用开发 > IT综合 > 【Haproxy】haproxy负载均衡

【Haproxy】haproxy负载均衡

原创 IT综合 作者:小亮520cl 时间:2016-02-18 14:32:08 0 删除 编辑
1.安装
  1. 下载 http://pan.baidu.com/s/1i3SYsVN
  2. # cd haproxy-1.4.20
  3. # make TARGET=linux26 PREFIX=/usr/local/haproxy #将haproxy安装到/usr/local/haproxy
  4. # make install PREFIX=/usr/local/haproxy

2.配置

安装完毕后,进入安装目录配置文件,默认情况下目录里是没有.cfg配置文件的,可以回到安装文件目录下将examples下的haproxy.cfg拷贝到usr/local/haproxy下。

# cd /usr/local/haproxy

# vi haproxy.cfg


3配置tcp负载均衡

  1. [root@localhost conf]# vi haproxy.cfg
  2. global
  3.         log 127.0.0.1 local0
  4.         maxconn 4096
  5.         stats socket /tmp/haproxy.socket uid haproxy mode 770 level admin
  6.         # nbproc 2
  7.         pidfile /tmp/haproxy.pid
  8.         daemon

  9. defaults
  10.         log global
  11.         log 127.0.0.1 local0
  12.         mode tcp
  13.         option tcplog
  14.         option dontlognull
  15.         retries 3
  16.         option redispatch
  17.         maxconn 2000
  18.         timeout connect 5s
  19.         timeout client 120s
  20.         timeout server 120s

  21. listen mysql_proxy :23306        ---分发均衡的端口
  22.         mode tcp                       ---类型
  23.         balance roundrobin
  24.         server mysql_65 192.168.6.119:3306 check inter 5000 rise 2 fall 3     ###需要分发的地址
  25.         server mysql_67 192.168.6.122:3306 check inter 5000 rise 2 fall 3

  26. listen private_monitoring :8101      ----web页面查看的端口
  27.         mode http
  28.         option httplog
  29.         stats enable
  30.         stats uri /stats                  ####web访问页面
  31.         stats refresh 5s
  32.         stats auth Frank:Frank            #设置监控页面的用户和密码:Frank
  33.         stats hide-version                #隐藏统计页面的HAproxy版本信息

我们启动haproxy命令
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg


4 测试tcp分发:
  1. #第一次访问:访问到了119数据库
  2. [root@localhost conf]# mysql -uroot -pESBecs00 -P23306 -h192.168.6.121

    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | db1                |
    | db2                |
    | db3                |
    | hhhh               |
    | mysql              |
    | performance_schema |
    | test               |
    | test1              |
    | test2              |
    | xdy_link           |
    +--------------------+
    11 rows in set (0.07 sec)

  3. #第二次访问:访问到了122数据库
  4. [root@localhost conf]# mysql -uroot -pESBecs00 -P23306 -h192.168.6.121
    Warning: Using a password on the command line interface can be insecure.

    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | db1                |
    | db2                |
    | db3                |
    | mysql              |
    | performance_schema |
    | test               |
    | test1              |
    | test2              |
    +--------------------+
    9 rows in set (0.09 sec)


    tcp分发成功
  5. 登录web页面检查



5 配置http负载均衡

点击(此处)折叠或打开

  1. [root@localhost conf]# vi haproxy.cfg
  2. global
  3.         log 127.0.0.1 local0
  4.         maxconn 4096
  5.         stats socket /tmp/haproxy.socket uid haproxy mode 770 level admin
  6.         # nbproc 2
  7.         pidfile /tmp/haproxy.pid
  8.         daemon

  9. defaults
  10.         log global
  11.         log 127.0.0.1 local0
  12.         mode tcp
  13.         option tcplog
  14.         option dontlognull
  15.         retries 3
  16.         option redispatch
  17.         maxconn 2000
  18.         timeout connect 5s
  19.         timeout client 120s
  20.         timeout server 120s

  21. listen mysql_proxy :23306
  22.         mode http           ---这个地方不一样
  23.         balance roundrobin
  24.         server mysql_65 192.168.6.119:80 check inter 5000 rise 2 fall 3
  25.         server mysql_67 192.168.6.122:80 check inter 5000 rise 2 fall 3

  26. listen private_monitoring :8101
  27.         mode http
  28.         option httplog
  29.         stats enable
  30.         stats uri /stats
  31.         stats refresh 5s
  32.         stats auth Frank:Frank #设置监控页面的用户和密码:Frank

  33.         stats hide-version #隐藏统计页面的HAproxy版本信息

6测试http分发
  1. [root@localhost conf]# curl 192.168.6.121:23306
  2. 1234 this is 192.168.6.119
  3. [root@localhost conf]# curl 192.168.6.121:23306
  4. thisi is 192.168.6.122
  5. [root@localhost conf]# curl 192.168.6.121:23306
  6. 1234 this is 192.168.6.119
  7. [root@localhost conf]# curl 192.168.6.121:23306
  8. thisi is 192.168.6.122
  9. 成功

7 还可以通过域名配置分发:(因为我们网站可能对应多个域名)
  1. global
            log 127.0.0.1 local0 info #[err warning info debug] //日志位置
            maxconn 4096
            daemon #设置成后台运行
            nbproc 1 #进程数量


    defaults
            log     global
            mode    http #默认模式
            option  httplog #http日志格式
            option  dontlognull
            retries 3  #三次失败后认为服务器不可用
            option  redispatch  #如果cookie写入了serverId而客户端不会刷新cookie,当serverId对应的服务器挂掉后,强制定向到其他健康的服务
    器
            maxconn 2000 #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接默认的最大连接数
            contimeout 5000 #连接超时
            clitimeout 30000 #客户端超时
            srvtimeout 30000 #服务器超时


    frontend web_in
            mode http
            maxconn 1000
            bind :80
            acl is_a hdr_beg(host) -i www.test1.com  #判断域名是不是www.test1,是则给与a服务器集群服务
            acl is_b hdr_beg(host) -i www.test2.com  #判断域名是不是www.test2,是则给与b服务器集群服务


            use_backend a_server if is_a
            use_backend b_server if is_b


    backend a_server
            mode http #http 模式
            stats   uri  /haproxy
            balance roundrobin
            cookie  JSESSIONID prefix
            stats   hide-version
            option  httpclose
            server web1 218.78.186.162:8000 check
            server web1 218.78.186.163:8000 check



    backend b_server
            mode http #http 模式
            stats   uri  /haproxy
            balance roundrobin
            cookie  JSESSIONID prefix


            stats   hide-version
            option  httpclose
            server web1 218.78.186.162:8080 check



参考文档
http://blog.csdn.net/youyudehexie/article/details/7606504

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29096438/viewspace-1990588/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
毕业以后专业任职数据库工程师职位,itpub一直作为自己的笔记记录的地方,blog写的不详细,草书笔记,仅供参考!

注册时间:2013-09-12

  • 博文量
    529
  • 访问量
    1126951