在本页面上

  • 前提条件
  • 在两个节点上安装 Nginx
  • 在两个节点上创建 Index.html 文件
  • 安装并配置 Keepalived
  • 在两个节点上配置防火墙
  • 验证 Keepalived
  • 结论

Nginx 是一款免费的开源软件,也是世界上最流行的 Web 服务器之一。它还可以用作反向代理、负载均衡器和 HTTP 缓存。高可用性允许应用程序在发生故障时将工作重新路由到另一个系统。有多种技术可用于搭建高可用系统。

Keepalived 是一个系统守护进程,它持续监控服务或系统,并在发生故障时实现高可用性。如果一个节点宕机,则由第二个节点提供资源。

在本教程中,我将向你展示如何在 CentOS 8 上使用 KeepAlived 搭建一个高可用的 Nginx Web 服务器。

前提条件

  • 两台运行 CentOS 8 的服务器,一台作为主节点,另一台作为备用节点。
  • 在你的服务器上配置了 root 密码。

在两个节点上安装 Nginx

首先,你需要在两个节点上都安装 Nginx 软件包。你可以使用以下命令来安装:

dnf install nginx -y

在两个节点上都安装好 Nginx 之后,启动 Nginx 服务并使其在系统重启时自动启动:

systemctl start nginx
systemctl enable nginx

完成后,你可以继续下一步。

在两个节点上创建 Index.html 文件

接下来,你需要在两个节点上都创建一个自定义的 index.html 文件,以标识每个节点。

在第一个节点上,使用以下命令创建 index.html 文件:

echo "<h1>This is My First NGINX Web Server Node</h1>" | tee /usr/share/nginx/html/index.html

在第二个节点上,使用以下命令创建 index.html 文件:

echo "<h1>This is My Second NGINX Web Server Node</h1>" | tee /usr/share/nginx/html/index.html

完成后保存并关闭文件。

安装并配置 Keepalived

接下来,你需要在两个节点上都安装 Keepalived。默认情况下,Keepalived 软件包在 CentOS 8 的默认存储库中可用。你可以通过运行以下命令来安装它:

dnf install keepalived -y

在两个节点上都安装好 keepalived 软件包后,你需要在两个节点上都编辑 keepalived 的默认配置文件。

在第一个节点上,编辑 keepalived.conf 文件:

nano /etc/keepalived/keepalived.conf

删除默认内容并添加以下内容:

global_defs {
  # Keepalived process identifier
  router_id nginx
}

# Script to check whether Nginx is running or not
vrrp_script check_nginx {
  script "/bin/check_nginx.sh"
  interval 2
  weight 50
}

# Virtual interface - The priority specifies the order in which the assigned interface to take over in a failover
vrrp_instance VI_01 {
  state MASTER
  interface eth0
  virtual_router_id 151
  priority 110

  # The virtual ip address shared between the two NGINX Web Server which will float
  virtual_ipaddress {
    192.168.1.10/24
  }
  track_script {
    check_nginx
  }
  authentication {
    auth_type AH
    auth_pass secret
  }
}

完成后保存并关闭文件。

在第二个节点上,编辑 keepalived.conf 文件:

nano /etc/keepalived/keepalived.conf

删除默认内容并添加以下内容:

global_defs {
  # Keepalived process identifier
  router_id nginx
}

# Script to check whether Nginx is running or not
vrrp_script check_nginx {
  script "/bin/check_nginx.sh"
  interval 2
  weight 50
}

# Virtual interface - The priority specifies the order in which the assigned interface to take over in a failover
vrrp_instance VI_01 {
  state BACKUP
  interface eth0
  virtual_router_id 151
  priority 100

  # The virtual ip address shared between the two NGINX Web Server which will float
  virtual_ipaddress {
    192.168.1.10/24
  }
  track_script {
    check_nginx
  }
  authentication {
    auth_type AH
    auth_pass secret
  }
}

保存并关闭文件,然后你需要创建一个脚本来检查 Nginx 服务是否正在运行。你可以使用以下命令来创建它:

注意:只需在上述配置文件中将 MASTER 替换为 BACKUP,并将 110 替换为 100。

nano /bin/check_nginx.sh

添加以下几行:

#!/bin/sh
if [ -z "`pidof nginx`" ]; then
  exit 1
fi

保存并关闭文件,然后使用以下命令设置适当的权限:

chmod 755 /bin/check_nginx.sh

最后,使用以下命令启动 keepalived 服务并使其在系统重启时自动启动:

systemctl start keepalived
systemctl enable keepalived

你还可以使用以下命令检查 keepalived 服务的状态:

systemctl status keepalived

你应该会得到以下输出:

? keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2021-04-08 04:24:22 EDT; 5s ago
  Process: 3141 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 3142 (keepalived)
    Tasks: 2 (limit: 12524)
   Memory: 2.1M
   CGroup: /system.slice/keepalived.service
           ??3142 /usr/sbin/keepalived -D
           ??3143 /usr/sbin/keepalived -D

Apr 08 04:24:22 node1 Keepalived_vrrp[3143]: (VI_01) Changing effective priority from 110 to 160
Apr 08 04:24:25 node1 Keepalived_vrrp[3143]: (VI_01) Receive advertisement timeout
Apr 08 04:24:25 node1 Keepalived_vrrp[3143]: (VI_01) Entering MASTER STATE
Apr 08 04:24:25 node1 Keepalived_vrrp[3143]: (VI_01) setting VIPs.
Apr 08 04:24:25 node1 Keepalived_vrrp[3143]: Sending gratuitous ARP on eth0 for 192.168.1.10
Apr 08 04:24:25 node1 Keepalived_vrrp[3143]: (VI_01) Sending/queueing gratuitous ARPs on eth0 for 192.168.1.10
Apr 08 04:24:25 node1 Keepalived_vrrp[3143]: Sending gratuitous ARP on eth0 for 192.168.1.10
Apr 08 04:24:25 node1 Keepalived_vrrp[3143]: Sending gratuitous ARP on eth0 for 192.168.1.10
Apr 08 04:24:25 node1 Keepalived_vrrp[3143]: Sending gratuitous ARP on eth0 for 192.168.1.10
Apr 08 04:24:25 node1 Keepalived_vrrp[3143]: Sending gratuitous ARP on eth0 for 192.168.1.10

你还可以使用以下命令在主节点上验证虚拟 IP 地址的状态:

ip add show

你应该会在以下输出中看到虚拟 IP 地址 192.168.1.10:

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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:00:2d:3a:20:9b brd ff:ff:ff:ff:ff:ff
    inet 45.58.32.155/24 brd 45.58.32.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 192.168.1.10/24 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::200:2dff:fe3a:209b/64 scope link 
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:00:0a:3a:20:9b brd ff:ff:ff:ff:ff:ff
    inet6 fe80::200:aff:fe3a:209b/64 scope link 
       valid_lft forever preferred_lft forever

完成后,你可以继续下一步。

在两个节点上配置防火墙

接下来,你需要在两个节点上都允许端口 80 并允许 VRRP。你可以使用以下命令来完成:

firewall-cmd --permanent --add-service=http
firewall-cmd --add-rich-rule='rule protocol value="vrrp" accept' --permanent

接下来,重新加载 firewalld 以应用更改:

firewall-cmd –reload

验证 Keepalived

此时,Nginx 和 Keepalived 已经安装并配置完成。是时候测试 Nginx 高可用性是否生效了。

打开你的 Web 浏览器并访问 URL http://your-virtual-ip 。你应该会看到以下页面:

如何在 CentOS 8 上用 Keepalived 搭建 Nginx 高可用集群的完整教程

现在,停止主节点上的 Nginx 服务,并测试虚拟 IP 是否已从节点 1 切换到节点 2。

在主节点上,使用以下命令停止 Nginx 服务:

systemctl stop nginx

接下来,登录到 Node2 并使用以下命令验证虚拟 IP:

ip add show

你应该会在以下输出中看到你的虚拟 IP:

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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:00:2d:3a:26:37 brd ff:ff:ff:ff:ff:ff
    inet 45.58.38.55/24 brd 45.58.38.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 192.168.1.10/24 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::200:2dff:fe3a:2637/64 scope link 
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:00:0a:3a:26:37 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::200:aff:fe3a:2637/64 scope link 
       valid_lft forever preferred_lft forever

现在,使用 URL http://your-virtual-ip 访问你的 Nginx Web 服务器。你应该会看到 Node2 的页面:

如何在 CentOS 8 上用 Keepalived 搭建 Nginx 高可用集群的完整教程

结论

恭喜!你已经成功使用 Keepalived 搭建了一个高可用的 Nginx 服务器。我希望你现在已经掌握了足够的知识,能够在生产环境中搭建高可用的 Nginx 服务器。