(1)A B两台服务器做keepalived高可用,同时作为haproxy动静分离后端代理。
(keepalived时主备模型,haproxy轮询调度)
(2)C 服务器搭建apache作为动态资源服务器
(3)D 服务器搭建nginx作为静态资源服务器
(4)VIP:172.18.0.42
A主机配置:
先搭建主备模型
[root@centos7 ~]#cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node1
vrrp_mcast_group4 224.0.102.78
}
vrrp_instance myr1 {
state MASTER
interface eth0
virtual_router_id 78
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass J21LEswe
}
virtual_ipaddress {
172.18.0.42/16 dev eth0
}
}
virtual_server 172.18.0.42 80 {
delay_loop 3
lb_algo rr
lb_kind DR
protocol TCP
real_server 172.18.0.9 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 172.18.0.11 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
配置VIP
[root@centos7 ~]#cat setpara.sh
#!/bin/bash
#
vip=’172.18.0.42′
netmask=’255.255.255.255′
iface=’lo:0′
case $1 in
start)
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/${ifcase}/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/${ifcase}/arp_announce
ifconfig $iface $vip netmask $netmask broadcast $vip up
route add -host $vip dev $iface
;;
stop)
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf${iface}/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/${iface}/arp_announce
;;
*)
echo “usage: $(basename $0) start|stop”
exit 1
;;
esac
编辑haproxy,实现动静分离
[root@centos7 haproxy]#cat /etc/haproxy/haproxy.cfg
#———————————————————————
# main frontend which proxys to the backends
#———————————————————————
frontend eshop *:80
# acl invalid_src src 172.18.253.194
# block if invalid_src
# errorfile 403 /etc/haproxy/errorfiles/403.html
# errorloc 403 http://www.baidu.com
# acl curl_agent hdr_sub(User-Agent) -i curl
# use_backend curlbe if curl_agent
acl phpapp path_end -i .php
use_backend dynssrvs if phpapp
maxconn 4000
rspadd X-Via:\ HAProxy
rspidel Server.*
default_backend websrvs
compression type text/html
compression algo gzip
backend dynssrvs
balance source
server dynsrv1 172.18.251.129:80 check
listen stats :9527
stats enable
stats uri /haadmin?admin
stats realm “Statistics Admin”
stats auth dan:138184
stats admin if TRUE
#———————————————————————
# static backend for serving up images, stylesheets and such
#———————————————————————
backend websrvs
balance leastconn
server websrv1 172.18.0.10:80 check
启动haproxy服务
systemctl start haproxy
B 主机配置:
搭建主备模型
[root@centos7 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node2
vrrp_mcast_group4 224.0.102.78
}
vrrp_instance myr1 {
state BACKUP
interface ens36
virtual_router_id 78
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass J21LEswe
}
virtual_ipaddress {
172.18.0.42/16 dev ens36
}
}
virtual_server 172.18.0.42 80 {
delay_loop 3
lb_algo rr
lb_kind DR
protocol TCP
real_server 172.18.0.9 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 172.18.0.11 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
其余的haproxy和vip配置同A主机一样。
D主机配置:(centos6主机)
316 yum install mysql-server
317 service mysqld start
318 mysql
grant all on wpdb.* to wpuser@’172.18.%’ identified by ‘138184’;
339 mkdir /data/www
341 cd /data/www
322 wget ftp://172.18.0.1/pub/Sources/sources/httpd/wordpress-4.3.1-zh_CN.zip
324 unzip wordpress-4.3.1-zh_CN.zip
321 chown -R apached.apached /data/www/wordpress
330 vim /etc/exports
/data/www/ 172.18.0.11(rw)
/data/www/ 172.18.0.12(rw)
331 yum install nfs-utils
332 yum install rpcbind
333 service nfs start
334 showmount -e 172.18.0.13
/data/www 172.18.0.11,172.18.0.12
C主机配置:
240 yum -y install nfs-utils httpd php php-mysql mysql-server
266 mount -t nfs 172.18.12.12;/data/www/ /var/www/html/
295 service mysqld start
D主机配置:(centos7主机)
[root@centos7 conf.d]# nginx
[root@centos7 conf.d]# systemctl start php-fpm
最后通过浏览器访问VIP:172.18.0.42:80 ,那么将由D服务器响应静态资源。通过VIP:172.18.0.42:80/index.php
将由C服务器提供动态资源。
当主节点A坏掉时,那么备节点B将代替A继续工作,实现高可用。
原创文章,作者:m22shanyongguo,如若转载,请注明出处:http://www.178linux.com/76308