本实验用4台 centos6 虚拟机,2台做负载均衡,2台做web服务器,都先装上nginx

lb01:192.168.0.235  --主负载均衡器

lb02:192.168.0.236  --备负载均衡器

web1:192.168.0.237

web2:192.168.0.238

VIP:192.168.0.248

一. 在web1和web2上配置基于域名的虚拟主机,以web1为例

1. 配置nginx.conf,  推荐方法 egrep -v "#|^$" nginx.conf.default > nginx.conf生成简洁配置再修改

worker_processes  ;
events {
worker_connections ;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout ;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; server {
listen ;
server_name bbs.axinfu.com;
location / {
root html/bbs; #站点目录
index index.html index.htm;
}
access_log logs/access_bbs.log main; #这里用于测试proxy_set_header X-Forwarded-For $remote_addr命令
} server {
listen ;
server_name www.axinfu.com;
location / {
root html/www; #站点目录
index index.html index.htm; }
access_log logs/access_www.log main;
}
}

注意

1. 这里有2个虚拟主机,bbs.axinfu.com和www.axinfu.com,这里故意把bbs放在上面,以便测试lb01和lb02上nginx的配置命令proxy_set_header Host $host;

2.类似于$remote_addr(客户端地址),$host这样的内置变量可参考 http://www.cnphp.info/nginx-embedded-variables-lasted-version.html

2. 在站点目录建立测试文件

我的nginx目录为/usr/local/nginx/,在此目录下新建bbs,www两个目录,都新建index.html,分别写入

hello, web1, i am bbs

hello, web1, i am www

在web2上配置相似,在index.html中分别写入hello, web2, i am bbs/ hello, web2, i am www

3. 检查语法并重启nginx服务

/application/nginx/sbin/nginx -t

/application/nginx/sbin/nginx -s reload

netstat -lntup | grep 80

4. 把域名加入hosts解析,在/etc/hosts写入

192.168.0.237  bbs.axinfu.com

192.168.0.237  www.axinfu.com

5. 测试

curl bbs.axinfu.com  应该返回 hello, web1, i am bbs

curl www.axinfu.com  应该返回 hello, web1, i am www

二.  在lb01和lb02上配置nginx负载均衡如下,cat nginx.conf

worker_processes  ;
events {
worker_connections ;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout ;
upstream www_server_pools {
server 192.168.0.237: weight=;
server 192.168.0.238: weight=; }
server {
listen 192.168.0.248; #指定监听的VIP
server_name www.axinfu.com;
location / {
proxy_pass http://www_server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
} }
}

此配置仅代理里www.etiantian.org域名

命令说明

1. proxy_set_header Host $host;

这个命令的作用是识别代理的哪个虚拟主机,这里代理的是www.axinfu.com( server_name写明了)

本实验在web服务器上配置了2个虚拟主机,如果不加这个命令,访问192.168.0.248时就把节点服务器的第一个虚拟主机发给反向代理,也就是bbs.axinfu.com

2. proxy_set_header X-Forwarded-For $remote_addr;

此命令与web1/2服务器上虚拟主机的访问日志有关,也就是 /usr/local/nginx/logs/access_www.log,正常情况下,这个文件最后一个字段应显示访客IP

比如使用192.168.0.230作为客户端进行测试时,不加这个命令第一个字段显示的是反向代理服务器的IP,最后一个字段显示的为"-",加上上面的命令,日志的最后一个字段显示的是客户端访问IP,”192.168.0.230"

这个命令要在web服务器上开启log_format功能,就是在nginx配置文件中的log_format块加上”http_x_forwarded_for"

3. 如果不要负载均衡,只是需要转发功能。那么删掉upstream代码块,在proxy_pass直接写要转发到的地址就好,比如proxy_pass http://127.0.0.1:9000

三.  在lb01和lb02上配置keepalived服务

1. 在lb01上keepalived服务单实例主节点的配置如下,cat keepalived.conf

! Configuration File for keepalived

global_defs {
notification_email {
@qq.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout
router_id lb01

} vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 55 #主备的虚拟路由ID号要一样
priority 150

advert_int
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.0.248/ dev eth0 label eth0:
}
}

2. lb02上keepalived服务单实例备节点配置如下,cat keepalived.conf

! Configuration File for keepalived

global_defs {
notification_email {
@qq.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout
router_id lb02

} vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id
priority 100

advert_int
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.0.248/ dev eth0 label eth0: }
}

四. 测试

先开启lb01的nginx和keepalived, 然后开启lb02的nginx和keepalived

在网页上输入 http://192.168.0.248,刷新即可看到切换hello, web1, i am www, hello, web2, i am www.

关闭lb01的keepalived,可以看到lb02启动切换到VIP:192.168.0.248, 可用ip add | grep 192.168.0.248查看

刷新页面192.168.0.248,内容还和之前一样,则说明服务正常

五. 可能存在的问题及注意

1. 要把防火墙关闭

2. lb02上开启nginx有时候会出现VIP地址冲突,不能分配的问题

在/etc/sysctl.conf加入内核参数配置,net.ipv4.ip_nonlocal_bind = 1 表示启动nginx而忽略配置中监听的IP是否存在, 执行sysctl -p使修改生效,注意环境是centos6

3. 默认情况下keepalived仅在对方机器停机或者keepalived停掉的时候才会接管业务,但在实际工作中,如果nginx服务停止而keepalived还在工作的情况下,就会导致用户访问的VIP无法找到对应的业务

通俗点说,就是lb01的nginx挂了,但是keeplived是好的,怎么让lb02来接管业务

第一种方法,写个守护进程脚本,当nginx挂了就停掉本地的keepalived

#!/bin/bash
while true
do
if [ `netstat -lntup | grep nginx | wc -l` -ne ];then
/etc/init.d/keepalived stop
fi
sleep
done

第二种方法,在keepalived的配置文件中,加入检测服务脚本,检测监本如下

cat chk_nginx_proxy.sh

#!/bin/bash
if [ `netstat -lntup | grep nginx | wc -l` -ne ];then
/etc/init.d/keepalived stop
fi

增加执行权限,chmod +x chk_nginx_proxy.sh。 然后修改keepalived.conf,完整配置如下

lb01 # cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
notification_email {
@qq.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout
router_id lb01
} vrrp_script chk_nginx_proxy { #定义vrrp脚本
script "/server/scripts/chk_nginx_proxy.sh" #脚本位置
interval 2
weight 2
} vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id
priority
advert_int
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.0.248/ dev eth0 label eth0:
} track_script{
chk_nginx_proxy #触发检查
}
}

当lb01停掉nginx后,keepalived2秒内会被自动停掉,VIP释放,由lb02接管,这样就实现了即使服务宕机也会进行IP漂移,业务切换

nginx负载均衡配合keepalived服务案例实战的更多相关文章

  1. 企业级Nginx负载均衡与keepalived高可用实战(二)keepalived篇

    1.Keepalived高可用软件 1.1.Keepalived介绍 Keepalived软件起初是专门为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实 ...

  2. 企业级Nginx负载均衡与keepalived高可用实战(一)Nginx篇

    1.集群简介 1.1.什么是集群 简单地说,集群就是指一组(若干个)相互独立的计算机,利用高速通信网络组成的一个较大的计算机服务系统,每个集群节点(即集群中的每台计算机)都是运行各自服务的独立服务器. ...

  3. Nginx负载均衡和Keepalived的安装设置

    一.Nginx设置负载均衡 (1)upstream的配置 http { upstream backend { #这里设置后台分发的服务器族群,有多少个可以添加,同时设置查询策略 server 192. ...

  4. Nginx负载均衡的详细配置及使用案例详解.

    感谢看过这一些列博文和评论的小伙伴, 我把自己所看到的学到的拿到这里来分享是想和大家一起学习进步, 想听听园友给出的意见, 也是对自己学习过程的一个总结. 技术无止境, 我们仍需努力! 1,话不多说, ...

  5. Net分布式系统之三:Keepalived+LVS+Nginx负载均衡之高可用

    上一篇写了nginx负载均衡,此篇实现高可用(HA).系统整体设计是采用Nginx做负载均衡,若出现Nginx单机故障,则导致整个系统无法正常运行.针对系统架构设计的高可用要求,我们需要解决Nginx ...

  6. [项目构建 十三]babasport Nginx负载均衡的详细配置及使用案例详解.

    在这里再次说明下, 这个项目是从网上 找到的一套学习资料, 自己在 空闲时间学习了这些东西. 这里面的code当然会有很多不完善的地方, 但是确实也能学到很多新东西.感谢看过这一些列博文和评论的小伙伴 ...

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

    Keepalived+LVS+Nginx负载均衡之高可用 上一篇写了nginx负载均衡,此篇实现高可用(HA).系统整体设计是采用Nginx做负载均衡,若出现Nginx单机故障,则导致整个系统无法正常 ...

  8. Nginx(七):keepalived实现Nginx负载均衡服务器的双机高可用

    前言 之前咱们通过 Nginx(六):Nginx HTTP负载均衡和反向代理的配置与优化 和 Nginx+tomcat组合实现高并发场景的动静分离和负载均衡方案 这两篇文章了解了Nginx对高并发应用 ...

  9. JAVAEE——宜立方商城03:Nginx负载均衡高可用、Keepalived+Nginx实现主备

    1 nginx负载均衡高可用 1.1 什么是负载均衡高可用 nginx作为负载均衡器,所有请求都到了nginx,可见nginx处于非常重点的位置,如果nginx服务器宕机后端web服务将无法提供服务, ...

随机推荐

  1. Unity3d 下websocket的使用

    今天介绍一下如何在Unity3D下使用WebSocket. 首先介绍一下什么是websocket,以及与socket,和http的区别与联系,然后介绍一下websocket的一些开源的项目. WebS ...

  2. hibernate 反向生实体类 and 为什么老是多一个id

    hibernate 反向生实体类 and 为什么老是多一个id 2017年04月01日 20:32:51 阅读数:548

  3. 554. Brick Wall最少的穿墙个数

    [抄题]: There is a brick wall in front of you. The wall is rectangular and has several rows of bricks. ...

  4. eclipse+hbase开发环境部署

    一.前言 1. 前提 因为hbase的运行模式是伪分布式,需要用到hdfs,所以在此之前,我已经完成了hadoop-eclipse的开发环境搭建,详细看另一篇文章:hadoop开发环境部署——通过ec ...

  5. cakephp中sql查询between

    $trading_list = $this->Trading->find('all', array('conditions' => array('buy_time BETWEEN ? ...

  6. png 深度图像 转为 点云(opencv2)

    https://github.com/kruglov-dmitry/pnd2pcd_batch

  7. 使用第三方库连接MySql数据库:PyMysql库和Pandas库

    使用PyMysql库和Pandas库链接Mysql 1 系统环境 系统版本:Win10 64位 Mysql版本: 8.0.15 MySQL Community Server - GPL pymysql ...

  8. 多校训练4——Hehe

    递推题: dp[i]表示字符串第i个字母前有多少种不同的方法 1.出现一个hehe:dp[i]=dp[i-4]+dp[i-2] 意思是dp[i]=当前的hehe换成wqnmlgb+当前的hehe不换成 ...

  9. bootstrap导航条相关知识

    在导航条(navbar)中有一个背景色.而且导航条可以是纯链接(类似导航),也可以是表单,还有就是表单和导航一起结合等多种形式. 为导航条添加标题.二级菜单及状态 <div class=&quo ...

  10. schedule-pool模拟并行任务分片

    模拟并行任务分片 代码部分: package com.pool; import com.alibaba.fastjson.JSON; import java.io.BufferedReader; im ...