运维项目实训 - Haproxy负载均衡配置

一、镜像安装haproxy 1.14


[root@server7 ~]# yum install haproxy 
Loaded plugins: product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package haproxy.x86_64 0:1.4.24-2.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package         Arch           Version              Repository            Size
================================================================================
Installing:
 haproxy         x86_64         1.4.24-2.el6         LoadBalancer         457 k

Transaction Summary
================================================================================
Install       1 Package(s)

Total download size: 457 k
Installed size: 1.5 M
Is this ok [y/N]: y
Downloading Packages:
haproxy-1.4.24-2.el6.x86_64.rpm                          | 457 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : haproxy-1.4.24-2.el6.x86_64                                  1/1 
  Verifying  : haproxy-1.4.24-2.el6.x86_64                                  1/1 

Installed:
  haproxy.x86_64 0:1.4.24-2.el6                                                 

Complete!
[root@server7 ~]# cd /etc/haproxy/
[root@server7 haproxy]# ls
haproxy.cfg
[root@server7 haproxy]# vim haproxy.cfg 
#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  main *:80
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js

    use_backend static          if url_static
    default_backend             westos

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
    balance     roundrobin
    server      static 127.0.0.1:80 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend westos
    balance     roundrobin
    server  westos1 172.25.51.2:80 check inter 2000 fall 3 weight 30
    server  westos1 172.25.51.3:80 check inter 2000 fall 3 weight 30

启动haproxy

[root@server7 haproxy]# /etc/init.d/haproxy start
Starting haproxy:                                          [  OK  ]

测试:

server2:

[root@server2: ~]# /etc/init.d/httpd start
Starting httpd: httpd: apr_sockaddr_info_get() failed for server2:
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName

server3

[root@server3: ~]# /etc/init.d/httpd start
Starting httpd: httpd: apr_sockaddr_info_get() failed for server3:
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName

物理机:

[kiosk@foundation51 Desktop]$ curl 172.25.51.7:80
<h1>server2</h1>
[kiosk@foundation51 Desktop]$ curl 172.25.51.7:80
<h1>server3</h1>
[kiosk@foundation51 Desktop]$ curl 172.25.51.7:80
<h1>server2</h1>
[kiosk@foundation51 Desktop]$ curl 172.25.51.7:80
<h1>server3</h1>
[kiosk@foundation51 Desktop]$ curl 172.25.51.7:80
<h1>server2</h1>
[kiosk@foundation51 Desktop]$ curl 172.25.51.7:80
<h1>server3</h1>
[kiosk@foundation51 Desktop]$ curl 172.25.51.7:80
<h1>server2</h1>
[kiosk@foundation51 Desktop]$ curl 172.25.51.7:80
<h1>server3</h1>
[kiosk@foundation51 Desktop]$ curl 172.25.51.7:80
<h1>server2</h1>
[kiosk@foundation51 Desktop]$ curl 172.25.51.7
<h1>server3</h1>
[kiosk@foundation51 Desktop]$ curl 172.25.51.7
<h1>server2</h1>
[kiosk@foundation51 Desktop]$ curl 172.25.51.7
<h1>server3</h1>
[kiosk@foundation51 Desktop]$ curl 172.25.51.7
<h1>server2</h1>
[kiosk@foundation51 Desktop]$ curl 172.25.51.7
<h1>server3</h1>
[kiosk@foundation51 Desktop]$ curl 172.25.51.7
<h1>server2</h1>
[kiosk@foundation51 Desktop]$ curl 172.25.51.7
<h1>server3</h1>

二、安装1.16.11版本gz包

安装目录:/opt/haproxy 
日志目录:/data/logs/haproxy/haproxy.log 
所属用户:haproxy.haproxy

获取haproxy

[root@server7 ~]# ls
haproxy-1.6.11.tar.gz

编译haproxy

[root@server7 ~]# tar -zxvf haproxy-1.5.9.tar.gz
[root@server7 ~]# cd haproxy-1.6.11
[root@server7 haproxy-1.6.11]# ls
CHANGELOG     doc       include      Makefile  src      VERDATE
contrib       ebtree    LICENSE      README    SUBVERS  VERSION
CONTRIBUTING  examples  MAINTAINERS  ROADMAP   tests
[root@server7 haproxy-1.6.11]# make TARGET=linux26 PREFIX=/opt/haproxy
##如果报错缺少gcc编译文件,下载即可。
[root@server7 haproxy-1.6.11]# make install PREFIX=/opt/haproxy

修改配置文件

Haproxy安装完后默认是没有配置文件的,需要手动vi haproxy.cfg编译一个。

[root@server7 ~]#  cd /opt/haproxy
[root@server7 ~]#  cp /opt/soft/haproxy-1.5.9/examples/haproxy.cfg /opt/haproxy/

修改配置文件:

[root@server7 haproxy]# vim haproxy.cfg
global
#       log 127:0:0:1  local1 info
        log /dev/log    local0
        log /dev/log    local1 notice
        chroot /var/lib/haproxy
        user haproxy
        group haproxy
        daemon

defaults
        log     global
        mode    tcp
#       option  httplog
        option  dontlognull
        maxconn 40960
        contimeout 5000
        clitimeout 50000
        srvtimeout 50000


listen admin
        bind 0.0.0.0:8080
        mode tcp
        balance leastconn  
        server westos 172.25.51.2:8080
        server westos 172.25.51.3:8080

测试方法和上面一样

猜你喜欢

转载自blog.csdn.net/lx543733371/article/details/80822633