您的位置:首页 > 运维架构 > Tomcat

单点Haproxy负载tomcat安装配置

2017-03-24 10:50 211 查看
1、最近在工作中遇到Haproxy相关的活,之前并没有玩过于是有点蒙圈。趁空闲的时候折腾了下,完成了Haproxy作为后端两台tomcat服务的负载均衡的配置实验。之前只配过LVS,没有玩过Haproxy,也一直没有写过博客。这篇属于处女之作,争取早日养成写技术博客的习惯。废话不多说,进入正题。

2、实验架构图

OS:Redhat 6.5 64位

Haproxy 版本:1.6.11



 

 

3、安装软件

1)官网下载Haproxy安装包  haproxy-1.6.11.tar.gz

上传到/tmp下并解压 

tar -zxvf haproxy-1.6.11.tar.gz

创建Haproxy的用户和组 

groupadd haproxy

useradd -g haproxy -u 503  -s /sbin/nologin haproxy

cd  /tmp/haproxy-1.6.11

编译Haproxy

make TARGET=linux2628 ARCH=x86_64 PREFIX=/usr/local/haproxy

//关于TARGET ARCH 的值

TARGET variable :

- linux2628 for Linux 2.6.28, 3.x, and above (enables splice and tproxy)

ARCH=x86_64  我的系统是Redhat 6.5  64位

PREFIX指定Haproxy安装的目录为/usr/local/haproxy

[root@csv-ora12c-rac1 haproxy-1.6.11]# make install PREFIX=/usr/local/haproxy 

install -d "/usr/local/haproxy/sbin" 

install haproxy "/usr/local/haproxy/sbin" 

install -d "/usr/local/haproxy/share/man"/man1 

install -m 644 doc/haproxy.1 "/usr/local/haproxy/share/man"/man1 

install -d "/usr/local/haproxy/doc/haproxy" 

for x in architecture close-options configuration cookie-options intro linux-syn-cookies lua management network-
namespaces proxy-protocol; do \ install -m 644 doc/$x.txt "/usr/local/haproxy/doc/haproxy" ; \ done

[root@csv-ora12c-rac1 haproxy]# pwd 

/usr/local/haproxy 

[root@csv-ora12c-rac1 haproxy]# ls -la 

total 24 

drwxr-xr-x 6 root root 4096 Mar 9 15:42 . 

drwxr-xr-x. 14 root root 4096 Mar 10 08:55 .. 

drwxr-xr-x 3 root root 4096 Mar 9 14:39 doc 

drwxr-xr-x 2 root root 4096 Mar 9 15:42 errorfiles 

drwxr-xr-x 2 root root 4096 Mar 9 14:39 sbin 

drwxr-xr-x 3 root root 4096 Mar 9 14:39 share

2)后端tomcat安装配置

省略。。。

两台tomcat 实例分别如下:
http://20.26.25.231:8080/test/ http://20.26.25.233:8080/test/
至此,软件环境安装已完成。

4、修改Haproxy配置文件

mkdir /etc/haproxy

vi /etc/haproxy/haproxy.cfg

本次实验的配置文件如下

[root@csv-ora12c-rac1 haproxy]# cat haproxy.cfg

#

# This is a sample configuration. It illustrates how to separate static objects

# traffic from dynamic traffic, and how to dynamically regulate the server load.

#

# It listens on 192.168.1.10:80, and directs all requests for Host 'img' or

# URIs starting with /img or /css to a dedicated group of servers. URIs

# starting with /admin/stats deliver the stats page.

#

 

global

        maxconn         10000                       

//最大连接数

        stats socket    /var/run/haproxy.stat mode 600 level admin

        log             127.0.0.1 local0

         log            127.0.0.1 local1 notice

         nbproc 1  

//进程数量(可以设置多个进程提高性能) 

         pidfile /var/run/haproxy.pid

//haproxy的pid存放路径,启动进程的用户必须有权限访问此文件

        uid             503   

        gid             510

//进程的属主

        chroot          /usr/local/haproxy

//运行的路径 

        daemon  

//后台形式运行haproxy

defaults

         log global

         mode http  

//所处理的类别 (7层 http;4层tcp  )

         option  httplog 
#日志类别http日志格式

         option  dontlognull  #不记录健康检查的日志信息

         retries 3    #3次连接失败就认为服务不可用,也可以通过后面设置

         log             127.0.0.1 local3

         option  forwardfor  #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip

         option  httpclose 
#每次请求完毕后主动关闭http通道

         option abortonclose   #当服务器负载很高的时候,自动结束掉当前队列处理比较久的连接 

         timeout connect  5000ms #连接超时 

         timeout client 30000ms #客户端超时

         timeout server 30000ms  #服务器超时 

         balance roundrobin  #默认的负载均衡的方式,轮询方式

 

 

listen admin_status                

        bind 0.0.0.0:9000          

        mode http                  

        stats refresh 5s    

         stats enable      

        stats uri /admin?stats     

        stats realm haproxy\Statistics

        stats auth admin:admin     

        stats hide-version         

        stats admin if TRUE

 

         errorfile 400 /usr/local/haproxy/errorfiles/400.http

         errorfile 403 /usr/local/haproxy/errorfiles/403.http

         errorfile 408 /usr/local/haproxy/errorfiles/408.http

         errorfile 500 /usr/local/haproxy/errorfiles/500.http

         errorfile 502 /usr/local/haproxy/errorfiles/502.http

         errorfile 503 /usr/local/haproxy/errorfiles/503.http

         errorfile 504 /usr/local/haproxy/errorfiles/504.http

 

         capture request  header Host           len 40

         capture request  header Content-Length len 10

         capture request  header Referer        len 200

         capture response header Server         len 40

         capture response header Content-Length len 10

         capture response header Cache-Control  len 8   

 

          bind-process    1

# The public 'www' address in the DMZ

frontend  http_80_in

        bind            0.0.0.0:8080

        mode            http

        log             global

        option          httplog

        option          dontlognull

        monitor-uri     /monitoruri

        maxconn         8000

        timeout client  30s

 

        stats uri       /admin/stats

        default_backend yhtest

 

# The static backend backend for 'Host: img', /img and /css.

backend yhtest

        mode            http

        balance         roundrobin

        option prefer-last-server

        retries         2

        option redispatch

        timeout connect 5s

        timeout server  5s

        option httpchk  HEAD /favicon.ico

        server          statsrv1 20.26.25.231:8080/test check inter 1000

        server          statsrv2 20.26.25.233:8080/test check inter 1000

修改rsyslog配置

[root@csv-ora12c-rac1 haproxy]#vi /etc/rsyslog.conf 

增加以下三行:

local3.*    /var/log/haproxy.log

local0.*    /var/log/haproxy.log

local1.*    /var/log/haproxy.log

去掉以下两行的#号

$ModLoad imudp                     //删除前面的#

$UDPServerRun 514               //删除前面的#

修改rsyslog启动参数

vi /etc/sysconfig/rsyslog 

SYSLOGD_OPTIONS="-r -m 0"

service rsyslog restart

5、启动Haproxy

/usr/local/haproxy/sbin/haproxy  -f /etc/haproxy/haproxy.cfg

6、登录web查看Haproxy信息
http://20.26.25.236:8080/admin/stats


7、测试Haproxy作为软件负载的功能,负载的方式是轮训,即第一个访问是节点1,第二次访问是节点2,第三次又是节点1 http://20.26.25.236:8080/test/   测试页面
测试结果:
 第一次访问,展现的结果是231,刷新一下当前页面,展现的结果是233.说明我们的Haproxy起到了负载均衡的效果





8、第一个折腾难免会遇到很多坑,在此列举下我本次实验遇到的问题

1)启动haproxy告警

[WARNING] 075/085900 (22322) : stats socket will not work as expected in multi-process mode (nbproc > 1), you 
should force process binding globally using 'stats bind-process' or per socket using the 'process' attribute.

[WARNING] 075/085900 (22322) : Proxy 'admin_status': in multi-process mode, stats will be limited to process 
assigned to the current request.

[WARNING] 075/085900 (22322) : Proxy 'admin_status': stats admin will not work correctly in multi-process mode.

[WARNING] 075/085900 (22322) : Proxy 'http_80_in': in multi-process mode, stats will be limited to process assigned 
to the current request.

刚开始/etc/haproxy/haproxy.cfg中nbproc 这个我配置的是2,就像上面告警信息的说我的nbproc 大于1,就要设
置stats bind-process,测试环境我涂省事就直接修改成了nbproc=1,上述告警全部消失了,有空再去深究下。

2)/var/log/haproxy无日志信息

这里参考了网上的博客http://blog.csdn.net/huithe/article/details/13984767,说以下三行信息中间不能用空格。然后我
尝试着用tab替换掉空格,但同时刚开始的时候我忘了取消以下两行的#号,当时我用lsof -i :514是看不到端口信息。
我想问题就是应该出在这里,而不是tab替换空格的问题。后面我把tab改回成空格后,日志照样能打印
到/var/log/haproxy.log文件中。当时我还怀疑过haproxy.log这个文件的权限属性,当时为600,修改rsyslog.conf的同
时我也把haproxy.log文件属性改成777,结果就能看到了。但是我改回600后,日志也照样输出,所以我得出的结果
就是下面这两行没有去掉#。

$ModLoad imudp                     //删除前面的#

$UDPServerRun 514                   //删除前面的#

vi /etc/rsyslog.conf    

local3.* 
/var/log/haproxy.log      

local0.*
  /var/log/haproxy.log 

local1.*
  /var/log/haproxy.log

9.本章到此结束,第一次写博客,很多不足之处还请原谅,欢迎留言交流,下次空闲时用keepalived做Haproxy集群
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息