2台服务器都安装nginx 和 keepalived:

[root@keepalived-1 ~]# cat /etc/redhat-release 

CentOS release 6.9 (Final)

[root@keepalived-1 ~]# uname -r

2.6.32-696.el6.x86_64

[root@keepalived1 ~]# yum list | grep keepalived

keepalived.x86_64                           1.2.13-5.el6_6               base 

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

[root@keepalived1 ~]# keepalived -v

Keepalived v1.2.13 (03/19,2015)




VIP实例配置

LB01

LB02

[root@lb01  keepalived]# cat -n keepalived.conf

     1   ! Configuration File for keepalived

     2

     3   global_defs {

     4      notification_email {

     5          12345678@qq.com

     6      }

     7      notification_email_from Alexandre.Cassen@firewall.loc

     8      smtp_server 127.0.0.1

     9      smtp_connect_timeout 30

    10      router_id lb01

    11   }

    12

    13   vrrp_instance VI_1 {

    14       state MASTER

    15       interface eth0

    16       virtual_router_id 55

    17       priority 150

    18       advert_int 1

    19       authentication {

    20          auth_type PASS

    21          auth_pass 1111

    22       }

    23       virtual_ipaddress {

    24          192.168.0.235/24 dev eth0 label  eth0:1

    25       }

    26   }

    27

    28   vrrp_instance VI_2 {

    29       state BACKUP

    30       interface eth0

    31       virtual_router_id 56

    32       priority 100

    33       advert_int 1

    34       authentication {

    35          auth_type PASS

    36          auth_pass 1111

    37       }

    38       virtual_ipaddress {

    39          192.168.0.236/24  dev eth0 label eth0:2

    40       }

    41   }

[root@lb02  keepalived]# cat -n keepalived.conf

     1   ! Configuration File for keepalived

     2

     3   global_defs {

     4      notification_email {

     5          12345678@qq.com

     6      }

     7      notification_email_from Alexandre.Cassen@firewall.loc

     8      smtp_server 127.0.0.1

     9      smtp_connect_timeout 30

    10      router_id lb02

    11   }

    12

    13   vrrp_instance VI_1 {

    14      state BACKUP

    15       interface eth0

    16       virtual_router_id 55

    17       priority 100

    18       advert_int 1

    19       authentication {

    20          auth_type PASS

    21          auth_pass 1111

    22       }

    23       virtual_ipaddress {

    24          192.168.0.235/24 dev eth0 label  eth0:1

    25       }

    26   }

    27

    28   vrrp_instance VI_2 {

    29       state MASTER

    30       interface eth0

    31      virtual_router_id 56

    32      priority 150

    33       advert_int 1

    34       authentication {

    35          auth_type PASS

    36          auth_pass 1111

    37       }

    38       virtual_ipaddress {

    39          192.168.0.236/24  dev eth0 label eth0:2

    40       }

    41   }

 

12.6Nginx 负载均衡配合Keepalived服务器案例

环境:结合第11章的四台虚拟主机LB01 LB02为负载均衡,WEB01 WEB02为网站。

LB01 nginx.conf配置如下:

[root@lb01 conf]# cat -n nginx.conf

    1  worker_processes  1;

    2

     3 events {

    4      worker_connections  1024;

    5  }

    6

    7

    8  http {

    9      include       mime.types;

   10      default_type  application/octet-stream;

   11      sendfile        on;

   12      keepalive_timeout  65;

   13      upstream www_server_pools{

   14          server192.168.0.233:80 weight=1;

   15          server192.168.0.234:80 weight=1;

   16          }

   17

   18

   19      server {

    20         listen  192.168.0.235:80; (这里指定的是VIP

   21          server_name  www.etiantian.org;

   22          location / {

   23          proxy_passhttp://www_server_pools;

   24          proxy_set_header Host$host;       

    25         proxy_set_header X-Forwarded-For $remote_addr; (功能暂时不详)

   26                  }

   27          }

   28  }

 

LB01 keepalived.conf配置如下:

! Configuration File for keepalived

 

global_defs {

  notification_email {

       12345678@qq.com

   }

  notification_email_from Alexandre.Cassen@firewall.loc

  smtp_server 127.0.0.1

  smtp_connect_timeout 30

  router_id lb01

}

 

vrrp_instance VI_1 {

   state MASTER

   interface eth0

   virtual_router_id 55

   priority 150

   advert_int 1

   authentication {

       auth_type PASS

       auth_pass 1111

    }

   virtual_ipaddress {

       192.168.0.235/24 dev eth0 label eth0:1

    }

}

 

LB02 keepalived.conf配置如下:

[root@lb02 keepalived]# vi keepalived.conf

! Configuration File for keepalived

 

global_defs {

  notification_email {

       12345678@qq.com

   }

  notification_email_from Alexandre.Cassen@firewall.loc

  smtp_server 127.0.0.1

  smtp_connect_timeout 30

  router_id lb02

}

 

vrrp_instance VI_1 {

   state BACKUP

   interface eth0

   virtual_router_id 55

   priority 100

   advert_int 1

   authentication {

       auth_type PASS

       auth_pass 1111

    }

   virtual_ipaddress {

       192.168.0.235/24 dev eth0 label eth0:1

    }

}