公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

众所周知,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 nginx2) 为两个节点自定义 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.html3) 放行 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.130This is NGINX Web Server from Node 1$ curl http://192.168.1.140This 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 identifierRouter_id nginx# script to check whether Nginx is running or notVRRP_script check_nginx {script "/bin/check_nginx.sh"interval 2weight 50# Virtual interface - The priority specifies the order in which the assigned interface to take over in a failovervrrp_instance VI_01 {state MASTERinterface enp0s3virtual_router_id 151priority 110# The virtual ip address shared between the two NGINX Web Server which will floatvirtual_ipaddress {192.168.1.150/24track_script {check_nginxauthentication {auth_type AHauth_pass secret


 

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

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

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

[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 服务器。

分享到:
标签:Nginx
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定