阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Keepalived做Nginx的高可用,企业版简单介绍

117次阅读
没有评论

共计 4250 个字符,预计需要花费 11 分钟才能阅读完成。

一、编译安装 keepalived-1.2.8

下载地址:http://www.keepalived.org/download.html

注意最新的不一定是最好的,所以选择 1.2.8

  1. tar xf keepalived-1.2.8.tar.gz

  2. cd keepalived-1.2.8

  3. mkdir -p /data/soft/keepalived

  4. ./configure –prefix=/data/soft/keepalived
    注意:编译安装之前,可能需要先安装 gcc 和一些额外的包,请自行解决

  5. make && make install
    编译报错 1
    ../include/vrrp_ipaddress.h:32:27: error: linux/if_addr.h: No such file or directory
    In file included from ../include/vrrp.h:31,
    from ../include/smtp.h:34,
    from smtp.c:27:
    ../include/vrrp_ipaddress.h:41: error: field‘ifa’has incomplete type
    make2: * [smtp.o] Error 1
    make2: Leaving directory `/root/keepalived-1.2.7/keepalived/core’
    make1:
    [all] Error 1
    make1: Leaving directory `/root/keepalived-1.2.7/keepalived’
    make: *
    * [all] Error 2
    原因及解决办法:
    这是 kernel-headers 软件版本过低造成的

  6. yum -y install kernel-headers
    问题解决
    编译报错 2
    make2: * [vrrp_ipaddress.o] Error 1
    make2: Leaving directory `/data/soft/keepalived-1.2.9/keepalived/vrrp’
    make1:
    [all] Error 1
    make1: Leaving directory `/data/soft/keepalived-1.2.9/keepalived’
    make: *
    * [all] Error 2
    解决办法:
    将编译 keepalived-1.2.9.tar.gz 改成编译 keepalived-1.2.8.tar.gz 即可
    转移文件到正确的位置

  7. mkdir /etc/keepalived

  8. pwd
    /data/soft/keepalived

  9. cp etc/keepalived/keepalived.conf /etc/keepalived/

  10. cp etc/rc.d/init.d/keepalived /etc/init.d/

  11. cp etc/sysconfig/keepalived /etc/sysconfig/

  12. cp sbin/keepalived /usr/sbin/

  13. chkconfig –add keepalived

  14. service keepalived start
    Starting keepalived: [OK]

  15. chkconfig –add keepalived

  16. chkconfig keepalived on

 

Keepalived 的详细介绍:请点这里
Keepalived 的下载地址:请点这里

推荐阅读:

CentOS 6.3 下 Haproxy+Keepalived+Apache 配置笔记 http://www.linuxidc.com/Linux/2013-06/85598.htm

Haproxy + KeepAlived 实现 WEB 群集 on CentOS 6 http://www.linuxidc.com/Linux/2012-03/55672.htm

Keepalived+Haproxy 配置高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/56748.htm

Haproxy+Keepalived 构建高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/55880.htm

二、配置文件以及监控脚本
配置文件
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
  notification_email {
  root@localhost
  }
  notification_email_from amos.lu@sky-mobi.com
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script “nohup /etc/keepalived/chk_nginx.sh  2> /root/error.log & &> /dev/null”
interval 20
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 200
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    172.18.5.10
    }
    track_script {
      chk_nginx
    }
}

监控脚本
# vim /etc/keepalived/chk_nginx.sh
#!/bin/bash
port=80
nc -z -w 10 localhost $port &> /dev/null
if [$? -ne 0];then
  sleep 10
  nc -z -w 10 localhost $port &> /dev/null
  if [$? -ne 0];then
    /etc/init.d/MySQLd restart &> /dev/null
    sleep 10
    nc -z -w 10 localhost $port &> /dev/null
    if [$? -ne 0];then
        /etc/init.d/keepalived stop
    fi
  fi
fi

注意监测端口连通性的时候,不能只用 netstat 看这个端口是否存在,有时候存在端口,却是无法连通的,所有要用 nc 命令检测。
主备节点做对应修改即可。

一、编译安装 keepalived-1.2.8

下载地址:http://www.keepalived.org/download.html

注意最新的不一定是最好的,所以选择 1.2.8

  1. tar xf keepalived-1.2.8.tar.gz

  2. cd keepalived-1.2.8

  3. mkdir -p /data/soft/keepalived

  4. ./configure –prefix=/data/soft/keepalived
    注意:编译安装之前,可能需要先安装 gcc 和一些额外的包,请自行解决

  5. make && make install
    编译报错 1
    ../include/vrrp_ipaddress.h:32:27: error: linux/if_addr.h: No such file or directory
    In file included from ../include/vrrp.h:31,
    from ../include/smtp.h:34,
    from smtp.c:27:
    ../include/vrrp_ipaddress.h:41: error: field‘ifa’has incomplete type
    make2: * [smtp.o] Error 1
    make2: Leaving directory `/root/keepalived-1.2.7/keepalived/core’
    make1:
    [all] Error 1
    make1: Leaving directory `/root/keepalived-1.2.7/keepalived’
    make: *
    * [all] Error 2
    原因及解决办法:
    这是 kernel-headers 软件版本过低造成的

  6. yum -y install kernel-headers
    问题解决
    编译报错 2
    make2: * [vrrp_ipaddress.o] Error 1
    make2: Leaving directory `/data/soft/keepalived-1.2.9/keepalived/vrrp’
    make1:
    [all] Error 1
    make1: Leaving directory `/data/soft/keepalived-1.2.9/keepalived’
    make: *
    * [all] Error 2
    解决办法:
    将编译 keepalived-1.2.9.tar.gz 改成编译 keepalived-1.2.8.tar.gz 即可
    转移文件到正确的位置

  7. mkdir /etc/keepalived

  8. pwd
    /data/soft/keepalived

  9. cp etc/keepalived/keepalived.conf /etc/keepalived/

  10. cp etc/rc.d/init.d/keepalived /etc/init.d/

  11. cp etc/sysconfig/keepalived /etc/sysconfig/

  12. cp sbin/keepalived /usr/sbin/

  13. chkconfig –add keepalived

  14. service keepalived start
    Starting keepalived: [OK]

  15. chkconfig –add keepalived

  16. chkconfig keepalived on

 

Keepalived 的详细介绍:请点这里
Keepalived 的下载地址:请点这里

推荐阅读:

CentOS 6.3 下 Haproxy+Keepalived+Apache 配置笔记 http://www.linuxidc.com/Linux/2013-06/85598.htm

Haproxy + KeepAlived 实现 WEB 群集 on CentOS 6 http://www.linuxidc.com/Linux/2012-03/55672.htm

Keepalived+Haproxy 配置高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/56748.htm

Haproxy+Keepalived 构建高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/55880.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-20发表,共计4250字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中