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

相关文章: