——Nginx负载均衡配合Keepalived双实例双主模式配置实战

一、Keepalived介绍

     Keepalived软件主要是通过VRRP协议实现高可用功能的。目的就是为了解决静态路由单点故障问题的,它能够保证当个别节点宕机时,整个网络可以不间断地运行。Keepalived具有三个重要功能:

1.有配置管理LVS的功能;

2.对LVS下面节点进行健康检查功能;

3.实现系统网络服务的高可用功能。

二、Keepalived高可用服务搭建

1.安装Keepalived环境说明

      HostName              IP                             说明
lb01 192.168.12.128 Keepalived主服务器(Nginx主负载均衡器)VIP:192.168.12.126
lb02 192.168.12.129 Keepalived备服务器(Nginx备负载均衡器)VIP:192.168.12.127
web01
192.168.12.130 web01服务器 
web02
192.168.12.131 web02服务器
web03 192.168.12.132 web03服务器

操作系统:CentOS release 6.10     

虚拟工具:VMware

2.开始安装Keepalived软件

[root@Nginx src]# wget http://www.keepalived.org/software/keepalived-1.2.24.tar.gz

[root@Nginx src]# tar -zxvf keepalived-1.2.24.tar.gz 

[root@Nginx src]# cd keepalived-1.2.24

[root@Nginx keepalived-1.2.24]# ./configure \

> --prefix=/usr/local/keepalived

[root@Nginx keepalived-1.2.24]# make && make install

或者通过yum源安装

#yum install keepalived -y

[root@Nginx]# cd /usr/local/keepalived/etc/rc.d/init.d

[root@Nginx init.d]# cp keepalived  /etc/init.d/keepalived

[root@Nginx init.d]# cp keepalived  /etc/init.d/keepalived

[root@Nginx init.d]# chmod +x /etc/init.d/keepalived 

[root@Nginx init.d]# chkconfig keepalived on

[root@Nginx /]# ln /usr/local/keepalived/etc/sysconfig/keepalived  /etc/sysconfig/keepalived

[root@Nginx sbin]# ln -s /usr/local/keepalived/sbin/keepalived  /usr/sbin/keepalived 

#启动Keepalived服务并检查

#service keepalived start   #启动服务方法一

#/etc/inti.d/keepalived start #启动服务方法二

[root@Nginx keepalived]# ps -ef|grep keep|grep -v gerep 

root      2310     1  0 05:20 ?        00:00:00 keepalived -D

root      2311  2310  0 05:20 ?        00:00:00 keepalived -D

root      2312  2310  0 05:20 ?        00:00:00 keepalived -D

root      2325  1899  0 05:49 pts/0    00:00:00 grep keep

[root@Nginx keepalived]# ip add|grep 192.168   #lb01主负载均衡 

    inet 192.168.12.128/24 brd 192.168.12.255 scope global eth0

    inet 192.168.12.126/24 scope global secondary eth0:1

[root@Nginx keepalived]# ip add|grep 192.168 #lb02备负载均衡

    inet 192.168.12.129/24 brd 192.168.12.255 scope global eth0

    inet 192.168.12.127/24 scope global secondary eth0:2

[root@Nginx /]# mkdir /etc/keepalived

[root@Nginx /]# vim /etc/keepalived/keepalived.conf

#配置lb01主负载均衡[192.168.12.128]的keepalived.conf

[root@Nginx keepalived]# cat keepalived.conf

! Configuration File for keepalived

global_defs {

     notification_email {

       [email protected]

      }

       notification_email_from  [email protected]

        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

    mcast_src_ip 192.168.12.128

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

      192.168.12.126/24  dev eth0 label eth0:1

    }

 }

vrrp_instance   VI_2 {

   state BACKUP

   interface eth0

   virtual_router_id 56

   priority 100

   advert_int 1

   authentication {

    auth_type PASS

    auth_pass 1111

  }

virtural_ipaddress {

   192.168.12.127/24   dev eth0 label eth0:2

   }

#配置lb02备负载均衡[192.168.12.129]的keepalived.conf

[root@Nginx keepalived]# cat keepalived.conf 

! Configuration File for keepalived

 global_defs {

  notification_email {

       [email protected]

  }

   notification_email_from [email protected]

    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

    mcast_src_ip 192.168.12.129

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.12.126/24  dev eth0 label  eth0:1

    }

}

vrrp_instance VI_2 {

   state MASTER

   interface eth0

   virtual_router_id 56

   priority 150

   advert_int 1

   authentication {

       auth_type PASS

       auth_pass 1111

  }

  virtual_ipaddress {

     192.168.12.127/24  dev eth0 label eth0:2

    }

}

[root@Nginx /]# iptables -I INPUT -s192.168.12.128 -p112 -j ACCEPT

[root@Nginx /]# service iptables save

[root@Nginx keepalived]# iptables -I INPUT -s192.168.12.129 -p112 -jACCEPT

[root@Nginx keepalived]# service iptables save

#Nginx负载均衡配合Keepalived服务案例实战

Keepalived高可用集群应用实践_第1张图片

#lb01[192.168.12.128]Nginx主负载均衡的配置如下:

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

     sendfile        on;

     keepalive_timeout  65;

     upstream www_server_pools {

            server 192.168.12.130:80 weight=2;

            server 192.168.12.131:80 weight=2;

            server 192.168.12.132:80 weight=1;

      }

     server {

        listen 192.168.12.126:80;

        server_name www.sky9890.com;

      location / {

        proxy_pass http://www_server_pools;

        proxy_set_header Host  $host;

        proxy_set_header X-Forwarded-For $remote_addr;

      }

  }

 server {

        listen 192.168.12.127:80;

        server_name www.sky9899.com;

      location / {

        proxy_pass http://www_server_pools;

        proxy_set_header Host  $host;

        proxy_set_header X-Forwarded-For $remote_addr;

      }

  }

#lb02[192.168.12.129]Nginx备负载均衡的配置如下:

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

     sendfile        on;

     keepalive_timeout  65;

     upstream www_server_pools {

            server 192.168.12.130:80 weight=2;

            server 192.168.12.131:80 weight=2;

            server 192.168.12.132:80 weight=1;

      }

     server {

        listen             80;

        server_name www.sky9890.com;

      location / {

        proxy_pass http://www_server_pools;

        proxy_set_header Host  $host;

        proxy_set_header X-Forwarded-For $remote_addr;

      }

  }

 server {

        listen              80;

        server_name  www.sky9899.com;

      location / {

        proxy_pass http://www_server_pools;

        proxy_set_header Host  $host;

        proxy_set_header X-Forwarded-For $remote_addr;

      }

  }

#Nginx web1 web2 web3的配置请点击如下链接地址:

https://blog.51cto.com/sky9896/2346547   #nginx web配置

#测试结果如下:


Keepalived高可用集群应用实践_第2张图片

Keepalived高可用集群应用实践_第3张图片

Keepalived高可用集群应用实践_第4张图片

Keepalived高可用集群应用实践_第5张图片

Keepalived高可用集群应用实践_第6张图片