众所周知,NGINX是一个广受好评的web服务器,也可以用作反向代理,负载均衡器和HTTP缓存。keepalive工作在虚拟路由器冗余协议VRRP(VirtualRouterRedundancyProtocol)上,它允许一个静态IP在两个Linux系统之间进行故障转移。 在本文中,我们将演示如何在Linux中使用keepalive设置高可用(HA)NGINXweb服务器。实验准备Node1192。168。1。130nginx1。example。comminimalCentOS8RHEL8Node2192。168。1。140nginx2。example。comminimalCentOS8RHEL8VirtualIP(VIP)192。168。1。150sudouserpkumarFirewalldenbledSELinuxRunning 废话不多说,让我们直接进入安装和配置步骤。1)安装NGINXWebServer ForCentOS8RHEL8 NGINX软件包在CentOS8RHEL8存储库默认可用,在两个节点上运行如下命令sudodnfinstallynginx ForCentOS7RHEL7 NGINX软件包在CentOS7RHEL7存储库中默认不可用,我们必须启用epel存储库,在两个节点上运行以下命令sudoyuminstallepelreleaseysudoyuminstallynginx ForUbuntuDebian 基于Debian的发行版,NGINX软件包在存储库中默认可用,在两个节点上运行如下命令sudoaptupdatesudoaptinstallynginx2)为两个节点自定义index。html 让我们为这两个节点创建自定义index。html,这样我们就可以很容易地识别哪个服务器在通过虚拟IP访问网站。 在node1上,执行如下命令〔pkumarnginx1〕echoh1ThisisNGINXWebServerfromNode1h1sudoteeusrsharenginxhtmlindex。html 在node2上,执行如下命令〔pkumarnginx2〕echoh1ThisisNGINXWebServerfromNode2h1sudoteeusrsharenginxhtmlindex。html3)放行NGINX端口并启动其服务 如果防火墙已启用,通过以下命令放行80端口 ForCentOSRHELSystemsudofirewallcmdpermanentaddservicehttpsudofirewallcmdreload ForUbuntuDebianSystemsudoufwallowNginxHTTP 启动并启用nginx服务sudosystemctlstartnginxsudosystemctlenablenginx 在外部运行curl命令测试两个节点的NGINX服务器curlhttp:192。168。1。130h1ThisisNGINXWebServerfromNode1h1curlhttp:192。168。1。140h1ThisisNGINXWebServerfromNode2h1 以上输出确认NGINX正在运行,并且可以从外部通过系统的IP地址访问。4)安装配置Keepalived 在两个节点上安装配置Keepalived ForCentOSRHELsystemssudodnfinstallykeepalivedCentOS8RHEL8sudoyuminstallykeepalivedCentOS7RHEL7 ForUbuntuDebianSystemaptinstallykeepalived 本文中,Node1作为主节点,Node2作为从节点。 备份配置文件〔pkumarnginx1〕sudocpetckeepalivedkeepalived。confetckeepalivedkeepalived。conforg 编辑配置文件〔pkumarnginx1〕echonsudoteeetckeepalivedkeepalived。conf〔pkumarnginx1〕sudovietckeepalivedkeepalived。conf 复制如下内容:globaldefs{Keepalivedprocessidentifierrouteridnginx}ScripttocheckwhetherNginxisrunningornotvrrpscriptchecknginx{scriptbinchecknginx。shinterval2weight50}VirtualinterfaceThepriorityspecifiestheorderinwhichtheassignedinterfacetotakeoverinafailovervrrpinstanceVI01{stateMASTERinterfaceenp0s3virtualrouterid151priority110ThevirtualipaddresssharedbetweenthetwoNGINXWebServerwhichwillfloatvirtualipaddress{192。168。1。15024}trackscript{checknginx}authentication{authtypeAHauthpasssecret}} 现在创建一个带有以下内容的脚本,它将检查nginx服务是否正在运行。keepalive将始终检查checknginx。sh脚本的输出,如果它发现nginx服务停止或没有响应,那么它将虚拟ip地址指向备份节点。〔pkumarnginx1〕sudovibinchecknginx。sh!binshif〔zpidofnginx〕;thenexit1fi 保存并关闭文件,设置所需权限〔pkumarnginx1〕sudochmod755binchecknginx。sh 使用scp命令把keepalive。conf和checknginx。sh文件从Node1复制到Node2〔pkumarnginx1〕scpetckeepalivedkeepalived。confroot192。168。1。140:etckeepalived〔pkumarnginx1〕scpbinchecknginx。shroot192。168。1。140:bin 复制完成后,登录到Node2,并在keepalive。conf文件中做一些更改。将state从MASTER更改为BACKUP,并将priority设置为100降低优先级。 如果开启防火墙,执行以下命令放行VRRP(两个节点都要执行) ForCentOSRHELSystemssudofirewallcmdaddrichruleruleprotocolvaluevrrpacceptpermanentsudofirewallcmdreload ForUbuntuDebianSystems 在主节点(Node1)上执行sudoufwallowto224。0。0。18commentVRRPBroadcastsudoufwallowfrom192。168。1。140commentVRRPRouter 在从节点(Node2)上执行sudoufwallowto224。0。0。18commentVRRPBroadcastsudoufwallowfrom192。168。1。130commentVRRPRouter 启动和开启keepalived服务sudosystemctlstartkeepalivedsudosystemctlenablekeepalived 验证keepalived服务状态sudosystemctlstatuskeepalived 验证主节点上的VIP(虚拟ip地址)状态,本例中VIP是192。168。1。130ipaddshow 以上输出确认在主节点的enp0s3接口上配置了VIP5)Keepalive和NGINX测试 使用虚拟IP(192。168。1。150)访问nginx服务器,目前它应该会显示Node1页面。 停止Node1上的NGINX服务,看看虚拟IP是否从Node1切换到Node2,这次它应该会显示Node1页面。〔pkumarnginx1〕sudosystemctlstopnginx〔pkumarnginx1〕ipaddshow 登录到Node2,查看虚拟IP是否正确〔pkumarnginx2〕ipaddshow 使用虚拟IP(192。168。1。150)访问nginx服务器 漂亮,以上证实我们已经成功地设置了高可用的NGINXWeb服务器。我的开源项目 酷瓜云课堂开源在线教育解决方案coursetencentcloud(酷瓜云课堂gitee仓库)coursetencentcloud(酷瓜云课堂github仓库)