1  概述

本文通过HAProxy,Keepalive,实现动静分离wordpress,在每台haproxy和wordpress服务器之间加入缓存,通过缓存实现加速。具体需求如下

.(1) 动静分离部署wordpress,动静都要能实现负载均衡


.(2) haproxy和后端主机之间添加varnish进行缓存


.(3) haproxy的设定要求:动静分离


.(4) haproxy高可用


.(5)通过VIP:172.18.50.80来访问wordpress

前提

1:node1 ip是172.18.50.62,node2 ip 是172.18.50.63.写入/etc/hosts文件里,node1和node2实现基于ssh-key的验证。

2:所有主机时间同步。

拓扑图如下

实现高可用WordPress部署_HA


2  配置

172.18.50.62  HAproxy配置,主备配置一样,172.18.50.63不附上代码

global
    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

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    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

frontend  http
    bind *:80
    acl url_dyn path_end -i .php
    acl url_stac path_end -i  .jpg .gif .png .css .js .html .txt .ico .svg
	reqadd sunny-x-via:\ haproxy6b
    default_backend dynamicblog if url_dyn
    use_backend    staticblog if url_stac
    option forwardfor    header sunny-x-client
	rspdel Server
	rspadd  Server:\ Sunny-proxy6b

backend staticblog
    balance     roundrobin
    cookie WEBSRV insert nocache
    server      varnish7b 172.18.50.72:80 check weight 1   inter 3000 rise 2 fall 2 cookie cksrv1
backend dynamicblog
    balance     roundrobin
    cookie WEBSRV insert nocache
    server     varnish7c 172.18.50.73:80 check weight 1  inter 3000 rise 2 fall 2 cookie cksrv2
listen stats
bind :9091
stats enable
stats auth  admin:admin
stats admin  if TRUE

172.18.50.62  HAproxy配置,主备配置有区别,区别在于四点:

notification_email_from node2@localhost

router_id node2

state BACKUP

priority 90

172.18.50.63不附上代码。

! Configuration File for keepalived

global_defs {
   notification_email {
   		root@localhost
   }
   notification_email_from node1@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node1
   vrrp_mcast_group4 224.0.100.100
}

vrrp_script  chk_down {
script "[[ -f   /etc/keepalived/down ]] && exit 1 || exit 0"
interval  1
weight  -20 
 }

vrrp_script  chk_haproxy {
script "killall -0 haproxy && exit 0 || exit 1"
interval 1
weight  -20
fall 2 
rise 1 
}

vrrp_instance VI_1 {
    state MASTER
    interface eth1
    virtual_router_id 50
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass sunny
    }
    virtual_ipaddress {
		172.18.50.80
    }
	notify_master "/etc/keepalived/vip1_notify.sh master"
    notify_backup "/etc/keepalived/vip1_notify.sh backup"
    notify_fault  "/etc/keepalived/vip1_notify.sh fault"

	track_script {
	 chk_down
	 chk_haproxy
	   }
}

172.18.50.62  HAproxy配置里notify.sh脚本如下

#!/bin/bash
contact='root@localhost'
notify() {
mailsubject="$(hostname) to be $1, vip
floating"
mailbody="$(date +'%F %T'): vrrp transition,
$(hostname) changed to be $1"
echo "$mailbody" | mail -s
"$mailsubject" $contact
}
case  $1  in
master)
notify master
;;
backup)
notify backup
;;
fault)
notify fault
;;
*)
echo "Usage: $(basename $0)
{master|backup|fault}"
exit 1
;;
esac

172.18.50.73 varnish配置default.vcl如下,172.18.50.72配置区别如下

backend static {

    .host = "172.18.50.65";

    .port = "80";

   .probe = check;

}

其他配置一样,172.18.50.72这里就附上脚本

vcl 4.0;

probe check {
   .url = "/index.html";
	.window = 5;
	.threshold = 4;
	.interval = 2s;
	.timeout = 1s;
}

backend dynamic {
    .host = "172.18.50.75";
    .port = "80";
	.probe = check;
}


sub vcl_recv { 
	if (req.restarts == 0){
		if (req.http.X-Forwarded-For){
			set req.http.X-Forwarded-For = req.http.X-Forwarded-For + "," + client.ip;
			} else {
				set req.http.X.Forwarded-For = client.ip;
				}
	}
}

sub vcl_backend_response {
   if (beresp.http.cache-control !~ "s-maxage") {  
        if (bereq.url ~ "(?i)\.(jpg|jpeg|png|gif|css|js|xml)$") {
                unset beresp.http.Set-Cookie;
                set beresp.ttl = 3600s;
        }
    }
    if (bereq.url ~ "(?i).*php.*") {
        unset beresp.http.Set-Cookie;
        set beresp.ttl = 3600s;
    }
}

sub vcl_deliver {
	if (obj.hits>0) {
		set resp.http.X-Cache = "Hit via " + server.ip;
		} else {
				set resp.http.X-Cache = "Miss from " + server.ip;
	}
}

后端的172.18.50.65和172.18.50.75这两台wordpress都要安装wordpress和http,同时172.18.50.75是处理动态资源的脚本,所以还需要安装php-fpm.同时在75这台安装mysql.这里就不演示了,相关部署见http://ghbsunny.blog.51cto.com/7759574/1970716文章。j

到这里部署就完成了。可以通过网页访问http://172.18.50.80/blog查看功能是否正常。其中,这里的blog是将wordpress安装包部署后重命名为blog,可根据实际情况自行设定。