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

news2025/1/11 12:48:30

在这里插入图片描述

Docker Compose实现负载均衡

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

version: "3.8"

services:
  flask-demo:
    build:
        context: .
        dockerfile: Dockerfile
    image: flask-demo:latest
    environment:
      - REDIS_HOST=redis-server
      - REDIS_PASS=${REDIS_PASS}
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:5000"]
      interval: 30s
      timeout: 3s
      retries: 3
      start_period: 40s
    depends_on:
      - redis-server
    deploy:
      replicas: 3
    networks:
      - backend
      - frontend

  redis-server:
    image: redis:latest
    command: redis-server --requirepass ${REDIS_PASS}
    networks:
      - backend
  nginx:
    image: nginx:stable-alpine
    ports:
      - 8000:80
    depends_on:
      flask-demo:
        condition: service_healthy
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
      - ./log/nginx:/var/log/nginx
    networks:
      - frontend

networks:
  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 ps
       Name                      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 ps
       Name                      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                    PORTS
 NAMES
6e1494379165   flask-demo:latest     "flask run -h 0.0.0.0"   53 seconds ago   Up 51 seconds (healthy)   5000/tcp
 app8_flask-demo_6
62733bdccdb8   flask-demo:latest     "flask run -h 0.0.0.0"   53 seconds ago   Up 51 seconds (healthy)   5000/tcp
 app8_flask-demo_5
77e74622fa4e   flask-demo:latest     "flask run -h 0.0.0.0"   53 seconds ago   Up 51 seconds (healthy)   5000/tcp
 app8_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/tcp
 app8_flask-demo_1
4eee9c8d54f1   flask-demo:latest     "flask run -h 0.0.0.0"   2 minutes ago    Up 2 minutes (healthy)    5000/tcp
 app8_flask-demo_2
77b2a2314533   flask-demo:latest     "flask run -h 0.0.0.0"   2 minutes ago    Up 2 minutes (healthy)    5000/tcp
 app8_flask-demo_3
215beaad114a   redis:latest          "docker-entrypoint.s…"   2 minutes ago    Up 2 minutes              6379/tcp
 app8_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服务器,缓存时间改为1s
    set $backend http://web:5000
    proxy_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.coloradmin.cn/o/1128280.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

有没有PC端的配音软件推荐?(免下载)

配音软件还是电脑上使用最方便&#xff0c;而且电脑上可以使用的配音软件也非常多。只是你平时使用的不多&#xff0c;所有想用的时候才会找不到&#xff0c;对于经常使用配音软件的人来说&#xff0c;那真的太多了。今天给大家推荐一个免下载的配音网站&#xff0c;微信扫码即…

如何解决网站被攻击的问题?

在数字时代&#xff0c;网站安全问题日益突出&#xff0c;网络攻击的形式不断演进&#xff0c;因此&#xff0c;保护网站免受威胁至关重要。本文将探讨如何解决网站被攻击的问题&#xff0c;分析未来的网络攻击形式&#xff0c;并提供一些通俗易懂的建议&#xff0c;以加强网站…

【机器学习合集】泛化与正则化合集 ->(个人学习记录笔记)

文章目录 泛化与正则化1. 泛化(generalization)2. 正则化方法2.1 显式正则化方法显式正则化方法对比提前终止模型的训练多个模型集成Dropout技术 2.2 参数正则化方法2.3 隐式正则化方法方法对比 泛化与正则化 1. 泛化(generalization) 泛化不好可能带来的问题 模型性能不稳定容…

安全响应中心 — 垃圾邮件事件报告(10.13)

2023年10月 第二周 一. 样本概况 ✅ 案例1&#xff1a;DocuSign钓鱼 本周收到一封看似来自 DocuSign&#xff08;DocuSign 是一种在企业环境中广泛使用的电子协议管理平台&#xff09;的网络钓鱼电子邮件反馈。 如下图所示&#xff1a; 以上样本内容大体是说XX发送了一份文…

【postman】postman的使用与postman汉化

postman的使用 Postman 是一个接口测试工具软件&#xff0c;可以帮助开发人员管理测试接口。 官网&#xff1a;Postman API Platform psotman环境 首先import的或则new 创建一个环境 Variable 变量名 Type 类型 Initial value 初始值 C…

ubuntu vbox 5.2 资源 virtualbox-dkms

各种 linux 包 https://pkgs.org/search/?qpython3.6 配置 python 默认版本 查看已安装python $ u82:~/Py_demo$ ls /usr/bin/python* /usr/bin/python2 /usr/bin/python2.7 /usr/bin/python3 /usr/bin/python3.8设置默认版本 $ u82:~/Py_demo$ sudo update-alternatives…

VR智慧景区,为游客开启智慧旅游新时代

近年来&#xff0c;文旅部加强了5G、VR虚拟技术等在文旅产业行业的运用&#xff0c;随着科技的不断发展&#xff0c;VR技术的运用越来越广泛&#xff0c;VR智慧景区作为一种全新的旅游方式&#xff0c;也渐渐的受到了人们广泛的关注&#xff0c;它可以让人们足不出户就欣赏到各…

数字IC后端面试题目汇总含解析,ICer上岸必备!

大家都知道&#xff0c;面试的表现会对于个人职业发展的重要性&#xff0c;不仅能决定是否录用&#xff0c;还会影响到后期的谈薪&#xff0c;所以面试前一定要做好充分的准备。 今天IC修真院为大家带来了数字IC后端的面试题目&#xff0c;希望大家能够用得上。 面试题目&…

javaEE -5(8000字详解多线程)

一&#xff1a;JUC(java.util.concurrent) 的常见类 1.1 ReentrantLock 可重入互斥锁. 和 synchronized 定位类似, 都是用来实现互斥效果, 保证线程安全&#xff0c;ReentrantLock 也是可重入锁. “Reentrant” 这个单词的原意就是 “可重入” ReentrantLock 的用法&#xf…

学习笔记二十四:K8S四层代理Service

K8S四层代理Service 四层负载均衡Service&#xff1a;概念、原理解读为什么要有ServiceService概述Service工作原理kubernetes集群中有三类IP地址 查看定义Service资源需要的字段有哪些Service的四种类型Service的端口 创建Service&#xff1a;type类型是ClusterIP创建Service&…

保护公司数据安全的措施

保护公司数据安全的措施 互联网时代&#xff0c;数据安全对企业而言是非常重要的&#xff0c;数据是每个组织的命脉&#xff0c;保护好数据安全企业才能稳定长久的发展下去&#xff0c;很多企业就是因为不重视数据安全防护&#xff0c;导致数据泄露从而遭受了很大的损失。因此…

Java入门讲解(1)---让你瞬间明白如何安装jdk

博主有话说&#xff1a;学习这个东西一定要持之以恒&#xff01;&#xff01;&#xff01;博主之前因为点事情半个月没学习&#xff0c;重新来过时&#xff0c;发现自己错过好多知识&#xff0c;正在一点一点往回补&#xff0c;博客也会陆续开始更新&#xff0c;希望大家多多支…

80.每日一练:移除元素(力扣)

问题描述 代码解决以及思想 解法一 class Solution { public:int removeElement(vector<int>& nums, int val) {int len 0; // 初始化一个用于记录非目标值个数的变量// 创建一个迭代器 it&#xff0c;指向 nums 的开头vector<int>::iterator it nums.beg…

【C语言】字符串+内存函数的介绍

&#x1f388;个人主页&#xff1a;.满船清梦压星河_-CSDN博客 &#x1f302;c/c领域新星创作者 &#x1f389;欢迎&#x1f44d;点赞✍评论❤️收藏 &#x1f61b;&#x1f61b;&#x1f61b;希望我的文章能对你有所帮助&#xff0c;有不足的地方还请各位看官多多指教&#xf…

【概率论教程01】对贝叶斯定理的追忆

一、说明 贝叶斯定理&#xff0c;是一个需要反复体悟的道理&#xff0c;不是说公式解释清除就算Grasp&#xff0c;而是需要反复在实际项目中发挥&#xff0c;才能算掌握了。而实际应用中&#xff0c;并不是简单给出条件就可以套用&#xff0c;而是隐藏在迷雾一样的事实中&#…

『C语言进阶』自定义类型详解

&#x1f525;博客主页&#xff1a; 小羊失眠啦. &#x1f516;系列专栏&#xff1a; C语言、Linux、Cpolar ❤️感谢大家点赞&#x1f44d;收藏⭐评论✍️ 文章目录 前言一、结构体1.1 结构体的基础知识1.2 结构体的声明1.3 结构体的特殊声明1.4 结构体的自引用1.5 结构体的初…

Unity开发过程中的一些小知识点

1、如何查询挂载了指定脚本的游戏物体 可以直接在Hierarchy面板上&#xff0c;搜索想要找的脚本名 2、如何将Unity生成的多个相同游戏物体获得序号 可以使用Unity的API Transform.GetSiblingIndex() 实现。 Transform.GetSiblingIndex()gameobject.idTransform.GetSiblingI…

PDF Reader Pro v2.9.8(pdf编辑阅读器)

PDF Reader Pro是一款PDF阅读和编辑软件&#xff0c;具有以下特点&#xff1a; 界面设计简洁&#xff0c;易于上手。软件界面直观清晰&#xff0c;用户可以轻松浏览文档&#xff0c;编辑注释和填写表单。功能强大&#xff0c;提供了多种PDF处理工具&#xff0c;包括阅读、注释…

高压放大器在扫描显微镜中的应用及优势是什么

随着科技的不断进步&#xff0c;扫描显微镜成为了现代显微镜技术的重要组成部分。它能够提供更高的分辨率、更广的视场和更强的功能&#xff0c;用于研究各种微观结构和材料的特性。而高压放大器在扫描显微镜系统中发挥着至关重要的作用。下面安泰电子Aigtek将详细介绍高压放大…