软件开发架构师

Nginx+Keepalived配置

架构 57 2020-07-10 11:08

1. Nginx安装

(1) 环境:分别在2台服务器上部署nginx且步骤一致: 如192.138.86.1和192.138.86.2

(2) 下载官网最新稳定版,地址:https://nginx.org/en/download.html,本教程使用的是1.14.2版

(3) 安装Nginx的依赖包

a. 源码编译使用gcc: yum -y install ggc-c++

b. 正则表达式解析时使用pcre:yum -y install pcre pcre-devel

c. 压缩时使用的zlib:yum -y install zlib zlib-devel

d. openssl: yum -y install openssl openssl-devel

(4) 解压:tar –zxvf nginx-1.14.2.tar.gz; cd nginx-1.14.2

(5) 配置并编译:./configure –prefix=/usr/local/nginx && make && make install

(6) 启动nginx: cd /usr/local/nginx/sbin/; ./nginx

(7) 关闭nginx: ./nginx -s stop

(8) 重新加载配置文件:./nginx -s reload

(9) 设置开机自启:在/etc/rc.local中增加一行"/usr/local/nginx/sbin/nginx",并增加执行权限 chmod 755 /etc/rc.local

2. Keepalived安装配置

(1) 环境准备:安装服务器与nginx相同,vip分别为192.138.86.3和192.138.86.4

(2) 安装依赖:yum -y install libnl libnl-devel libnfnetlink-devel

(3) 官网下载稳定版的Keepalived,地址:http://www.keepalived.org/download.html,本文下载的是2.0.10

(4) 解压缩:tar -zxvf keepalived-2.0.10.tar.gz

(5) 配置并编译:./configure –prefix=/usr/local/keepalived && make && make install

(6) keepalived注册为系统服务:

a. cp /usr/local/src/keepalived-2.0.10/keepalived/etc/init.d/keepalived /etc/init.d/

b. cp /usr/local/src/keepalived-2.0.10/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

c. cp /usr/local/src/keepalived-2.0.10/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

(7) keepalived启动:service keepalvied start|stop|restart

(8) 86.1上的配置文件/etc/keepalived/keepalived.conf的内容如下:

! Configuration File for keepalived

global_defs {
   notification_email {
        test@example.com
   }
   notification_email_from research@nagios3.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id NGINX_DEVEL
}

vrrp_script chk_http_port {
    script "/usr/local/nginx/monitor_nginx.sh"
    interval 2
    weight -2
    fall 3
    rise 2    
}

vrrp_instance VI_1 {
    state MASTER
    interface em1
    virtual_router_id 104
    priority 100
    advert_int 1
    mcast_src_ip 192.138.86.1
    authentication {
        auth_type PASS
        auth_pass 1234
    }
    virtual_ipaddress {
        192.138.86.3
    }
    track_interface {
        em1
    }
    track_script {
        chk_http_port
    }
}

vrrp_instance VI_2 {
    state BACKUP
    interface em1
    virtual_router_id 105
    priority 90
    advert_int 1
    mcast_src_ip 192.138.86.1
    authentication {
        auth_type PASS
        auth_pass 1234
    }
    virtual_ipaddress {
        192.138.86.4
    }
    track_interface {
        em1
    }
    track_script {
        chk_http_port
    }
}
View Code

(9) 86.2上的配置文件/etc/keepalived/keepalived.conf的内容如下:

! Configuration File for keepalived

global_defs {
   notification_email {
        test@example.com
   }
   notification_email_from research@nagios3.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id NGINX_DEVEL
}

vrrp_script chk_http_port {
    script "/usr/local/nginx/monitor_nginx.sh"
    interval 2
    weight -2
    fall 3
    rise 2    
}

vrrp_instance VI_1 {
    state BACKUP
    interface em1
    virtual_router_id 104
    priority 90
    advert_int 1
    mcast_src_ip 192.138.86.2
    authentication {
        auth_type PASS
        auth_pass 1234
    }
    virtual_ipaddress {
        192.138.86.3
    }
    track_interface {
        em1
    }
    track_script {
        chk_http_port
    }
}

vrrp_instance VI_2 {
    state MASTER
    interface em1
    virtual_router_id 105
    priority 100
    advert_int 1
    mcast_src_ip 192.138.86.2
    authentication {
        auth_type PASS
        auth_pass 1234
    }
    virtual_ipaddress {
        192.138.86.4
    }
    track_interface {
        em1
    }
    track_script {
        chk_http_port
    }
}
View Code

说明:

a. 1和2上分别有虚拟ip3和4

b. 1和2上的keepalived基于vip互为主从

c. vrrp_script中的内容为nginx的心跳检测,其内容为:

  1 #!/bin/bash
  2 
  3 DATE=$(date)
  4 LogName=$(date +"%Y%m%d")
  5 A=`ps -C nginx --no-header |wc -l`
  6 logs=/usr/local/nginx/logs/nginx_check.$LogName.logs
  7 
  8 if [ $A -eq 0 ];then
  9         cd /usr/local/nginx
 10         echo "$DATE keepalived monitor start nginx" >> $logs
 11         sudo ./sbin/nginx
 12         sleep 3
 13         if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
 14                 echo "$DATE killall keepalived" >> $logs
 15                 sudo killall keepalived
 16         fi
 17 else
 18         echo "$DATE keepalived monitornginx is ok" >> $logs
 19 f
View Code

(10) 重启keepalived,然后尝试关闭主keepalived和nginx验证配置是否生效

文章评论