nginx使用keepalived实现高可用

环境:

主:linux-node1  110.0.0.137

备:linux-node2  110.0.0.138

VIP: 110.0.0.120

NGINX安装:

# rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

# yum install nginx

1.安装keepalived

<pre><code>

# yum -y install kernel-devel make gcc openssl-devel  libnl* popt*

# yum install -y openssl openssl-devel

# wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz

# tar zxf keepalived-1.2.2.tar.gz

# cd keepalived-1.2.2

# ./configure  --prefix=/usr/local/keepalived

# make && make install

# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

/bin/cp
/usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

# chmod +x /etc/init.d/keepalived

# mkdir /etc/keepalived

# /bin/cp /usr/local/sbin/keepalived /usr/sbin/

# ll /usr/sbin/

</code></pre>

2.配置keepalived

2.1.配置主nginx(linux-node1)

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

! 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_script chk_http_port {

script "/opt/nginx_pid.sh"

interval 2

weight 2

}

vrrp_instance VI_1 {

state MASTER

interface eth0

virtual_router_id 51

mcast_src_ip 110.0.0.137

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

track_script {

chk_http_port

}

virtual_ipaddress {

110.0.0.120

}

}

2.2.配置从nginx(linux-node2)

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

! 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_script chk_http_port {

script "/opt/nginx_pid.sh"

interval 2

weight 2

}

vrrp_instance VI_1 {

state BACKUP

interface eth0

virtual_router_id 51

mcast_src_ip 110.0.0.138

priority 99

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

track_script {

chk_http_port

}

virtual_ipaddress {

110.0.0.120

}

}

3.编写nginx检查脚本(主备都需要):

#!/bin/bash

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

if [ $A -eq 0 ];then

/etc/init.d/nginx start

sleep 3

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

/etc/init.d/keepalived stop

fi

fi

4.启动nginx和keepalived

/etc/init.d/nginx start

/etc/init.d/keepalived start

5.检查虚拟化IP 是否绑定成功

主节点:

可以看到vip 110.0.0.120 已经绑定到主的nginx服务器上了。

6.测试:

停止nginx

# /etc/init.d/nginx stop

发现主的nginx无法停止,因为/opt/nginx_pid.sh  会检查nginx状态,如果nginx停止了,那么这个脚本就会自动启动nginx。如果nginx启动失败,那么会立刻停止keepalived,把vip转移到backup  nginx上面

我们把nginx的配置文件设置错误,然后停止nginx,这个主的nginx就无法启动了。

检查主nginx机器ip状态:

检查backup nginx主机状态:

看到VIP已经转移到backup主机上了。

来自为知笔记(Wiz)

时间: 2024-11-10 10:43:15

nginx使用keepalived实现高可用的相关文章

nginx结合keepalived做高可用负载集群服务

一.概述 前面几篇介绍了nginx做web网站以及https网站,反向代理,LVS的NAT与DR调度负载,但由于nginx本身负载是七层,需要套接字,即使基于upstream模块模拟成四层代理也是瓶颈,因此本文介绍nginx基于keepalived做高可用负载均衡集群服务,目标就是两台keepalived对nginx反向代理负载服务做检查与调度,做成双主模式,nginx负载调度后端的httpd网站,任何一台调度器(nginx或keepalived服务)故障,不影响业务;后端任何一台web故障也不

Nginx+Tomcat+Keepalived实现高可用web集群

Nginx+Tomcat+Keepalived实现高可用web集群: 环境:CenOS 6.5Nginx-Master:10.10.10.128Nginx-Backup:10.10.10.129Tomcat1:10.10.10.130Tomcat2:10.10.10.131VIP:10.10.10.100 一.环境基础配置1.更换国内yum源2.关闭防火墙.SELinux3.时间同步 二.Web端安装Web服务 1.查看是否安装JDK [[email protected] ~]# java -v

nginx结合keepalived实现高可用负载均衡

1 规划和准备 需要统一接入的应用系统   应用系统 域名/虚拟目录 应用服务器及URL svn dev.mycompany.com/svn http://50.1.1.21/svn svn web管理 dev.mycompany.com/submin http://50.1.1.21/submin 网站 www.mycompany.com http://50.1.1.10; http://50.1.1.11; http://50.1.1.12 OA oa.mycompany.com http:

Nginx与keepalived实现高可用

主keepalived设置 #安装keepalived [[email protected] ~]# yum -y install keepalived #安装nginx [[email protected] ~]# yum -y install nginx —————————————————————————————————— #keepalived配置文件 [[email protected] ~]# vim /etc/keepalived/keepalived.conf ! Configur

nginx通过keepalived实现高可用的负载均衡

使用集群是网站解决高并发.海量数据问题的常用手段.当一台服务器的处理能力.存储空间不足时,不要企图去换更强大的服务器,对大型网站而言,不管多么强大的服务器,都满足不了网站持续增长的业务需求.这种情况下,更恰当的做法是增加一台服务器分担原有服务器的访问及存储压力.通过负载均衡调度服务器,将来自浏览器的访问请求分发到应用服务器集群中的任何一台服务器上,如果有更多的用户,就在集群中加入更多的应用服务器,使应用服务器的负载压力不再成为整个网站的瓶颈. 摘自<大型网站技术架构_核心原理与案例分析> 一,

nginx+keepalived的高可用负载均衡集群构建

实验架构图: 实验环境 Nginx和Keepalived原理介绍 参考博客:http://467754239.blog.51cto.com/4878013/1541421 1.nginx Nginx进程基于于Master+Slave(worker)多进程模型,自身具有非常稳定的子进程管理功能.在Master进程分配模式下,Master进程永远不进行业务处理,只是进行任务分发, 从而达到Master进程的存活高可靠性,Slave(worker)进程所有的业务信号都 由主进程发出,Slave(wor

Keepalived实现高可用Nginx反向代理和基于NAT的LVS及分析

1. 前言 keepalived是一个C语言开发的,能够基于Linux基础架构提供一个HA实现的软件.HA是基于VRRP协议实现,可以为LVS.Nginx.HAProxy等实现的LB提供高可用. 下图是keepalived的软件架构图 主要核心模块: Checkers:负责对Real Server进行健康检查. VRRP栈:实现了VRRP协议,实现了vrrp_sync_group扩展,不依赖于LVS可以独立的使用.例如下面第一个实验keepalived+nginx反代. IPVS Wrapper

Keepalived+LVS+Nginx负载均衡之高可用

Keepalived+LVS+Nginx负载均衡之高可用 上一篇写了nginx负载均衡,此篇实现高可用(HA).系统整体设计是采用Nginx做负载均衡,若出现Nginx单机故障,则导致整个系统无法正常运行.针对系统架构设计的高可用要求,我们需要解决Nginx负载均衡出现单机故障时,系统正常运行的需求.所以系统架构引入Keepalived组件,实现系统高可用. 一.Keepalived介绍 Keepalived是分布式部署系统解决系统高可用的软件,结合LVS(Linux Virtual Serve

架构设计:负载均衡层设计方案(6)——Nginx + Keepalived构建高可用的负载层

1.概述 前两遍文章中,我们一直在说后文要介绍Nginx + Keepalived的搭建方式.这篇文章开始,我们就来兑现前文的承诺,后续的两篇文章我们将介绍Nginx + Keepalived和 LVS + Keepalived搭建高可用的负载层系统.如果你还不了解Nginx和LVS的相关知识,请参见我之前的两篇文章<架构设计:负载均衡层设计方案(2)--Nginx安装>(http://blog.csdn.net/yinwenjie/article/details/46620711).<