lvs+keepalived

環境node

10.0.0.20   主負載
10.0.0.21   負負載
10.0.0.22   web
10.0.0.23   web
10.0.0.24   web

部署LVSweb

10.0.0.20算法

[root@node1 ~]# yum -y install ipvsadm

進入到 /usr/src 目錄查看結果,若是有ip_vs_rr、ip_vs,表示正常 vim

[root@node1 ~]# cd /usr/src/
[root@node1 src]# lsmod|grep ip_vs
ip_vs 136798 0 
nf_conntrack 105702 1 ip_vs
libcrc32c 12644 2 xfs,ip_vs

若是沒有顯示加載一下,在此查看oop

複製代碼
[root@node1 src]# lsmod|grep ip_vs
[root@node1 src]# modprobe  ip_vs 
[root@node1 src]# lsmod|grep ip_vs
ip_vs                 136798  0 
nf_conntrack          105702  1 ip_vs
libcrc32c              12644  2 xfs,ip_vs
複製代碼

添加VIP(虛擬IP),虛擬IP需和實際IP爲一個網段測試

[root@node1 ~]# ip addr add 10.0.0.28/24 dev eth0 label eth0:0

查看虛擬IPspa

複製代碼
[root@node1 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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
       valid_lft forever preferred_lft forever
    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 qlen 1000
    link/ether 00:0c:29:29:39:b8 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.20/24 brd 10.0.0.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 10.0.0.28/24 scope global secondary eth0:0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe29:39b8/64 scope link 
       valid_lft forever preferred_lft forever
複製代碼

清空ipvsadm記錄rest

[root@node1 ~]# ipvsadm -C

配置ipvs,並查看code

複製代碼
[root@node1 ~]# ipvsadm -A -t 10.0.0.28:80 -s wrr
[root@node1 ~]# ipvsadm -a -t 10.0.0.28:80 -r 10.0.0.22:80 -g -w 1
[root@node1 ~]# ipvsadm -a -t 10.0.0.28:80 -r 10.0.0.23:80 -g -w 1
[root@node1 ~]# ipvsadm -a -t 10.0.0.28:80 -r 10.0.0.24:80 -g -w 1 [root@node1 ~]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 10.0.0.28:80 wrr -> 10.0.0.22:80 Route 1 0 0 -> 10.0.0.23:80 Route 1 0 0
-> 10.0.0.24:80 Route 1 0 0
複製代碼

加入開機啓動router

[root@node1 ~]# systemctl enable ipvsadm

Web

綁定虛擬IP(VIP)

[root@node2 ~]# ip addr add 10.0.0.28/32 dev lo label lo:0  每臺web都要執行

抑制VIP,防止LVS和RSVIP衝突

[root@node2 ~]# echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
[root@node2 ~]# echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
[root@node2 ~]# echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
[root@node2 ~]# echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce

安裝keepalived

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

查看版本

[root@node1 ~]# keepalived -v

 配置 keepalived.conf 文件

[root@node1 ~]# vim /etc/keepalived/keepalived.conf 

! Configuration File for keepalived

global_defs {
   router_id wl #設置LVS的ID,在一個集羣內是惟一的,主備要一致
}

vrrp_instance VI_1 {
    state MASTER   #指定keepalived的角色,MASTER爲主,BACKUP爲備
    interface eth0
    virtual_router_id 51 #虛擬路由編號,主備要一致
    priority 100  #定義優先級,數值越大,級別越高,主必須大於備
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 111
    }
    virtual_ipaddress {
        10.0.0.28   #定義虛擬IP
    }
}

virtual_server 10.0.0.28 80 {  #定義對外提供服務的lvs的VIP及端口
    delay_loop 6 #健康檢查的時間,單位爲秒
    lb_algo wrr 負載調度算法
    lb_kind DR 實現負載的機制
    nat_mask 255.255.255.0
    persistence_timeout 0
    protocol TCP

    real_server 10.0.0.22 80 {
        weight 1  #配置節點的權值,數值越大權位越高
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
    real_server 10.0.0.23 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
    real_server 10.0.0.24 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }

}

重啓keepalived

[root@node1 ~]# systemctl restart keepalived

 重啓後能夠經過網頁訪問虛擬IP進行測試

 

配置備負載(10.0.0.21)

安裝ipvsadm

[root@node2 ~]# yum -y install ipvsadm

進入到 /usr/src 目錄查看結果,若是有ip_vs_rr、ip_vs,表示正常 

[root@node2 ~]# cd /usr/src/
[root@node1 src]# lsmod|grep ip_vs
ip_vs 136798 0 
nf_conntrack 105702 1 ip_vs
libcrc32c 12644 2 xfs,ip_vs

若是沒有顯示加載一下,在此查看

複製代碼
[root@node2 src]# lsmod|grep ip_vs
[root@node2 src]# modprobe  ip_vs 
[root@node2 src]# lsmod|grep ip_vs
ip_vs                 136798  0 
nf_conntrack          105702  1 ip_vs
libcrc32c              12644  2 xfs,ip_vs
複製代碼

添加VIP(虛擬IP),虛擬IP需和實際IP爲一個網段

[root@node2 ~]# ip addr add 10.0.0.28/24 dev eth0 label eth0:0

查看虛擬IP

複製代碼
[root@node2 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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
       valid_lft forever preferred_lft forever
    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 qlen 1000
    link/ether 00:0c:29:29:39:b8 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.21/24 brd 10.0.0.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 10.0.0.28/24 scope global secondary eth0:0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe29:39b8/64 scope link 
       valid_lft forever preferred_lft forever
複製代碼

清空ipvsadm記錄

[root@node2 ~]# ipvsadm -C

配置ipvs,並查看

複製代碼
[root@node2 ~]# ipvsadm -A -t 10.0.0.28:80 -s wrr
[root@node2 ~]# ipvsadm -a -t 10.0.0.28:80 -r 10.0.0.22:80 -g -w 1
[root@node2 ~]# ipvsadm -a -t 10.0.0.28:80 -r 10.0.0.23:80 -g -w 1
[root@node2 ~]# ipvsadm -a -t 10.0.0.28:80 -r 10.0.0.24:80 -g -w 1 [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 10.0.0.28:80 wrr -> 10.0.0.22:80 Route 1 0 0 -> 10.0.0.23:80 Route 1 0 0
-> 10.0.0.24:80 Route 1 0 0
複製代碼

加入開機啓動

[root@node1 ~]# systemctl enable ipvsadm

抑制VIP,防止LVS和RSVIP衝突

[root@node2 ~]# echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
[root@node2 ~]# echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce [root@node2 ~]# echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore [root@node2 ~]# echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce

安裝keepalived

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

查看版本

[root@node1 ~]# keepalived -v

加入開機啓動

[root@node2 ~]# systemctl enable keepalived

配置 keepalived.conf 文件

[root@node1 ~]# vim /etc/keepalived/keepalived.conf 

! Configuration File for keepalived global_defs { router_id wl } vrrp_instance VI_1 { state BACKER interface eth0 virtual_router_id 51 priority 90 nopreempt advert_int 1 authentication { auth_type PASS auth_pass 111 } virtual_ipaddress { 10.0.0.28 } } virtual_server 10.0.0.28 80 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 0 protocol TCP real_server 10.0.0.22 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 10.0.0.23 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 10.0.0.24 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }

重啓keepalived

[root@node1 ~]# systemctl restart keepalived

搭好之後能夠先用10.0.0.20 主機測試,測試無誤後將10.0.0.20主機宕掉,再用10.0.0.21主機測試

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息