Nginx + Keepalived integrated high availability

1. Download

Download keepalived Address: http://www.keepalived.org/download.html

2, extract the installation

2.1, the installation dependencies

Keepalived need to install the compiler dependencies:

yum install -y openssl openssl-devel

2.2, install keepalived

tar -zxvf keepalived-1.2.18.tar.gz -C /usr/local/  #解压到 /usr/local/keepalived 目录下
cd keepalived-1.2.18/ && ./configure --prefix=/usr/local/keepalived  # prefix指定 keepalived 安装路径
make && make install

3, configure boot

The keepalived to install Linux system services, because there is no use keepalived the default installation path (the default path: / usr / local), after the installation is complete, you need to make some changes work:

3.1, first create a folder, copy the keepalived configuration file:

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

3.2, then copy the script file keepalived:

cp  /usr/local/keepalived/etc/rc.d/init.d/keepalived   /etc/init.d/
cp  /usr/local/keepalived/etc/sysconfig/keepalived     /etc/sysconfig/
ln  -s /usr/local/sbin/keepalived                      /usr/sbin/
ln  -s /usr/local/keepalived/sbin/keepalived           /sbin/

You can set the boot: chkconfig keepalived on, this we installed.

4, keepalived.conf configuration:

vim  /etc/keepalived/keepalived.conf

keepalived.conf profile comments:

4.1、Master

## ! Configuration File for keepalived

global_defs {
   router_id bhz005     ## 标识节点的字符串,通常为 hostname
}

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"     ## 执行脚本位置
    interval 2             ## 检测时间间隔
    weight -20             ## 如果条件成立则权重减20(-20)
}
## 定义虚拟路由 VI_1 为自定义标识。
vrrp_instance VI_1 {
    state MASTER         ## 主节点为MASTER,备份节点为BACKUP      
    interface eth6       ## 绑定虚拟IP的网络接口(网卡),与本机IP地址所在的网络接口相同(我这里是eth6)
    
    virtual_router_id 172           ## 虚拟路由ID号
    mcast_src_ip 192.168.1.172      ## 本机ip地址
    priority 100                    ## 优先级配置(0-254的值)
    Nopreempt  ## 
    advert_int 1                   ## 组播信息发送间隔,俩个节点必须配置一致,默认1s
    authentication {  
        auth_type PASS
        auth_pass bhz              ## 真实生产环境下对密码进行匹配
    }

    track_script {
        chk_nginx
    }
    
    virtual_ipaddress {
        192.168.1.170             ## 虚拟ip(vip),可以指定多个
    }
}

Chk_nginx description of:

keepalived regularly execute scripts and script execution result of the analysis, dynamic adjustment of priorities vrrp_instance. The heavy weight is right here with the following priority priority, and if a check script execution is successful, the weight will be 20, which is made 100--20 became 80, Master's priority is lower than the 80 to the Backup priority 90, it will automatically switch to standby.
If the script execution result is 0 and the value of greater than 0 weight configuration, the priority will increase.
If the script execution result is not 0 and the value of weight is less than 0 configuration, the priority will be reduced accordingly.

4.2、Backup

## ! Configuration File for keepalived

global_defs {
   router_id bhz006
}

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 2
    weight -20
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth7
    
    virtual_router_id 173
    mcast_src_ip 192.168.1.173
    priority 90 ##优先级配置
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass bhz
    }

    track_script {
        chk_nginx
    }
    
    virtual_ipaddress {
        192.168.1.170
    }
}

5, nginx_check.sh script:

#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
    /usr/local/nginx/sbin/nginx
    sleep 2
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
        killall keepalived
    fi
fi

Nginx_check.sh script will copy to each of the two machines /etc/keepalived/folder.

5.1, nginx_check.sh authorization script

Given executable permissions:

chmod +x /etc/keepalived/nginx_check.sh

6, two machines start keepalived

After the first start nginx 2 machine, and then start keepalived two machines.

Start nginx 2 machines:

  /usr/local/nginx/sbin/nginx  # 先启动 nginx 

Restart keepalived:

  service keepalived start     

View process:

  ps -ef | grep nginx 
  ps -ef | grep keepalived

7, test

Can be tested, first of all look at the two machines ip acommand will appear a virtual ip, we stopped a machine keepalived, and then test the command: service keepalived stop. The results showed that the current machine has stopped unavailable, keepalived will automatically switch to another machine.

We can test in the case of nginx problems, to achieve switching, this time we just need to nginx configuration file is modified, let it become unavailable, then the strong can kill nginx process, also found that automatic switching server node.

Guess you like

Origin blog.csdn.net/xiaojin21cen/article/details/91489773