Lvs+keepalived+httpd+NFS搭建高可用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Lvs+keepalived+httpd+NFS搭建高可用相关的知识,希望对你有一定的参考价值。

Lvs+keepalived+httpd+NFS搭建高可用

自己捯饬的模型图

  • NAT模型图

注意事项:RealServer需要把网关指向Director,并且Director要打开转发功能命令如下:

技术分享

echo "1" > /proc/sys/net/ipv4/ip_foreward
  • DR模型图

技术分享

注意事项:需要在RealServer配置如下信息:

echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig lo:0 $vip netmask 255.255.255.255 broadcast $vip
route add -host $vip dev lo:0

Lvs DR 模型搭建,后端Apache使用共享NFS

环境

  • 搭建NFS共享存储服务,ip

    NIP:192.168.220.18

  • 搭建Keepalived,ip

    DIP1:192.168.220.28

    DIP2:192.168.220.24

    VIP:192.168.220.5

  • 搭建RealServer,ip

    RIP1:192.168.220.25

    RIP2:192.168.220.26

  • Client Test Host

    IP:192.168.11.30

    ps:自己三层交换做Vlan


一、NFS搭建步骤

1、查看R1/R2用于跑httpd服务的User

[[email protected] ~]# ps -ef |grep httpd | head -2
root   3318  1  0 14:44 ?00:00:01 /usr/sbin/httpd -DFOREGROUND
apache 3319   3318  0 14:44 ?00:00:00 /usr/sbin/httpd -DFOREGROUND	#发现apache
[[email protected] ~]# id apache			#查看apache的UID
uid=48(apache) gid=48(apache) groups=48(apache)
[[email protected] ~]# id apache          #查看apache的UID
uid=48(apache) gid=48(apache) groups=48(apache)

2、在NFS-Server上面创建用于共享的文件,并且创建apapche用户UID为48

[[email protected] ~]# clear[[email protected] ~]# mkdir -p /data/site/
[[email protected] ~]# useradd  -M -r -s /sbin/nologin -u 48 apache
[[email protected] ~]# id apache
uid=48(apache) gid=48(apache) groups=48(apache)

3、设定/data/site/www.sunshineboy.com用户与用户组

[[email protected] ~]# chown apache.apache /data/site/
[[email protected] ~]# ls -l /data/site/
total 0drwxr-xr-x 2 apache apache 39 Oct 24 12:22 www.sunshineboy.com

4、安装nfs-utils组件,与启动服务

yum install -y nfs-utlis
[[email protected] ~]# systemctl start rpcbind
[[email protected] ~]# systemctl start nfs.service
[[email protected] ~]# ps -ef | grep nfsroot   
3400  2  0 11:30 ?00:00:00 [nfsd4]
root   3401  2  0 11:30 ?00:00:00 [nfsd4_callbacks]
root   3407  2  0 11:30 ?00:00:00 [nfsd]
root   3408  2  0 11:30 ?00:00:00 [nfsd]
root   3409  2  0 11:30 ?00:00:00 [nfsd]
root   3410  2  0 11:30 ?00:00:00 [nfsd]
root   3411  2  0 11:30 ?00:00:00 [nfsd]
root   3412  2  0 11:30 ?00:00:00 [nfsd]
root   3413  2  0 11:30 ?00:00:00 [nfsd]
root   3414  2  0 11:30 ?00:00:00 [nfsd]
root   3859   2471  0 15:48 pts/100:00:00 grep --color=auto nfs

5、NFS配置文件设定及exportfst

[[email protected] ~]# cat /etc/exports/data/site/www.sunshineboy.com 
192.168.220.0/24(rw,root_squash)
[[email protected] ~]# exportfs 
/data/site/www.sunshineboy.com		192.168.220.0/24

6、清空iptables及关闭SELinux

[[email protected] ~]# iptables -F
[[email protected] ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
[[email protected] ~]# cat /etc/selinux/config | grep disabled
# disabled - No SELinux policy is loaded.SELINUX=disabled
[[email protected] ~]# getenforce Disabled

二、Keepalived搭建步骤

1、安装keepalived程序

[[email protected] ~]# yum install -y keepalived 	#DIP1安装
[[email protected] ~]# yum install -y keepalived    #DIP2安装

2、keepalived的D1/D2配置

  • Director1配置


[[email protected] keepalived]# cat keepalived.conf  #查看D1配置
    
       !Configuration File for keepalived
    global_defs {
    notification_email {
    [email protected]
    }
    notification_email_from [email protected]
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id LVS_DEVEL
    vrrp_mcast_group4 224.0.100.18
    }
    vrrp_instance VI_1 {
    state MASTER
    interface eno16777728
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    virtual_ipaddress {    	192.168.220.5
    }
    }
    
    virtual_server 192.168.220.5 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    protocol TCP
    
    real_server 192.168.220.25 80 {
    weight 1
    HTTP_GET {
    url {
      path /
      status_cde 200
    }
    connect_timeout 3
    nb_get_retry 3
    delay_before_retry 3
    }
    }
    real_server 192.168.220.26 80 {
    weight 1
    HTTP_GET {
    url {
      path /
      status_cde 200
    }
    connect_timeout 3
    nb_get_retry 3
    delay_before_retry 3
    }
    }
    }
    
    [[email protected] keepalived]# systemctl start keepalived		#DIP1启动服务
    [[email protected] keepalived]# ip add sh | grep "192.168.220." #查看,220.5地址有了
    inet 192.168.220.24/27 brd 192.168.220.31 scope global dynamic eno16777728
    inet 192.168.220.5/32 scope global eno16777728

  • Director2配置


[[email protected] keepalived]# cat keepalived.conf     #查D2看配置

            ! Configuration File for keepalived
    
    global_defs {
       notification_email {
    	[email protected]
       }
       notification_email_from [email protected]
       smtp_server 127.0.0.1
       smtp_connect_timeout 30
       router_id LVS_DEVEL
       vrrp_mcast_group4 224.0.100.18
    }
    
    vrrp_instance VI_1 {
    state BACKUP
    interface eno16777728
    virtual_router_id 51
    priority 98
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    virtual_ipaddress {    	192.168.220.5
    }
    }
    
    virtual_server 192.168.220.5 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    protocol TCP
    
    real_server 192.168.220.25 80 {
    weight 1
    HTTP_GET {
    url {
      path /
      status_cde 200
    }
    connect_timeout 3
    nb_get_retry 3
    delay_before_retry 3
    }
    }
    real_server 192.168.220.26 80 {
    weight 1
    HTTP_GET {
    url {
      path /
      status_cde 200
    }
    connect_timeout 3
    nb_get_retry 3
    delay_before_retry 3  
    }
    }
    }
    [[email protected] keepalived]# ip add sh | grep "192.168.220"
    inet 192.168.220.28/27 brd 192.168.220.31 scope global dynamic eno16777728

3、安装ipvsadm查看Lvs调度是否如我们所设定那样

[[email protected] keepalived]# yum install ipvsadm -y    #在D2安装ipvsadm
[[email protected] keepalived]# yum install ipvsadm -y	  #在D1安航ipvsadm
[[email protected] keepalived]# ipvsadm -Ln				  #在D1查看Lvs规则
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port   Forward Weight ActiveConn InActConn
TCP  192.168.220.5:80 rr
  -> 192.168.220.25:80Route   1  0  0 
  -> 192.168.220.26:80Route   1  0  0  
[[email protected] keepalived]# ipvsadm -Ln	#在D2查看Lvs规则
  IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port   Forward Weight ActiveConn InActConn
TCP  192.168.220.5:80 rr
  -> 192.168.220.25:80Route   1  0  0 
  -> 192.168.220.26:80Route   1  0  0

三、RealServer步骤搭建

1、编写设定VIP及内核功能参数脚本

[[email protected] ~]# vim skp.sh			#在R2上编辑skp.sh脚本添加下面内容
[[email protected] ~]# chmod +x skp.sh               #赋予执行权限
[[email protected] ~]# vim skp.sh		        #在R2上编辑skp.sh脚本添加下面内容
[[email protected] ~]# chmod +x skp.sh 		#赋予执行权限
#!/bin/bash#vip=192.168.220.5case $1 instart)
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig lo:0 $vip netmask 255.255.255.255 broadcast $vip
route add -host $vip dev lo:0;;
stop)
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig lo:0 del $vip
;;
esac

2、在RealServer1/RealServer2Z执行该脚本

[[email protected] ~]# sh skp.sh start	                #执行脚本
[[email protected] ~]# ip add sh | grep "192.168.220"	        #查看是否成功
inet 192scope global lo:0inet 192.168.220.25/27 brd 
192.168.220.31 scope global dynamic eno16777728

[[email protected] ~]# sh skp.sh start		        #执行脚本
[[email protected] ~]# ip add sh | grep "192.168.220."       #查看是否成功inet 
192.168.220.5/32 brd 192.168.220.5 scope global lo:0inet 
192.168.220.26/27 brd 192.168.220.31 scope global dynamic eno16777728

测试:

[[email protected] ~]# ip add sh | grep "192.168.11."				#查看IP地址是否是11.30
inet 192.168.11.17/32 brd 192.168.11.17 scope global lo:0inet 
192.168.11.30/24 brd 192.168.11.255 scope global eno16780032
[[email protected] ~]# curl 
  <h1>Test files 25.</h1>
[[email protected] ~]# curl 
  <h1>Test files 26.</h1>
[[email protected] ~]# curl 
  <h1>Test files 25.</h1>
[[email protected] ~]# curl 
 <h1>Test files 26.</h1>
[[email protected] ~]# curl 
[[email protected] ~]# curl 
[[email protected] ~]# curl 
[[email protected] ~]# curl http://192.168.220.5/www.sunshineboy.com/index.php | grep "One"	#特意把hostname改为不同
 % Total% Received % Xferd  Average Speed   TimeTime Time  Current
 Dload  Upload   Total   SpentLeft  Speed  0 00 00 0  0  0 --:--:-- --:--:-- --:--:-- 
 0<tr><td class="e">System </td><td class="v">Linux Real-Serve-One 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 
 19 22:10:57 UTC 2015 x86_64 </td></tr>100 421770 421770 0   758k  0 --:--:-- --:--:-- --:--:--  762k
[[email protected] ~]# curl http://192.168.220.5/www.sunshineboy.com/index.php | grep "Two"	#特意把hostname改为不同
 % Total% Received % Xferd  Average Speed   TimeTime Time  Current
 Dload  Upload   Total   SpentLeft  Speed  0 00 00 0  0  0 --:--:-- --:--:-- --:--:-- 
 0<tr><td class="e">System </td><td class="v">Linux Real-Server-Two 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 
 19 22:10:57 UTC 2015 x86_64 </td></tr>100 421790 421790 0  1084k  0 --:--:-- --:--:-- --:--:-- 1113k

测试静态动态页面分离

依然使用11.30 IP Client Test

server {
    listen       80;
    server_name  localhost;


    location / {
        root   /var/www/html;
        index  index.html index.htm;
    }

    location ~ .*\.php$ {
	proxy_pass http://192.168.220.25;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    }

	
-  nginx RealServer1配置
-  

    server {
    listen   80;
    server_name  www.855.com;
   
    location / {
    root   /var/www/html;
    index  index.html index.htm;
    }
    location ~ .*\.php$ {
    	root /var/www/html/www.sunshineboy.com;
    	fastcgi_pass 127.0.0.1:9000;
    	fastcgi_index index.php;
    	fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    	include fastcgi_params;
    }
   
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    root   /usr/share/nginx/html;
    }

    }
  • 访问测试

[[email protected] conf]# curl http://192.168.220.25/index.php | grep -o "Two\|One"  #访问25,发现One
     % Total% Received % Xferd  Average Speed   TimeTime Time  Current
     Dload  Upload   Total   SpentLeft  Speed
      0 00 00 0  0  0 --:--:-- --:--:-- --:--:-- 0One
[[email protected] conf]# curl http://192.168.220.5/index.php | grep -o "Two\|One"   #访问192.168.220.5,也是One	
      % Total% Received % Xferd  Average Speed   TimeTime Time  Current
     Dload  Upload   Total   SpentLeft  Speed
      0 00 00 0  0  0 --:--:-- --:--:-- --:--:-- 0One    100 491620 491620 0  1328k  0 --:--:-- --:--:-- --:--:-- 1333k
[[email protected] conf]# curl http://192.168.220.5/index.php | grep -o "Two\|One"
      % Total% Received % Xferd  Average Speed   TimeTime Time  Current
     Dload  Upload   Total   SpentLeft  Speed
      0 00 00 0  0  0 --:--:-- --:--:-- --:--:-- 0One    100 491620 491620 0  1091k  0 --:--:-- --:--:-- --:--:-- 1116k
[[email protected] conf]# curl http://192.168.220.26/index.php | grep -o "Two\|One"  #还是One,证明代理成功,动态静态分开了
      % Total% Received % Xferd  Average Speed   TimeTime Time  Current
     Dload  Upload   Total   SpentLeft  Speed
      0 00 00 0  0  0 --:--:-- --:--:-- --:--:-- 0One    100 492440 492440 0  1297k  0 --:--:-- --:--:-- --:--:-- 1335k


本文出自 “SunshineBoySZF” 博客,请务必保留此出处http://sunshineboyszf.blog.51cto.com/12087328/1865265

以上是关于Lvs+keepalived+httpd+NFS搭建高可用的主要内容,如果未能解决你的问题,请参考以下文章

keepalived + LVS DR 构建高可用负载均衡

LVS + Keepalived 搭建高可用的负载均衡群集

LVS+keepalived+httpd高可用集群

keepalived+lvs+tomcat+nfs架构部署

LVS+Keepalived搭建高可用负载均衡

使用keepalived搭建高可用的LVS-DR集群