高可用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.178linux.com/76308

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

相关推荐

  • 磁盘管理

    linux的哲学思想:一切皆文件; 设备类型: 块(block):随机访问,交换数据单位是“块”; 字符(character):线性访问,数据交换单位是“字符”; 设备文件:FHS /dev     设备文件:关联至设备驱动程序;设备的访问入口;       &nbsp…

    Linux干货 2016-08-30
  • sed讲解与使用

            sed是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用,功能不同凡响。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理…

    Linux干货 2016-08-15
  • 马哥教育面授班22期:第一天课程练习

    Linux发展史:        1984年:Richard Stallman发起GNU项目和自由软件基金会创建开源的UNIX实用工具版本        创建通用公共许可证(GPL)        1991年:Linux Torvalds发…

    Linux干货 2017-05-16
  • Linux文本搜索工具grep

    Linux文本搜索工具grep Linux文本搜索工具grep grep 基本正则表达式 egrep 扩展正则表达式 fgrep 简介 grep(全称Globally search a Regular Expression and Print,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,根据用户指定的“模式(pattern)”对目标文本进行匹…

    2016-04-05
  • 马哥教育网络班21期+第8周课程练习

    1、请描述网桥、集线器、二层交换机、三层交换机、路由器的功能、使用场景与区别。 网桥:桥接器,是连接两个局域网的一种存储/转发设备,它能将一个大的LAN分割为多个网段,或将两个以上的LAN互联为一个逻辑LAN,使LAN上的所有用户都可访问服务器 集线器:集线器的英文称为“Hub”.集线器(hub)属于纯硬件网络底层设备,基本上不具有类似于交换机的"…

    Linux干货 2016-08-29
  • 安装Redmine

    Redmine是一个开源的、基于Web的项目管理和缺陷跟踪工具。它用日历和甘特图辅助项目及进度可视化显示。同时它又支持多项目管理。Redmine是一个自由开放源码软件解决方案,它提供集成的项目管理功能,问题跟踪,并为多个版本控制选项的支持。虽说像IBM Rational Team Concert的商业项目调查工具已经很强大了,但想坚持一个自由和开放源码的解决…

    Linux干货 2016-12-01