keepalived的原理和基本實現

一、keepalived的基本原理介紹

keepalived最初設計的目的是爲了實現lvs前端director的高可用,非常輕量級。主要的實現的vrrp協議。

VRRP是一種容錯協議,它保證當主機的下一跳路由器出現故障時,由另一臺路由器來代替出現故障的路由器進行工作,從而保持網絡通信的連續性和可靠性。

VRRP具有如下優點:

  • 簡化網絡管理:在具有多播或廣播能力的局域網(如以太網)中,藉助VRRP 能在某臺設備出現故障時仍然提供高可靠的缺省鏈路,有效避免單一鏈路發生故障後網絡中斷的問題,而無需修改動態路由協議、路由發現協議等配置信息,也無需修改主機的默認網關配置。

  • 適應性強:VRRP 報文封裝在 IP 報文中,支持各種上層協議。

  • 網絡開銷小:VRRP 只定義了一種報文——VRRP 通告報文,並且只有處於Master 狀態的路由器可以發送 VRRP 報文。

keepalived的軟件架構如下:(圖片來源於keepalived官網)

 

1


組件簡單介紹:

IPVS: 爲lvs生成ipvs規則的組件,是內核級別的。

NETLINK:Netlink是套接字家族中的一員,主要用內核與用戶空間的進程間、用戶進程間的通訊。然而它並不像網絡套接字可以用於主機間通訊,Netlink只能用於同一主機上進程通訊,並通過PID來標識它們。Netlink被設計爲在Linux內核與用戶空間進程傳送各種網絡信息。網絡工具iproute2利用 Netlink從用戶空間與內核進行通訊。Netlink由一個在用戶空間的標準的Socket接口和內核模塊提供的內核API組成。Netlink的設計比ioctl更加靈活,Netlink使用了AF_NETLINK Socket 家族。(摘自維基百科)

IPVS wrapper:藉助於Checkers實現後端lvs主機的健康狀態檢測

VRRP Stack: 實現VRRP協議,實現虛擬IP地址的轉移。

更多詳細的介紹,參考官網地址:http://www.keepalived.org/documentation.html

二、keepalived的安裝配置

在CentOS6.4以後,keepalived直接收錄到內置的rpm倉庫中,可以直接安裝使用。

yum install keepalived -y

keepalived配置文件:

/etc/keepalived/keepalived.conf

1、簡單的實現vrrp協議的配置

配置模式(主從模式):
主節點(Master):172.16.10.9
從節點(Backup):172.16.10.77
虛擬地址:172.16.10.68

配置過程:

### 172.16.10.9:/etc/keepalived/keepalived.conf,實例內容: 
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 10
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass MasBac
    }
    virtual_ipaddress {
        172.16.10.68
    }
}
### 172.16.10.77:/etc/keepalived/keepalived.conf,實例內容: 
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 10
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass MasBac
    }
    virtual_ipaddress {
        172.16.10.68
    }
}

配置完成後,先啓動172.16.10.77的keepalived服務,查看日誌信息:

2

3

此時,啓動Master端172.16.10.9的keepalived服務:

4

配置模式(雙主模式):
節點地址:172.16.10.9,172.16.10.77
虛擬地址:172.16.10.68 172.16.10.69
說明:這裏的雙主模型的實現實際上是配置了2組vrrp的示例。
如下關係:
示例1:
    主節點:172.16.10.9
    從節點:172.16.10.77 
示例2:
    主節點:172.16.10.77
    從節點:172.16.10.9

配置信息:

## 172.16.10.9的vrrp實例信息:
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 10
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass MasBac
    }
    virtual_ipaddress {
        172.16.10.68
    }
}

vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    virtual_router_id 15
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass DouMas
    }
    virtual_ipaddress {
        172.16.10.69
    }
}
## 172.16.10.77的vrrp實例信息:
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 10
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass MasBac
    }
    virtual_ipaddress {
        172.16.10.68
    }
}

vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 15
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass DouMas
    }
    virtual_ipaddress {
        172.16.10.69
    }
}

重新啓動keepalived服務:

5 

此時,如果 172.16.10.77 出現故障,172.16.10.9會擁有2個虛擬ip地址,實現真正的冗餘

6 

2、配置keepalived爲實現lvs高可用

環境說明:
keepalived採用雙主模型:節點是172.16.10.9 172.16.10.77
後端的realserver:172.16.10.122 172.16.10.133

配置說明:

## 配置後端節點:172.16.10.122 172.16.10.133
## ifconfig eth0 172.16.10.122/16 up 
## ifconfig eth0 172.16.10.122/16 up  # 這個也可以寫到配置文件

echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
ifconfig lo:0 172.16.10.68 netmask 255.255.255.255 broadcast 172.16.10.68
route add -host 172.16.10.68  dev lo:0
ifconfig lo:1 172.16.10.69 netmask 255.255.255.255 broadcast 172.16.10.69
route add -host 172.16.10.69  dev lo:1

配置keepalived:

### 172.16.10.9:/etc/keepalived/keepalived.conf的配置:
global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 172.16.10.9
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 10
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass MasBac
    }
    virtual_ipaddress {
        172.16.10.68    
    }
}

vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    virtual_router_id 15
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass DouMas
    }
    virtual_ipaddress {
        172.16.10.69
    }
}

virtual_server 172.16.10.68 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP 
    sorry_server 172.16.10.77 80

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


virtual_server 172.16.10.69 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR 
    #persistence_timeout 50
    protocol TCP
    sorry_server 172.16.10.77 80

    real_server 172.16.10.122 80 {
        weight 1
        HTTP_GET {
            url {
                path /
                status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 172.16.10.133 80 {
        weight 1
        HTTP_GET {
            url {
                path /
                status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
### 172.16.10.77:/etc/keepalived/keepalived.conf的配置:
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 172.16.10.9
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 10
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass MasBac
    }
    virtual_ipaddress {
        172.16.10.68
    }
}

vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 15
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass DouMas
    }
    virtual_ipaddress {
        172.16.10.69
    }
}

virtual_server 172.16.10.68 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP
    sorry_server 172.16.10.77 80

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

virtual_server 172.16.10.69 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP
    sorry_server 172.16.10.77 80

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

補充 對於後端健康狀態的檢測,還有:

TCP_CHECK {
     connect_port 80
     connect_timeout 3
}

測試:

測試前,重啓keepalived服務。

8

image

image

當後端realserver都出現故障時:

image

配置完成。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章