LVS/NAT雙主 + keepalived負載均衡實現

1、keepalived簡介前端

keepalived是分佈式部署解決系統高可用的軟件,結合lvs(LinuxVirtual Server)使用,解決單機宕機的問題。
keepalived是一個基於VRRP協議來實現IPVS的高可用的解決方案。對於LVS負載均衡來講,若是前端的調度器direct發生故障,則後端的realserver是沒法接受請求並響應的。所以,保證前端direct的高可用性是很是關鍵的,不然後端的服務器是沒法進行服務的。而咱們的keepalived就能夠用來解決單點故障(如LVS的前端direct故障)問題。keepalived的主要工做原理是:運行keepalived的兩臺服務器,其中一臺爲MASTER,另外一臺爲BACKUP,正常狀況下,全部的數據轉換功能和ARP請求響應都是由MASTER完成的,一旦MASTER發生故障,則BACKUP會立刻接管MASTER的工做,這種切換時很是迅速的。linux

 

2、測試環境web

下面拿4臺虛擬機進行環境測試,實驗環境爲centos6.6 x86_64,lvs NAT模式環境下只有前端兩臺keepalived調度機有公網ip,後端真實機只有內網ip,具體用途和ip以下後端

服務器類型centos

公網ip服務器

內網ip負載均衡

LVS VIP1分佈式

192.168.214.70ide

192.168.211.254oop

LVS VIP2

192.168.214.71

192.168.211.253

Keepalived host1

192.168.214.76

192.168.211.76

Keepalived host2

192.168.214.77

192.168.211.77

Realserver A


192.168.211.79

       Realserver B


192.168.211.83

拓撲圖以下

0.jpg

3、軟件安裝

1、安裝lvs所需包ipvsadm

yum install -y ipvsadm

ln -s /usr/src/kernels/`uname -r`  /usr/src/linux

lsmod |grep ip_vs

 

#注意Centos 6.X安裝lvs,使用1.26版本。而且須要先安裝yuminstall libnl* popt* -y

 

執行ipvsadm(modprobe ip_vs)ip_vs模塊加載到內核

[root@test85 ~]# ipvsadm -L -n

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

 -> RemoteAddress:Port          Forward Weight ActiveConn InActConn

 

#IP Virtual Server version 1.2.1 ---- ip_vs內核模塊版本

 

 

2、安裝keepalived

yum install -y keepalived

chkconfig keepalived on

注:在centos7系列系統中開機自動啓動使用systemctl enable keepalived

 

4、開啓路由轉發 

lvs/NAT模式下前端兩臺調度機192.168.211.76192.168.211.77上須要開啓路由轉發功能

vi /etc/sysctl.conf

修改net.ipv4.ip_forward = 0net.ipv4.ip_forward= 1

保存退出後,使用systcl –p命令讓其生效

 

5、keepalived配置

先看下211.76 keepalived配置文件

根據拓撲圖得知211.76對應的是vip1 192.168.214.70的主,vip2192.168.214.71的備。

具體詳細的參數說明見上篇LVS/DR + keepalived負載均衡實現


[root@localhost ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
     charles@test.com
   }
   notification_email_from reportlog@test.com
   smtp_server mail.test.com
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_sync_group VG1 {        #lvs vip1組
   group {
      VI_1
      VI_GATEWAY1
   }
} 
vrrp_sync_group VG2 {   #lvs vip2組
   group {
      VI_2
      VI_GATEWAY2
   }
} 

vrrp_instance VI_GATEWAY1 {      #lvs vip1的配置
    state MASTER
    interface eth1
    lvs_sync_daemon_inteface eth1
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.211.254
    }
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    lvs_sync_daemon_inteface eth0
    virtual_router_id 52
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    192.168.214.70  
    }
}
vrrp_instance VI_GATEWAY2 {  #lvs vip2的配置
    state BACKUP
    interface eth1
    lvs_sync_daemon_inteface eth1
    virtual_router_id 53
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.211.253
    }
}
vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    lvs_sync_daemon_inteface eth0
    virtual_router_id 54
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    192.168.214.71
    }
}

virtual_server 192.168.214.70 80 {     #vip1 
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    #nat_mask 255.255.255.0
    persistence_timeout 10
    protocol TCP

    real_server 192.168.211.79 80 {       #vip1對應的後端真實機
        weight 100
        TCP_CHECK {
            connect_timeout 3
            connect_port 80
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
virtual_server 192.168.214.71 8080 {     #vip2
    delay_loop 6
    lb_algo rr
    lb_kind NAT
   #nat_mask 255.255.255.0
    persistence_timeout 10
    protocol TCP

   real_server 192.168.211.83 8080 {   #vip2對應的後端真實機
        weight 100
        TCP_CHECK {
            connect_timeout 3
            connect_port 8080
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}


再看下211.77 keepalived配置文件

根據拓撲圖得知211.77對應的是vip1 192.168.214.70的備,vip2192.168.214.71的主。


[root@localhost ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
  notification_email {
    charles@test.com
   }
  notification_email_from reportlog@test.com
  #smtp_server 192.168.200.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
}
vrrp_sync_group VG1 {
  group {
     VI_1
     VI_GATEWAY1
   }
} 
vrrp_sync_group VG2 {
  group {
     VI_2
     VI_GATEWAY2
   }
} 

vrrp_instance VI_GATEWAY1 {
   state BACKUP
   interface eth1
   lvs_sync_daemon_inteface eth1
    virtual_router_id51
   priority 100
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass 1111
    }
   virtual_ipaddress {
       192.168.211.254
    }
}
vrrp_instance VI_1 {
   state BACKUP
   interface eth0
   lvs_sync_daemon_inteface eth0
   virtual_router_id 52
   priority 100
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass 1111
    }
   virtual_ipaddress {
   192.168.214.70     
    }
}
vrrp_instance VI_GATEWAY2 {
   state MASTER
    interfaceeth1
   lvs_sync_daemon_inteface eth1
   virtual_router_id 53
   priority 150
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass 1111
    }
   virtual_ipaddress {
       192.168.211.253
    }
}
vrrp_instance VI_2 {
   state MASTER
   interface eth0
   lvs_sync_daemon_inteface eth0
   virtual_router_id 54
   priority 150
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass 1111
    }
   virtual_ipaddress {
   192.168.214.71
    }
}

virtual_server 192.168.214.70 80 {
   delay_loop 6
   lb_algo rr
   lb_kind NAT
   #nat_mask 255.255.255.0
   persistence_timeout 10
   protocol TCP

   real_server 192.168.211.79 80 {
       weight 100
       TCP_CHECK {
           connect_timeout 3
           connect_port 80
           nb_get_retry 3
           delay_before_retry 3
       }
    }
}
virtual_server 192.168.214.71 8080 {
   delay_loop 6
   lb_algo rr
   lb_kind NAT
  #nat_mask 255.255.255.0
   persistence_timeout 10
   protocol TCP

  real_server 192.168.211.83 8080 {
       weight 100
       TCP_CHECK {
           connect_timeout 3
           connect_port 8080
           nb_get_retry 3
           delay_before_retry 3
       }
    }
}


6、後端真實機配置

後端web應用機器192.168.211.79須要配置默認網關爲vip1內網ip地址192.168.211.254web應用機器192.168.211.83須要配置默認網關爲vip2內網ip地址192.168.211.253

注意,線上環境若是更改默認網關爲vip內網地址後,可能會形成鏈接不上服務器,須要提早添加相關靜態路由。


7、啓動keepalived服務及查看相關信息

211.76211.77上分別啓動keepalived服務

211.76主機上查看信息

經過ip addr能夠看到vip1 地址已經綁定在eth0eth1網口上

[root@localhost ~]# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu65536 qdisc noqueue state UNKNOWN

   link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

   inet 127.0.0.1/8 scope host lo

   inet6 ::1/128 scope host

      valid_lft forever preferred_lft forever

2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000

   link/ether 52:54:00:55:b2:d4 brd ff:ff:ff:ff:ff:ff

   inet 192.168.214.76/24 brd 192.168.214.255 scope global eth0

    inet 192.168.214.70/32 scope global eth0

   inet6 fe80::5054:ff:fe55:b2d4/64 scope link

      valid_lft forever preferred_lft forever

3: eth1:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000

   link/ether 52:54:00:85:11:95 brd ff:ff:ff:ff:ff:ff

   inet 192.168.211.76/24 brd 192.168.211.255 scope global eth1

    inet192.168.211.254/32 scope global eth1

   inet6 fe80::5054:ff:fe85:1195/64 scope link

      valid_lft forever preferred_lft forever


211.76上查看日誌信息,看到已成功進入keepalived vip1組的主機模式,vip2組的備機模式。

1.jpg

211.77主機上查看信息

經過ip addr能夠看到vip2 地址已經綁定在eth0eth1網口上

[root@localhost ~]# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu65536 qdisc noqueue state UNKNOWN

   link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

   inet 127.0.0.1/8 scope host lo

   inet6 ::1/128 scope host

      valid_lft forever preferred_lft forever

2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000

   link/ether 52:54:00:1b:a2:11 brd ff:ff:ff:ff:ff:ff

   inet 192.168.214.77/24 brd 192.168.214.255 scope global eth0

    inet 192.168.214.71/32 scope global eth0

   inet6 fe80::5054:ff:fe1b:a211/64 scope link

      valid_lft forever preferred_lft forever

3: eth1:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000

   link/ether 52:54:00:64:47:7d brd ff:ff:ff:ff:ff:ff

   inet 192.168.211.77/24 brd 192.168.211.255 scope global eth1

    inet192.168.211.253/32 scope global eth1

   inet6 fe80::5054:ff:fe64:477d/64 scope link

      valid_lft forever preferred_lft forever

 

211.77上查看日誌信息,看到已成功進入keepalived vip1組的備機模式,vip2組的主機模式。

1.jpg

經過ipvsadm -L –n 查看相應lvs鏈接信息

211.76上查看

[root@localhost ~]# ipvsadm -L -n

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

 -> RemoteAddress:Port          Forward Weight ActiveConn InActConn

TCP 192.168.214.70:80 rr persistent 10

 -> 192.168.211.79:80           Masq    100    3         5        

TCP 192.168.214.71:8080 rr persistent 10

 -> 192.168.211.83:8080         Masq    100    0         0

 

211.77上查看

[root@localhost ~]# ipvsadm -L -n

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

 -> RemoteAddress:Port          Forward Weight ActiveConn InActConn

TCP 192.168.214.70:80 rr persistent 10

 -> 192.168.211.79:80           Masq    100    0         0        

TCP 192.168.214.71:8080 rr persistent 10

 -> 192.168.211.83:8080         Masq    100    3         4 


8、keepalived測試

使用vip1地址192.168.214.70訪問後端web192.168.211.79的頁面

2.jpg

使用vip2地址192.168.214.71訪問後端web192.168.211.83的頁面

3.jpg

正常訪問沒問題後,咱們來模擬lvs集羣故障,把前端lvs調度機主機211.76宕機,看211.77可否把vip1的地址漂移過來

經過ip addr命令查看發現vip1的地址都已經漂移過來了

[root@localhost ~]# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu65536 qdisc noqueue state UNKNOWN

   link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

   inet 127.0.0.1/8 scope host lo

   inet6 ::1/128 scope host

      valid_lft forever preferred_lft forever

2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000

   link/ether 52:54:00:1b:a2:11 brd ff:ff:ff:ff:ff:ff

    inet 192.168.214.77/24 brd 192.168.214.255scope global eth0

   inet 192.168.214.71/32 scope global eth0

    inet 192.168.214.70/32scope global eth0

   inet6 fe80::5054:ff:fe1b:a211/64 scope link

      valid_lft forever preferred_lft forever

3: eth1:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000

   link/ether 52:54:00:64:47:7d brd ff:ff:ff:ff:ff:ff

   inet 192.168.211.77/24 brd 192.168.211.255 scope global eth1

   inet 192.168.211.253/32 scope global eth1

    inet192.168.211.254/32 scope global eth1

   inet6 fe80::5054:ff:fe64:477d/64 scope link

      valid_lft forever preferred_lft forever

 

211.77上經過日誌查看,看到vip1的地址已漂移了過來,變成了主機狀態

May 15 14:38:26 localhostKeepalived_vrrp[11898]: VRRP_Instance(VI_1) Transitionto MASTER STATE

May 15 14:38:26 localhostKeepalived_vrrp[11898]: VRRP_Group(VG1) Syncing instances to MASTER state

May 15 14:38:26 localhostKeepalived_vrrp[11898]: VRRP_Instance(VI_GATEWAY1) Transitionto MASTER STATE

May 15 14:38:27 localhostKeepalived_vrrp[11898]: VRRP_Instance(VI_GATEWAY1)Entering MASTER STATE

May 15 14:38:27 localhostKeepalived_vrrp[11898]: VRRP_Instance(VI_GATEWAY1) setting protocol VIPs.

May 15 14:38:27 localhostKeepalived_vrrp[11898]: VRRP_Instance(VI_GATEWAY1) Sending gratuitous ARPs oneth1 for 192.168.211.254

May 15 14:38:27 localhostKeepalived_healthcheckers[11897]: Netlink reflector reports IP 192.168.211.254added

May 15 14:38:27 localhostKeepalived_vrrp[11898]: VRRP_Instance(VI_1) EnteringMASTER STATE

May 15 14:38:27 localhostKeepalived_vrrp[11898]: VRRP_Instance(VI_1) setting protocol VIPs.

May 15 14:38:27 localhostKeepalived_vrrp[11898]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for192.168.214.70

May 15 14:38:27 localhostKeepalived_healthcheckers[11897]: Netlink reflector reports IP 192.168.214.70added

May 15 14:38:32 localhostKeepalived_vrrp[11898]: VRRP_Instance(VI_GATEWAY1) Sending gratuitous ARPs oneth1 for 192.168.211.254

May 15 14:38:32 localhostKeepalived_vrrp[11898]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for192.168.214.70


若是想了解更多,請關注咱們的公衆號
公衆號ID:opdevos
掃碼關注

gongzhouhao.jpg

相關文章
相關標籤/搜索