引言

Nginx是一个高性能的代理服务器,单台Nginx容易出现单点故障,使用keepalived可以实现Nginx的故障转移,保证了网站的高可用性

一、使用Nginx+keepalived的两种方案

1、主从模式

使用一个VIP,前端有2台服务器,一主一从,正常情况下是主服务器提供服务只有当主服务器不能正常提供服务之后,从服务器才提供服务,此时总会有一台服务器是空闲状态。

2、双主模式

使用两个VIP,前段有2台服务器,互为主从,两台服务器同时工作,不存在资源浪费情况。同时在前段的DNS服务器对网站做多条A记录,实现了Nginx
的负载均衡,当一台服务器故障时候,资源会转移到另一台服务器,继续提供服务,在大型的网站中多数都使用此种架构。在此使用主主模式配置
Nginx+keepalived的高可用性。

二、准备实验环境

1、服务器IP地址规划

VIP:172.16.10.8

VIP:172.16.10.9

Keepalived1:172.16.10.1

Keepalived2:172.16.10.2

2、服务器操作系统

Keepalived1:Centos 6.4 x86_64

Keepalived2:Centos 6.4 x86_64

3、网络拓扑图

4、修改主机名以及hosts文件keepalived1

1
2
3
4
5
6
7
8
9
10
11
12
13
####keepalived1 server############
sed -i 's@\(HOSTNAME=\).*@\1keepalived1@g'/etc/sysconfig/network
hostname keepalived1
[root@keepalived1 ~]# echo "172.16.10.1 keepalived1">> /etc/hosts
[root@keepalived1 ~]# echo "172.16.10.2 keepalived2">> /etc/hosts
[root@keepalived1 ~]# ssh-keygen -t rs
[root@keepalived1 ~]# ssh-copy-id -i .ssh/id_rsa.pub keepalived2
[root@keepalived1 ~]# scp /etc/hosts keepalived1:/etc/
####keepalived2 server############
sed -i 's@\(HOSTNAME=\).*@\1keepalived2@g'/etc/sysconfig/network
hostname keepalived2
[root@keepalived2 ~]# ssh-keygen -t rsa
[root@keepalived2 ~]# ssh-copy-id -i .ssh/id_rsa.pub keepalived1

三、编译安装Nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@keepalived1 ~]# yum install openssl-devel pcre-devel gcc -y
[root@keepalived1 ~]# tar xf nginx-1.4.2.tar.gz -C /usr/local/
[root@keepalived1 ~]# cd /usr/local/
[root@keepalived1 local]# groupadd -r nginx
[root@keepalived1 local]# useradd -r -g nginx nginx
[root@keepalived1 local]# cd nginx-1.4.2/
[root@keepalived1 nginx-1.4.2]# ./conf
conf/      configure
[root@keepalived1 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
[root@keepalived1 nginx-1.4.2]# make && make install

注意 在此只上传了keepalived1的代码,keepalived2也需要同样的操作

1、修改默认网页以方便后期测试

1
2
3
4
###############keepalived1######################
[root@keepalived1 ~]# echo "<h1>keepalived1</h1>" > /usr/html/index.html
###############keepalived2######################
[root@keepalived2 ~]# echo "<h1>keepalived2</h1>" > /usr/html/index.html

四、 安装与配置keepalived

1、安装keepalived

1
2
3
4
###############keepalived1######################
[root@keepalived1 ~]# yum install keepalived -y
###############keepalived2######################
[root@keepalived2 ~]# yum install keepalived -y

2、修改配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
[root@keepalived1 keepalived]# grep -v "#" /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from Alexandre.Cassen@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
 vrrp_script chk_nginx {        #监控nginx脚本
    script "killall -0 nginx"   #监控nginx进程
    interval 1                  #监控间隔
    weight -2                   #优先级-2
}
vrrp_instance VI_1 {
    state MASTER                 #主server 
    interface eth0             
    virtual_router_id 80
      priority 100               #优先级
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.10.8            #定义vip
    }
    track_script {
    chk_nginx                 #跟踪脚本
}
    notify_master "/etc/keepalived/notify8.sh master"  #定义邮件通知
    notify_backup "/etc/keepalived/notify8.sh backup"
    notify_fault "/etc/keepalived/notify8.sh fault"
}
                                                                                                                                                              
vrrp_instance VI_2 {
    state BACKUP            #从server
    interface eth0
    virtual_router_id 81
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.10.9
    }
    track_script {
    chk_nginx
}
    notify_master "/etc/keepalived/notify9.sh master" #定义邮件通知
    notify_backup "/etc/keepalived/notify9.sh backup"
    notify_fault "/etc/keepalived/notify9.sh fault"
}
[root@keepalived1 keepalived]#

3、编辑邮件通知脚本(notify8.sh notify9.sh)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#####################notify8.sh##############
[root@keepalived1 keepalived]# cat notify8.sh
#!/bin/bash
# Author: xiaodong <xiaodong@foxmail.com>
# description: An example of notify script
#
vip=172.16.10.8
contact='root@localhost'
notify() {
    mailsubject="`hostname` to be $1: $vip floating"
    mailbody="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` changed to be $1"
    echo $mailbody | mail -s "$mailsubject" $contact
}
case "$1" in
    master)
        notify master
        /etc/rc.d/init.d/nginx start
        exit 0
    ;;
    backup)
        notify backup
        /etc/rc.d/init.d/nginx stop
       exit 0
    ;;
    fault)
        notify fault
        exit 0
    ;;
    *)
        echo 'Usage: `basename $0` {master|backup|fault}'
        exit 1
    ;;
esac
####################notfiy9.sh#################
[root@keepalived1 keepalived]# cat notify9.sh
#!/bin/bash
# Author: xiaodong <xiaodong@foxmail.com>
# description: An example of notify script
#
vip=172.16.10.9
contact='root@localhost'
notify() {
    mailsubject="`hostname` to be $1: $vip floating"
    mailbody="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` changed to be $1"
    echo $mailbody | mail -s "$mailsubject" $contact
}
case "$1" in
    master)
        notify master
         exit 0
    ;;
    backup)
        notify backup
       exit 0
    ;;
    fault)
        notify fault
        exit 0
    ;;
    *)
        echo 'Usage: `basename $0` {master|backup|fault}'
        exit 1
    ;;
esac
[root@keepalived1 keepalived]# chmod +x notify8.sh
[root@keepalived1 keepalived]# chmod +x notify9.sh

4、复制配置文件到keepalived2,并做修改.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
[root@keepalived1 keepalived]# scp -p  keepalived.conf notify8.sh notify9.sh keepalived2:/etc/keepalived/
[root@keepalived2 keepalived]# grep -v "#" /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
     root@localhost
   notification_email_from Alexandre.Cassen@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
  vrrp_script chk_nginx {
   script "killall -0 nginx "
    interval 1
    weight -2
}
vrrp_instance VI_1 {
    state BACKUP                   #改为backup
    interface eth0
    virtual_router_id 80
    priority 99                    #改为99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.10.8
    }
    track_script {
        chk_nginx
}
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
}
vrrp_instance VI_2 {
    state MASTER              #改为MASTER
    interface eth0
    virtual_router_id 81
    priority 100              #改为100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.10.9
    }
    track_script {
        chk_nginx
}
    notify_master "/etc/keepalived/notify9.sh master"
    notify_backup "/etc/keepalived/notify9.sh backup"
    notify_fault "/etc/keepalived/notify9.sh fault"
}

注释:此处使用本地的邮件服务器接受邮件,如果需要用其它邮件服务器请修改contact='root@localhost'

5、启动keepalived服务

1
2
3
4
###############keepalived1######################
[root@keepalived1 ~]# service keepalived start
###############keepalived2######################
[root@keepalived2 ~]# service keepalived start

6、查看两个节点的vip是否启动正常

五、测试nginx+keepalived的高可用性

1、使用游览器访问测试

 

2、模拟节点出现故障,nginx服务器是否能自动转移

1
[root@keepalived1 keepalived]# service keepalived stop

 

通过以上测试,节点出现故障的时候,服务可以自动转移到备用节点上

3、测试主节点服务down掉之后,备用节点服务是否能正常运行

1
2
[root@keepalived1 keepalived]# service keepalived start
[root@keepalived1 keepalived]# killall nginx

通过以上测试,实现了Nginx的高可用性,但是,运维人员是否能第一时间得知服务器出现故障,这时候就需要查看邮件了

4、查看邮件是否收到信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@keepalived1 keepalived]# mail   #查看邮件命令
Heirloom Mail version 12.4 7/29/08.  Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N  1 root                  Wed Sep 25 20:15  18/728   "keepalived1 to be backup: 172.16.10.8 floating"
& 1
Message  1:
From root@keepalived1.localdomain  Wed Sep 25 20:15:46 2013
Return-Path: <root@keepalived1.localdomain>
X-Original-To: root@localhost
Delivered-To: root@localhost.localdomain
Date: Wed, 25 Sep 2013 20:15:46 +0800
To: root@localhost.localdomain
Subject: keepalived1 to be backup: 172.16.10.8 floating
User-Agent: Heirloom mailx 12.4 7/29/08
Content-Type: text/plain; charset=us-ascii
From: root@keepalived1.localdomain (root)
Status: R
2013-09-25 20:15:46: vrrp transition, keepalived1 changed to be backup
& quit                               #退出邮件

5、当nginx服务启动之后,主节点恢复

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@keepalived1 keepalived]# service nginx start
[root@keepalived1 keepalived]# mail
Heirloom Mail version 12.4 7/29/08.  Type ? for help.
"/var/spool/mail/root": 2 messages 1 unread
    1 root                  Wed Sep 25 20:15  19/739   "keepalived1 to be backup: 172.16.10.8 floating"
>U  2 root                  Wed Sep 25 20:16  19/738   "keepalived1 to be master: 172.16.10.8 floating"
&
Message  2:
From root@keepalived1.localdomain  Wed Sep 25 20:16:22 2013
Return-Path: <root@keepalived1.localdomain>
X-Original-To: root@localhost
Delivered-To: root@localhost.localdomain
Date: Wed, 25 Sep 2013 20:16:22 +0800
To: root@localhost.localdomain
Subject: keepalived1 to be master: 172.16.10.8 floating
User-Agent: Heirloom mailx 12.4 7/29/08
Content-Type: text/plain; charset=us-ascii
From: root@keepalived1.localdomain (root)
Status: RO
2013-09-25 20:16:22: vrrp transition, keepalived1 changed to be master

Nginx+keepalived的高可用负载均衡配置完成,本博客至此结束,如有任何疑问请留言!

 

Nginx+keepalived构建双主负载均衡代理服务器的更多相关文章

  1. Nginx keepalived实现高可用负载均衡详细配置步骤

    Keepalived是一个免费开源的,用C编写的类似于layer3, 4 & 7交换机制软件,具备我们平时说的第3层.第4层和第7层交换机的功能.主要提供loadbalancing(负载均衡) ...

  2. Centos7.2下基于Nginx+Keepalived搭建高可用负载均衡(一.基于Keepalived搭建HA体系)

    说明 本文只为方便日后查阅,不对一些概念再做赘述,网上都有很多明确的解释,也请大家先了解相关概念. 两台搭建HA的服务器是华为云上的ECS(不要忘记开通VPC,保证我们的服务器都处在一个内网环境),由 ...

  3. Nginx+Keepalived+Tomcat高可用负载均衡,Zookeeper集群配置,Mysql(MariaDB)搭建,Redis安装,FTP配置

    JDK 安装步骤 下载 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html rpm ...

  4. nginx+keepalived高可用web负载均衡

    一:安装环境 准备2台虚拟机,都安装好环境 centos 7keepalived:vip: 192.168.1.112192.168.1.110 nginxip 192.168.1.109 maste ...

  5. 基于Haproxy+Keepalived构建高可用负载均衡集群

    实验环境: 主机名 IP地址 VIP 192.168.200.254 Haproxy-1 192.168.200.101 Haproxy-2 192.168.200.102 Nginx1 192.16 ...

  6. Haproxy+Keepalived构建高可用负载均衡集群

    实验环境: 主机名 IP地址 VIP 192.168.200.254 Haproxy-1 192.168.200.101 Haproxy-2 192.168.200.102 Nginx1 192.16 ...

  7. nginx+keepalived(双主)

    一.环境 nginx1   192.168.40.211 nginx2   192.168.40.132 vip1     192.168.40.223  主为keep1,从为keep2 vip2   ...

  8. Nginx+Keepalived搭建高可用负载均衡集群

    本文的重点是Keepalived的配置,Nginx的配置就简略带过.软件:CentOS 7.2 / Nginx 1.12.2 / Keepalived 1.3.9 ha-01:192.168.1.97 ...

  9. HAProxy+Keepalived构建高可用负载均衡

    http://www.linuxidc.com/Linux/2012-03/55880.htm web1 IP 192.168.0.47 web2 IP 192.168.0.48 haproxy_ma ...

随机推荐

  1. 团队作业-Beta冲刺(周三)

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1 这个作业要求在哪里 https://edu.cnblo ...

  2. rcnn,sppnet,fast rcnn,ohem,faster rcnn,rfcn

    https://zhuanlan.zhihu.com/p/21412911 rcnn需要固定图片的大小,fast rcnn不需要 rcnn,sppnet,fast rcnn,ohem,faster r ...

  3. CMDB 数据加密 最终整合API验证+AES数据加密

    当CMDB运行在内网的时候,经过API验证的三关是没有问题的,但是如果运行在外网,有一个问题是,黑客截取后的访问速度比客户端快的时候还会造成数据泄露.为了解决这个问题,就要对数据进行加密 RSA加密 ...

  4. mac 使用homebrew 安装mysql

    1. 安装homebrew ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)" brew update ...

  5. mac 升级EI Capitan后遇到c++转lua时遇到libclang.dylib找不到的错

    升级EI Capitan后,打包lua脚本时,会报这个错: LibclangError: dlopen(libclang.dylib, 6): image not found. To provide ...

  6. Android深度探索总结

    Android深度探索前四章总结 通过这几章的学习真实体会到“移植”的概念:为特定设备定制Android的过程,但是移植的过程中开发最多的就是支持各种硬件设备的Linux驱动程序,本章对Android ...

  7. noip考前注意事项

    明天就要离开学校,后天的现在Day1已经考完啦,临近考试了紧不紧张呢(不紧张才怪),那么考前我们应该注意点什么呢? 1.带好自己的证件,带好自己的证件,带好自己的证件,这很重要. 2.试机的时候一定要 ...

  8. 使用TensorFlow的卷积神经网络识别手写数字(3)-识别篇

    from PIL import Image import numpy as np import tensorflow as tf import time bShowAccuracy = True # ...

  9. CodeForces 703A Mishka and trip

    Description Little Mishka is a great traveller and she visited many countries. After thinking about ...

  10. SDUSToj第十一次作业源代码格式问题

    Problem I: 源代码的格式 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 1471  Solved: 634 [Submit][Status][W ...