准备四台主机

首先都关闭防火墙

systemctl stop firewalld

setenforce 0

iptables -F

 

第一台主机

 

#安装keepalived

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

#安装nginx

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

#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 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  #引用脚本

    }

}

安装nginx

[root@localhost ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel

[root@localhost~]# useradd -M -s /sbin/nologin  nginx

[root@localhost~]# tar xf nginx-1.6.2.tar.gz -C /usr/src

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

[root@localhost nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

31     keepalive_timeout  65;

 32     upstream httpd1 {

 33       server 192.168.200.14:80 weight=1;

 34       server 192.168.200.15:80 weight=2;

 35   }

 

46         location / {

 47             root   html;

 48             proxy_pass http://httpd1;

 49             proxy_set_header Host $host;

 50         }

[root@localhost ~]# nginx

第二台主机

从keepalived设置

 

#安装keepalived

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

#安装nginx

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

______________________________________________________________

#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.201

    }

    track_script {

       check_nginx

    }

}

安装nginx

[root@localhost ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel

[root@localhost~]# useradd -M -s /sbin/nologin  nginx

[root@localhost~]# tar xf nginx-1.6.2.tar.gz -C /usr/src

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

[root@localhost nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

31     keepalive_timeout  65;

 32     upstream httpd1 {

 33       server 192.168.200.14:80 weight=1;

 34       server 192.168.200.15:80 weight=2;

 35   }

 

46         location / {

 47             root   html;

 48             proxy_pass http://httpd1;

 49             proxy_set_header Host $host;

 50         }

[root@localhost ~]# nginx

 

四、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

#配置周期任务不再演示

第三台主机

首先安装nginx1

[root@localhost ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel

[root@localhost~]# useradd -M -s /sbin/nologin  nginx

[root@localhost~]# tar xf nginx-1.6.2.tar.gz -C /usr/src

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

[root@localhost nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install

[root@localhoost nginx-1.6.2]# cd /usr/local/nginx/html/

[root@localhost html]# echo "11111" > index.html

[root@localhost html]# /usr/local/nginx/sbin/nginx

[root@localhost html]# netstat -anpt |grep nginx

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4503/nginx 

第四台

安装nginx2,同nginx1搭建方式一样

与Nginx1唯一不同的是:

[root@localhost html]# echo "22222" > index.html

 

测试浏览器