千家信息网

HAproxy+Keepalived负载均衡-高可用web站详细流程介绍

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,下文给大家带来HAproxy+Keepalived负载均衡-高可用web站详细流程介绍,希望能够给大家在实际运用中带来一定的帮助,负载均衡涉及的东西比较多,理论也不多,网上有很多书籍,今天我们就用在行
千家信息网最后更新 2026年11月01日HAproxy+Keepalived负载均衡-高可用web站详细流程介绍

下文给大家带来HAproxy+Keepalived负载均衡-高可用web站详细流程介绍,希望能够给大家在实际运用中带来一定的帮助,负载均衡涉及的东西比较多,理论也不多,网上有很多书籍,今天我们就用在行业内累计的经验来做一个解答。

haproxy+keepalived负载均衡高可用web站

OS

IP

子网掩码

路由网关

Centos6.6

HAproxy

Keepalived

Eth0:192.168.26.210

255.255.252.0

192.168.25.3

VIP:192.168.27.210



Centos6.6

HAporxy

Keepalived

Eth0:192.168.26.211

255.255.252.0

192.168.25.3

VIP:192.168.27.210



Centos6.6(WEB)

Eth0:192.168.26.212

255.255.252.0

192.168.25.3

Centos6.6(WEB)

Eth0:192.168.26.218

255.255.252.0

192.168.25.3

1、安装Apache服务192.168.26.212和192.168.26.218:(yum install httpd -y)略启动

启动Apache,分别在两台云服务器上创建WEB页并确保网络能正常访问:

2、安装HAproxy:192.168.26.210和192.168.26.211:

Yum install -y haproxy

编辑配置文件: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 /p_w_picpaths /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 p_w_picpaths, stylesheets and such

##---------------------------------------------------------------------

#backend static

# balance roundrobin

# server static 127.0.0.1:4331 check

frontend websrv *:80

default_backend webservers

backend webservers

balance roundrobin

server node3 192.168.26.212:80 check

server node4 192.168.26.218:80 check

listen statick

bind *:1024

stats enable

stats uri /haadmin?stats

stats auth admin:admin

stats hide-version

stats admin if TRUE

192.168.26.211上配置文件也一样:因此我们直接SCP过去。

scp -p haproxy.cfg node2:/etc/haproxy/

启动两台服务器上的HAproxy服务。

安装keepalived:192.168.26.210和192.168.26.211

首先192.168.26.210配置:

Yum install -y keepalived

编辑配置文件:vim /etc/keepalived/keepalived.conf

配置文件:

! Configuration File for keepalived

global_defs {

notification_email {

acassen@firewall.loc

failover@firewall.loc

sysadmin@firewall.loc

}

notification_email_from jwenshan@163.com

smtp_server 192.168.200.1

smtp_connect_timeout 30

router_id LVS_DEVEL

}

vrrp_script chk_haproxy {

script "/etc/keepalived/chk.sh" //检查haproxy的脚本

interval 2 //每两秒检查一次

}

vrrp_instance VI_1 {

state BACKUP //定义为BACKUP节点

nopreempt //开启不抢占

interface eth0

virtual_router_id 51

priority 100 //开启了不抢占,所以此处优先级必须高于另一台

advert_int 1

authentication {

auth_type PASS

auth_pass jerry

}

virtual_ipaddress {

192.168.27.210 //配置VIP

}

track_script {

chk_haproxy //调用检查脚本

}

notify_backup "/etc/init.d/haproxy restart"

notify_fault "/etc/init.d/haproxy stop"

}

创建脚本文件:主要用于检测haproxy状态。

Vim /etc/keepalived/Chk.sh

#!/bin/bash

#

if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then

/etc/init.d/keepalived stop

Fi

启动Keepalived服务:

service keepalived start

192.168.26.211配置:

Yum install -y keepalived

编辑配置文件:Vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

notification_email {

acassen@firewall.loc

failover@firewall.loc

sysadmin@firewall.loc

}

notification_email_from jwenshan@163.com

smtp_server 192.168.200.1

smtp_connect_timeout 30

router_id LVS_DEVEL

}

vrrp_script chk_haproxy {

script "/etc/keepalived/chk.sh"

interval 2

}

vrrp_instance VI_1 {

state BACKUP

nopreempt

interface eth0

virtual_router_id 51

priority 99

advert_int 1

authentication {

auth_type PASS

auth_pass jerry

}

virtual_ipaddress {

192.168.27.210

}

track_script {

chk_haproxy

}

notify_backup "/etc/init.d/haproxy restart"

notify_fault "/etc/init.d/haproxy stop"

}

脚本文件:vim /etc/keepalived/chk.sh

#!/bin/bash

#

if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then

/etc/init.d/keepalived stop

Fi

启动Keepalived服务

Service keepalived start

测试VIP网络是否通畅:ping 192.168.27.210 -t

访问VIP网络:

Http://192.168.27.210

不断刷新浏览器观察显示结果

测试通过VIP访问后台监控页面:

访问成功。

测试高可用:

停掉192.168.26.210上Keepalived 观察结果:

Service keepalived stop

首先VIP网络出现波动:

再次访问VIP成功:

VIP已经转移动192.168.26.211上:

恢复192.168.26.210keepalived观察

:vip没有自动转移回192.168.26.210 这和我们的设置参数有关。

本试验keepalived配置中涉及几个重要参数,nopreempt state MASTER/ state BACKUP priority 可以更改其设置观察其变化,适应不同场景中的应用。

看了以上关于HAproxy+Keepalived负载均衡-高可用web站详细流程介绍,如果大家还有什么地方需要了解的可以在行业资讯里查找自己感兴趣的或者找我们的专业技术工程师解答的,技术工程师在行业内拥有十几年的经验了。


配置 文件 服务 均衡 网络 脚本 观察 检查 测试 流程 在行 成功 业内 参数 工程 工程师 技术 服务器 经验 结果 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 手冲咖啡和咖啡机区别 云南咖啡 新华互联网科技学校 万易互联网科技有限公司 互联网科技排名 上海喔噻互联网科技 壹账通互联网科技 中国互联网金融科技基金 海南科技产业互联网平台 宝灵互联网科技有限公司 深圳艾果互联网通讯科技 工业互联网等科技实现智能化赋能 易企秀互联网科技是做什么的 宁波跨境商互联网科技有限公司 上海同邻互联网科技有限公司规模 昆明中奇联合互联网科技 易租星互联网科技有限公司 伍佰万互联网科技是干嘛的 知域互联网科技有限公司官网 颐高云石互联网科技 互联网科技活动banner 互联网科技公司怎么样 碳银互联网科技 奇士达互联网科技 上海电发互联网科技有限公司 科技与互联网的利与弊 迪庆互联网科技招生 世界互联网领先科技成果获奖 恒生互联网科技业指数调整周期 玉溪新华互联网科技怎么选
0