keepalived+nginx+lvs 安装流程

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keepalived+nginx+lvs 安装流程相关的知识,希望对你有一定的参考价值。

原理

http://www.it165.net/admin/html/201401/2248.html

一、安装环境

CentOS-6.7-x86_64 四台

分别部署的服务

keepalived 主

keepalived 从

nginx 两台

二、ip分布

keepalived 主:192.168.1.4

keepalived 从 :192.168.1.5

nginx         主    :192.168.1.7

nginx         从     :192.168.19

lvs          虚拟ip  :192.168.1.20

三、服务部署

1、keepalived 主

1.安装keepalived ipvsadm 服务,主从一样安装这两个服务

     yum -y install keepalived ipvsadm

2.获取MD5加密字符串 得到的MD5加密字 添加到keepalived 配置文件中

      genhash -s 192.168.3.153  -p 80 -u  /index.html

      genhash -s 192.168.3.154  -p 80 -u  /index.html

3.配置主配置文件

########主配置文件

! Configuration File for keepalived

global_defs {

 router_id LVS-1                                   #实例名,可以配置多个实例,一个实例一行,一个实例一个虚拟块                                    


}                                                                                           vrrp_instance VI_1 {                                 #跟实例组中的实例名一 样                                                                        

state MASTER                                     ##设置lvs的状态,MASTER和BACKUP两种,必须大 写                                                  

 interface eth0                               #对外提供服务的网络接口

virtual_router_id 52                          #虚拟路由标识,MASTER和BACKUP是一致的,但在整个VRRP中是唯一的

priority 100                                     #优先级,数值越大,优先级越高,MASTER要高于 BACKUP                                              

advert_int 1                                     #同步检查间隔时间,MASTER和BACKUP之间同步检查的时间间隔,单位为秒                              

authentication {                                 #同步验证,验证密码为明文,MASTER和BACKUP之间的密码和验证类型必须一致                          

auth_type PASS                               #验证方式,就用 PASS                                                                            

auth_pass 1112                                #VRRP密 码                                                                                        }                                                                                           virtual_ipaddress {                       # 虚拟地址,可写多个,一个IP一行                                                            

192.168.3.30                                    ##这里设置的IP,一定要和real server的lvs脚本中的VIP一致                                       }                                                                                                                                               

}                                                                                           virtual_server 192.168.3.30 80 {                        #虚拟服务器配置,此处IP就是之前的VIP,端口就是需要提供负载的端口,比如80,3306等等            

delay_loop 6                                                   #延时等待时间,单位为秒                                                          

 lb_algo rr                                                                                           lb_kind DR                                                       #HA调度算法,互联网一般就用wlc加权最小连接调度和rr轮询round robin              

 #   persistence_timeout 600                               #会话保持时间,单一链接重连保持时间秒,可以理解为session共                               

protocol TCP                                 #转发协议,一般都是采用TCP方式,也可以使用UDP                                  

real_server 192.168.3.153 80 {                         #真实服务器的IP+端口,理解为需要高可用的应用服务,比如80,3306等等                        

weight 1                                           #权重值,值越大,权重越高,可以承担的负载雨大,服务器硬件比较好,可以使用高一点的权重    

 HTTP_GET {                                          #HTTP协议检 查                                                                           

url {    path /index.html

     digest 070b49915221a57de67e89932afed22

}   

connect_timeout 3                               #连接超时时间,单位秒                                

 nb_get_retry 3                                  #检测失败后,重试次数,超出设定值,将后端服务器移除

  delay_before_retry 3                            #失败重试时间                                             

 }   

 }

 real_server 192.168.3.154 80 {

 weight 1

 HTTP_GET {

            url {

              path /index.html

              digest e8c723dd72644280df67f74deb090bf9

            }   

           connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }   

    }

}


 3.从配置文件

! Configuration File for keepalived

global_defs {

   router_id LVS-2

}

vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 52

    priority 50

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1112

    }   

    virtual_ipaddress {

        192.168.3.30

    }   

}

virtual_server 192.168.3.30 80 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

   # persistence_timeout 600

    protocol TCP

    real_server 192.168.3.153 80 {

        weight 1

        HTTP_GET {

            url {

              path /index.html

              digest 070b49915221a57de67e89932afed227

            }

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }   

    real_server 192.168.3.154 80 {

        weight 1

        HTTP_GET {

            url {

              path /index.html

              digest e8c723dd72644280df67f74deb090bf9

            }

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }   

}


3、nginx 安装两台安装方法一样  可以安装脚本teng_..

  1.下载nginx 包

  从该网址下载 http://nginx.org/en/download.html

  nginx-1.2.9.tar.gz

  下载完成之后解压

   tar -zxvf nginx-1.2.9.tar.gz -C /usr/local/

  yum 安装nginx 需要的包

  yum install -y gcc gcc-c++ zlib-devel    openssl openssl-devel net-snmp-*

  编译安装nginx

   cd /usr/local/nginx-1.2.9

   ./configure --prefix=/usr/local/nginx  && make && make install

  进入编译的安装目录里面编写index 首页做为测试区别

   cd /usr/local/nginx/html

   echo -e "MASTER" > index.html

   echo -e "BACKUP" > index.html  ##另一台nginx 服务

   拉起nginx 服务

   ./usr/local/nginx/sbin

4、配置realserver  两台nginx 都需要配置nginx 服务

  vi /etc/init.d/realserver

#! /bin/bash

SYN_VIP=192.168.3.30

source /etc/rc.d/init.d/functions

case "$1" in

start)

    echo "start LVS of real server"

    ifconfig lo:0 $SYN_VIP netmask 255.255.255.255 broadcast $SYN_VIP

    echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore

       echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce

       echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore

       echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce

       sysctl -p >/dev/null 2>&1

       echo "RealServer Start OK"

    ;;

stop)

    echo "stop LVS of real server"

       ifconfig lo:0 down

       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore

       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce

       echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore

       echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce

       echo "RealServer Stoped"

       ;;

*)

    echo "USAGE: $0 {start|stop}"

    exit 1

esac

 2.拉起服务 /etc/init.d/realserver start

四、测试

在wen 上输入虚拟IP 看是否会自动切换 随便更改一个html 的主页信息看是否可以能够显示。

更改回来之后有能访问则说明成功

五、问题总结

1.selinux 是否关闭

2.keepalived配置文件是否有问题

3.ip 之间是否全部通信

4.iptables 是否关闭


本文出自 “无知不如无言” 博客,请务必保留此出处http://19930412.blog.51cto.com/6974556/1760917

以上是关于keepalived+nginx+lvs 安装流程的主要内容,如果未能解决你的问题,请参考以下文章

Lvs+keepalived+Nginx双机热备实现Nginx高可用

Nginx安装 配置反向代理 负载均衡 upstream ssl证书提供https访问 ha nginx keepalived双主热备 LVS实现高可用负载 Keepalived+Lvs+Nginx

nginx+lvs+keepalived安装

架构设计:负载均衡层设计方案——LVS + Keepalived + Nginx安装及配置

lvs+keepalived+nginx

Linux下部署LVS(DR)+keepalived+Nginx负载均衡