高可用keepalived結合haproxy代理WordPress(動靜分離)

高可用keepalived結合haproxy代理WordPress(動靜分離)

(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 ~]# yum install -y nginx php-fpm php-mysql nfs-utils
[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

(0)
m22shanyongguom22shanyongguo
上一篇 2017-05-20 11:44
下一篇 2017-05-20 14:40

相關推薦

  • lvs-dr

            通過為請求報文重新封裝一個MAC首部進行轉發,源MAC是DIP所在的接口的MAC,目標MAC是某挑選出的RS的RIP所在接口的MAC地址;源IP/PORT,以及目標IP/PORT均保持不變;     VIP通常配置在lo:0…

    2017-06-29
  • Linux基礎指令(1)

    1.Linux上的文件管理類命令都有哪些,其常用的使用方法及相關實例演示  文件管理工具:cp mv rm cp命令 : 單源復制: cp [option]… [-T]  SOURCE DEST     -bash-4.1# …

    Linux干貨 2016-11-09
  • Linux基礎知識——文件查找

    Linux系統的核心思想之一就是一切皆文件,可想而知你要靠記憶去查找一個文件該是多么費勁;今天我們介紹幾個文件查找命令:whereis,locate,find whereis locate the binary, source, and manual page files for…

    Linux干貨 2016-10-11
  • 用shell腳本打等腰三角新

       *   ***  ***** *******   預覽圖形 發現圖形由三個變量組成 行數n 每行的空格數a 和每行的符號數b 關系是    a=n-當前行數    b=2xn-1   并且空格都在符號前 根據關系編寫腳本 #!/bin/bash read…

    Linux干貨 2016-08-22
  • http加速器varnish

    一、web緩存概述       緩存,又稱加速器,用于加速運行速度較快的設備與較慢設備之間的通信。基于程序的運行具有局部性特征其能實現加速的功能:       時間局部性:一個數據被訪問之后,在隨后較短的時間內有可能被訪問。   &nbsp…

    2016-11-18
  • shell編程及rpm包

    1. shell編程    case 實現條件判斷, 無論是case后面還是值后面都是沒有任何標點符號的。    case支持通配符, 如果想要表示其他的話, 可以使用*放到最后。 用*表示其他。   2. 用tar打包的文件或則目錄,這些文件或則目錄的的ACL(訪問控制列表)權限會丟失?!?/p>

    Linux干貨 2016-08-18
欧美性久久久久