目 录CONTENT

文章目录

Haproxy安装配置验证web轮询

所念皆星河
2023-12-06 / 0 评论 / 0 点赞 / 22 阅读 / 6535 字

一、安装配置nginx

1. 安装源

curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache

2. 简单配置web2同web1

echo 'This is web 1' > /usr/share/nginx/html/index.html
systemctl start nginx

二、安装Haprox及配置

1. 安装haproxy

yum -y install haproxy

2. 编辑配置文件

vim /etc/haproxy/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 *:5000
    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             app

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

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
    balance     roundrobin
    server  app1 127.0.0.1:5001 check
    server  app2 127.0.0.1:5002 check
    server  app3 127.0.0.1:5003 check
    server  app4 127.0.0.1:5004 check
#listen webserver 0.0.0.0:80
#       balance roundrobin #轮训算法
#       server web1 172.21.8.145 check inter 2000 fall 3  #check inter 2000检测心跳频率单位毫秒,fall 3 3是3次失败认为服务器不可用,
#       server web2 172.21.8.244 check inter 2000 fall 3

####权重倍数 3比1  三次web1 一次web2
listen webserver 0.0.0.0:80
       balance roundrobin #轮训算法
       server web1 172.21.8.145 weight 30 check inter 2000 fall 3  #weight 30 权重
       server web2 172.21.8.244 weight 10 check inter 2000 fall 3

实现效果

1. 正常轮询 依次web1 web2 轮询

lps4ufkh.png
lps4us5l.png

2. 权重轮询3次web1 一次web2 轮询

0

评论区