RedHat+Keepalived+Tengine实现高可用(源码安装)


title: RedHat+Keepalived+Tengine实现高可用(源码安装)
categories: Linux
tags:
- Keepalived
- Tengine
- Nginx
- Cluster
- HA
timezone: Asia/Shanghai
date: 2019-01-06


Keepalived官网:http://www.keepalived.org/

Tengine官网:http://tengine.taobao.org/

PCRE官网:http://www.pcre.org/

OpenSSL官网:https://www.openssl.org/

zile官网:http://www.zlib.net/

环境(在RedHat6和7下分别测试通过)

[root@redhat610-ch-02 ~]# cat /etc/redhat-release 
Red Hat Enterprise Linux Server release 6.10 (Santiago)

[root@localhost keepalived]# cat /etc/redhat-release 
Red Hat Enterprise Linux Server release 7.3 (Maipo)

tengine-2.2.3.tar.gz
keepalived-2.0.10.tar.gz

pcre-8.42.tar.gz
zlib-1.2.11.tar.gz
openssl-1.1.1a.tar.gz

前提

1.关闭系统防火墙
2.设置本地yum安装开发工具包,其他所有通过源码安装

第一步:安装依赖项

1.安装开发工具包

yum groupinstall -y "Development Tools"

2.安装pcre

wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz
tar vxzf pcre-8.42.tar.gz
cd pcre-8.42
./configure
make
make install

3.安装OpenSSL

wget https://www.openssl.org/source/openssl-1.1.1a.tar.gz
tar vxzf openssl-1.1.1a.tar.gz
cd openssl-1.1.1a
./config
make
make install

4.安装zlib

wget http://www.zlib.net/zlib-1.2.11.tar.gz
tar vxzf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make
make install

第二步:安装Tengine

1.安装Tengine

wget http://tengine.taobao.org/download/tengine-2.2.3.tar.gz
tar vxzf tengine-2.2.3.tar.gz
cd tengine-2.2.3
./configure --prefix=/home/bin/tengine
make
make install

2.建立测试主页并启动Tengine测试

echo "1111111111" > /home/bin/tengine/html/index.html
echo "2222222222" > /home/bin/tengine/html/index.html

/home/bin/tengine/sbin/nginx

http://10.0.1.101
http://10.0.1.102

3.在运行nginx的时候报错处理。

错误提示:/home/bin/tengine/sbin/nginx: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory

解决方法for RedHad6:
ln -s /usr/local/lib/libpcre.so.1 /lib64

解决方法for RedHad7:
ln -s /usr/local/lib64/libssl.so.1.1 /usr/lib64/libssl.so.1.1
ln -s /usr/local/lib64/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1

第三步:安装Keepalived并设置HA高可用

1.安装Keepalived

wget http://www.keepalived.org/software/keepalived-2.0.10.tar.gz
tar vxzf keepalived-2.0.10.tar.gz
cd keepalived-2.0.10
./configure
make
make install

keepalived --help

2.编辑配置文件

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

Keepalived启动的时候会从/etc/keepalived/keepalived.conf读取配置文件.安装后默认的地址是在/usr/local/etc/keepalived/keepalived.conf所以需要复制过来。

两个节点配置文件不一样的地方只有state、priority和router_id,其他完全保持一致。

节点1配置:

! Configuration File for keepalived

global_defs {
#   notification_email {
#     [email protected]
#     [email protected]
#     [email protected]
#   }
#   notification_email_from [email protected]
#   smtp_server 192.168.200.1
#   smtp_connect_timeout 30
   router_id node1
#   vrrp_skip_check_adv_addr
#   vrrp_strict
#   vrrp_garp_interval 0
#   vrrp_gna_interval 0
}

vrrp_script chk_nginx {
    script "/etc/keepalived/check_nginx.sh"
    interval 1
    weight -20
}

vrrp_instance VI_1 {
    state MASTER                ## 这里只能接受MASTER和BACKUP,而且必须为大写
    interface ens33             ## 绑定虚拟IP的网卡接口
    virtual_router_id 51        ## 虚拟ID,同一个ID之间互相通讯,两个节点之间必须保持一致
    priority 100                ## 优先级(0-254),一般设置100左右,主节点要比从节点高
    advert_int 1                ## 发VRRP包的时间间隔,即多久进行一次master选举(可以认为是健康查检时间间隔)。
    authentication {
        auth_type PASS          ## 设置验证方式,这里采用的是明文密码认证,两个节点必须保持一致
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.1.100              ## 虚拟IP,可以设置多个
#        192.168.200.16
#        192.168.200.17
#        192.168.200.18
    }

    track_script {
       chk_nginx  # nginx存活状态检测脚本
    }
}

节点2配置:

! Configuration File for keepalived

global_defs {
#   notification_email {
#     [email protected]
#     [email protected]
#     [email protected]
#   }
#   notification_email_from [email protected]
#   smtp_server 192.168.200.1
#   smtp_connect_timeout 30
   router_id node2
#   vrrp_skip_check_adv_addr
#   vrrp_strict
#   vrrp_garp_interval 0
#   vrrp_gna_interval 0
}

vrrp_script chk_nginx {
    script "/etc/keepalived/check_nginx.sh"
    interval 1
    weight -20
}

vrrp_instance VI_1 {
    state BACKUP                ## 这里只能接受MASTER和BACKUP,而且必须为大写
    interface ens33             ## 绑定虚拟IP的网卡接口
    virtual_router_id 51        ## 虚拟ID,同一个ID之间互相通讯,两个节点之间必须保持一致
    priority 90         ## 优先级(0-254),一般设置100左右,主节点要比从节点高
    advert_int 1                ## 发VRRP包的时间间隔,即多久进行一次master选举(可以认为是健康查检时间间隔)。
    authentication {
        auth_type PASS          ## 设置验证方式,这里采用的是明文密码认证,两个节点必须保持一致
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.1.100              ## 虚拟IP,可以设置多个
#        192.168.200.16
#        192.168.200.17
#        192.168.200.18
    }
    track_script {
       chk_nginx  # nginx存活状态检测脚本
    }
}

3.分别启动Keepalived

keepalived -D

4.查看运行状态和日志

通过ip a可以看到1.100只绑定在MASTER节点
inet 10.0.1.100/32 scope global eth1

ps命令查看进程,yum方式安装的有3个进程,源码安装的有2个进程
ps -ef | grep keepalived

日志默认保存在/var/log/messages文件
tail -f -n 5 /var/log/messages

通过抓包软件检测
tcpdump -i eth1 -n 'host 10.0.1.101'

5.关闭主节点,看vip是否能自动切换到BACKUP节点。

通过ping 10.0.1.100可以看到中间只断一个包,然后就马上恢复了。至此HA高可用配置成功。

6.默认为抢占模式,也就是如果这个时候将节点1再启动的话,vip会自动再切换回节点1

第四步:配置Tengine检测脚本,实现对Tengine状态的监控

1.编辑检测脚本,此脚本实现监控Tengine状态,如果发现没有进程会自动启动,如果启动失败将自动结束掉Keepalived

vim /etc/keepalived/check_nginx.sh

#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]
    then
    /home/bin/tengine/sbin/nginx
    sleep 2
    
    counter=$(ps -C nginx --no-heading|wc -l)
    if [ "${counter}" = "0" ] 
    then
        kill -9 `ps -ef | grep keepalived | grep D`
    fi
fi

chmod +x /etc/keepalived/check_nginx.sh

2.修改Keepalived配置文件,增加检测脚本部分

vim /etc/keepalived/keepalived.conf

在全局配置和vrrp_instance VI_1中间增加以下内容:
vrrp_script chk_nginx {
script "/etc/keepalived/check_nginx.sh" # 这里要写绝对路径
interval 2      # 每2秒检测一次nginx的运行状态

在vrrp_instance VI_1增加以下内容:
track_script {
   chk_nginx  # nginx存活状态检测脚本
}

3.重启Keepalived服(只需要开启keepalived即可,因为有自动检测脚本,开启的同时会自动启动Tengine)

keepalived

ps -ef | grep keep
ps -ef | grep nginx

tail -f -n 5 /var/log/messages

你可能感兴趣的:(RedHat+Keepalived+Tengine实现高可用(源码安装))