nginx与keepalived实现高可用

本实验使用了两台虚拟机

每台都需要安装nginx及keepalived

nginx可以源码安装也可以用yum安装nginx

yum安装nginx需要epel源

 

一:网络拓扑结构图

 

二:keepalived及nginx安装

源码安装nginx

[root@localhost ~]#yum -y install pcre-devel zlib-devel make gcc gcc-c++ openssl-devel             #安装Nginx依赖包

[root@localhost ~]#useradd -M -s /sbin/nologin nginx                                                   #创建一个Nginx测试用户

[root@localhost ~]#tar xf nginx -C /usr/src                                                         #将Nginx解包到/usr/src下

[root@localhost ~]#cd /usr/src/nginx                                                          

[root@localhost ~]#./configure  --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-pcre  &&  make && make install                                                                #对Nginx进行编译安装

 

 --prefix 设定Nginx的安装目录

 

 --user和--group 指定Nginx运行用户和组

 

 --with-http_stub_status_module 启用http_stub_status_module模块以支持状态统计

 

 --with-http_ssl_module 启用SSL模块

 

 --with-http_flv_module 启用FLV模块,提供寻求内存使用基于时间的偏移量文件

 

[root@localhost ~]#ln -s /usr/local/nginx/sbin/nginx  /usr/local/bin                   #给Nginx一个软连接

[root@localhost ~]#nginx -t                                                                        #检查语法

[root@localhost ~]# nginx                                                                     #开启Nginx服务

[root@localhost ~]#netstat -lnpt | grep 80                                                #查看端口

yum安装nginx

#安装nginx
[root@localhost ~]# yum -y install nginx

输入命令:

whereis nginx
即可看到类似于如下的内容:

nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx   

以下是Nginx的默认路径:

(1) Nginx配置路径:/etc/nginx/

(2) PID目录:/var/run/nginx.pid

(3) 错误日志:/var/log/nginx/error.log

(4) 访问日志:/var/log/nginx/access.log

(5) 默认站点目录:/usr/share/nginx/html

事实上,只需知道Nginx配置路径,其他路径均可在/etc/nginx/nginx.conf 以及/etc/nginx/conf.d/default.conf 中查询到。

常用命令

(1) 启动:

nginx

(2) 测试Nginx配置是否正确:

nginx -t
(3) 优雅重启:

nginx -s reload   

该命令与以下命令类似:

kill -HUP nginx进程号   

#安装keepalived
[root@localhost ~]# yum -y install keepalived

三:keepalived配置

主eepalived设置 

[root@localhost ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
vrrp_script check_nginx {          #引入脚本文件
 script "/shell/nginx_check.sh"
 interval 2
 weight -20
}
vrrp_instance VI_1 {
    state MASTER               #主
    interface eno16777728         #心跳网卡
    virtual_router_id 51
    priority 100             #优先级
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.200.254
    }
    track_script {
 check_nginx        #引用脚本
    }
}
#准备测试文件
[root@localhost ~]# echo "1111111" > /usr/share/nginx/html/index.html
从keepalived设置
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
vrrp_script check_nginx {
 script "/shell/nginx_check.sh"
 interval 2
 weight -20
}
vrrp_instance VI_1 {
    state BACKUP
    interface eno16777728
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.200.254
    }
    track_script {
 check_nginx 
    }
}
#准备测试文件
[root@localhost ~]# echo "22222" > /usr/share/nginx/html/index.html
 

四:测试
主服务器工作时

从服务器工作时

五:nginx_check.sh shell文件,配置为周期性任务

#!/bin/bash
count="$(ps -C nginx --no-header|wc -l)"
if [ $count -eq 0 ];then
        systemctl restart nginx
        sleep 2
        if [ ps -c nginx --no-header|wc -l -eq 0 ];then
                systemctl stop keepalived
        fi
fi
01-19 21:20