two load balancer methods

news/2025/1/6 20:26:00/文章来源:https://www.cnblogs.com/lightsong/p/18652331

microservice without load balancer

 

不带负载均衡例子。

https://github.com/GavriloviciEduard/fastapi-microservices/tree/master

 

multiple upstream for load balancer

将上游服务看成独立的server

traefik multiple upstream

https://github.com/tanishqmanuja/demo.traefik-load-balancing

 

https://github.com/tanishqmanuja/demo.traefik-load-balancing/blob/main/config/traefik/config.yaml

http:routers:delhi:entryPoints:- webrule: PathPrefix(`/`) && Headers(`X-FOR-LOCATION`, `DEL`)service: delhibombay:entryPoints:- webrule: PathPrefix(`/`) && Headers(`X-FOR-LOCATION`, `BOM`)service: bombayall:entryPoints:- webrule: PathPrefix(`/`)service: allservices:delhi:loadbalancer:servers:- url: http://server-delhi-alpha:8080- url: http://server-delhi-bravo:8080bombay:loadbalancer:servers:- url: http://server-bombay-alpha:8080- url: http://server-bombay-bravo:8080all:weighted:services:- name: delhiweight: 1- name: bombayweight: 1

 

x-server: &serverimage: bun-server:latestpull_policy: neverservices:server-delhi-alpha:<<: *serverbuild: ./server # build onceenvironment:LOCATION: DELINSTANCE_ID: alphaserver-delhi-bravo:<<: *serverenvironment:LOCATION: DELINSTANCE_ID: bravoserver-bombay-alpha:<<: *serverenvironment:LOCATION: BOMINSTANCE_ID: alphaserver-bombay-bravo:<<: *serverenvironment:LOCATION: BOMINSTANCE_ID: bravotraefik:image: traefikports:- 8080:80volumes:- ./config/traefik:/etc/traefik

 

 

nginx multiple upstream

https://dev.to/mazenr/how-to-implement-a-load-balancer-using-nginx-docker-4g73

Implement Nginx Load Balancer

 

Let's consider having 3 Python servers each one is deployed to a container. After that we will use Nginx as a load balancer for the 3 servers.

Here is our files structure:

nginx-load-balancer||---app1|     |-- app1.py|     |-- Dockerfile|     |-- requirements.txt||---app2|     |-- app2.py|     |-- Dockerfile|     |-- requirements.txt||---nginx|     |-- nginx.conf|     |-- Dockerfile||------ docker-compose.yml

 

https://medium.com/@vinodkrane/microservices-scaling-and-load-balancing-using-docker-compose-78bf8dc04da9

https://stackoverflow.com/questions/50203408/docker-compose-scale-x-nginx-conf-configuration

 

Nginx

Dynamic upstreams are possible in Nginx (normal, sans Plus) but with tricks and limitations.

  1. You give up on upstream directive and use plain proxy_pass.

    It gives round robin load balancing and failover, but no extra feature of the directive like weights, failure modes, timeout, etc.

  2. Your upstream hostname must be passed to proxy_pass by a variable and you must provide a resolver.

    It forces Nginx to re-resolve the hostname (against Docker networks' DNS).

  3. You lose location/proxy_pass behaviour related to trailing slash.

    In the case of reverse-proxying to bare / like in the question, it does not matter. Otherwise you have to manually rewrite the path (see the references below).

Let's see how it works.

docker-compose.yml

version: '2.2'
services:reverse-proxy:image: nginx:1.15-alpinevolumes:- ./nginx.conf:/etc/nginx/nginx.confports:- 8080:8080app:# A container that exposes an API to show its IP addressimage: containous/whoamiscale: 4

nginx.conf

worker_processes  1;events {worker_connections  1024;
}http {access_log /dev/stdout;error_log /dev/stderr;server {listen 8080;server_name localhost;resolver 127.0.0.11 valid=5s;set $upstream app;location / {proxy_pass http://$upstream:80;}}
}

Then...

docker-compose up -d
seq 10 | xargs -I -- curl -s localhost:8080 | grep "IP: 172"

...produces something like the following which indicates the requests are distributed across 4 app containers:

IP: 172.30.0.2
IP: 172.30.0.2
IP: 172.30.0.3
IP: 172.30.0.3
IP: 172.30.0.6
IP: 172.30.0.5
IP: 172.30.0.3
IP: 172.30.0.6
IP: 172.30.0.5
IP: 172.30.0.5

References:

  1. Nginx with dynamic upstreams
  2. Using Containers to Learn Nginx Reverse Proxy
  3. Dynamic Nginx configuration for Docker with Python

Traefik

Traefik relies on Docker API directly and may be a simpler and more configurable option. Let's see it in action.

docker-compose.yml

version: '2.2'
services:reverse-proxy:image: traefik  # Enables the web UI and tells Traefik to listen to dockercommand: --api --docker  ports:- 8080:80      - 8081:8080  # Traefik's web UI, enabled by --apivolumes:# So that Traefik can listen to the Docker events- /var/run/docker.sock:/var/run/docker.sock  app:image: containous/whoamiscale: 4labels:- "traefik.frontend.rule=Host:localhost"

Then...

docker-compose up -d
seq 10 | xargs -I -- curl -s localhost:8080 | grep "IP: 172"

...also produces something the output that indicates the requests are distributed across 4 app containers:

IP: 172.31.0.2
IP: 172.31.0.5
IP: 172.31.0.6
IP: 172.31.0.4
IP: 172.31.0.2
IP: 172.31.0.5
IP: 172.31.0.6
IP: 172.31.0.4
IP: 172.31.0.2
IP: 172.31.0.5

In the Traefik UI (http://localhost:8081/dashboard/ in the example) you can see it recognised the 4 app containers:

Backends

References:

  1. The Traefik Quickstart (Using Docker)

 

https://github.com/mazen-r/articles/blob/main/nginx-docker-load-balancer/nginx/nginx.conf

upstream loadbalancer {server 172.17.0.1:5001 weight=5;server 172.17.0.1:5002 weight=5;
}server {location / {proxy_pass http://loadbalancer;}
}

 

version: '3'
services:app1:build: ./app1ports:- "5001:5000"app2:build: ./app2ports:- "5002:5000"nginx:build: ./nginx ports:- "8080:80"depends_on:- app1- app2

 

docker compose scale for load balancer

 

traefik example

https://doziestar.medium.com/effortless-scaling-and-deployment-a-comprehensive-guide-for-solo-developers-and-time-savers-88bfa4118940

 

Docker Compose — scale Command:

The docker-compose --scale command allows you to scale your Docker Compose services by specifying the number of replicas (instances) for each service. This command makes it easy to scale services up or down on demand.

docker-compose up --scale SERVICE=NUM_REPLICAS

To both services, we can simply run

docker-compose up --build --scale server=3 --scale computations=3

If we run this, we can see something like this

We have 3 instances of computations and 3 instances of the server

You can see how we are now running 3 instances of each service that we scale.

Load Balancing Strategy

By default, Traefik uses the Round Robin load balancing strategy, but you can change this by adding the appropriate label to your service. For example, to use the Weighted Round Robin strategy, you would add:

labels:
- "traefik.http.services.web.loadbalancer.method=wrr"

But without changing anything. Traefik will automatically discover the new instances of your computations and serverservice and load balance incoming requests.

 

nginx example

https://milaan.hashnode.dev/scaling-docker-containers-with-nginx-a-guide-to-reverse-proxy-and-load-balancing

Now that we have everything we need, let's use the following command to see if everything works out properly or not. If everything works properly, then nginx should act as a reverse proxy and load balancer.

 
 
docker compose up --scale api=3

docker compose up --scale api=3 is a command that is used to start a set of containers defined in a Docker Compose file and scale an API service in the Compose file to run three instances of that service.

The hostname that is displayed, is actually the container ID. Since we have 3 containers let's check if the load balancer is working properly or not. For context, the round-robin algorithm is used in this demo.

Here is the container ID.

Here we can see that every time we request for API server we see that a new container is responding.

Voilà, there you go that's how you scale docker containers with nginx. Big thanks to trulymittal <3

 

code

https://github.com/ofstudio/docker-compose-scale-example/tree/master

version: '2.3'
# Note: `services.app.scale` available only version 2.x
# In version 3.x scale option will produce error:
# "Unsupported config option for services.app: 'scale'"
# You can use `docker-compose up --scale app=3`
# instead of `scale` field for version 3.x compose filesservices:app:build: .image: "scale-app-example"scale: 3nginx:image: nginx:stable-alpineports:- 8000:80depends_on:- appvolumes:- ./nginx.conf:/etc/nginx/conf.d/default.conf:ro- ./var/log/nginx:/var/log/nginx

 

server {listen  80 default_server;location / {proxy_pass http://app:3000;}
}

 

Both tutorial

https://github.com/twtrubiks/docker-django-nginx-uwsgi-postgres-load-balance-tutorial/blob/master/nginx/my_nginx.conf

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/863993.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

GDPR——管辖权和域外效力

判定企业是否需要遵循GDPR的要求,第一步需要判断是否属于GDPR的管辖范围。粗略讲分为两类: 1、营业地在欧盟(域内):注册地、在欧盟区域设有办事处等分支机构 2、营业地不在欧盟(域外):但针对欧盟公民处理数据(提供服务、监控等) 进一步的判定如下:“营业活动”:指通…

NOI Linux 2.0 竞赛环境食用指北

NOI Linux 2.0 竞赛环境食用指北 终端 使用 Ctrl+Alt+T 在当前登录用户的目录 ~ 中打开终端,也可以在文件夹中 右键 或 Shift+右键 在当前文件夹打开终端。 运行可执行文件需要加 ./,例如 ./<filename>。注意 Linux 中可执行文件是没有后缀名的。 mkdir 新建目录,ls 列…

学期2024-2025-1 学号20241424 《计算机基础与程序设计》第15周学习总结

学期2024-2025-1 学号20241424 《计算机基础与程序设计》第15周学习总结 作业信息 |这个作业属于2024-2025-1-计算机基础与程序设计)| |-- |-- | |这个作业要求在2024-2025-1计算机基础与程序设计第15周作业| |这个作业的目标|<作业总结>| |作业正文|https://www.cnblog…

中考英语优秀范文-008 when you in China, do as the Chinese do! 当你在中国时,入乡随俗!

1 写作要求 在不同的地方有不同的风俗习惯,饮食文化很重要。中国的饮食文化不同于西方。 请你谈谈中国的三餐,以“when you in China, do as the Chinese do!” 为题写一篇短文。 要求: 80词左右,内容合理; 要点齐全; 句子及篇章结构准确、连贯;书写规范。 2 优秀范文 W…

MongoDB集群中数据分布与分片

MongoDB集群中数据分布 Chunk是什么 在一个shard server内部,MongoDB还是会把数据分为chunks,每个chunk代表这个shard server内部一部分数据。chunk的产生,会有以下两个用途:Splitting:当一个chunk的大小超过配置中的chunk size时,MongoDB的后台进程会把这个chunk切分成更…

RoomFormer、FRI-Net

WSL CUDA安装 【一文解决】已安装CUDA与Pytorch但torch.cuda.is_available()为False_torch.cuda.is available返回false-CSDN博客 cuda安装 Ubuntu 20.04安装CUDA & CUDNN 手把手带你撸_ubuntu20.04安装cuda-CSDN博客 【CUDA】Ubuntu系统如何安装CUDA保姆级教程(2022年最新…

5G核心网短信解决方案概述

短信作为传统无线网络的基础业务,随着移动网络的飞速发展,我们会发现,在日常生活中我们使用短信进行沟通的情况越来越少了。但是在我们使用各种业务的时候,仍然离不开短信,例如: 注册新的APP、密码找回,快递取件,收发验证码等,对于安全性要求较高的业务,更要使用短信…

docker 安装doris

下载镜像docker pull apache/doris:build-env-ldb-toolchain-latest下载安装包 https://doris.apache.org/zh-CN/downloadwget https://apache-doris-releases.oss-accelerate.aliyuncs.com/apache-doris-2.1.7-bin-x64.tar.gz然后需要下载MySQL,这里提供MySQL的免安装版MySQL…

Windows单机安装MongoDB分片集群

Windows单机部署MongoDB分片集群 规划和准备 端口规划 操作系统:Windows Server 2012 MongoDB版本:4.2.25IP/节点名 mongos config shard1 shard2 shard3127.0.0.1(mongo1) mongos1(27017) config1(27018) 主(27001) 主(27002) 主(27003)127.0.0.1(mongo2) mongos2(27027…

学习-Nginx-安装nginx1.21.6开源软件

下载地址 http://nginx.org/download/nginx-1.21.6.tar.gz 通过网盘分享的文件:Nginx1.21.6 链接: https://pan.baidu.com/s/1tcsTs2IEmN80wt5VQ5U3PA?pwd=sky1 提取码: sky1 Xftp 传输安装包解压缩安装包 tar zxvf nginx-1.21.6进入到 nginx文件夹查看需要的依赖 ./configur…

C# 内嵌数据库 SQLite

最近,看到一个软件,软件是使用的内嵌数据库。我对这个东西没有实践过,今天突然想亲手做一做!。关于SQLIte的资料我就不多说了,网上都有。我自己也整理了一部分,基本上可以对SQLite有个全面的了解了。我这里就不废话了,直接上我自己的代码。 1:首先要先下载一个SQLite的…

Rust远程加载shellcode

学习rust, 练习写一个loader, 不足之处还请指教编写 隐藏黑框 在注释掉所有打印语句后编译运行还是会弹黑框, 解决方法是头部添加一行(指定 Rust 编译器生成的可执行文件为 Windows 子系统应用程序,而不是控制台应用程序): #![windows_subsystem = "windows"]‍ 反…