本文共 3374 字,大约阅读时间需要 11 分钟。
项目要求:
(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.2haproxy2 172.16.75.2动态web集群 172.16.75.4 172.16.75.5静态web集群 172.16.75.6 172.16.75.7设计拓扑:
一、两台haproxy主配置文件配置
~]#cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bck //进行备份防止改错是吧,每次我们在更改主配置文件时最好备份~]# vim /etc/haproxy/haproxy.cfg //两台haproxy主机设置是相同的先将原先配置文件中maxconn这行以下的内容删除,然后添加图示内容即可;~]# systemctl start haproxy //确保之前没有开启httpd服务,此条命令执行后ss -tnl可以看到5000端口打开,查看状态,正常开启即可二、keepalive配置
haproxy1 172.16.75.3 //要保证这两台haproxy的时间是同步的,且将防火墙以及SElinux关闭~]# hostnamectl set-hostname drct1~]# vim /etc/keepalived/keepalived.confglobal_defs { notification_email {vrrp_script check_httpd {
script "killall -0 haproxy && exit 0 || exit 1"interval 1notification_email {vrrp_script check_httpd {
script "killall -0 haproxy && exit 0 || exit 1"interval 1weight -20}vrrp_instance VI_1 { state MASTERinterface ens33virtual_router_id 51priority 100advert_int 1authentication { auth_type PASSauth_pass 1111nopreempt}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/bash
case $1 in
master)notify MASTER;;backup)notify BACKUPif ! killall -0 haproxy ; thensystemctl start haproxyfi;;fault)notify FAULT;;*)echo "Usage: $(basename $0) { master | backup | fault}";;esac~]# systemctl start keepalivedhaproxy2 172.16.75.4 //注意文件路径呦
~]# hostnamectl set-hostname drct2keepalived]# vim keepalived.conf! Configuration File for keepalivedglobal_defs { notification_email {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 BACKUPif ! killall -0 haproxy ; thensystemctl start haproxyfi;;
fault)
notify FAULTif ! killall -0 haproxy ; thensystemctl start haproxyfi;;
*)
echo "Usage: $(basename $0) { master | backup | fault}";;esac然后我们这里将haproxy1服务器的keepalived宕掉 然后在haproxy2服务器中开启keepalived可以看到:这时就证明我们所做的高可用成功了;三、动态主机和静态主机的配置
在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三、访问测试
访问:;访问:
访问: //这是状态检测页面
转载于:https://blog.51cto.com/huliheeyu/2150141