Linux + Nginx + Keepalived 高可用配置教程

众所周知,NGINX 是一个广受好评的 web 服务器,也可以用作反向代理,负载均衡器和 HTTP 缓存。keepalive 工作在虚拟路由器冗余协议 VRRP (Virtual Router Redundancy Protocol) 上,它允许一个静态 IP 在两个 Linux 系统之间进行故障转移。

在本文中,我们将演示如何在 Linux 中使用 keepalive 设置高可用 (HA) NGINX web 服务器。

实验准备

废话不多说,让我们直接进入安装和配置步骤。

1) 安装 NGINX Web Server

For CentOS 8 / RHEL 8

NGINX 软件包在 CentOS 8 / RHEL 8 存储库默认可用,在两个节点上运行如下命令

$ sudo dnf install -y nginx

For CentOS 7 / RHEL 7

NGINX 软件包在 CentOS 7 / RHEL 7 存储库中默认不可用,我们必须启用 epel 存储库,在两个节点上运行以下命令

$ sudo yum install epel-release -y
$ sudo yum install -y nginx

For Ubuntu / Debian

基于 Debian 的发行版,NGINX 软件包在存储库中默认可用,在两个节点上运行如下命令

$ sudo apt update
$ sudo apt install -y nginx

2) 为两个节点自定义 index.html

让我们为这两个节点创建自定义 index.html,这样我们就可以很容易地识别哪个服务器在通过虚拟 IP 访问网站。

在 node 1 上,执行如下命令

[pkumar@nginx1 ~]$ echo "

This is NGINX Web Server from Node 1

" | sudo tee /usr/share/nginx/html/index.html

在 node 2 上,执行如下命令

[pkumar@nginx2 ~]$ echo "

This is NGINX Web Server from Node 2

" | sudo tee /usr/share/nginx/html/index.html

3) 放行 NGINX 端口并启动其服务

如果防火墙已启用,通过以下命令放行 80 端口

For CentOS / RHEL System

$ sudo firewall-cmd --permanent --add-service=http
$ sudo firewall-cmd –reload

For Ubuntu / Debian System

$ sudo ufw allow 'Nginx HTTP'

启动并启用 nginx 服务

$ sudo systemctl start nginx
$ sudo systemctl enable nginx

在外部运行 curl 命令测试两个节点的 NGINX 服务器

$ curl http://192.168.1.130

This is NGINX Web Server from Node 1

$ curl http://192.168.1.140

This is NGINX Web Server from Node 2

以上输出确认 NGINX 正在运行,并且可以从外部通过系统的 IP 地址访问。

4) 安装配置 Keepalived

在两个节点上安装配置 Keepalived

For CentOS / RHEL systems

$ sudo dnf install -y keepalived       // CentOS 8/ RHEL 8
$ sudo yum install -y keepalived      // CentOS 7 / RHEL 7

For Ubuntu / Debian System

$ apt install -y keepalived

本文中,Node 1 作为主节点,Node 2 作为从节点。

备份配置文件

[pkumar@nginx1 ~]$ sudo cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf-org

编辑配置文件

[pkumar@nginx1 ~]$ echo -n | sudo tee /etc/keepalived/keepalived.conf
[pkumar@nginx1 ~]$ sudo vi /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 enp0s3
  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.150/24
  }
  track_script {
    check_nginx
  }
  authentication {
    auth_type AH
    auth_pass secret
  }
}

现在创建一个带有以下内容的脚本,它将检查 nginx 服务是否正在运行。keepalive 将始终检查 check_nginx.sh 脚本的输出,如果它发现 nginx 服务停止或没有响应,那么它将虚拟 ip 地址指向备份节点。

[pkumar@nginx1 ~]$ sudo vi /bin/check_nginx.sh
#!/bin/sh
if [ -z "`pidof nginx`" ]; then
  exit 1
fi

保存并关闭文件,设置所需权限

[pkumar@nginx1 ~]$ sudo chmod 755 /bin/check_nginx.sh

使用 scp 命令把 keepalive .conf 和 check_nginx.sh 文件从 Node 1 复制到 Node 2

[pkumar@nginx1 ~]$ scp /etc/keepalived/keepalived.conf root@192.168.1.140:/etc/keepalived/
[pkumar@nginx1 ~]$ scp /bin/check_nginx.sh root@192.168.1.140:/bin/

复制完成后,登录到 Node 2,并在 keepalive .conf 文件中做一些更改。将 state 从 MASTER 更改为 BACKUP,并将 priority 设置为 100 降低优先级。

如果开启防火墙,执行以下命令放行 VRRP(两个节点都要执行)

For CentOS / RHEL Systems

$ sudo firewall-cmd --add-rich-rule='rule protocol value="vrrp" accept' --permanent
$ sudo firewall-cmd --reload

For Ubuntu / Debian Systems

在主节点 (Node 1) 上执行

$ sudo ufw allow to 224.0.0.18 comment 'VRRP Broadcast'
$ sudo ufw allow from 192.168.1.140 comment 'VRRP Router'

在从节点 (Node 2) 上执行

$ sudo ufw allow to 224.0.0.18 comment 'VRRP Broadcast'
$ sudo ufw allow from 192.168.1.130 comment 'VRRP Router'

启动和开启 keepalived 服务

$ sudo systemctl start keepalived
$ sudo systemctl enable keepalived

验证 keepalived 服务状态

$ sudo systemctl status keepalived

验证主节点上的 VIP (虚拟 ip 地址) 状态,本例中 VIP 是 192.168.1.130

$ ip add show

以上输出确认在主节点的 enp0s3 接口上配置了 VIP

5) Keepalive 和 NGINX 测试

使用虚拟 IP (192.168.1.150) 访问 nginx 服务器,目前它应该会显示 Node 1 页面。

停止 Node 1 上的 NGINX 服务,看看虚拟 IP 是否从 Node 1 切换到 Node 2,这次它应该会显示 Node 1 页面。

[pkumar@nginx1 ~]$ sudo systemctl stop nginx
[pkumar@nginx1 ~]$ ip add show

登录到 Node 2,查看虚拟 IP 是否正确

[pkumar@nginx2 ~]$ ip add show

使用虚拟 IP (192.168.1.150) 访问 nginx 服务器

漂亮,以上证实我们已经成功地设置了高可用的 NGINX Web 服务器。

我的开源项目

酷瓜云课堂 - 开源在线教育解决方案

展开阅读全文

页面更新:2024-03-02

标签:均衡器   软件包   节点   文中   课堂   命令   两个   地址   服务器   文件   教程

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top