haproxy负载均衡

1、安装haproxyhtml

1.下载haproxy软件包并解压linux

[root@CentOS-01 ~]# wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz
[root@CentOS-01 ~]# ll
总用量 2060
-rw-r--r--   1 root root 836768 6月  17 21:28 haproxy-1.4.24.tar.gz
[root@CentOS-01 ~]# tar xf haproxy-1.4.24.tar.gz
[root@CentOS-01 ~]# 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.安装haporxyredis

[root@CentOS-01 ~]# cd haproxy-1.4.24
[root@CentOS-01 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
[root@CentOS-01 haproxy-1.4.24]# uname -r
2.6.32-431.el6.x86_64
[root@CentOS-01 haproxy-1.4.24]# make TARGET=linux26 PREFIX=/usr/local/haproxy
[root@CentOS-01 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.配置haproxyssh

[root@CentOS-01 haproxy-1.4.24]# cp examples/haproxy.cfg /usr/local/haproxy/
[root@CentOS-01 haproxy-1.4.24]# cd /usr/local/haproxy/
[root@CentOS-01 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
[root@CentOS-01 ~]# 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.启动haproxytcp

[root@CentOS-01 haproxy]#sbin/haproxy -f haproxy.cfg
[root@CentOS-01 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  



2、在s1端安装httpdide

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


3、在s2端安装httpdui

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



4、验证this

180539405.png

180541378.png



进入haproxy的监控页面查看spa

180653799.png