一脸懵逼学习keepalived(对Nginx进行热备)

Posted --->别先生<---

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一脸懵逼学习keepalived(对Nginx进行热备)相关的知识,希望对你有一定的参考价值。

1:Keepalived的官方网址:http://www.keepalived.org/

2:Keepalived:可以实现高可靠;

  高可靠的概念:

    HA(High Available), 高可用性集群,是保证业务连续性的有效解决方案,一般有两个或两个以上的节点,且分为活动节点及备用节点。

3:高可靠软件:keepalived:

  keepalive是一款可以实现高可靠的软件,通常部署在2台服务器上,分为一主一备。Keepalived可以对本机上的进程进行检测,一旦Master检测出某个进程出现问题,将自己切换成Backup状态,然后通知另外一个节点切换成Master状态。

4:keepalived的安装操作:

   4.1:下载keepalived官网:http://keepalived.org

首先在两台机器上面部署两个nginx,具体操作见上篇部署一台,另一台的部署过程省略:

将keepalived上传到虚拟机以后进行解压缩操作:

[root@master package]# tar -zxvf keepalived-1.2.19.tar.gz -C /home/hadoop/

解压缩以后进入到解压缩的目录里面:

[root@master package]# cd /home/hadoop/keepalived-1.2.19/

检查安装环境,并指定将来要安装的路径:

[root@master keepalived-1.2.19]# ./configure --prefix=/home/hadoop/keepalived
最后编译和安装:

[root@master keepalived-1.2.19]# make && make install

5:将Keepalived添加到系统服务中:

 拷贝执行文件

  [root@master keepalived-1.2.19]# cp /home/hadoop/keepalived/sbin/keepalived /usr/sbin/

将init.d文件拷贝到etc,加入开机启动项

  [root@master keepalived-1.2.19]# cp /home/hadoop/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived

将keepalived文件拷贝到etc下

  [root@master keepalived-1.2.19]# cp /home/hadoop/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

创建keepalived文件夹

  [root@master hadoop]# mkdir -p /etc/keepalived

将keepalived配置文件拷贝到etc下:

  [root@master hadoop]# cp /home/hadoop/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf

添加可执行权限:

  [root@master hadoop]# chmod +x /etc/init.d/keepalived

 

添加keepalived到开机启动:

  [root@master hadoop]# chkconfig --add keepalived
  [root@master hadoop]# chkconfig keepalived on

 

6:配置keepalived虚拟IP:修改配置文件:

[root@master hadoop]# vim /etc/keepalived/keepalived.conf

这里配置虚拟Ip就开始分keepalived的master节点和keepalived的backup节点:

#master节点

vrrp_instance VI_1 {
    state MASTER  #指定A节点为主节点 备用节点上设置为BACKUP即可
    interface eth0  #绑定虚拟IP的网络接口
    virtual_router_id 51 #VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组
    priority 100 #主节点的优先级(1-254之间),备用节点必须比主节点优先级低
    advert_int 1 #组播信息发送间隔,两个节点设置必须一样
    authentication { #设置验证信息,两个节点必须一致
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress { #指定虚拟IP, 两个节点设置必须一样,
                        #如果两个nginx的ip分别是192.168.199.130,,...131,则此处的虚拟ip跟它俩同一个网段即可
        192.168.199.141/24
    }
}

配置好master节点以后,可以配置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.199.141/24
    }
}

 7:分别启动两台机器上面的keepalived:

给一个虚拟机设置两个ip地址方法:

ip addr add 192.168.199.150 dev eth0

 [root@master hadoop]# service keepalived start

最后测试一下:

如果杀掉master上的keepalived 进程,你会发现,在slaver即另外一台配置keepalived的机器上的eth0网卡多了一个ip地址

查看ip地址的命令:ip addr

 这里测试的时候出现问题了,因为我的电脑安装的虚拟机都是同一个,所以第一台机器的ip配置在eth0,而其他的竟然配置在了eth1,而这里需要修改一下,ip所在的位置:

解决方法:

1:编辑/etc/udev/rules.d/70-persistent-net.rules,找到与ifconfig -a得出的MAC相同的一行(NAME=\'eth1\'这一行),把它改为"NAME=eth0 ",然后把上面一行(NAME=\'eth0\')删除掉。

vim /etc/udev/rules.d/70-persistent-net.rules

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:bb:41:2b", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

2:编辑/etc/sysconfig/network-script/ifcfg-eth0,把MAC改为正确的,把UUID删掉。

3:编辑/etc/sysconf/network,把hostname也改一下。

4:重启生效!

 8:配置keepalived心跳检查:

  原理:

    Keepalived并不跟nginx耦合,它俩完全不是一家人

    但是keepalived提供一个机制:让用户自定义一个shell脚本去检测用户自己的程序,返回状态给keepalived就可以了;

master节点:

 vrrp_instance VI_1 {
    state MASTER  #指定A节点为主节点 备用节点上设置为BACKUP即可
    interface eth0  #绑定虚拟IP的网络接口
    virtual_router_id 51 #VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组
    priority 100 #主节点的优先级(1-254之间),备用节点必须比主节点优先级低
    advert_int 1 #组播信息发送间隔,两个节点设置必须一样
    authentication { #设置验证信息,两个节点必须一致
        auth_type PASS
        auth_pass 1111
    }

     track_script { #跟踪用户程序脚本
        chk_health
    }

    virtual_ipaddress { #指定虚拟IP, 两个节点设置必须一样,
                        #如果两个nginx的ip分别是192.168.199.130,,...131,则此处的虚拟ip跟它俩同一个网段即可
        192.168.199.141/24
    }

    notify_master "/home/hadoop/keepalived/sbin/notify.sh master"
    notify_backup "/home/hadoop/keepalived/sbin/notify.sh backup"
    notify_fault "/home/hadoop/keepalived/sbin/notify.sh fault"

}

添加切换通知脚本:

[root@master keepalived]# vim /home/hadoop/keepalived/sbin/notify.sh

#!/bin/bash
case "$1" in
    master)
        /home/hadoop/nginx/sbin/nginx
        exit 0
    ;;
backup)

        /home/hadoop/nginx/sbin/nginx -s stop
        /home/hadoop/nginx/sbin/nginx
        exit 0
    ;;
    fault)
        /home/hadoop/nginx/sbin/nginx -s stop
        exit 0
    ;;
    *)
        echo \'Usage: notify.sh {master|backup|fault}\'
        exit 1
    ;;
esac

添加执行权限:

[root@master keepalived]# chmod +x /home/hadoop/keepalived/sbin/notify.sh

然后配置一下slaver即另一台keepalived:

 global_defs {

 

}

vrrp_script chk_health {

    script "[[ `ps -ef | grep nginx | grep -v grep | wc -l` -ge 2 ]] && exit 0 || exit 1"

    interval 1

    weight -2
}

vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 1

    priority 99

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

 

    track_script {

        chk_health

    }

 

    virtual_ipaddress {

      192.168.199.141/24

    }


    notify_master "/home/hadoop/keepalived/sbin/notify.sh master"
    notify_backup "/home/hadoop/keepalived/sbin/notify.sh backup"
    notify_fault "/home/hadoop/keepalived/sbin/notify.sh fault"

}

最后:

在第二台机器上添加notify.sh脚本

#分别在两台机器上启动keepalived

service keepalived start

chkconfig keepalived on

以上是关于一脸懵逼学习keepalived(对Nginx进行热备)的主要内容,如果未能解决你的问题,请参考以下文章

一脸懵逼学习Linux的Shell编程

一脸懵逼学习Struts数据校验,数据回显,模型驱动,防止表单重复提交。

一脸懵逼学习Java操作Excel之POI(Apache POI)

一脸懵逼学习Java操作Excel之POI(Apache POI)

一脸懵逼学习Hive的使用以及常用语法

一脸懵逼学习Hive的安装