Keepalived 是一個基于 VRRP(虛擬路由冗余協議)實現的高可用解決方案,常用于構建高可用性的服務器集群,特別是在負載均衡場景中,可確保服務的不間斷運行。以下為你詳細介紹它:
0主要功能
- 高可用性:借助 VRRP 協議,Keepalived 能在多臺服務器間自動切換,當主服務器出現故障時,備用服務器可迅速接替工作,保障服務的持續可用。
- 負載均衡:Keepalived 可與 LVS(Linux 虛擬服務器)集成,實現對多臺服務器的負載均衡,依據預設的算法將客戶端請求分發到不同的服務器上。
- 健康檢查:它能對服務器的健康狀況進行檢查,實時監測服務器的服務狀態,一旦發現服務器異常,就會將其從服務列表中移除,待恢復正常后再添加回來。
Keepalived 的工作原理主要基于 VRRP 協議。VRRP 將多臺路由器(或服務器)組成一個虛擬路由器,這個虛擬路由器有一個唯一的虛擬 IP 地址(VIP)。在這個虛擬路由器中,有一個主路由器(Master)和多個備用路由器(Backup)。
- 主路由器:承擔處理客戶端請求的任務,同時定期發送 VRRP 通告給備用路由器,告知它們自己的存活狀態。
- 備用路由器:處于監聽狀態,接收主路由器發送的 VRRP 通告。若在一定時間內未收到通告,備用路由器會認為主路由器出現故障,然后通過選舉機制選出新的主路由器,并接管虛擬 IP 地址,繼續提供服務。
1環境準備
IP地址 | 主機名 | 軟件 | 節點 |
---|---|---|---|
192.168.72.30 | master | keepalived, nginx | 主節點 |
192.168.72.32 | backup | keepalived, nginx | 從節點 |
192.168.72.100 | Vip地址 |
1.1前期準備
1.1.1修改IP
#master
[root@master ~]# nmcli c modify ens160 ipv4.method manual ipv4.addresses 192.168.33.30/24 ipv4.gateway 192.168.33.30 ipv4.dns 223.5.5.5 connection.autoconnect yes
[root@master ~]# nmcli c up ens160
?#backup
[root@master ~]# nmcli c modify ens160 ipv4.method manual ipv4.addresses 192.168.33.30/24 ipv4.gateway 192.168.33.30 ipv4.dns 223.5.5.5 connection.autoconnect yes
[root@master ~]# nmcli c up ens160
1.1.2關閉防火墻
[root@master ~]# systemctl stop firewalld
[root@backup ~]# systemctl stop firewalld
1.1.3安裝nginx服務
#master
[root@master ~]# systemctl stop firewalld
[root@master ~]# mount /dev/sr0 /mnt
mount: /mnt: WARNING: source write-protected, mounted read-only.
[root@master ~]# dnf install nginx -y
Updating Subscription Management repositories.
Unable to read consumer identityThis system is not registered with an entitlement server. You can use "rhc" or "subscription-manager" to register.
BaseOS ? ? ? ? ? 757 kB/s | 2.7 kB ? ? 00:00 ? ?
AppStream ? ? ? ?1.3 MB/s | 3.2 kB ? ? 00:00 ? ?
baseos ? ? ? ? ? 2.7 MB/s | 2.7 kB ? ? 00:00 ? ?
appstream ? ? ? ?3.1 MB/s | 3.2 kB ? ? 00:00 ? ?
Dependencies resolved.
=================================================
?Package ?Arch ? Version ? ? ? ? Repo ? ? ? Size
=================================================
Installing:
?nginx ? ?x86_64 2:1.20.1-20.el9 AppStream ?40 k
Installing dependencies:
?nginx-core
? ? ? ? ? x86_64 2:1.20.1-20.el9 AppStream 574 kTransaction Summary
=================================================
Install ?2 PackagesTotal size: 614 k
Installed size: 1.7 M
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
? Preparing ? ? ? ?: ? ? ? ? ? ? ? ? ? ? ? ? 1/1?
? Installing ? ? ? : nginx-core-2:1.20.1-2 ? 1/2?
? Installing ? ? ? : nginx-2:1.20.1-20.el9 ? 2/2?
? Running scriptlet: nginx-2:1.20.1-20.el9 ? 2/2?
? Verifying ? ? ? ?: nginx-2:1.20.1-20.el9 ? 1/2?
? Verifying ? ? ? ?: nginx-core-2:1.20.1-2 ? 2/2?
Installed products updated.Installed:
? nginx-2:1.20.1-20.el9.x86_64 ? ? ? ? ? ? ? ? ??
? nginx-core-2:1.20.1-20.el9.x86_64 ? ? ? ? ? ? ?Complete!
?#backup
[root@backup ~]# mount /dev/sr0 /mnt
mount: /mnt: WARNING: source write-protected, mounted read-only.
[root@backup ~]# dnf install nginx -y
Updating Subscription Management repositories.
Unable to read consumer identityThis system is not registered with an entitlement server. You can use "rhc" or "subscription-manager" to register.
BaseOS ? ? ? 1.4 MB/s | 2.7 kB ? ? 00:00 ? ?
AppStream ? ?1.4 MB/s | 3.2 kB ? ? 00:00 ? ?
baseos ? ? ? 2.7 MB/s | 2.7 kB ? ? 00:00 ? ?
appstream ? ?3.1 MB/s | 3.2 kB ? ? 00:00 ? ?
Dependencies resolved.
=============================================
?Package
? ? ? Arch ? Version ? ? ? ? Repo ? ? ? Size
=============================================
Installing:
?nginx
? ? ? x86_64 2:1.20.1-20.el9 AppStream ?40 k
Installing dependencies:
?nginx-core
? ? ? x86_64 2:1.20.1-20.el9 AppStream 574 kTransaction Summary
=============================================
Install ?2 PackagesTotal size: 614 k
Installed size: 1.7 M
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
? Preparing ? ? ? ?: ? ? ? ? ? ? ? ? ? ? 1/1?
? Installing ? ? ? : nginx-core-2:1.20 ? 1/2?
? Installing ? ? ? : nginx-2:1.20.1-20 ? 2/2?
? Running scriptlet: nginx-2:1.20.1-20 ? 2/2?
? Verifying ? ? ? ?: nginx-2:1.20.1-20 ? 1/2?
? Verifying ? ? ? ?: nginx-core-2:1.20 ? 2/2?
Installed products updated.Installed:
? nginx-2:1.20.1-20.el9.x86_64 ? ? ? ? ? ? ??
? nginx-core-2:1.20.1-20.el9.x86_64 ? ? ? ? ?Complete!
#區分頁面
[root@master ~]# echo "hello master" > /usr/share/nginx/html/index.html?
[root@backup ~]# echo "hello backup" > /usr/share/nginx/html/index.html#啟動服務
[root@master ~]# systemctl start nginx
[root@backup ~]# systemctl start nginx
#測試
[root@master ~]# curl 192.168.33.30
hello master
[root@backup ~]# curl 192.168.33.32
hello backup
2keepalived配置
2.1下載keepalived
#master
[root@master ~]# dnf install keepalived -y
Updating Subscription Management repositories.
Unable to read consumer identityThis system is not registered with an entitlement server. You can use "rhc" or "subscription-manager" to register.
Last metadata expiration check: 0:08:59 ago on Tue 18 Mar 2025 07:25:43 PM CST.
Dependencies resolved.
=================================================
?Package ? ?Arch ? Version ? ? ? Repo ? ? ? Size
=================================================
Installing:
?keepalived x86_64 2.2.8-3.el9 ? AppStream 564 kTransaction Summary
=================================================
Install ?1 PackageTotal size: 564 k
Installed size: 1.6 M
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
? Preparing ? ? ? ?: ? ? ? ? ? ? ? ? ? ? ? ? 1/1?
? Installing ? ? ? : keepalived-2.2.8-3.el ? 1/1?
? Running scriptlet: keepalived-2.2.8-3.el ? 1/1?
? Verifying ? ? ? ?: keepalived-2.2.8-3.el ? 1/1?
Installed products updated.Installed:
? keepalived-2.2.8-3.el9.x86_64 ? ? ? ? ? ? ? ? ?Complete!
#backup
[root@backup ~]# dnf install keepalived -y
Updating Subscription Management repositories.
Unable to read consumer identityThis system is not registered with an entitlement server. You can use "rhc" or "subscription-manager" to register.
Last metadata expiration check: 0:08:59 ago on Tue 18 Mar 2025 07:25:43 PM CST.
Dependencies resolved.
=============================================
?Package ?Arch ? Version ? ? Repo ? ? ? Size
=============================================
Installing:
?keepalived
? ? ? ? ? x86_64 2.2.8-3.el9 AppStream 564 kTransaction Summary
=============================================
Install ?1 PackageTotal size: 564 k
Installed size: 1.6 M
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
? Preparing ? ? ? ?: ? ? ? ? ? ? ? ? ? ? 1/1?
? Installing ? ? ? : keepalived-2.2.8- ? 1/1?
? Running scriptlet: keepalived-2.2.8- ? 1/1?
? Verifying ? ? ? ?: keepalived-2.2.8- ? 1/1?
Installed products updated.Installed:
? keepalived-2.2.8-3.el9.x86_64 ? ? ? ? ? ? ?Complete!
2.2配置keepalived
#備份配置文件
[root@master ~]# rpm -qc keepalived?
/etc/keepalived/keepalived.conf
/etc/sysconfig/keepalived
[root@master ~]# cp /etc/keepalived//keepalived.conf{,.bak}
?[root@backup?~]# rpm -qc keepalived?
/etc/keepalived/keepalived.conf
/etc/sysconfig/keepalived
[root@backup ~]# cp /etc/keepalived//keepalived.conf{,.bak}#master
[root@master ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {
? ?router_id master
}vrrp_instance VI_1 {
? ? state MASTER
? ? interface ens160
? ? virtual_router_id 51
? ? priority 100
? ? advert_int 1
? ? authentication {
? ? ? ? auth_type PASS
? ? ? ? auth_pass 1111
? ? }
? ? virtual_ipaddress {?
? ? ? ? 192.168.33.100
? ? }
}
#backup[root@backup ~]# vim /etc/keepalived/keepalived.conf
[root@backup ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {
? ?router_id master
}vrrp_instance VI_1 {
? ? state MASTER
? ? interface ens160
? ? virtual_router_id 51
? ? priority 90
? ? advert_int 1
? ? authentication {
? ? ? ? auth_type PASS
? ? ? ? auth_pass 1111
? ? }
? ? virtual_ipaddress {
? ? ? ? 192.168.33.100
? ? }
}#啟動keepalived服務
[root@master ~]# systemctl start keepalived
[root@backup ~]# systemctl start keepalived#IP查看
[root@master ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
? ? 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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
? ? link/ether 00:0c:29:7b:ad:14 brd ff:ff:ff:ff:ff:ff
? ? altname enp3s0
? ? inet 192.168.33.30/24 brd 192.168.33.255 scope global noprefixroute ens160
? ? ? ?valid_lft forever preferred_lft forever
? ? inet 192.168.33.100/32 scope global ens160
? ? ? ?valid_lft forever preferred_lft forever
? ? inet6 fe80::20c:29ff:fe7b:ad14/64 scope link noprefixroute?
? ? ? ?valid_lft forever preferred_lft forever
[root@backup ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
? ? 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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
? ? link/ether 00:0c:29:0f:fe:20 brd ff:ff:ff:ff:ff:ff
? ? altname enp3s0
? ? inet 192.168.33.32/24 brd 192.168.33.255 scope global noprefixroute ens160
? ? ? ?valid_lft forever preferred_lft forever
? ? inet6 fe80::20c:29ff:fe0f:fe20/64 scope link noprefixroute?
? ? ? ?valid_lft forever preferred_lft forever
##暫停服務,虛擬ip消失
[root@master ~]# ?systemctl stop keepalived.service?
[root@master ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
? ? 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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
? ? link/ether 00:0c:29:7b:ad:14 brd ff:ff:ff:ff:ff:ff
? ? altname enp3s0
? ? inet 192.168.33.30/24 brd 192.168.33.255 scope global noprefixroute ens160
? ? ? ?valid_lft forever preferred_lft forever
? ? inet6 fe80::20c:29ff:fe7b:ad14/64 scope link noprefixroute?
? ? ? ?valid_lft forever preferred_lft forever[root@backup ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
? ? 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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
? ? link/ether 00:0c:29:0f:fe:20 brd ff:ff:ff:ff:ff:ff
? ? altname enp3s0
? ? inet 192.168.33.32/24 brd 192.168.33.255 scope global noprefixroute ens160
? ? ? ?valid_lft forever preferred_lft forever
? ? inet6 fe80::20c:29ff:fe0f:fe20/64 scope link noprefixroute?
? ? ? ?valid_lft forever preferred_lft forever
實驗完成!!!