Haproxy负载均衡示例

 负载均衡通过分散请求到多个服务器,确保系统稳定运行,避免单点过载,提升整体处理能力。

OK!废话不多说直接上!!!

# 环境

1.需要四台虚拟机

2.关闭防火墙,selinux,配置好yum源

3.虚拟机clink(客户端),haproxy(进行负载均衡的服务器),web1,web2

# 步骤

## 首先统一服务器时间

-命令ntpdate ntp.aliyun.com或者ntpdate cn.ntp.org.cn

## web1,web2创建测试页面

接下来配置web1,web2虚拟机

1.web1,web2安装http

命令:yum install -y httpd

2. 标识机器

-命令 web1:echo web11111 > /var/www/html/index.html

         web2:echo web2222 > /var/www/html/index.html

3. 启动httpd服务

命令:systemctl start httpd

4.测试-在浏览器中输入web1,web2ip,查询配置是否正确

--------------------------------------------------------分割线----------------------------------------------------------------

## 安装配置haproxy虚拟机

1.yum install epel-release -y

2.yum install haproxy -y

3.配置文件 vim /etc/haproxy/haproxy.cfg

haproxy中配置主要分为五部分

第一部分global: 设置全局配置参数,属于进程的配置,通常是和操作系统相关。

第二部分defaults:配置默认参数,这些参数可以被用到frontend,backend,Listen组件;

第三部分frontend:接收请求的前端虚拟节点,Frontend可以更加规则直接指定具体使用后端的backend;

第四部分backend:后端服务集群的配置,是真实服务器,一个Backend对应一个或者多个实体服务器;

第五部分Listen :frontend和backend的组合体。

配置示例:

----------------------------------------------------------分割线--------------------------------------------------------------

global
    log 127.0.0.1 local3 info
    maxconn 4096
        uid nobody
#       uid 99
        gid nobody
#       gid 99
    daemon
    nbproc 1
    pidfile /run/haproxy.pid
defaults
    log           global
    mode       http
    maxconn 2048
    retries     3
    option    redispatch
    contimeout    5000
    clitimeout        50000
    srvtimeout        50000
#timeout connect 5000
#timeout client 50000
#timeout server 50000
    option abortonclose

    stats uri /admin?stats
    stats realm Private lands
    stats auth admin:password
    stats hide-version

frontend http-in
    bind 0.0.0.0:80
    mode http
    log global
    option httplog
    option httpclose
     acl html url_reg  -i  \.html$
     use_backend html-server if  html
     default_backend html-server

backend html-server
    mode http
    balance roundrobin
    option httpchk GET /index.html
    cookie SERVERID insert indirect nocache
    server html-A web1:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5
    server html-B web2:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5
---------------------------------------------------------分割线---------------------------------------------------------------

4.域名解析 vim /etc/hosts

192.168.80.108 web1
192.168.80.109 web2

5.启用haproxy服务 systemctl start haproxy.service

ok-到此haproxy这台虚拟机配置完成,可以进入clink虚拟机进行测试

-----------------------------------------------------------分割线-------------------------------------------------------------

##测试

### clink虚拟机测试

1.下载elinks   yum install -y elinks

2.配置域名解析  

3.重复执行:elinks --dump http://haproxy

实现负载均衡,到此结束。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值