Nginx+keepalived构建双主负载均衡代理服务器

引言

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 ‘[email protected]\(HOSTNAME=\).*@\[email protected]‘/etc/sysconfig/network

hostname keepalived1

[[email protected] ~]# echo "172.16.10.1 keepalived1">> /etc/hosts

[[email protected] ~]# echo "172.16.10.2 keepalived2">> /etc/hosts

[[email protected] ~]# ssh-keygen -t rs

[[email protected] ~]# ssh-copy-id -i .ssh/id_rsa.pub keepalived2

[[email protected] ~]# scp /etc/hosts keepalived1:/etc/

####keepalived2 server############

sed -i ‘[email protected]\(HOSTNAME=\).*@\[email protected]‘/etc/sysconfig/network

hostname keepalived2

[[email protected] ~]# ssh-keygen -t rsa

[[email protected] ~]# 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

[[email protected] ~]# yum install openssl-devel pcre-devel gcc -y

[[email protected] ~]# tar xf nginx-1.4.2.tar.gz -C /usr/local/

[[email protected] ~]# cd /usr/local/

[[email protected] local]# groupadd -r nginx

[[email protected] local]# useradd -r -g nginx nginx

[[email protected] local]# cd nginx-1.4.2/

[[email protected] nginx-1.4.2]# ./conf

conf/      configure

[[email protected] 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

[[email protected] nginx-1.4.2]# make && make install

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

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


1

2

3

4

###############keepalived1######################

[[email protected] ~]# echo "<h1>keepalived1</h1>" > /usr/html/index.html

###############keepalived2######################

[[email protected] ~]# echo "<h1>keepalived2</h1>" > /usr/html/index.html

四、 安装与配置keepalived

1、安装keepalived


1

2

3

4

###############keepalived1######################

[[email protected] ~]# yum install keepalived -y

###############keepalived2######################

[[email protected] ~]# 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

[[email protected] keepalived]# grep -v "#" /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_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"

}

[[email protected] 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##############

[[email protected] keepalived]# cat notify8.sh

#!/bin/bash

# Author: xiaodong <[email protected]>

# description: An example of notify script

#

vip=172.16.10.8

contact=‘[email protected]‘

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#################

[[email protected] keepalived]# cat notify9.sh

#!/bin/bash

# Author: xiaodong <[email protected]>

# description: An example of notify script

#

vip=172.16.10.9

contact=‘[email protected]‘

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

[[email protected] keepalived]# chmod +x notify8.sh

[[email protected] 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

[[email protected] keepalived]# scp -p  keepalived.conf notify8.sh notify9.sh keepalived2:/etc/keepalived/

[[email protected] keepalived]# grep -v "#" /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

   notification_email {

     [email protected]

   notification_email_from Alexand[email protected]

   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=‘[email protected]‘

5、启动keepalived服务


1

2

3

4

###############keepalived1######################

[[email protected] ~]# service keepalived start

###############keepalived2######################

[[email protected] ~]# service keepalived start

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

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

1、使用游览器访问测试

 

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


1

[[email protected] keepalived]# service keepalived stop

 

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

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


1

2

[[email protected] keepalived]# service keepalived start

[[email protected] keepalived]# killall nginx

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

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


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

[[email protected] 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 [email protected]  Wed Sep 25 20:15:46 2013

Return-Path: <[email protected]>

X-Original-To: [email protected]

Delivered-To: [email protected]

Date: Wed, 25 Sep 2013 20:15:46 +0800

To: [email protected]

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: [email protected] (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

[[email protected] keepalived]# service nginx start

[[email protected] 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 [email protected]  Wed Sep 25 20:16:22 2013

Return-Path: <[email protected]>

X-Original-To: [email protected]

Delivered-To: [email protected]

Date: Wed, 25 Sep 2013 20:16:22 +0800

To: [email protected]

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: [email protected] (root)

Status: RO

2013-09-25 20:16:22: vrrp transition, keepalived1 changed to be master

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

时间: 2024-11-10 03:34:53

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

Haproxy+keepalived实现双主负载均衡高可用集群

项目说明 1.         使用LVS负载均衡用户请求到后端web服务器,并且实现健康状态检查 2.         使用keepalived高可用LVS,避免LVS单点故障 3.         集群中分别在LK-01和LK-02运行一个VIP地址,实现LVS双主 4.         用户通过DNS轮训的方式实现访问集群的负载均衡(不演示) 实验拓扑 环境介绍: IP地址 功能描述 HK-01 172.16.4.100 调度用户请求到后端web服务器,并且和LK-02互为备份 HK-02

Nginx配合keepalived实现双主负载均衡

一.架构规划 1.服务器IP地址规划 VIP1:192.168.1.149 VIP2:192.168.1.150 Keepalived1:192.168.1.151 Keepalived2:192.168.1.152 WebServer1:192.168.1.201 WebServer2:192.168.1.202 2.服务器操作系统 所使用的操作系统均为CentOS release 6.6 (Final) x86_64,最小化安装. 3.网络拓扑图 二.配置Nginx代理服务器 此部分Node

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

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

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

Nginx+Keepalived搭建高可用负载均衡集群   一. 环境说明 前端双Nginx+keepalived,nginx反向代理到后端的tomcat集群实现负载均衡,Keepalived实现集群高可用. 操作系统: Centos 6.6_X64 Nginx版本: nginx-1.9.5 Keepalived版本:keepalived-1.2.13 结构: Keepalived+nginx-MASTER:10.6.1.210         Keepalived+nginx-BACKUP:

Nginx+keepalived双机热备+负载均衡 ???待续

keepalived+nginx双机热备+负载均衡 最近因业务扩展,需要将当前的apache 转为nginx(web), 再在web前端放置nginx(负载均衡).同时结合keepalived 对前端nginx实现HA.nginx进程基于于Master+Slave(worker)多进程模型,自身具有非常稳定的子进程管理功能.在Master进程分配模式下,Master进程永远不进行业务处理,只是进行任务分发,从而达到Master进程的存活高可靠性,Slave(worker)进程所有的业务信号都 由

Nginx+Keepalived 实现反代 负载均衡 高可用(HA)配置

Nginx+Keepalived实现反代负载均衡高可用(HA)配置 Nginx+Keepalived实现反代负载均衡高可用配置 OS IP 子网掩码 路由网关 Centos6.6 nginx Keepalived Eth0:192.168.26.210 255.255.252.0 192.168.25.3 VIP:192.168.27.210 Centos6.6 Nginx Keepalived Eth0:192.168.26.211 255.255.252.0 192.168.25.3 VIP

ubuntu 14.04.3 LTS 版本 通过 nginx + keepalived 配置 高可用 负载均衡集群演示

系统版本:ubuntu 14.04.3 LTS 服务器准备: lb01-> ifconfig 显示结果: 192.168.91.136 作用:安装keepalived 及 nginx lb02-> ifconfig 显示结果: 192.168.91.135 作用:安装keepalived 及 nginx web01-> ifconfig 显示结果: 192.168.91.134 作用:安装nginx 负责展示 index.html页面 web02-> ifconfig 显示结果:

单主模型的IPVS高可用和nginx+keepalived的双主高可用模型

一.IPVS的单主高可用 (1)配置RS,我们使用两台服务器作为RS服务器配置过程我们可以使用脚本来配置,这样当我们完成试验后便于卸载,脚本如下: #!/bin/bash # vip=192.168.125.198 #你用的虚拟路由的IP地址 case $1 in start) echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 1 > /p

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

实验环境: 主机名 IP地址 VIP 192.168.200.254 Haproxy-1 192.168.200.101 Haproxy-2 192.168.200.102 Nginx1 192.168.200.103 Nginx2 192.168.200.104   1.在Nginx1/2上编译安装nginx服务1.1 首先安装Nginx1 [[email protected] ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel