(25) Installation and configuration of keepalived

1. Highly reliable concept

HA (High Available), high availability cluster is a valid business continuity solutions, generally have two or more nodes, and into the active node and the backup node.

 

2. Highly reliable software keepalived

keepalive is a software that can achieve high reliability, usually deployed in 2 on the server, is divided into a main one. Keepalived may be made to the detection processes on the machine, once the Master detects a problem with the process, will switch themselves into Backup state, then switched to another node notifies Master state.

3.keepalived installation

 

Download keepalived official website : http: //keepalived.org

 

The keepalived extract to / usr / local / src directory

 

tar -zxvf  keepalived-1.2.19.tar.gz -C /usr/local/src

 

Into /usr/local/src/keepalived-1.2.19 directory

 

cd /usr/local/src/keepalived-1.2.19

 

Began to configure

 

./configure --prefix=/usr/local/keepalived

 

# Compile and install

 

make && make install

 

4. keepalived services added to the system (which can be started with the service command) in

Copy the executable file

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

The init.d files are copied to etc under , adding boot entry

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

Keepalived will copy the file to the etc under

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

Creating keepalived folder

mkdir -p /etc/keepalived

The keepalived copy the configuration file to etc under

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

Add executable permissions

chmod +x /etc/init.d/keepalived

 

## All of the above one-time command execution:

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

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

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

mkdir -p /etc/keepalived

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

chmod +x /etc/init.d/keepalived

chkconfig --add keepalived

chkconfig keepalived on

 

Add keepalived to boot automatically activated

chkconfig --add keepalived

chkconfig keepalived on

 

5. Configure virtual IP keepalived

Modify the configuration file: /etc/keepalived/keepalived.conf

#MASTER node

global_defs {

}

vrrp_instance VI_1 {

    state MASTER # Specify A is provided on the spare node to node is a primary node BACKUP to

    interface eth0 # Bind virtual IP network interface

    #VRRP virtual_router_id 51 disposed group name must be the same two nodes to indicate that each node belonging to the same VRRP group

    priority 100 # priority (master node 1-254 between), the node must be lower than the standby node priority

    advert_int 1 # multicast transmission interval ( heartbeat detection interval ) , the same two nodes must be provided

    authentication {# provided authentication information, both nodes must agree

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {    #指定虚拟IP(用户可见的ip地址), 两个节点设置必须一样

        192.168.33.60/24    #如果两个nginxip分别是192.168.33.61,,...62,则此处的虚拟ip跟它俩同一个网段即可

    }

}

 

#BACKUP节点

global_defs {

}

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.33.60/24

    }

}

 

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

service keepalived start

测试:

杀掉master上的keepalived进程,你会发现,在slave机器上的eth0网卡多了一个ip地址

查看ip地址的命令:  ip addr 

 

6.配置keepalived对管理的nginx进行心跳检测

 

原理:

 

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

 

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

 

#MASTER节点

 

global_defs {

}

 

vrrp_script chk_health {

    script "[[ `ps -ef | grep nginx | grep -v grep | wc -l` -ge 2 ]] && exit 0 || exit 1"  # 自定义脚本内容 返回1表示nginx挂了,返回0表示没挂

    interval 1    #每隔1秒执行上述的脚本,去检查用户的程序ngnix

    weight -2    # 如果本机器的nginx挂了,就降低自己的权重2

}

 

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 1

    priority 100

    advert_int 2

    authentication {

        auth_type PASS

        auth_pass 1111

    }

 

    track_script {

        chk_health  # 用户自定义的脚本名

    }

 

    virtual_ipaddress {

        10.0.0.10/24

    }

 

    notify_master "/usr/local/keepalived/sbin/notify.sh master"  # 当切换到master状态时,要执行的脚本

    notify_backup "/usr/local/keepalived/sbin/notify.sh backup"  # 当切换到backup状态时,要执行的脚本

    notify_fault "/usr/local/keepalived/sbin/notify.sh fault"  # 当出现异常时,要执行的脚本

}

 

 

 

#添加切换通知脚本

 

vi /usr/local/keepalived/sbin/notify.sh

 

#!/bin/bash

 

case "$1" in

    master)

        /usr/local/nginx/sbin/nginx

        exit 0

    ;;

backup)

        /usr/local/nginx/sbin/nginx -s stop

        /usr/local/nginx/sbin/nginx

        exit 0

    ;;

    fault)

        /usr/local/nginx/sbin/nginx -s stop

        exit 0

    ;;

    *)

        echo 'Usage: notify.sh {master|backup|fault}'

        exit 1

    ;;

esac

 

 

 

#添加执行权限

 

chmod +x /usr/local/keepalived/sbin/notify.sh

 

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 {

        10.0.0.10/24

    }

 

    notify_master "/usr/local/keepalived/sbin/notify.sh master"

    notify_backup "/usr/local/keepalived/sbin/notify.sh backup"

    notify_fault "/usr/local/keepalived/sbin/notify.sh fault"

}

 

 

 

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

 

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

 

service keepalived start

 

Guess you like

Origin www.cnblogs.com/paradis/p/11366666.html