博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nginx+keepalived高可用实战
阅读量:5910 次
发布时间:2019-06-19

本文共 3071 字,大约阅读时间需要 10 分钟。

1、整体架构图如下

                       

2、环境准备

今天所配置的是keepalived+nginx 的负载均衡

下载keepalived软件

[root@LB01 tools]# wget http://www.keepalived.org/software/keepalived-1.1.17.tar.gz

注意安装前检查内核的link文件

root@LB02 tools]# ll /usr/src/

total 8

drwxr-xr-x. 2 root root 4096 Sep 23  2011 debug

drwxr-xr-x. 3 root root 4096 Oct 19 02:03 kernels

lrwxrwxrwx. 1 root root 43 Oct 19 02:05 linux -> /usr/src/kernels/2.6.32-642.6.1.el6.x86_64/

安装keepalived之前,安装几个依赖包

yum install openssl-devel -y

yum install popt* -y

然后进行编译安装keepalived,前面介绍了安装过程,这里就不演示了

./configure得出下面的结果

Keepalived configuration

------------------------

Keepalived version    : 1.1.17

Compiler         : gcc

Compiler        : -g -O2

Extra Lib      : -lpopt -lssl -lcrypto

Use IPVS Framework   : Yes

IPVS sync daemon support : Yes

Use VRRP Framework    : Yes

Use LinkWatch      : No

Use Debug flags     : No

注意./configure之后的结果,没有错误就可以了

make && make install

之后规范配置、启动文件路径

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

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

mkdir /etc/keepalived -p

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

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

/etc/init.d/keepalived start

注:nginx负载均衡相关配置请参考前面的文章

 

3、实战配置keepalived

[root@LB01 keepalived]# vi keepalived.conf 

! Configuration File for keepalived 

global_defs {

   notification_email {

   abc@qq.com

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 1.1.1.1

   smtp_connect_timeout 30

   router_id LVS_3

}

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 19

    priority 150

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.1.254/24

    }

}

[root@LB02 keepalived]# vi keepalived.conf

! Configuration File for keepalived

global_defs {

   notification_email {

   abc@qq.com

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 1.1.1.1

   smtp_connect_timeout 30

   router_id LVS_6

}

vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 19

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.1.254/24

    }

}

[root@LB01 keepalived]# /etc/init.d/keepalived start

Starting keepalived:    [  OK  ]

[root@LB02 keepalived]# /etc/init.d/keepalived start

Starting keepalived    [  OK  ]

[root@LB01 keepalived]# ip add|grep 192.168.1.254

    inet 192.168.1.254/24 scope global secondary eth0

[root@LB02 keepalived]# ip add|grep 192.168.1.254

测试访问

 

表明可以正常切换

现在我们模拟keepalived主宕机,再测试

[root@LB01 conf]# /etc/init.d/keepalived stop

Stopping keepalived:   [  OK  ]

[root@LB02 ~]# ip add|grep 254

    inet 192.168.1.254/24 scope global secondary eth0

 

4、反向代理服务故障自动切换

如果实际生产环境中当keeplived主的服务器nginx服务宕机,但是主又有VIP,这时就出现无法访问的现象,因此可以做如下的配置,使得这种情况可自已切换

vi check_nginx.sh

#!/bin/sh

white true

do

PNUM=`ps -ef|grep nginx|wc -l`

#这里也可使用nmap 192.168.1.3 -p 80|grep open|wc -l来判断个数

if [ $PNUM -lt 3 ];then

/etc/init.d/keepalived stop >/dec/null 2>&1

kill -9 keealived >/dec/null 2>&1

kill -9 keealived >/dec/null 2>&1

fi

sleep 5

done

sh check_nginx.sh &

启动个守护进程进行检查(或者加入定时任务定时执行检查),如果nginx服务出现故障,就立马停掉keepalived的服务,让它自动切换到备节点上去,这样就实现了自动切换的工作

 

 

转载于:https://www.cnblogs.com/guarderming/p/9466465.html

你可能感兴趣的文章
一个单元测试 学习 aysnc await
查看>>
Linux驱动总结3- unlocked_ioctl和堵塞(waitqueue)读写函数的实现 【转】
查看>>
iOS开发网络篇—HTTP协议
查看>>
jboss7 添加虚拟目录 上传文件路径
查看>>
在eclipse中建立lua开发环境
查看>>
CRT/LCD/VGA Information and Timing
查看>>
C# PPT 为形状设置三维效果
查看>>
Android DecorView浅析
查看>>
C 双向链表
查看>>
hdu 5452(树链刨分)
查看>>
LVM Linear vs Striped Logical Volumes
查看>>
Mysql主从备份和SQL语句的备份
查看>>
DEDECMS之三 首页、列表页怎么调用文章内容
查看>>
iOS开发多线程篇 09 —NSOperation简单介绍
查看>>
WINDOWS下调用GetTokenInformation的奇怪之处--两次调用
查看>>
HDU 5813 Elegant Construction 构造
查看>>
Tomcat就是个容器,一种软件
查看>>
php结合redis实现高并发下的抢购、秒杀功能
查看>>
统计服务连接状况
查看>>
Tomcat编译jsp生成Servlet文件的存放位置
查看>>