实验环境及软件版本:

CentOS版本:    6.6(2.6.32.-504.el6.x86_64)

nginx版本:     nginx-1.6.2

keepalived版本:keepalived-1.2.7

准备工作:

关闭防火墙:service iptables stop

chkconfig iptables off

关闭selinux: sed -i 's/SELINUX=disabled/SELINUX=enforcing/g' /etc/selinux/config

init 6 重启系统一定要!

 

1.准备安装依赖包:

yum -y install pcre pcre-devel openssl openssl-devel zlib zlib-devel gcc gcc-c++ popt-devel

安装keepalived出现configure: error: Popt libraries is required

解决方法:yum -y install popt-devel

2.下载安装包

wget http://nginx.org/download/nginx-1.6.2.tar.gz

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

3.安装NginX

tar zxvf nginx-1.6.2.tar.gz

cd nginx-1.6.2

./configure

make && make install

4.安装keepalived

tar zxvf keepalived-1.2.7.tar.gz

cd keepalived-1.2.7

./configure

make

make install

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

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

mkdir /etc/keepalived

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

cp /usr/local/sbin/keepalived /usr/sbin/

5.加入开机自启动服务

方法一

echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local

echo "/etc/init.d/keepalived start" >> /etc/rc.local

方法二

[root@nginx keepalived-1.2.7]# cat >>/etc/rc.local<<EOF

> /usr/local/nginx/sbin/nginx

> /etc/init.d/keepalived start

> EOF

[root@nginx keepalived-1.2.7]# cat /etc/rc.local

#!/bin/sh

#

# This script will be executed *after* all the other init scripts.

# You can put your own initialization stuff in here if you don't

# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local

/usr/local/nginx/sbin/nginx

/etc/init.d/keepalived start

6.配置

  6.1 配置nginx

两台接入服务器的nginx的配置完全一样,主要配置/usr/local/nginx/conf/nginx.conf的http.其中多域名指向是通过虚拟主机(配置http下面的server)实现;同一域名的不同虚拟目录通过每

个server下面的不同location实现.

http {

include       mime.types;

default_type  application/octet-stream;

#log_format  main  '$remote_addr - $remote_user [$time_local] "$re

quest" '

#                  '$status $body_bytes_sent "$http_referer" '

#                  '"$http_user_agent" "$http_x_forwarded_for"';

#access_log  logs/access.log  main;

sendfile        on;

#tcp_nopush     on;

#keepalive_timeout  0;

keepalive_timeout  65;

#gzip  on;

upstream myserver{

server 192.168.0.110:80  max_fails=3 fail_timeout=20s;

server 192.168.0.111:80  max_fails=3 fail_timeout=20s;

ip_hash;

}

server {

listen       80;

server_name  192.168.0.150;

#charset koi8-r;

#access_log  logs/host.access.log  main;

location / {

root /var/www/html;

index index.php index.htm index.html;

proxy_redirect off;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_next_upstream http_500 http_502 http_503 error timeo

ut invalid_header;

proxy_set_header X-Forwarded-For $remote_addr;

proxy_pass   http://myserver;

}

}

6.2 配置keepalived

按照上面的安装方法,keepalived的配置文件在/etc/keepalived/keepalived.conf.主、从服务器的

 配置相关联但有所不同。如下:

 Master:

! Configuration File for keepalived

global_defs {

notification_email {

acassen@firewall.loc

failover@firewall.loc

sysadmin@firewall.loc

}

notification_email_from Alexandre.Cassen@firewall.loc

smtp_server smtp.etiantian.org

smtp_connect_timeout 30

router_id nginx_master

}

vrrp_instance VI_1 {

state MASTER

interface eth0

virtual_router_id 51

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.0.150

}

}

  Backup:

! Configuration File for keepalived

global_defs {

notification_email {

acassen@firewall.loc

failover@firewall.loc

sysadmin@firewall.loc

}

notification_email_from Alexandre.Cassen@firewall.loc

smtp_server smtp.etiantian.org

smtp_connect_timeout 30

router_id nginx_backup

}

vrrp_instance VI_1 {

state BACKUP

interface eth0

virtual_router_id 51

priority 99

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.0.150

}

}

7.验证

先后在主、从服务器上启动/usr/local/nginx/sbin/nginx

/etc/init.d/keepalived start

在主服务器上查看是否已经绑定了虚拟IP(VIP):ip addr

[root@nginx ~]# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN

link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

inet 127.0.0.1/8 scope host lo

inet6 ::1/128 scope host

valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

link/ether 00:0c:29:af:00:4e brd ff:ff:ff:ff:ff:ff

inet 192.168.0.130/24 brd 192.168.0.255 scope global eth0

inet 192.168.0.150/32 scope global eth0

inet6 fe80::20c:29ff:feaf:4e/64 scope link

valid_lft forever preferred_lft forever

停止主服务器上的keepalived:/etc/init.d/keepalived stop然后在从服务器上查看是否已经绑

定了虚拟IP(VIP):

[root@nginx_back ~]# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN

link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

inet 127.0.0.1/8 scope host lo

inet6 ::1/128 scope host

valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

link/ether 00:0c:29:21:b6:b1 brd ff:ff:ff:ff:ff:ff

inet 192.168.0.131/24 brd 192.168.0.255 scope global eth0

inet 192.168.0.150/32 scope global eth0

inet6 fe80::20c:29ff:fe21:b6b1/64 scope link

valid_lft forever preferred_lft forever

启动主服务器上的keepalived:/etc/init.d/keepalived start,再看看主服务器能否重新接管虚拟IP(VIP)

[root@nginx ~]# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN

link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

inet 127.0.0.1/8 scope host lo

inet6 ::1/128 scope host

valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

link/ether 00:0c:29:af:00:4e brd ff:ff:ff:ff:ff:ff

inet 192.168.0.130/24 brd 192.168.0.255 scope global eth0

inet 192.168.0.150/32 scope global eth0

inet6 fe80::20c:29ff:feaf:4e/64 scope link

valid_lft forever preferred_lft forever

Nginx+Keepalived主备负载均衡的更多相关文章

  1. keepalived+nginx双机热备+负载均衡

    Reference: http://blog.csdn.net/e421083458/article/details/30092795 keepalived+nginx双机热备+负载均衡 最近因业务扩 ...

  2. 使用Ansible实现nginx+keepalived高可用负载均衡自动化部署

    本篇文章记录通过Ansible自动化部署nginx的负载均衡高可用,前端代理使用nginx+keepalived,端web server使用3台nginx用于负载效果的体现,结构图如下: 部署前准备工 ...

  3. Nginx+Keepalived主备切换(包含nginx服务停止)

    原文地址:http://blog.sina.com.cn/s/blog_79ac6aa80101bmed.html Nginx+Keepalived主备切换(包含nginx服务停止) 环境: VM中4 ...

  4. Nginx+Keepalived 主备高可用 安装与配置

    环境说明:操作系统:CentOS6.7 x86_64Nginx版本:nginx-1.9.7Keepalived版本:keepalived-1.2.24 主nginx + Keepalived :10. ...

  5. Nginx+Keepalived高可用负载均衡

    转自 https://www.jianshu.com/p/da26df4f7d60 Keepalived+Nginx实现高可用Web负载均衡 Master backup vip(虚拟IP) 192.1 ...

  6. nginx实现请求的负载均衡 + keepalived实现nginx的高可用

    前言 使用集群是网站解决高并发.海量数据问题的常用手段.当一台服务器的处理能力.存储空间不足时,不要企图去换更强大的服务器,对大型网站而言,不管多么强大的服务器,都满足不了网站持续增长的业务需求.这种 ...

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

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

  8. Nginx+Keepalived主主负载均衡服务器

    Nginx+keepalived主主负载均衡服务器测试实验环境: 主Nginx之一:192.168.11.27主Nginx之二:192.168.11.28Web服务器一:192.168.11.37We ...

  9. LVS + keepalived + nginx + tomcat 实现主从热备 + 负载均衡

    前言 首先声明下,由于这两天找资料,看了不少博客 ,但是出于不细心,参考者的博客地址没有记录下来,所有文中要是出现了与大家博客相同的地方,那么请大家在评论区说明并附上博客地址,我好引用进来:这里表示抱 ...

随机推荐

  1. linux文件 编辑常用 inux手把手vi ---针对文件操作

    命令语法 说明 使用频率(1:常用;2一般:3:偶尔) 1.VI编辑器的启动与退出 VI编辑器的启动与退出 vi  file1 新建一个文本文件名为file1 :q 退出,如果对缓存去进行过修改,则提 ...

  2. unicode 编码总结

    unicode简介: unicode又称为unicode character set,缩写为ucs,意为字符集.编码方式有utf-7,utf-8,utf-16,utf-32几种,常用的是utf-8和u ...

  3. c语言实例

    #include <stdio.h> int main() { ; ; i=max(j,k); printf("i=%d\n",i); ; } int max(int ...

  4. Java基础知识强化之IO流笔记69:Properties练习之 判断文件中是否有指定的键,如果有就修改值的案例

    1. 我有一个文本文件(user.txt),我知道数据是键值对形式的,但是不知道内容是什么. 请写一个程序判断是否有"lisi"这样的键存在,如果有就改变其值为"100& ...

  5. check_area

    CCTouch* pTouch = ...; CCSprite* pSprite = ...; CCRect rect = pSprite ->boundingBox(); if ((& ...

  6. 听说alphago又要挑战sc2了?——我眼中的人工智能

    乱谈: 之前alphago进行的围棋比赛相当火爆. 一时间我的朋友圈都爆了,因为同学以及相关专业的同学都在发这个,毕竟逼格一下就起来了,我也大肆转发.各种角度,不同层次的不同深度的文章也都扫了几眼. ...

  7. C语言内存管理

    作用域: 1.代码块儿作用域: 注意的是:这里面{}就代表一个代码块儿,里面的东西是独立的,不可被其他地方访问的. 2.函数作用域 3.文件作用域 先提一下 在当前有一个文件,main.c 还有一个文 ...

  8. logstash input jdbc连接数据库

    示例 以下配置能够实现从 SQL Server 数据库中查询数据,并增量式的把数据库记录导入到 ES 中. 1. 查询的 SQL 语句在 statement_filepath => " ...

  9. 【原】NGUI中的UIAnchor脚本功能

    UIAnchor的功能是把对象锚定在屏幕的边缘(左上,左中,左下,上,中,下,右上,右中,右下),或缩放物体使其匹配屏幕的尺寸. 在1.90版本后,拉长(缩放)的功能被放到UIStretch中,UIA ...

  10. orale做报表常用函数和表达式的总结

    最近一段时间连续的做了几十张报表,通过原生sql对数据进行分析 ,也算是有了一定的了解,发现其中一些函数和表达式使用频率较高,现总结如下: (1).round()函数   round函数说白了就是把一 ...