一、安装haproxy

1.下载haproxy软件包并解压

[aaa@qq.com ~]# wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz
[aaa@qq.com ~]# ll
总用量 2060
-rw-r--r--   1 root root 836768 6月  17 21:28 haproxy-1.4.24.tar.gz
[aaa@qq.com ~]# tar xf haproxy-1.4.24.tar.gz
[aaa@qq.com ~]# ll
总用量 2064
drwxrwxr-x   9 root root   4096 6月  17 21:28 haproxy-1.4.24
-rw-r--r--   1 root root 836768 6月  17 21:28 haproxy-1.4.24.tar.gz


2.安装haporxy

[aaa@qq.com ~]# cd haproxy-1.4.24
[aaa@qq.com haproxy-1.4.24]# ll
总用量 248
-rw-rw-r-- 1 root root 125093 6月  17 21:28 CHANGELOG
drwxrwxr-x 7 root root   4096 6月  17 21:28 contrib
drwxrwxr-x 4 root root   4096 6月  17 21:28 doc
drwxrwxr-x 2 root root   4096 6月  17 21:28 ebtree
drwxrwxr-x 3 root root   4096 6月  17 21:28 examples
drwxrwxr-x 6 root root   4096 6月  17 21:28 include
-rw-rw-r-- 1 root root   1767 6月  17 21:28 LICENSE
-rw-rw-r-- 1 root root  22059 6月  17 21:28 Makefile
-rw-rw-r-- 1 root root   4974 6月  17 21:28 Makefile.bsd
-rw-rw-r-- 1 root root   5039 6月  17 21:28 Makefile.osx
-rw-rw-r-- 1 root root  21052 6月  17 21:28 README
-rw-rw-r-- 1 root root   4139 6月  17 21:28 ROADMAP
drwxrwxr-x 2 root root   4096 6月  17 21:28 src
-rw-rw-r-- 1 root root      1 6月  17 21:28 SUBVERS
drwxrwxr-x 2 root root   4096 6月  17 21:28 tests
-rw-rw-r-- 1 root root   6991 6月  17 21:28 TODO
-rw-rw-r-- 1 root root     11 6月  17 21:28 VERDATE
-rw-rw-r-- 1 root root      7 6月  17 21:28 VERSION
[aaa@qq.com haproxy-1.4.24]# uname -r
2.6.32-431.el6.x86_64
[aaa@qq.com haproxy-1.4.24]# make TARGET=linux26 PREFIX=/usr/local/haproxy
[aaa@qq.com haproxy-1.4.24]# 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 configuration architecture haproxy-en haproxy-fr; do \
                install -m 644 doc/$x.txt /usr/local/haproxy/doc/haproxy ; \
        done


3.配置haproxy

[aaa@qq.com haproxy-1.4.24]# cp examples/haproxy.cfg /usr/local/haproxy/
[aaa@qq.com haproxy-1.4.24]# cd /usr/local/haproxy/
[aaa@qq.com haproxy]# ll
总用量 16
drwxr-xr-x 3 root root 4096 12月 11 16:29 doc
-rw-r--r-- 1 root root 2366 12月 11 16:30 haproxy.cfg
drwxr-xr-x 2 root root 4096 12月 11 16:29 sbin
drwxr-xr-x 3 root root 4096 12月 11 16:29 share
[aaa@qq.com ~]# cat /usr/local/haproxy/haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
        log 127.0.0.1   local0
        #log 127.0.0.1  local1 notice
        #log loghost    local0 info
        maxconn 4096
        chroot /usr/share/haproxy
        uid 99
        gid 99
        daemon
        nbproc 1
        pidfile /usr/local/haproxy/haproxy.pid
        #debug
        #quiet
defaults
        #log    global
        log 127.0.0.1   local3
        mode    http
        option  httplog
        option  dontlognull
        option  forwardfor
        option  httpclose
        retries 3
        option  redispatch
        maxconn 2000
        stats uri       /haproxy-admin
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000
listen localhost *:8888
        mode http
        option httpchk GET /index.html
        server s1 192.168.1.210:80 weight 3 check
        server s2 192.168.1.220:80 weight 5 check


4.启动haproxy

[aaa@qq.com haproxy]#sbin/haproxy -f haproxy.cfg
[aaa@qq.com sbin]# netstat -lnpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name  
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      1574/rpcbind       
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1825/sshd          
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      1691/cupsd         
tcp        0      0 0.0.0.0:8888                0.0.0.0:*                   LISTEN      11389/./haproxy    
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1917/master        
tcp        0      0 0.0.0.0:34150               0.0.0.0:*                   LISTEN      1629/rpc.statd     
tcp        0      0 :::111                      :::*                        LISTEN      1574/rpcbind       
tcp        0      0 :::22                       :::*                        LISTEN      1825/sshd          
tcp        0      0 ::1:631                     :::*                        LISTEN      1691/cupsd         
tcp        0      0 ::1:25                      :::*                        LISTEN      1917/master        
tcp        0      0 :::48898                    :::*                        LISTEN      1629/rpc.statd     
tcp        0      0 :::873                      :::*                        LISTEN      1833/xinetd  



二、在s1端安装httpd

[aaa@qq.com sbin]# yum -y install httpd
[aaa@qq.com sbin]# echo "CentOS-01" > /var/www/html/index.html
[aaa@qq.com sbin]#
[aaa@qq.com sbin]# cat /var/www/html/index.html
CentOS-01
[aaa@qq.com sbin]


三、在s2端安装httpd

[aaa@qq.com ~]# yum -y install httpd
[aaa@qq.com ~]# echo "CentOS-02" > /var/www/html/index.html
[aaa@qq.com ~]#
[aaa@qq.com ~]# cat /var/www/html/index.html
CentOS-02
[aaa@qq.com ~]



四、验证

haproxy负载均衡

haproxy负载均衡



进入haproxy的监控页面查看

haproxy负载均衡