lvs+keepalived+nginx环境部署

Redhat搭建centos镜像作为yum源

2.删除原有的yum

[[email protected] ~]# rpm -qa | grep yum |xargs rpm -e --nodeps

[[email protected] ~]# rpm -qa | grep yum

3.下载需要的安装包

python-urlgrabber-3.9.1-11.el6.noarch.rpm

yum-metadata-parser-1.1.2-16.el6.x86_64.rpm

yum-3.2.29-81.el6.centos.noarch.rpm

yum-plugin-fastestmirror-1.1.30-40.el6.noarch.rpm

yum-utils-1.1.30-40.el6.noarch.rpm

4.安装rpm包

[[email protected] soft]# rpm -ivh --force python-urlgrabber-3.9.1-11.el6.noarch.rpm

[[email protected] soft]# rpm -ivh yum-*.rpm

[[email protected] ~]# mkdir /yum

上传镜像到/yum目录下面,并且配置yum源

[[email protected] soft]# cd /etc/yum.repos.d/

[[email protected] yum.repos.d]# vi redhat.repo

[base]

name=RedHat

baseurl=file:///yum

enabled=1

gpgcheck=0

gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

[[email protected] yum.repos.d]# yum clean all

[[email protected] yum.repos.d]# yum repolist

  


             Nginx代理HA集群的配置


1.在两台机器上分别安装keepalived,做nginx代理的HA集群。


主机安装keepalived

[[email protected] soft]# tar -zxvf keepalived-1.2.7.tar.gz

[[email protected] soft]# cd keepalived-1.2.7

[[email protected] keepalived-1.2.7]# yum -y install popt-devel

[[email protected] keepalived-1.2.7]# ./configure --prefix=/usr/local/keepalived/

[[email protected] keepalived-1.2.7]# make

[[email protected] keepalived-1.2.7]# make install

[[email protected] keepalived-1.2.7]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

[[email protected] keepalived-1.2.7]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

[[email protected] keepalived-1.2.7]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

[[email protected] keepalived-1.2.7]# mkdir /etc/keepalived

[[email protected] keepalived-1.2.7]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

[[email protected] keepalived-1.2.7]# cd /etc/keepalived/

[[email protected] keepalived-1.2.7]# cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

notification_email {

[email protected]

[email protected]

[email protected]

}

notification_email_from [email protected]

smtp_server 192.168.200.1

smtp_connect_timeout 30

router_id LVS_DEVEL

}

vrrp_instance VI_1 {

state MASTER                (主的身份)

interface eth0                (主的网卡接口)

virtual_router_id 51

mcast_src_ip 192.168.0.111        (主的IP地址)

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.0.114

}

}

备机安装keepalived

安装过程一致如上,配置文件如下

[[email protected] keepalived]# cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

notification_email {

[email protected]

[email protected]

[email protected]

}

notification_email_from [email protected]

smtp_server 192.168.200.1

smtp_connect_timeout 30

router_id LVS_DEVEL

}

vrrp_instance VI_1 {

state SLAVER

interface eth0

virtual_router_id 51

mcast_src_ip 192.168.0.112

priority 90

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.0.114

}

}

2.两台机器上启动keepalived服务

[[email protected] ~]# service  keepalived restart

3.在主机上查看是否有虚拟IP地址

4.监控和主备切换

编写脚本

[[email protected] ~]# cat nginx_check.sh

#!/bin/bash

a=`ps -C nginx --no-header | wc -l`

if [ $a -eq 0 ];then

/usr/local/nginx/sbin/nginx &> /dev/null

sleep 3

if [ `ps -C nginx --no-header | wc -l` -eq 0 ];then

service keepalived stop &> /dev/null

fi

fi

[[email protected] ~]# chmod +x /root/nginx_check.sh

脚本写好后,现在到keepalived的配置文件中调用脚本

[[email protected] ~]# cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

notification_email {

[email protected]

[email protected]

[email protected]

}

notification_email_from [email protected]

smtp_server 192.168.200.1

smtp_connect_timeout 30

router_id LVS_DEVEL

}

vrrp_script check_nginx_status {

script "/root/nginx_check.sh"

interval 2

}

vrrp_instance VI_1 {

state MASTER

interface eth0

virtual_router_id 51

mcast_src_ip 192.168.0.111

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

track_script {

check_nginx_status

}

virtual_ipaddress {

192.168.0.114

}

}

 

Lvs负载均衡

1.在主机器上安装ipvsadm

[[email protected] ~]# yum -y install ipvsadm

2.使用keepalived来管理lvs

[[email protected] ~]# cat  /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

   notification_email {

     [email protected]

     [email protected]

     [email protected]

   }

   notification_email_from [email protected]

   smtp_server 192.168.200.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

vrrp_script check_nginx_status {

    script "/root/nginx_check.sh"

    interval 2

    }

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

     track_script {

        check_nginx_status

    }

    virtual_ipaddress {

        192.168.0.114

    }

}

virtual_server 192.168.0.114 80 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    nat_mask 255.255.255.0

    persistence_timeout 50

    protocol TCP

    real_server 192.168.0.111 80 {

        weight 1

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

    }

}

    real_server 192.168.0.112 80 {

        weight 1

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

}

3.编写lvs管理脚本

[[email protected] ~]# cat lvs_keepd.sh

#!/bin/sh

#description:start realserver

vip=192.168.0.114

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

case "$1" in

 

start)

    echo "start server"

    /sbin/ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up

    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

;;

stop)

    echo "stop server"

    /sbin/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 "Usage: $0 (start | stop)"

  exit 1

esac

exit 0

4.启动脚本

[[email protected] ~]# bash /root/lvs_keepd.sh start

 

5.测试lvs是否启动

[[email protected] ~]# ipvsadm -ln

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  192.168.0.114:80 rr persistent 50

  -> 192.168.0.111:80             Local   1      1          0         

  -> 192.168.0.112:80             Route   1      0          0         

备机上操作也是一样


时间: 2024-11-13 07:55:08

lvs+keepalived+nginx环境部署的相关文章

LVS+keepalived+nginx+tomcat部署实现

# 拓扑如下所示 # 节点分布情况 LVS-dr-master eth0: 192.168.146.141 LVS-dr-slave eth0: 192.168.146.142 nginx1: eth0: 192.168.146.139 nginx2: eth0: 192.168.146.140 tomcat1: eth0: 192.168.146.138 启用了4个tomcat VIP: 192.168.146.200 # 具体配置 ### lvs master ### 1.安装ipvsadm

LVS+Nginx(LVS + Keepalived + Nginx安装及配置)

(也可以每个nginx都挂在上所有的应用服务器) nginx大家都在用,估计也很熟悉了,在做负载均衡时很好用,安装简单.配置简单.相关材料也特别多. lvs是国内的章文嵩博士的大作,比nginx被广泛接受还要早7年,并且已经被红帽作为了系统内置软件,可谓很牛了.lvs相对于nginx来说配置上就要相对复杂一些. 但是,有时候我们会看到大牛们分享的经验里面是lvs+nginx作为负载均衡了,一直想不明白这是个什么道理. 为什么会出现两者被同时使用呢?其实,这要从两者的各自优势来说了. nginx用

LVS+Keepalived+Nginx+Tomcat高可用集群搭建(转)

LVS+Keepalived+Nginx+Tomcat高可用集群搭建 最近公司重整架构,十几台服务器而且还要尽可能节约成本.我就谷歌了一下在几种集群之前进行比较最终采用了Keepalived+Nginx做负债均衡高可用.虽然之前也研究过集群,看过很多集群方面的原理和架构,但毕竟没有真正操作过以下案例是在虚拟机中完成实验其实对于高可用搭建来说只用给出Keepalived和Nginx的配置即可后面的一些安装搭建完全是本人项目需要可以不用理睬仅供参考. 本文只是实验搭建记录方便以后在服务器中实施搭建.

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

1.概述 上篇文章<架构设计:负载均衡层设计方案(6)--Nginx + Keepalived构建高可用的负载层>(http://blog.csdn.net/yinwenjie/article/details/47130609) 我们解说了Nginx的故障切换.而且承诺各位读者会尽快解说 LVS + Keepalived + Nginx的安装和配置.在中间由于工作的原因.我又插写了三篇关于zookeeper的原理使用的文章.今天这边文章我们回归主题.为各位读者解说LVS + Keepalive

LVS + Keepalived + Nginx安装及配置

1.概述 上篇文章<架构设计:负载均衡层设计方案(6)——Nginx + Keepalived构建高可用的负载层>(http://blog.csdn.net/yinwenjie/article/details/47130609) 我们讲解了Nginx的故障切换,并且承诺各位读者会尽快讲解 LVS + Keepalived + Nginx的安装和配置.在中间由于工作的原因,我又插写了三篇关于zookeeper的原理使用的文章.今天这边文章我们回归主题,为各位读者讲解 LVS + Keepaliv

lvs+keepalived+nginx实现高性能负载均衡集群(转)

lvs+keepalived+nginx实现高性能负载均衡集群 一.为什么要使用负载均衡技术? 1.系统高可用性 2.  系统可扩展性 3.  负载均衡能力 LVS+keepalived能很好的实现以上的要求,LVS提供负载均衡,keepalived提供健康检查,故障转移,提高系统的可用性!采用这样的架构以后很容易对现有系统进行扩展,只要在后端添加或者减少realserver,只要更改lvs的配置文件,并能实现无缝配置变更! 二.LVS+Keepalived介绍 1.  LVS LVS是一个开源

搭建LVS+Keepalived+nginx+tomcat高可用性,高性能jsp集群

LVS-master:192.168.0.210 LVS-backup:192.168.0.211 LVS-VIP:192.168.0.209 nginx+tomcat:192.168.0.212 nginx+tomcat:192.168.0.227 安装nginx所需包: Nginx-1.6.0.tar.gz和pcre-8.35.zip 一.安装pcre-8.35 1 #unzip pcre-8.35.zip 2 #cd pcre-8.35 3 #./configure 4 #make 5 #

lvs+keepalived+nginx实现高性能负载均衡集群

一.为什么要使用负载均衡技术? 1.系统高可用性 2.  系统可扩展性 3.  负载均衡能力 LVS+keepalived能很好的实现以上的要求,LVS提供负载均衡,keepalived提供健康检查,故障转移,提高系统的可用性!采用这样的架构以后很容易对现有系统进行扩展,只要在后端添加或者减少realserver,只要更改lvs的配置文件,并能实现无缝配置变更! 二.LVS+Keepalived介绍 1.  LVS LVS是一个开源的软件,可以实现LINUX平台下的简单负载均衡.LVS是Linu

LVS+Keepalived+Nginx+Tomcat高可用负载均衡集群配置(DR模式,一个VIP,多个端口)

一.概述 LVS作用:实现负载均衡 Keepalived作用:监控集群系统中各个服务节点的状态,HA cluster. 配置LVS有两种方式: 1. 通过ipvsadm命令行方式配置 2. 通过Redhat提供的工具piranha来配置LVS 软件下载: ipvsadm下载地址: http://www.linuxvirtualserver.org/software/kernel-2.6/ Keepalived下载地址: http://www.keepalived.org/software/ 安装