高可用haproxy调度后端服务器实现动静分离集群架构

项目要求:

(1) 动静分离部署wordpress,动静都要能实现负载均衡,要注意会话的问题;

(2) 设计拓扑;

(3) haproxy的设计要求:

(a) stats page,要求仅能通过本地访问使用管理接口;

(b) 动静分离;

(4) haproxy高可用;

项目需求:
(1) 要实现haproxy高可用,可利用keepalived主备高可用两个haproxy服务器;
(2)动态web集群【实现负载均衡,项目实验中使用两台即可】;
(3)静态web集群【实现负载均衡,项目实验中使用两台即可】;
使用CentOS 7.5镜像虚拟机完成项目试验:
wordpress服务器 172.16.75.1 进行NFS共享给集群
haproxy1 172.16.75.2
haproxy2 172.16.75.2
动态web集群 172.16.75.4 172.16.75.5
静态web集群 172.16.75.6 172.16.75.7

设计拓扑:
高可用haproxy调度后端服务器实现动静分离集群架构

一、两台haproxy主配置文件配置
~]#cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bck //进行备份防止改错是吧,每次我们在更改主配置文件时最好备份
~]# vim /etc/haproxy/haproxy.cfg //两台haproxy主机设置是相同的
高可用haproxy调度后端服务器实现动静分离集群架构
先将原先配置文件中maxconn这行以下的内容删除,然后添加图示内容即可;
~]# systemctl start haproxy //确保之前没有开启httpd服务,此条命令执行后ss -tnl可以看到5000端口打开,查看状态,正常开启即可
高可用haproxy调度后端服务器实现动静分离集群架构

二、keepalive配置
haproxy1 172.16.75.3 //要保证这两台haproxy的时间是同步的,且将防火墙以及SElinux关闭
~]# hostnamectl set-hostname drct1
~]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {br/>root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id drct1
vrrp_mcast_group4 224.0.100.18
}

vrrp_script check_httpd {
script "killall -0 haproxy && exit 0 || exit 1"
interval 1
notification_email {
br/>root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id drct1
vrrp_mcast_group4 224.0.100.18
}

vrrp_script check_httpd {
script "killall -0 haproxy && exit 0 || exit 1"
interval 1
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
nopreempt
}

virtual_ipaddress {
    172.16.72.1
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"

}
[root@drct1 ~]# vim /etc/keepalived/notify.sh

#!/bin/bashbr/>#
CONTACT='root@localhost'
notify() {
MAILSUBJECT="$(hostname) changed state to $1, VIP reflact."
MAILBODY="$(date +'%F %T'): VRRP STATUS CHANGED, $(hostname) change state to $1"
echo "$MAILBODY" | mail -s "$MAILSUBJECT" "$CONTACT"
}

case $1 in
master)
notify MASTER
;;
backup)
notify BACKUP
if ! killall -0 haproxy ; then
systemctl start haproxy
fi
;;
fault)
notify FAULT
;;
*)
echo "Usage: $(basename $0) { master | backup | fault}"
;;
esac
~]# systemctl start keepalived
高可用haproxy调度后端服务器实现动静分离集群架构
高可用haproxy调度后端服务器实现动静分离集群架构

haproxy2 172.16.75.4 //注意文件路径呦
~]# hostnamectl set-hostname drct2
keepalived]# vim keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
br/>root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id drct2
vrrp_mcast_group4 224.0.100.18
}
vrrp_script check_httpd {
script "killall -0 haproxy && exit 0 || exit 1"
interval 1
weight -20
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
nopreempt
}
virtual_ipaddress {
172.16.72.1
}

notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"

}

drct2 keepalived]# vim notify.sh

#!/bin/bash
#

CONTACT='root@localhost'
notify() {
MAILSUBJECT="$(hostname) changed state to $1, VIP reflact."
MAILBODY="$(date +'%F %T'): VRRP STATUS CHANGED, $(hostname) change state to $1"
echo "$MAILBODY" | mail -s "$MAILSUBJECT" "$CONTACT"
}

case $1 in
master)
notify MASTER
;;
backup)
notify BACKUP
if ! killall -0 haproxy ; then
systemctl start haproxy
fi

;;

fault)
notify FAULT
if ! killall -0 haproxy ; then
systemctl start haproxy
fi

;;

*)
echo "Usage: $(basename $0) { master | backup | fault}"
;;
esac
然后我们这里将haproxy1服务器的keepalived宕掉 然后在haproxy2服务器中开启keepalived可以看到:
高可用haproxy调度后端服务器实现动静分离集群架构
这时就证明我们所做的高可用成功了;

三、动态主机和静态主机的配置
在172.16.75.1中部署wordpress并用NFS进行共享,这里共享以及wordpress我们不再赘述,因为博主有一篇专门写共享的,不懂的可以看博主的NFS共享的博客;
动态主机中:
172.16.75.4
~]# mount -t nfs 172.16.72.1:/var/www/html /var/www/html
~]# vim /var/www/html/health.php

<h1>DynamicServer 1 is Health.</h1>
172.16.75.5
~]# mount -t nfs 172.16.72.1:/var/www/html /var/www/html
~]# vim /var/www/html/health.php

<h1>DynamicServer 2 is Health.</h1>

静态主机中:
172.16.75.6
~]# vim /var/www/html/health.html

<h1>DynamicServer 1 is Health.</h1>
172.16.75.7
~]# vim /var/www/html/health.php

<h1>DynamicServer 2 is Health.</h1>

开启http服务并进将防火墙关闭:
~]# systemctl start httpd
~]# setenforce 0

三、访问测试
访问:
http://172.16.72.1:5000/health.php并重复刷新,要保证此地址能够在客户端主技能被解析,即更改你本地客户机的host文件,添加这些地址即可
高可用haproxy调度后端服务器实现动静分离集群架构
高可用haproxy调度后端服务器实现动静分离集群架构

访问:http://172.16.72.1:5000/health.html并重复刷新
高可用haproxy调度后端服务器实现动静分离集群架构
高可用haproxy调度后端服务器实现动静分离集群架构

访问:http://172.16.72.1:5000/haproxy?stats //这是状态检测页面
高可用haproxy调度后端服务器实现动静分离集群架构

相关文章
相关标签/搜索