骞没蕴 发表于 2018-12-30 10:21:44

KeepAlived实现HAProxy双主并实现资源的动静分离

KeepAlived实现HAProxy双主并实现资源的动静分离
  

  大纲

[*]  前言
[*]  HAProxy介绍
[*]  实验拓扑
[*]  实验环境
[*]  实验步骤

[*]  配置后端httpd服务器
[*]  配置HAProxy实现backend负载均衡
[*]  测试动静分离效果
[*]  配置KeepAlived
[*]  测试KeepAlived
[*]  配置DNS
[*]  DNS测试

[*]  最终测试
[*]  总结
  

前言
我们这次使用HAProxy作为负载均衡调度器来实现后端httpd服务的负载均衡和动静分离,并使用KeepAlived让HAproxy实现双主高可用, 再使用DNS轮询将用户对www.anyisalin.com的访问负载均衡至两个HAProxy实现的负载均衡调度器上
HAProxy介绍
HAProxy的是一个免费的,非常快速和可靠的解决方案,提供高可用性,负载均衡和代理对TCP和HTTP的应用程序。它特别适用于非常高流量网站。多年来,它已成为标准的开源的负载均衡程序,现在随最主流的Linux发行版,并且通常默认的云平台部署。其运作模式使得其集成到现有的架构非常容易,无风险,同时还提供了可能性不暴露脆弱的Web服务器到网络
__转自HAProxy官方站点
我们今天只演示HAProxy作为负载均衡器的场景
实验拓扑

实验环境
  VIP1: 172.16.1.10 VIP2: 172.16.1.11
主机IP功用node1.anyisalin.com172.16.1.2, VIPHAproxy, KeepAlived Nodenode2.anyisalin.com172.16.1.3, VIPHAproxy, KeepAlived Nodenode3.anyisalin.com172.16.1.4httpd, Image资源node4.anyisalin.com172.16.1.5httpd, 动态网页node5.anyisalin.com172.16.1.8dns  注意: 本文实验中所有主机SElinux和iptables都是关闭的
实验步骤
配置后端httpd服务器
node3存放图片
# yum install httpd -y      #安装httpd
# cd /var/www/html/
# ls   
mage.jpg    #这里有一张实现存好的图片
# service httpd start#启动httpd服务node4存放php文件
# yum install httpd php
# ls /var/www/html/#查看目录下的文件, 什么都没有
# cat >> /var/www/html/index.php
>   Welcome to magedu.com
>   
>            #我们的网页目录下并没有这张图片
>         
>   
>
> EOF
# service httpd start#启动httpd服务配置HAProxy实现backend负载均衡
# yum install-y haproxy    #安装haproxy

# vim /etc/haproxy/haproxy.cfg   #配置文件如下
    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
    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
frontendmain
    bind *:80
    ##开启stats界面
    stats enable
    stats hide-version
    stats uri /haproxyadmin
    default_backend             dynamic       #默认backend为dynamic
    acl    url_static    path_end    -i .jpg#访问控制列表, 匹配结尾为.jpg的资源
    use_backend   staticif url_static       #如果结尾为.jpg, 则使用backend为static
backend dynamic
    balance   roundrobin      #这里使用roundrobin算法
    server dynamic 172.16.1.5:80 check
backend static
   balance   uri                #这里使用uri算法
   server static172.16.1.4:80 check测试动静分离效果
我们访问172.16.1.2


当我们将node3的httpd服务停止

再次将node3的httpd服务启动

我们打开了stats页面, 可以通过设置的URI进行访问

配置KeepAlived
KeepAlived的配置不做解释, 有兴趣的请看官方文档
node1和node2上都需要一个脚本文件监控haproxy的运行状态
# vim /etc/keepalived/haproxytest.sh
#!/bin/bash
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then
    /etc/init.d/haproxy start
fi
sleep 2
if [ $(ps -C haproxy --no-header | wc -l ) -eq 0 ]; then
    /etc/init.d/haproxy start
fi

node1的配置文件如下
# vim /etc/keepalived/keepalived.conf
global_defs {
   notification_email {
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server localhost
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script chk_haproxy {
    script /etc/keepalived/haproxytest.sh
    interval 2
    weight 2
}
vrrp_script chk_down {
    script "[ -f /etc/keepalived/down] && exit 1 || exit 0"
    interval 2
    weight -20
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 1111
    }
track_script {
    chk_haproxy
   }
track_script {
    chk_down
   }
    virtual_ipaddress {
    172.16.1.10/24
    }
}
vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 52
    priority 99
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 2222
    }
track_script {
    chk_haproxy
   }
track_script {
    chk_down
   }
    virtual_ipaddress {
    172.16.1.11/24
    }
}

node2的配置文件如下
# cat keepalived.conf
global_defs {
   notification_email {
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server localhost
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script chk_haproxy {
    script /etc/keepalived/haproxytest.sh
    interval 2
    weight 2
}
vrrp_script chk_down {
    script "[ -f /etc/keepalived/down] && exit 1 || exit 0"
    interval 2
    weight -20
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 1111
    }
track_script {
    chk_haproxy
   }
track_script {
    chk_down
   }
    virtual_ipaddress {
    172.16.1.10/24
    }
}
vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 2222
    }
track_script {
    chk_haproxy
   }
track_script {
    chk_down
   }
    virtual_ipaddress {
    172.16.1.11/24
    }
}

两个节点启动Keepalived并查看状态
# service keepalived start
# ssh node2.anyisalin.com -- service keepalived start
# ip a | grep "inet\>"               #ip地址为172.16.1.10
    inet 127.0.0.1/8 scope host lo
    inet 172.16.1.2/24 brd 172.16.1.255 scope global eth0
    inet 172.16.1.10/24 scope global secondary eth0
# ip a | grep "inet\>"       #ip地址为172.16.1.11
    inet 127.0.0.1/8 scope host lo
    inet 172.16.1.3/24 brd 172.16.1.255 scope global eth0
    inet 172.16.1.11/24 scope global secondary eth0
# scp /etc/haproxy/haproxy.cfg node2.anyisalin.com:/etc/haproxy/ #同步文件到node2
我们这样配置haproxy会自动启动在两个节点上测试KeepAlived
我们分别对172.16.1.10和172.16.1.11进行测试, 都能够正常调度

我们强行停止node1的KeepAlived,IP已经转移, 再次进行测试

配置DNS
过程没什么好说的, 不懂可以看我以前的文章DNS and BIND 配置指南
# yum install bind bind-utils
# vim /etc/named.conf   #编辑配置文件
options {
      directory       "/var/named";
};
logging {
      channel default_debug {
                file "data/named.run";
                severity dynamic;
      };
};
zone "." IN {
      type hint;
      file "named.ca";
};
include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";
# vim /etc/named.rfc1912.zones   #添加以下字段
zone "anyisalin.com" IN {
      type master;
      file "anyisalin.com.zone";
};

# vim /var/named/anyisalin.com.zone   #创建区域解析库文件
$TTL 600
$ORIGIN anyisalin.com.
@   INSOAns.anyisalin.comadmin.anyisalin.com (
      20160416
      1D
      5M
      7D
      1D
)
      IN   NS   ns
ns      IN      A   172.16.1.8
www   IN      A   172.16.1.10
www   IN      A   172.16.1.11
# service named start    #启动dnsDNS测试
# nslookup      #测试能达到以下效果
> www.anyisalin.com
Server:   172.16.1.1
Address:    172.16.1.1#53
Name:   www.anyisalin.com
Address: 172.16.1.11
Name:   www.anyisalin.com
Address: 172.16.1.10
> www.anyisalin.com
Server:   172.16.1.1
Address:    172.16.1.1#53
Name:   www.anyisalin.com
Address: 172.16.1.10
Name:   www.anyisalin.com
Address: 172.16.1.11最终测试
直接访问www.anyisalin.com

将node2的服务停止, 再次进行测试, 能够正常访问

总结
我们轻松地通过HAProxy实现资源的动静分离和后端httpd主机的负载均衡,也通过KeepAlived实现HAProxy的高可用, 最后在再通过DNS轮询实现HAProxy的负载均衡, 整套架构还是很完整的, 如果有小伙伴有兴趣可以多放点资源上去, 哈哈, 我这算不算侵犯马哥的肖像权啊, 如果侵犯了, 请告知, 我会删除的!
作者水平很低, 如果有错误及时指出, 如果你觉得本文写的好请点一波赞~(≧▽≦)/~
作者: AnyISaIln QQ: 1449472454
感谢: MageEdu
  




页: [1]
查看完整版本: KeepAlived实现HAProxy双主并实现资源的动静分离