Linux下Keepalived+LVS-DR模式配置高可用負載均衡集羣

1、環境說明:

    操做系統:Centos-6.5_x86_64node

   keepalived軟件安裝在node2和node3機器上。oop

    實際安裝以前,先關閉keepalived節點(node2,node3)上的防火牆測試

[root@node2 ~]# service iptables stop

2、軟件安裝:

    這裏須要安裝的軟件是keepalived。node2和node3都須要安裝。url

[root@node2 ~]# yum install keepalived

    安裝完畢後,配置文件在/etc/keepalived/keepalived.conf下。spa

3、配置文件:

一、MASTER配置文件(node2):操作系統

! Configuration File for keepalived

bal_defs {
   notification_email {
     root@localhost
   }
   notification_email_from laoxiao@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.103.100/24 dev eth0 label eth0:1
    }
}

virtual_server 192.168.103.100 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP

    real_server 192.168.103.101 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.103.105 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

二、BACKUP配置文件(node3):3d

! Configuration File for keepalived

bal_defs {
   notification_email {
     root@localhost
   }
   notification_email_from laoxiao@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.103.100/24 dev eth0 label eth0:1
    }
}

virtual_server 192.168.103.100 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP

    real_server 192.168.103.101 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.103.105 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

4、測試:

一、首先分別啓動node2和node3上的keepalived服務:

[root@node2 ~]# service keepalived start
[root@node3 ~]# service keepalived start

啓動時產生的日誌在/var/log/messages文件中,能夠觀察其中的日誌。MASTER效果以下:BACKUP會略有不一樣。日誌

image

 

二、查看node2(MASTER)中的狀態:

[root@node2 ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:CE:06:D7  
          inet addr:192.168.103.102  Bcast:192.168.103.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fece:6d7/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:11485 errors:0 dropped:0 overruns:0 frame:0
          TX packets:33202 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:2217383 (2.1 MiB)  TX bytes:2537512 (2.4 MiB)

eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:CE:06:D7  
          inet addr:192.168.103.100  Bcast:0.0.0.0  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

image

[root@node2 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.103.100:80 wrr persistent 50
  -> 192.168.103.101:80           Route   1      0          0         
  -> 192.168.103.105:80           Route   1      0          0

上述能夠看到MASTER中的網卡是啓用的。code

三、查看BACKUP(node3)上的狀態:

[root@node3 keepalived]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:BE:D3:CA  
          inet addr:192.168.103.103  Bcast:192.168.103.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:febe:d3ca/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:249981 errors:0 dropped:0 overruns:0 frame:0
          TX packets:72173 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:284573915 (271.3 MiB)  TX bytes:5287459 (5.0 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:1898 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1898 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:229406 (224.0 KiB)  TX bytes:229406 (224.0 KiB)
[root@node3 keepalived]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.103.100:80 wrr persistent 50
  -> 192.168.103.101:80           Route   1      0          0         
  -> 192.168.103.105:80           Route   1      0          0

若是node3沒有關閉防火牆的話,也會啓用eth0:1網卡,因此注意將防火牆關閉。router

相關文章
相關標籤/搜索