高可用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
下一篇 2017-05-20

相關推薦

  • 文本查看及處理工具

    文本查看及處理工具 wc wc命令用來計算數字。利用wc指令我們可以計算文件的Byte數、字數或是列數 語法 wc(選項)(參數) 選項 -c或–bytes或——chars:只顯示Bytes數; -l或——lines:只顯示列數; -w或——words:只顯示字數。 參數 文件:需要統計的文件列表 實例 [root@localhost tmp]# cat …

    Linux干貨 2018-03-18
  • Linux發展及簡單命令

    一 計算機的組成及其功能 馮·諾依曼提出的計算機體系結構:計算機由控制器、運算器、存儲器、輸入設備、輸出設備五部分組成。 二?Linux的發行版 Linux發行版主要有三個分支:Debian、Slackware、Redhat。 (1)Debian:(以社區的方式運作) Ubuntu:基于Debian開發的開源Linux操作系統,主要針對桌面和服務器; Lin…

    2018-03-02
  • Linux系統中的Inode節點表

    索引節點是指在許多類Unix文件系統中的一種數據結構。每個索引節點保存了文件系統中的一個文件系統對象的元信息數據,但不包括數據內容或者文件名。

    2017-11-16
  • linux學習總結-linux基礎(之二)

    六、在Linux基礎目錄命名法則及功用 根據FHS(http://www.pathname.com/fhs/)的官方文件指出, 他們的主要目的是希望讓使用者可以了解到已安裝軟件通常放置于那個目錄下, 所以他們希望獨立的軟件開發商、操作系統制作者、以及想要維護系統的用戶,都能夠遵循FHS的標準。 也就是說,FHS的重點在于規范每個…

    Linux干貨 2016-09-23
  • python-多進程

    進程是由系統自己管理的。 1:最基本的寫法 from multiprocessing import Pool def f(x):     return x*x if __name__ == '__main__': &nb…

    Linux干貨 2016-11-05
  • Centos7 服務器部署ssh證書授權登錄

    在當前服務器生成密鑰對 ssh-keygen –t rsa –P ” ssh-keygen –t rsa –P ''[root@localhost ~]# ssh-keygen -t rsaGenerating public/private rsa key pair.Enter file in which to save the key …

    Linux干貨 2016-12-05
欧美性久久久久