关于linux:Linux-Nginx-Keepalived-高可用配置

2次阅读

共计 3892 个字符,预计需要花费 10 分钟才能阅读完成。

家喻户晓,NGINX 是一个广受好评的 web 服务器,也能够用作反向代理,负载均衡器和 HTTP 缓存。keepalive 工作在虚构路由器冗余协定 VRRP (Virtual Router Redundancy Protocol) 上,它容许一个动态 IP 在两个 Linux 零碎之间进行故障转移。

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

试验筹备

  • Node 1 – 192.168.1.130 – nginx1.example.com – minimal CentOS 8 / RHEL 8
  • Node 2 – 192.168.1.140 – nginx2.example.com – minimal CentOS 8 / RHEL 8
  • Virtual IP (VIP) – 192.168.1.150
  • sudo user pkumar
  • Firewalld enbled
  • SELinux Running

废话不多说,让咱们间接进入装置和配置步骤。

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 "<h1>This is NGINX Web Server from Node 1</h1>" | sudo tee /usr/share/nginx/html/index.html

在 node 2 上,执行如下命令

[pkumar@nginx2 ~]$ echo "<h1>This is NGINX Web Server from Node 2</h1>" | 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
<h1>This is NGINX Web Server from Node 1</h1>
$ curl http://192.168.1.140
<h1>This is NGINX Web Server from Node 2</h1>

以上输入确认 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 服务器。

我的开源我的项目

  • course-tencent-cloud(酷瓜云课堂 – gitee 仓库)
  • course-tencent-cloud(酷瓜云课堂 – github 仓库)
正文完
 0