- VisualStudio2022插件的安装及使用-编程手把手系列文章
- pprof-在现网场景怎么用
- C#实现的下拉多选框,下拉多选树,多级节点
- 【学习笔记】基础数据结构:猫树
1. Nginx 负载均衡基础配置 。
首先,搭建一个基础的 Nginx 负载均衡器,用于将流量分发到多个后端服务器上.
在每台要作为负载均衡器的服务器上,安装 Nginx。可以使用包管理工具进行安装,例如在 Ubuntu 上执行以下命令:
sudo apt update
sudo apt install nginx
Nginx 的核心是配置文件 nginx.conf,我们可以在其中定义后端服务器池以及负载均衡策略。以下是一个简单的 Nginx 负载均衡配置:
# 定义一个名为 backend 的后端服务器池
upstream backend {
server backend1.example.com weight=5; # 设置权重
server backend2.example.com weight=3;
server backend3.example.com weight=2;
# 启用健康检查(需要 Nginx Plus 支持开箱配置,开源版本需要第三方模块)
# Nginx Plus 示例:
health_check interval=10s fails=3 passes=2;
}
# 配置 HTTP 服务器
server {
listen 80;
server_name loadbalancer.example.com;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
upstream 指令:定义了后端服务器池(backend),并为各服务器分配了不同的权重,Nginx 根据权重将流量按照比例分发到后端服务器.
健康检查:此配置会定期检查后端服务器是否可用,确保当某个服务器宕机时,不会继续向其发送请求.
proxy_pass:将客户端请求代理到后端服务器池.
确保配置无误后,启动或重启 Nginx 服务:
sudo nginx -t # 测试配置文件是否正确
sudo systemctl restart nginx # 重启 Nginx
测试:通过访问 http://loadbalancer.example.com,验证请求是否被均匀分发到后端服务器.
单独使用 Nginx 进行负载均衡仍然会面临单点故障问题。如果前端的 Nginx 宕机,整个服务将不可用。因此,我们需要通过 Keepalived 实现高可用的 Nginx 集群.
在每台 Nginx 服务器上安装 Keepalived。以 Ubuntu 为例:
sudo apt install keepalived
Keepalived 通过虚拟 IP 地址(VIP)实现故障转移。当主服务器宕机时,VIP 自动切换到备用服务器,确保服务的高可用性.
在主 Nginx 服务器上,编辑 Keepalived 的配置文件 /etc/keepalived/keepalived.conf:
vrrp_instance VI_1 {
state MASTER # 主服务器
interface eth0 # 网络接口
virtual_router_id 51
priority 100 # 主服务器优先级较高
advert_int 1 # 广播间隔
authentication {
auth_type PASS
auth_pass 123456 # 密码
}
virtual_ipaddress {
192.168.0.100 # 虚拟IP地址
}
track_script {
chk_nginx # 监控 Nginx 状态的脚本
}
}
在备用 Nginx 服务器上,将 state 设置为 BACKUP,并将 priority 设置为较低的值,例如 90.
Keepalived 可以通过监控 Nginx 的运行状态来决定是否切换 VIP。创建一个监控脚本 /etc/keepalived/check_nginx.sh:
将此脚本添加为可执行:
sudo chmod +x /etc/keepalived/check_nginx.sh
在 Keepalived 的配置文件中添加监控脚本:
vrrp_script chk_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 2
}
完成配置后,启动或重启 Keepalived 服务:
sudo systemctl restart keepalived
测试:关闭主服务器的 Nginx,VIP 应该自动切换到备用服务器,确保服务不中断.
Nginx 可以结合健康检查功能,确保只有状态正常的服务器参与负载均衡。另外,动态扩展是应对突发流量的关键。以下是相关配置和实战方案.
Nginx 开源版本不自带健康检查模块,可以通过第三方模块(如 ngx_http_healthcheck_module)实现健康检查。假设已安装此模块,配置如下:
upstream backend {
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
# 使用第三方模块实现健康检查
check interval=5000 rise=2 fall=5 timeout=2000;
}
结合容器化技术(如 Docker 或 Kubernetes),可以根据流量自动扩展后端服务器。例如,在 Kubernetes 集群中可以使用 Horizontal Pod Autoscaler (HPA) 自动扩展应用服务的副本数.
以下是在 Kubernetes 中配置自动扩展的示例:
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: backend-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: backend
minReplicas: 3
maxReplicas: 10
targetCPUUtilizationPercentage: 70 # 当 CPU 利用率超过 70% 时扩容
通过这种方式,后端服务可以根据负载动态扩展,Nginx 通过配置服务发现机制可以自动识别新的后端服务器.
在生产环境中,启用 HTTPS 是必不可少的。以下是启用 SSL/TLS 的配置:
使用 Let's Encrypt 生成免费的 SSL 证书:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com
server {
listen 443 ssl;
server_name yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
# 自动将 HTTP 请求重定向到 HTTPS
server {
listen 80;
server_name yourdomain.com;
return 301 https://$host$request_uri;
}
通过 Nginx 的负载均衡、Keepalived 实现高可用、动态扩展后端服务器以及健康检查,构建了一个高效、可扩展且高可用的互联网集群架构.
最后此篇关于基于Nginx的大型互联网集群架构与实战方案的文章就讲到这里了,如果你想了解更多关于基于Nginx的大型互联网集群架构与实战方案的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
实战-行业攻防应急响应 简介: 服务器场景操作系统 Ubuntu 服务器账号密码:root/security123 分析流量包在/home/security/security.pcap 相
背景 最近公司将我们之前使用的链路工具切换为了 OpenTelemetry. 我们的技术栈是: OTLP C
一 同一类的方法都用 synchronized 修饰 1 代码 package concurrent; import java.util.concurrent.TimeUnit; public c
一 简单例子 1 代码 package concurrent.threadlocal; /** * ThreadLocal测试 * * @author cakin */ public class T
1. 问题背景 问题发生在快递分拣的流程中,我尽可能将业务背景简化,让大家只关注并发问题本身。 分拣业务针对每个快递包裹都会生成一个任务,我们称它为 task。task 中有两个字段需要
实战环境 elastic search 8.5.0 + kibna 8.5.0 + springboot 3.0.2 + spring data elasticsearch 5.0.2 +
Win10下yolov8 tensorrt模型加速部署【实战】 TensorRT-Alpha 基于tensorrt+cuda c++实现模型end2end的gpu加速,支持win10、
yolov8 tensorrt模型加速部署【实战】 TensorRT-Alpha 基于tensorrt+cuda c++实现模型end2end的gpu加速,支持win10、linux,
目录如下: 为什么需要自定义授权类型? 前面介绍OAuth2.0的基础知识点时介绍过支持的4种授权类型,分别如下: 授权码模式 简化模式 客户端模式 密码模式
今天这篇文章介绍一下如何在修改密码、修改权限、注销等场景下使JWT失效。 文章的目录如下: 解决方案 JWT最大的一个优势在于它是无状态的,自身包含了认证鉴权所需要的所有信息,服务器端
前言 大家好,我是捡田螺的小男孩。(求个星标置顶) 我们日常做分页需求时,一般会用limit实现,但是当偏移量特别大的时候,查询效率就变得低下。本文将分四个方案,讨论如何优化MySQL百万数
前言 大家好,我是捡田螺的小男孩。 平时我们写代码呢,多数情况都是流水线式写代码,基本就可以实现业务逻辑了。如何在写代码中找到乐趣呢,我觉得,最好的方式就是:使用设计模式优化自己
我们先讲一些arm汇编的基础知识。(我们以armv7为例,最新iphone5s上的64位暂不讨论) 基础知识部分: 首先你介绍一下寄存器: r0-r3:用于函数参数及返回值的传递 r4-r6
一 同一类的静态方法都用 synchronized 修饰 1 代码 package concurrent; import java.util.concurrent.TimeUnit; public
DRF快速写五个接口,比你用手也快··· 实战-DRF快速写接口 开发环境 Python3.6 Pycharm专业版2021.2.3 Sqlite3 Django 2.2 djangorestfram
一 添加依赖 org.apache.thrift libthrift 0.11.0 二 编写 IDL 通过 IDL(.thrift 文件)定义数据结构、异常和接口等数据,供各种编程语言使用 nam
我正在阅读 Redis in action e-book关于semaphores的章节.这是使用redis实现信号量的python代码 def acquire_semaphore(conn, semn
自定义控件在WPF开发中是很常见的,有时候某些控件需要契合业务或者美化统一样式,这时候就需要对控件做出一些改造。 目录 按钮设置圆角
师父布置的任务,让我写一个服务练练手,搞清楚socket的原理和过程后跑了一个小demo,很有成就感,代码内容也比较清晰易懂,很有教育启发意义。 代码 ?
? 1 2
我是一名优秀的程序员,十分优秀!