【Docker】Docker-Compose内置DNS负载均衡失效问题

在这里插入图片描述

Docker Compose实现负载均衡

还是对前面的例子docker-compose.yml稍微修改:

version: "3.8"services:flask-demo:build:context: .dockerfile: Dockerfileimage: flask-demo:latestenvironment:- REDIS_HOST=redis-server- REDIS_PASS=${REDIS_PASS}healthcheck:test: ["CMD", "curl", "-f", "http://localhost:5000"]interval: 30stimeout: 3sretries: 3start_period: 40sdepends_on:- redis-serverdeploy:replicas: 3networks:- backend- frontendredis-server:image: redis:latestcommand: redis-server --requirepass ${REDIS_PASS}networks:- backendnginx:image: nginx:stable-alpineports:- 8000:80depends_on:flask-demo:condition: service_healthyvolumes:- ./nginx.conf:/etc/nginx/conf.d/default.conf:ro- ./log/nginx:/var/log/nginxnetworks:- frontendnetworks:backend:frontend:

主要是修改flask-demo启动3个容器。

nginx.conf文件的内容如下:

server {listen  80 default_server;location / {proxy_pass http://flask-demo:5000;}
}

启动服务:

$ docker-compose up -d
Creating network "app8_backend" with the default driver
Creating network "app8_frontend" with the default driver
Creating app8_redis-server_1 ... done
Creating app8_flask-demo_1   ... done
Creating app8_flask-demo_2   ... done
Creating app8_flask-demo_3   ... done
Creating app8_nginx_1        ... done$ docker-compose psName                      Command                  State                      Ports
----------------------------------------------------------------------------------------------------------
app8_flask-demo_1     flask run -h 0.0.0.0             Up (healthy)   5000/tcp
app8_flask-demo_2     flask run -h 0.0.0.0             Up (healthy)   5000/tcp
app8_flask-demo_3     flask run -h 0.0.0.0             Up (healthy)   5000/tcp
app8_nginx_1          /docker-entrypoint.sh ngin ...   Up             0.0.0.0:8000->80/tcp,:::8000->80/tcp
app8_redis-server_1   docker-entrypoint.sh redis ...   Up             6379/tcp

访问服务:

$ curl localhost:8000
Hello Container World! I have been seen 4 times and my hostname is 448b5d70d3d8.$ curl localhost:8000
Hello Container World! I have been seen 5 times and my hostname is 77b2a2314533.$ curl localhost:8000
Hello Container World! I have been seen 9 times and my hostname is 4eee9c8d54f1.

从运行结果可以发现我们可以根据service的名称访问容器,Docker会使用内置的DNS服务器将service的名称解析成IP,如果service对应的容器有多个,nginx会进行负载均衡。

Docker带有一个内置的DNS服务器。默认情况下,可以通过127.0.0.11:53访问服务器。这个DNS的IP可以通过进入容器内部查看/etc/resolv.conf获得:

$ docker-compose exec nginx cat /etc/resolv.conf
nameserver 127.0.0.11
options ndots:0

负载均衡的实现

在上面的例子中我们将flask-demo容器的个数提高到6个:

$ docker-compose up -d --scale flask-demo=6
app8_redis-server_1 is up-to-date
Creating app8_flask-demo_4 ... done
Creating app8_flask-demo_5 ... done
Creating app8_flask-demo_6 ... done
app8_nginx_1 is up-to-date$ docker-compose psName                      Command                  State                      Ports
----------------------------------------------------------------------------------------------------------
app8_flask-demo_1     flask run -h 0.0.0.0             Up (healthy)   5000/tcp
app8_flask-demo_2     flask run -h 0.0.0.0             Up (healthy)   5000/tcp
app8_flask-demo_3     flask run -h 0.0.0.0             Up (healthy)   5000/tcp
app8_flask-demo_4     flask run -h 0.0.0.0             Up (healthy)   5000/tcp
app8_flask-demo_5     flask run -h 0.0.0.0             Up (healthy)   5000/tcp
app8_flask-demo_6     flask run -h 0.0.0.0             Up (healthy)   5000/tcp
app8_nginx_1          /docker-entrypoint.sh ngin ...   Up             0.0.0.0:8000->80/tcp,:::8000->80/tcp
app8_redis-server_1   docker-entrypoint.sh redis ...   Up             6379/tcp$ docker container ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS                    PORTSNAMES
6e1494379165   flask-demo:latest     "flask run -h 0.0.0.0"   53 seconds ago   Up 51 seconds (healthy)   5000/tcpapp8_flask-demo_6
62733bdccdb8   flask-demo:latest     "flask run -h 0.0.0.0"   53 seconds ago   Up 51 seconds (healthy)   5000/tcpapp8_flask-demo_5
77e74622fa4e   flask-demo:latest     "flask run -h 0.0.0.0"   53 seconds ago   Up 51 seconds (healthy)   5000/tcpapp8_flask-demo_4
b67132189d90   nginx:stable-alpine   "/docker-entrypoint.…"   2 minutes ago    Up 2 minutes              0.0.0.0:8000->80/tcp, :::8000->80/tcp   app8_nginx_1
448b5d70d3d8   flask-demo:latest     "flask run -h 0.0.0.0"   2 minutes ago    Up 2 minutes (healthy)    5000/tcpapp8_flask-demo_1
4eee9c8d54f1   flask-demo:latest     "flask run -h 0.0.0.0"   2 minutes ago    Up 2 minutes (healthy)    5000/tcpapp8_flask-demo_2
77b2a2314533   flask-demo:latest     "flask run -h 0.0.0.0"   2 minutes ago    Up 2 minutes (healthy)    5000/tcpapp8_flask-demo_3
215beaad114a   redis:latest          "docker-entrypoint.s…"   2 minutes ago    Up 2 minutes              6379/tcpapp8_redis-server_1

再次访问我们的服务:

$ curl localhost:8000
Hello Container World! I have been seen 28 times and my hostname is 448b5d70d3d8.$ curl localhost:8000
Hello Container World! I have been seen 29 times and my hostname is 77b2a2314533.$ curl localhost:8000
Hello Container World! I have been seen 30 times and my hostname is 4eee9c8d54f1.$ curl localhost:8000
Hello Container World! I have been seen 31 times and my hostname is 448b5d70d3d8.$ curl localhost:8000
Hello Container World! I have been seen 32 times and my hostname is 77b2a2314533.$ curl localhost:8000
Hello Container World! I have been seen 33 times and my hostname is 4eee9c8d54f1.

连续访问6次之后发现请求并没有转发到我们新启动的3个容器中,这是为什么呢?

负载均衡失效原因分析

初步怀疑是DNS解析失效,我们进入nginx容器查看DNS在解析flask-demo时解析出哪些IP:

$ docker-compose exec nginx sh
/ # apk update
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/community/x86_64/APKINDEX.tar.gz
v3.14.10-47-g7553b19fe26 [https://dl-cdn.alpinelinux.org/alpine/v3.14/main]
v3.14.10-42-gd8ce7b89082 [https://dl-cdn.alpinelinux.org/alpine/v3.14/community]
OK: 14983 distinct packages available/ # apk add bind-tools
(1/11) Installing fstrm (0.6.1-r0)
(2/11) Installing krb5-conf (1.0-r2)
(3/11) Installing libcom_err (1.46.2-r1)
(4/11) Installing keyutils-libs (1.6.3-r0)
(5/11) Installing libverto (0.3.2-r0)
(6/11) Installing krb5-libs (1.18.5-r0)
(7/11) Installing json-c (0.15-r1)
(8/11) Installing protobuf-c (1.3.3-r6)
(9/11) Installing libuv (1.41.0-r0)
(10/11) Installing bind-libs (9.16.39-r0)
(11/11) Installing bind-tools (9.16.39-r0)
Executing busybox-1.33.1-r6.trigger
OK: 31 MiB in 53 packages
/ # dig flask-demo; <<>> DiG 9.16.39 <<>> flask-demo
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 55582
;; flags: qr rd ra; QUERY: 1, ANSWER: 6, AUTHORITY: 0, ADDITIONAL: 0;; QUESTION SECTION:
;flask-demo.                    IN      A;; ANSWER SECTION:
flask-demo.             600     IN      A       192.168.96.7
flask-demo.             600     IN      A       192.168.96.2
flask-demo.             600     IN      A       192.168.96.3
flask-demo.             600     IN      A       192.168.96.4
flask-demo.             600     IN      A       192.168.96.8
flask-demo.             600     IN      A       192.168.96.6;; Query time: 0 msec
;; SERVER: 127.0.0.11#53(127.0.0.11)
;; WHEN: Tue Oct 10 08:45:53 UTC 2023
;; MSG SIZE  rcvd: 184

发现DNS解析出了6个IP,然后我们在nginx容器中访问flask-demo

/ # curl flask-demo:5000
Hello Container World! I have been seen 281 times and my hostname is 77e74622fa4e./ # curl flask-demo:5000
Hello Container World! I have been seen 282 times and my hostname is 4eee9c8d54f1./ # curl flask-demo:5000
Hello Container World! I have been seen 283 times and my hostname is 62733bdccdb8./ # curl flask-demo:5000
Hello Container World! I have been seen 284 times and my hostname is 448b5d70d3d8./ # curl flask-demo:5000
Hello Container World! I have been seen 285 times and my hostname is 6e1494379165./ # curl flask-demo:5000
Hello Container World! I have been seen 289 times and my hostname is 77b2a2314533.

在nginx容器内访问就能访问到6个节点,那就说明nginx容器中DNS的解析没有问题,问题出在Nginx服务上。

Nginx在启动的时候会将代理服务器域名解析的ip地址缓存起来,后续不会再更新这个缓存,除非重启。

我们可以给nginx的缓存设置一个失效事件来解决这个问题,nginx.conf配置修改如下:

server {listen  80 default_server;location / {resolver 127.0.0.11 valid=1s; #设置dns服务器,缓存时间改为1sset $backend http://web:5000proxy_pass $backend;}
}

然后再让nginx重新加载配置,再访问服务,发现已经生效了:

$ docker-compose exec nginx nginx -s reload$ curl localhost:8000
Hello Container World! I have been seen 414 times and my hostname is 77b2a2314533.$ curl localhost:8000
Hello Container World! I have been seen 415 times and my hostname is 448b5d70d3d8.$ curl localhost:8000
Hello Container World! I have been seen 417 times and my hostname is 4eee9c8d54f1.$ curl localhost:8000
Hello Container World! I have been seen 420 times and my hostname is 77e74622fa4e.$ curl localhost:8000
Hello Container World! I have been seen 421 times and my hostname is 6e1494379165.$ curl localhost:8000
Hello Container World! I have been seen 422 times and my hostname is 62733bdccdb8.

docker compose参考例子

  • 投票app练习:https://github.com/dockersamples/example-voting-app
  • docker-compose example:https://github.com/docker/awesome-compose

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

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

相关文章

回归预测 | MATLAB实现IWOA-LSTM改进鲸鱼算法算法优化长短期记忆神经网络的数据回归预测(多指标,多图)

回归预测 | MATLAB实现IWOA-LSTM改进鲸鱼算法算法优化长短期记忆神经网络的数据回归预测&#xff08;多指标&#xff0c;多图&#xff09; 目录 回归预测 | MATLAB实现IWOA-LSTM改进鲸鱼算法算法优化长短期记忆神经网络的数据回归预测&#xff08;多指标&#xff0c;多图&#…

游戏研发的解决方案有哪些?

游戏研发的解决方案可以根据不同的需求和情境而有所不同&#xff0c;以下是一些常见的游戏研发解决方案&#xff1a; 游戏引擎&#xff1a; 游戏引擎是游戏研发的基础&#xff0c;它提供了开发游戏所需的核心功能&#xff0c;如图形渲染、物理引擎、音效管理、动画等。一些流行…

一、高效构建Java应用:Maven入门和进阶

一、高效构建Java应用&#xff1a;Maven入门和进阶 目录 一、Maven简介和快速入门 1.1 Maven介绍1.2 Maven主要作用理解1.3 Maven安装和配置 二、基于IDEA的Maven工程创建 2.1梳理Maven工程GAVP属性2.2 Idea构建Maven JavaSE工程2.3 Idea构建Maven JavaEE工程2.4 Maven工程项…

LVS集群-NAT模式

集群的概念&#xff1a; 集群&#xff1a;nginx四层和七层动静分离 集群标准意义上的概念&#xff1a;为解决特定问题将多个计算机组合起来形成一个单系统 集群的目的就是为了解决系统的性能瓶颈。 垂直扩展&#xff1a;向上扩展&#xff0c;增加单个机器的性能&#xff0c;…

KV STUDIO的安装与实践(一)

目录 什么是KV STUDIO&#xff1f; 如何安装KV STUDIO&#xff1f; 如何学习与使用KV STUDIO&#xff08;在现实中的应用&#xff09;&#xff1f; 应用一&#xff08;在现实生活中机器内部plc的读取与替换&#xff09; 读取 KV STUDIO实现显示器的检测&#xff01;&#…

Keepalived

一、Keepalived概述 1.1 Keepalived定义 Keepalived为LVS应运而生的高可用服务。由于LVS的调度器无法做高可用&#xff0c;于是引入keepalived软件。实现的是调度器的高可用。 但是keepalived不是专门为lvs集群服务的&#xff0c;也可以做其他代理服务器的高可用。 1.2 LVS…

kubeadm安装k8s集群

目录 一、环境部署 1、关闭防火墙规则、关闭selinux、关闭swap交换分区 2、修改主机名、DNS解析 3、调整内核参数 二、所有节点安装Docker 三、安装k8s集群 1、所有节点配置K8S源 2、所有节点安装kubeadm、kubelet和kubectl 3、部署K8S集群 3.1 初始化操作&#xff08…

车载音频ADI-ADSP21569音频DSP开发

车载音频ADI-ADSP21489音频DSP开发 是否需要申请加入数字音频系统研究开发交流答疑群(课题组)?可加我微信hezkz17, 本群提供音频技术答疑服务,+群赠送蓝牙音频,车载DSP音频项目核心开发资料, 1 芯片手册 2 电路原理图

网工内推 | 急招网工,思科、华为认证优先,法定节假日三薪

01 江苏臻云技术 招聘岗位&#xff1a;网络工程师 职责描述&#xff1a; 1、负责落实数据中心机房日常网络监测及巡检任务&#xff1b; 2、负责数据中心网络设备日常监控、变更、维护、巡检&#xff1b; 3、负责日常巡检报告、故障维护报告、变更申请的文档的编制&#xff1b;…

竞赛 深度学习人体跌倒检测 -yolo 机器视觉 opencv python

0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; **基于深度学习的人体跌倒检测算法研究与实现 ** 该项目较为新颖&#xff0c;适合作为竞赛课题方向&#xff0c;学长非常推荐&#xff01; &#x1f947;学长这里给一个题目综合评分(每项满…

番外8.2 --- 后续

### 01&#xff1a;dd命令&#xff1a;在新挂载点创建swap文件大小10MB&#xff1b;&#xff08;dd if/dev/zero of/swap bs1024 count10240&#xff09; 02&#xff1a;给swap建立文件系统&#xff0c;将其分属到swap文件&#xff08;mkswap /swap&#xff1b; swapon /swap &…

21.2 Python 使用Scapy实现端口探测

Scapy 是一款使用纯Python编写的跨平台网络数据包操控工具&#xff0c;它能够处理和嗅探各种网络数据包。能够很容易的创建&#xff0c;发送&#xff0c;捕获&#xff0c;分析和操作网络数据包&#xff0c;包括TCP&#xff0c;UDP&#xff0c;ICMP等协议&#xff0c;此外它还提…

ExcelPatternTool 开箱即用的Excel工具包现已发布!

文章目录 ExcelPatternTool功能特点&#xff1a;快速开始使用说明常规类型高级类型Importable注解Exportable注解IImportOption导入选项IExportOption导出选项单元格样式StyleMapping样式映射使用数据库作为数据源 示例Sample1&#xff1a;不同类型字段导出Sample2&#xff1a;…

Macos视频增强修复工具:Topaz Video AI for mac

Topaz Video AI是一款使用人工智能技术对视频进行增强和修复的软件。它可以自动降噪、去除锐化、减少压缩失真、提高清晰度等等。Topaz Video AI可以处理各种类型的视频&#xff0c;包括低分辨率视频、老旧影片、手机录制的视频等等。 使用Topaz Video AI非常简单&#xff0c;…

【Linux】部署单机OA项目及搭建spa前后端分离项目

目录 部署OA项目 ​编辑 搭建spa前后端分离项目 后端 前端 配置坏境变量 部署OA项目 在虚拟机中&#xff0c;将项目打包成war文件放置到tomcat根目录下的webapps文件目录下 再在主机数据库中连接数据库&#xff0c;并定义数据库名导入相关的表 继续进入tomcat目录下双击点…

【REDIS】redis-命令大全

【REDIS】redis-命令大全 redis-命令的官方文档 键命令 序号命令及描述1DEL key 该命令用于在 key 存在时删除 key。2DUMP key 序列化给定 key &#xff0c;并返回被序列化的值。3EXISTS key 检查给定 key 是否存在。4EXPIRE key seconds 为给定 key 设置过期时间&#xf…

升级 Xcode 15模拟器 iOS 17.0 Simulator(21A328) 下载失败

升级 IDE Xcode 15 后本地模拟器 Simulator 全被清空,反复重新尝试 Get 下载频频因网络异常断开而导致失败 ... 注:通过 Get 方式下载一定要保证当前网络环境足够平稳,网络环境不好的情况下该方法几乎成不了 解决办法 Get 方式行不通可以尝试通过 官网 途径先下载 模拟器安装包…

p5.js 到底怎么设置背景图?

本文简介 点赞 关注 收藏 学会了 在 《p5.js 光速入门》 里我们学过加载图片元素&#xff0c;学过过背景色的用法&#xff0c;但当时没提到背景图要怎么使用。 本文就把背景图这部分内容补充完整&#xff0c;并且会提到在 p5.js 里使用背景图的一些注意点。 背景图的用法…

JDK JVM JRE和Java API的关系

Java SE 英文全称是Java Standared Edition&#xff0c;它是Java的标准版。 Java SE由四部分组成&#xff1a;JDK JVM JRE和Java语言。 1.JDK Java Development Kit Java开发工具包。包含了所有编译&#xff0c;运行Java程序所需要的工具&#xff0c;还包含了Java运行环境&a…

npm改变npm缓存路径和改变环境变量

在安装nodejs时&#xff0c;系统会自动安装在系统盘C&#xff0c; 时间久了经常会遇到C盘爆满&#xff0c;有时候出现红色&#xff0c;此时才发现很多时候是因为npm 缓存保存在C盘导致的&#xff0c;下面就介绍下如何改变npm缓存路径。 1、首先找到安装nodejs的路径&#xff0c…