简介

Nginx可以实现高并发反向代理,lvs集群可以实现负载均衡,但是他们都有一个共同的弊端,就是Nginx,lvs架构中Director是单点故障,有没有一个好的方案解决这个问题呢?答案是有。通过Keepalived就可以实现,前端Nginx,lvs中Director服务器的高可用和负载均衡,通过shell命令或者脚本可以实现对服务器状态和服务的监控!

一、环境介绍
1、系统环境及软件版本
操作系统:CentOS6.4-i386
软件版本:Nginx-1.4.2
keepalived-1.2.7
yum源:
# vim /etc/yum.repos.d/centos6.repo
[base]
name=centos-base
baseurl=http://mirrors.sohu.com/centos/$releasever/os/$basearch
gpgcheck=1
enable=1
gpgkey=http://mirrors.sohu.com/centos/RPM-GPG-KEY-CentOS-6
[epel]
name=Fedora-epel
baseurl=http://mirrors.sohu.com/fedora-epel/$releasever/$basearch/
enable=1
gpgcheck=0

提示:如果你的系统是centos,系统默认的yum源是可以用的
2、拓扑图

3、IP地址规划
Client: 172.16.254.28
Keepalived+Nginx1: 172.16.3.3 Vip: 172.16.3.100
Keepalived+Nginx2: 172.16.3.4 Vip: 172.16.3.200

二、安装
1、安装keepalived
[root@node1 ~]# yum install keepalived

2、编译安装Nginx

[root@node1 ~]#useradd -r nginx
[root@node1 ~]#yum -y groupinstall “Development tools” “Server  Platform Development”
[root@node1 ~]#yum -y install pcre-devel
[root@node1 ~]#tar xf nginx-1.4.2.tar.gz
[root@node1 ~]#cd nginx-1.4.2
[root@node1 nginx-1.4.2]# ./configure \
–prefix=/usr\
–sbin-path=/usr/sbin/nginx\
–conf-path=/etc/nginx/nginx.conf \
–error-log-path=/var/log/nginx/error.log \
–http-log-path=/var/log/nginx/access.log \
–pid-path=/var/run/nginx/nginx.pid  \
–lock-path=/var/lock/nginx.lock \
–user=nginx \
–group=nginx \
–with-http_ssl_module \
–with-http_flv_module \
–with-http_stub_status_module \
–with-http_gzip_static_module \
–http-client-body-temp-path=/var/tmp/nginx/client/\
–http-proxy-temp-path=/var/tmp/nginx/proxy/\
–http-fastcgi-temp-path=/var/tmp/nginx/fcgi/\
–http-uwsgi-temp-path=/var/tmp/nginx/uwsgi\
–http-scgi-temp-path=/var/tmp/nginx/scgi\
–with-pcre

提示:在两台服务器上都要安装Nginx
3、提供Nginx服务启动脚本

[root@node1 nginx-1.4.2]# vim /etc/rc.d/init.d/nginx
#!/bin/sh
#
# nginx – this script starts and stops the nginx daemon
#
# chkconfig:  – 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#              proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:    /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING"= "no"] && exit0
nginx=”/usr/sbin/nginx”
prog=$(basename$nginx)
NGINX_CONF_FILE=”/etc/nginx/nginx.conf”
[ -f /etc/sysconfig/nginx] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep”configure arguments:”| sed’s/[^*]*–user=\([^ ]*\).*/\1/g’-`
options=`$nginx -V 2>&1 | grep’configure arguments:’`
foropt in$options; do
if[ `echo$opt | grep'.*-temp-path'` ]; then
value=`echo$opt | cut-d “=”-f 2`
if[ ! -d "$value"]; then
# echo “creating” $value
mkdir-p $value && chown-R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit5
[ -f $NGINX_CONF_FILE ] || exit6
make_dirs
echo-n $”Starting $prog: “
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq0 ] && touch$lockfile
return$retval
}
stop() {
echo-n $”Stopping $prog: “
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq0 ] && rm-f $lockfile
return$retval
}
restart() {
configtest || return$?
stop
sleep1
start
}
reload() {
configtest || return$?
echo-n $”Reloading $prog: “
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null2>&1
}
case”$1″in
start)
rh_status_q && exit0
$1
;;
stop)
rh_status_q || exit0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit0
;;
*)
echo$”Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}”
exit2
esac
[root@node1 nginx-1.4.2]# chmod +x /etc/rc.d/init.d/nginx
[root@node1 nginx-1.4.2]# chkconfig –add nginx
[root@node1 nginx-1.4.2]# chkconfig nginx on
[root@node1 nginx-1.4.2]# service nginx start
Starting nginx:                                            [  OK  ]

提示:服务脚本在两台Nginx服务器上都要提供

接下来请看第2页精彩内容:http://www.linuxidc.com/Linux/2013-10/90809p2.htm

Nginx 的详细介绍请点这里
Nginx 的下载地址请点这里

相关阅读:

Nginx反向代理+负载均衡+健康探测+缓存 http://www.linuxidc.com/Linux/2013-09/89774.htm

Nginx Tomcat 集群负载均衡解决笔记 http://www.linuxidc.com/Linux/2013-07/86827.htm

Nginx 配置轮询分流-实现负载均衡【测试通过】 http://www.linuxidc.com/Linux/2013-06/86692.htm

Nginx负载均衡引起的网站不可用 http://www.linuxidc.com/Linux/2013-05/84063.htm

在Linux上使用Nginx为Solr集群做负载均衡 http://www.linuxidc.com/Linux/2012-12/75257.htm

Keepalived+Nginx实现高可用和双主节点负载均衡的更多相关文章

  1. Keepalived+Nginx实现高可用Web负载均衡

    1.安装编译 Nginx 所需的依赖包# yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zli ...

  2. Dubbo入门到精通学习笔记(十六):Keepalived+Nginx实现高可用Web负载均衡

    文章目录 Keepalived+Nginx实现高可用Web负载均衡 Keepalived+Nginx实现高可用Web负载均衡 高可用架构篇 Keepalived + Nginx 实现高可用 Web 负 ...

  3. Keepalived + Nginx 实现高可用 Web 负载均衡

    一.Keepalived 简要介绍 Keepalived 是一种高性能的服务器高可用或热备解决方案, Keepalived 可以用来防止服务器单点故障的发生,通过配合 Nginx 可以实现 web 前 ...

  4. Keepalived+Nginx实现高可用(HA)

    Keepalived+Nginx实现高可用(HA) service iptables stopchkconfig iptables offsetenforce 0/etc/selinux/config ...

  5. 001/Nginx高可用模式下的负载均衡与动静分离(笔记)

    Nginx高可用模式下的负载均衡与动静分离 Nginx(engine x)是一个高性能的HTTP和反向代理服务器,具有内存少,并发能力强特点. 1.处理静态文件.索引文件以及自动索引:打开文件描述符缓 ...

  6. [转]搭建Keepalived+Nginx+Tomcat高可用负载均衡架构

    [原文]https://www.toutiao.com/i6591714650205716996/ 一.概述 初期的互联网企业由于业务量较小,所以一般单机部署,实现单点访问即可满足业务的需求,这也是最 ...

  7. Keepalived+Nginx实现高可用负载均衡集群

    一 环境介绍 1.操作系统CentOS Linux release 7.2.1511 (Core) 2.服务keepalived+nginx双主高可用负载均衡集群及LAMP应用keepalived-1 ...

  8. KeepAlived+Nginx实现高可用负载

    一.环境及安装版本: centos6.5.Nginx1.4.7.keepalived1.3.2 虚拟IP 真是IP Nginx端口 主从分配 10.0.90.215 10.0.90.217 80 MA ...

  9. Linux巩固记录(9) keepalived+nginx搭建高可用负载分发环境

    环境准备(继续服用hadoop节点) slave1  192.168.2.201(CentOs 7) slave2  192.168.2.202(CentOs 7) slave1 和 slave2 上 ...

随机推荐

  1. Docker入门到实战

    1.系统要求 Docker CE 支持 64 位版本 CentOS 7,并且要求内核版本不低于 3.10. CentOS 7满足最低内核的要求,但由于内核版本比较低,部分功能(如 overlay2 存 ...

  2. Ocelot网关统一查看多个微服务asp.net core项目的swagger API接口

    0.前言 整体架构目录:ASP.NET Core分布式项目实战-目录 一.准备 前提需要下载安装consul,项目需要懂添加swagger 统一在网关中配置多个微服务的swagger,需要用到服务注册 ...

  3. Dictionary<TKey,TValue>

    /* * 先将 key 和 bucket 的长度一起,经过简单的 hash 算法计算出元素应该放在哪个 bucket . * 但是,元素并不是放在 bucket 里面的,bucket 只是对元素存放位 ...

  4. 关于python安全性的问题

    收集总结了一下python安全方面的知识点以及近年来的相关漏洞,如果有需要修正或补充的地方,欢迎各位师傅的指出. 常见web漏洞在python中的示例. xss python下的xss其原理跟php是 ...

  5. luoguP4457 [BJOI2018]治疗之雨 概率期望 + 高斯消元

    应该是最后一道紫色的概率了....然而颜色啥也代表不了.... 首先看懂题意: 你现在有$p$点体力,你的体力上限为$n$ 在一轮中, 1.如果你的体力没有满,你有$\frac{1}{m + 1}$的 ...

  6. wampserver -- 取消PHP页面Warning和Notice级别的报错

    Learn from:http://yige.org/p/91 一般遇到这样的问题,有两个方法:1.如果有服务器权限,直接把服务器上的php.ini的配置改了,改成不输出Warning和Notice级 ...

  7. python开发_搜索本地文件信息写入文件

    功能:#在指定的盘符,如D盘,搜索出与用户给定后缀名(如:jpg,png)相关的文件 #然后把搜索出来的信息(相关文件的绝对路径),存放到用户指定的 #文件(如果文件不存在,则建立相应的文件)中 之前 ...

  8. SpringMvc的服务器端跳转和客户端跳转

    首先,找到 package org.springframework.web.servlet.view; public class InternalResourceViewResolver extend ...

  9. Choosing an ORM strategy

    One of the mistakes I see people make (and have made myself) is assuming that you must choose exactl ...

  10. OpenCV特征描述

    特征描述 目标 在本教程中,我们将涉及: 使用 DescriptorExtractor 接口来寻找关键点对应的特征向量. 特别地: 使用 SurfDescriptorExtractor 以及它的函数  ...