(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.www58058.com/76308