Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li><a href="#_label0">前言</a></li><li><a href="#_label1">一、架构设计</a></li><li><a href="#_label2">二、环境准备</a></li><li><a href="#_label3">三、案例部署</a></li><ul class="second_class_ul"><li><a href="#_lab2_3_0">配置 前端 Keepalived</a></li><li><a href="#_lab2_3_1">配置 前端 Nginx 负载均衡</a></li><li><a href="#_lab2_3_2">配置前端 Nginx监控脚本</a></li><li><a href="#_lab2_3_3">配置后端 web 服务</a></li></ul><li><a href="#_label4">四、测试</a></li><ul class="second_class_ul"><li><a href="#_lab2_4_4">1、Keepalived 健康检查</a></li><li><a href="#_lab2_4_5">2、Keepalived MASTER/BACKUP 切换</a></li></ul></ul></div><p class="maodian"><a name="_label0"></a></p><h2>前言</h2><p>Keepalived 作为一个高性能的集群高可用解决方案。提供了集群节点心跳检测、健康检查以及故障切换的功能。原生支持 LVS 负载均衡集群。除了原生支持的LVS + Keepalived 外,现在 Nginx + Keepalived 也比较常用。接下来,我将详细介绍 Nginx + Keepalived。</p>
<p class="maodian"><a name="_label1"></a></p><h2>一、架构设计</h2>
<p><strong>负载均衡方案系统架构拓扑图</strong><br /><img alt="" src="https://img.jbzj.com/file_images/article/202505/2025051715121949.png" /></p>
<p class="maodian"><a name="_label2"></a></p><h2>二、环境准备</h2>
<table><thead><tr><th>role</th><th>host</th><th>ip</th><th>software installed</th><th>OS</th></tr></thead><tbody><tr><td>Nginx proxy、Keepalive MASTER</td><td>node01</td><td>192.168.5.11</td><td>Nginx-1.10.0、keepalived</td><td>Centos 7.8</td></tr><tr><td>Nginx proxy、Keepalive BACKUP</td><td>node02</td><td>192.168.5.12</td><td>Nginx-1.10.0、keepalived</td><td>Centos 7.8</td></tr><tr><td>nginx web server1</td><td>node03</td><td>192.168.5.13</td><td>Nginx-1.18.0</td><td>Centos 7.8</td></tr><tr><td>nginx web server1</td><td>node04</td><td>192.168.5.14</td><td>Nginx-1.18.0</td><td>Centos 7.8</td></tr><tr><td>Client</td><td>node05</td><td>192.168.5.15</td><td>----</td><td>Centos 7.8</td></tr></tbody></table>
<p class="maodian"><a name="_label3"></a></p><h2>三、案例部署</h2>
<p class="maodian"><a name="_lab2_3_0"></a></p><h3>配置 前端 Keepalived</h3>
<div class="jb51code"><pre class="brush:bash;">---node01
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.5.10
smtp_connect_timeout 30
router_id LVS_DEVEL1
}
vrrp_script check_nginx_service {
script "/etc/keepalived/check_web_server_keepalive.sh"
#script "killall -0 nginx"
interval 2
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 200
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_nginx_service
}
virtual_ipaddress {
192.168.5.100
}
}
# systemctl restart keepalived.service
---node02
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.5.10
smtp_connect_timeout 30
router_id LVS_DEVEL2
}
vrrp_script check_nginx_service {
script "/etc/keepalived/check_web_server_keepalive.sh"
#script "killall -0 nginx"
interval 2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_nginx_service
}
virtual_ipaddress {
192.168.5.100
}
}
# systemctl restart keepalived.service
</pre></div>
<p class="maodian"><a name="_lab2_3_1"></a></p><h3>配置 前端 Nginx 负载均衡</h3>
<div class="jb51code"><pre class="brush:bash;">---node01
# mv /etc/nginx/conf.d/default.conf{,.bak}
# vim /etc/nginx/conf.d/vhost.conf
upstream nginx_keepalived_webservers {
server 192.168.5.13:80 weight=1;
server 192.168.5.14:80 weight=1;
}
server{
listen 80;
server_namewan.ngin_keepalived.org;
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://nginx_keepalived_webservers;
}
}
# systemctl restart nginx
node02
# mv /etc/nginx/conf.d/default.conf{,.bak}
# vim /etc/nginx/conf.d/vhost.conf
upstream nginx_keepalived_webservers {
server 192.168.5.13:80 weight=1;
server 192.168.5.14:80 weight=1;
}
server{
listen 80;
server_namewan.ngin_keepalived.org;
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://nginx_keepalived_webservers;
}
}
# systemctl restart nginx
</pre></div>
<p class="maodian"><a name="_lab2_3_2"></a></p><h3>配置前端 Nginx监控脚本</h3>
<div class="jb51code"><pre class="brush:bash;">---node01
# vim /etc/keepalived/check_web_server_keepalive.sh
#!/bin/bash
http_status=`ps -C nginx --no-header | wc -l`
if [ $http_status -eq 0 ];then
systemctl start nginx
sleep 3
if [ `ps -C nginx --no-header | wc -l` -eq 0 ]
then
systemctl stop keepalived
fi
fi
# vim /etc/keepalived/check_web_server_keepalive.sh
#!/bin/bash
http_status=`ps -C nginx --no-header | wc -l`
if [ $http_status -eq 0 ];then
systemctl start nginx
sleep 3
if [ `ps -C nginx --no-header | wc -l` -eq 0 ]
then
systemctl stop keepalived
fi
fi
</pre></div>
<p class="maodian"><a name="_lab2_3_3"></a></p><h3>配置后端 web 服务</h3>
<div class="jb51code"><pre class="brush:bash;">---node03
# yum install nginx-1.18.0-1.el7.ngx.x86_64.rpm -y
# echo "`hostname -I` web test page..." > /usr/share/nginx/html/index.html
# systemctl enable --now nginx
----node04
# yum install nginx-1.18.0-1.el7.ngx.x86_64.rpm -y
# echo "`hostname -I` web test page..." > /usr/share/nginx/html/index.html
# systemctl enable --now nginx
</pre></div>
<p>客户端访问 VIP<br /><img alt="" src="https://img.jbzj.com/file_images/article/202505/2025051715121950.png" /><br />实现 Web 服务负载均衡 !</p>
<p class="maodian"><a name="_label4"></a></p><h2>四、测试</h2>
<p>node05 添加hosts解析</p>
<p class="maodian"><a name="_lab2_4_4"></a></p><h3>1、Keepalived 健康检查</h3>
<p><strong>检测 VIP 访问 Web 服务</strong><br /><img alt="" src="https://img.jbzj.com/file_images/article/202505/2025051715122051.png" /></p>
<p><strong>模拟后端服务故障</strong></p>
<div class="jb51code"><pre class="brush:bash;"># systemctl stop nginx
# systemctl is-active nginx
inactive
</pre></div>
<p><strong>检测 VIP 访问 Web 服务</strong><br /><img alt="" src="https://img.jbzj.com/file_images/article/202505/2025051715122052.png" /><br /><strong>模拟后端服务故障恢复</strong></p>
<div class="jb51code"><pre class="brush:bash;"># systemctl start nginx
# systemctl is-active nginx
active
</pre></div>
<p><strong>检测 VIP 访问 Web 服务</strong><br /><img alt="" src="https://img.jbzj.com/file_images/article/202505/2025051715122053.png" /><br />注:Nginx upstream 模块默认支持对后端服务健康监测,Haproxy 同样也自带这种功能!</p>
<p class="maodian"><a name="_lab2_4_5"></a></p><h3>2、Keepalived MASTER/BACKUP 切换</h3>
<p>查看keeapalived VIP 地址状况</p>
<p>node01<br /><img alt="" src="https://img.jbzj.com/file_images/article/202505/2025051715122054.png" /><br />node02<br /><img alt="" src="https://img.jbzj.com/file_images/article/202505/2025051715122055.png" /><br /><strong>模拟 Keepalived MASTER 故障</strong></p>
<div class="jb51code"><pre class="brush:bash;"># systemctl stop keepalived.service
</pre></div>
<p>node01<br /><img alt="" src="https://img.jbzj.com/file_images/article/202505/2025051715122056.png" /><br />node02<br /><img alt="" src="https://img.jbzj.com/file_images/article/202505/2025051715122057.png" /><br />web 服务访问不受影响<br /><img alt="" src="https://img.jbzj.com/file_images/article/202505/2025051715122058.png" /><br /><strong>模拟 Keepalived MASTER 故障恢复</strong></p>
<div class="jb51code"><pre class="brush:bash;"># systemctl start keepalived.service
</pre></div>
<p>node01<br /><img alt="" src="https://img.jbzj.com/file_images/article/202505/2025051715122159.png" /><br />node02<br /><img alt="" src="https://img.jbzj.com/file_images/article/202505/2025051715122160.png" /><br /><strong>实现 keeapalived VIP 漂移 !</strong><br /><strong>测试前端 Nginx 负载均衡服务器</strong><br />node01<br /><img alt="" src="https://img.jbzj.com/file_images/article/202505/2025051715122161.png" /><br />Nginx 服务异常后 自动启动!</p>
<p>node02<br /><img alt="" src="https://img.jbzj.com/file_images/article/202505/2025051715122162.png" /><br />Web 访问不受影响<br /><img alt="" src="https://img.jbzj.com/file_images/article/202505/2025051715122163.png" /><br /><strong>模拟node01 nginx 服务无法启动</strong><br /><img alt="" src="https://img.jbzj.com/file_images/article/202505/2025051715122164.png" /></p>
<p>node01<br /><img alt="" src="https://img.jbzj.com/file_images/article/202505/2025051715122165.png" /><br />node02<br /><img alt="" src="https://img.jbzj.com/file_images/article/202505/2025051715122166.png" /><br />Web 访问不受影响<br /><img alt="" src="https://img.jbzj.com/file_images/article/202505/2025051715122163.png" /></p>
<p></p>
<p>到此这篇关于Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例的文章就介绍到这了,更多相关Nginx使用Keepalived部署web集群内容请搜索琼殿技术社区以前的文章或继续浏览下面的相关文章希望大家以后多多支持琼殿技术社区!</p>
頁:
[1]