阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Haproxy+Keepalived+Apache实现高可用

126次阅读
没有评论

共计 3293 个字符,预计需要花费 9 分钟才能阅读完成。

Haproxy 通过结合 Keepalived 实现负载均衡器节点的高可用

环境介绍:CentOS 6.5 平台
Haproxy1:10.10.10.128/24
Haproxy2:10.10.10.129/24
web1:10.10.10.130/24
web2:10.10.10.131/24
VIP:10.10.10.100/24

部署前准备:
1、时间同步:
ntpdate ntp1.aliyun.com
2、防火墙策略:
service iptables stop
chkconfig iptables off
3、SELinux 策略:
sed -i ‘s#SELINUX=enforcing#SELINUX=disabled#’ /etc/selinux/config
4、更换国内 yum 源:
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
yum clean all
yum makecache
5、重启服务器
reboot

web 服务器配置:
1、安装 httpd 服务
    yum install httpd -y
2、修改配置文件:/etc/httpd/conf/httpd.conf
    …..
    Listen 8080
    ServerName 10.10.10.130:8080  #web1
    ServerName 10.10.10.131:8080  #web2
3、修改 web 主页:/var/www/html/index.html
    echo “web1” >/var/www/html/index.html    #web1
    echo “web2” >/var/www/html/index.html    #web2
4、启动 web 服务:
    service httpd start
    chkconfig httpd on
5、验证服务是否正常:
    lsof -i:8080
    curl 10.10.10.130:8080  #返回 web1
    curl 10.10.10.131:8080  #返回 web2

Haproxy 服务器配置:
1、安装 Haproxy 服务
    yum install haproxy -y
2、修改配置文件:/etc/haproxy/haproxy.cfg
    cp /etc/haproxy/haproxy /etc/haproxy/haproxy.cfg.bak
    vim /etc/haproxy/haproxy.cfg
  ………
  ………
#———————————————————————
# main frontend which proxys to the backends
#———————————————————————
frontend  main
    bind 0.0.0.0: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            dynamic

#———————————————————————
# 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 dynamic
    balance    roundrobin
    server  web1 10.10.10.130:8080 check maxconn 2000
    server  web2 10.10.10.131:8080 check maxconn 2000

3、检测配置文件是否正确:
    haproxy -c -f /etc/haproxy/haproxy.cfg

4、启动 Haproxy
    service haproxy start
    chkconfig haproxy on
5、验证是否正常解析:
    curl 10.10.10.128  // 重复两次,分别显示 web1 和 web2
    curl 10.10.10.129  // 重复两次,分别显示 web1 和 web2

Keepalived 配置:
1、安装 keepalived 服务:
    yum -y install keepalived
2、修改配置文件:
#MASTER 端:
! Configuration File for keepalived
# 定义检查脚本
vrrp_script check_haproxy {
        script “/etc/keepalived/check_haproxy.sh”
        interval 2
        weight 2
}
global_defs {
  notification_email {
        root@localhost
  }
  notification_email_from keepalived@localhost
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id haproxy1
}

vrrp_instance ha1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.10.10.100/24 dev eth0
    }
    track_script{
        check_haproxy
    }
}

#BACKUP 端:
………
router_id haproxy2  #修改虚拟路由的 ID
state BACKUP  #修改角色
priority 80    #修改优先级

重启各服务:
service keepalived restart
service haproxy restart

验证:
ip addr
发现虚拟 ip:10.10.10.100 在 MASTER 端
访问 10.10.10.100 正常

/etc/keepalived/check_haproxy.sh

#!/bin/bash
A=`ps -C haproxy –no-header |wc -l`
if [$A -eq 0];then
/etc/init.d/keepalived stop
fi

验证 Haproxy+Keepalived 服务的可靠性:

web 端:关闭 web1 的 httpd 服务,service httpd stop
curl 10.10.10.100  #正常返回 web2
Haproxy 端:关闭 haproxy1 的 keepalived 服务,service keepalived stop
curl 10.10.10.100 #正常轮询返回 web1/web2
通过 ip addr 可以查看 VIP 漂移到 Haproxy2 中

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计3293字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中