traefik use label data of service container in compose yaml to route

news/2025/1/7 5:51:49/文章来源:https://www.cnblogs.com/lightsong/p/18652974

example

https://doc.traefik.io/traefik/user-guides/docker-compose/basic-example/

traefik通过docker engine访问其它容器的label信息,来做路由。

version: "3.3"services:traefik:image: "traefik:v3.2"container_name: "traefik"command:#- "--log.level=DEBUG"- "--api.insecure=true"- "--providers.docker=true"- "--providers.docker.exposedbydefault=false"- "--entryPoints.web.address=:80"ports:- "80:80"- "8080:8080"
    volumes:- "/var/run/docker.sock:/var/run/docker.sock:ro"whoami:image: "traefik/whoami"container_name: "simple-service"labels:- "traefik.enable=true"- "traefik.http.routers.whoami.rule=Host(`whoami.localhost`)"- "traefik.http.routers.whoami.entrypoints=web"

 

Fourth, you allow Traefik to gather configuration from Docker:


traefik:
  command:# Enabling Docker provider
    - "--providers.docker=true"# Do not expose containers unless explicitly told so
    - "--providers.docker.exposedbydefault=false"
  volumes:
    - "/var/run/docker.sock:/var/run/docker.sock:ro"whoami:
  labels:# Explicitly tell Traefik to expose this container
    - "traefik.enable=true"# The domain the service will respond to
    - "traefik.http.routers.whoami.rule=Host(`whoami.localhost`)"# Allow request only from the predefined entry point named "web"
    - "traefik.http.routers.whoami.entrypoints=web"

 

Routing Configuration with Labels

https://doc.traefik.io/traefik/providers/docker/#routing-configuration-with-labels

By default, Traefik watches for container level labels on a standalone Docker Engine.

When using Docker Compose, labels are specified by the directive labels from the "services" objects.

 

Docker object labels

https://docs.docker.com/engine/manage-resources/labels/

Labels are a mechanism for applying metadata to Docker objects, including:

  • Images
  • Containers
  • Local daemons
  • Volumes
  • Networks
  • Swarm nodes
  • Swarm services

You can use labels to organize your images, record licensing information, annotate relationships between containers, volumes, and networks, or in any way that makes sense for your business or application.

Label keys and values

A label is a key-value pair, stored as a string. You can specify multiple labels for an object, but each key must be unique within an object. If the same key is given multiple values, the most-recently-written value overwrites all previous values.

Key format recommendations

A label key is the left-hand side of the key-value pair. Keys are alphanumeric strings which may contain periods (.), underscores (_), slashes (/), and hyphens (-). Most Docker users use images created by other organizations, and the following guidelines help to prevent inadvertent duplication of labels across objects, especially if you plan to use labels as a mechanism for automation.

  • Authors of third-party tools should prefix each label key with the reverse DNS notation of a domain they own, such as com.example.some-label.

  • Don't use a domain in your label key without the domain owner's permission.

  • The com.docker.*, io.docker.*, and org.dockerproject.* namespaces are reserved by Docker for internal use.

  • Label keys should begin and end with a lower-case letter and should only contain lower-case alphanumeric characters, the period character (.), and the hyphen character (-). Consecutive periods or hyphens aren't allowed.

  • The period character (.) separates namespace "fields". Label keys without namespaces are reserved for CLI use, allowing users of the CLI to interactively label Docker objects using shorter typing-friendly strings.

These guidelines aren't currently enforced and additional guidelines may apply to specific use cases.

Value guidelines

Label values can contain any data type that can be represented as a string, including (but not limited to) JSON, XML, CSV, or YAML. The only requirement is that the value be serialized to a string first, using a mechanism specific to the type of structure. For instance, to serialize JSON into a string, you might use the JSON.stringify() JavaScript method.

Since Docker doesn't deserialize the value, you can't treat a JSON or XML document as a nested structure when querying or filtering by label value unless you build this functionality into third-party tooling.

Manage labels on objects

Each type of object with support for labels has mechanisms for adding and managing them and using them as they relate to that type of object. These links provide a good place to start learning about how you can use labels in your Docker deployments.

Labels on images, containers, local daemons, volumes, and networks are static for the lifetime of the object. To change these labels you must recreate the object. Labels on Swarm nodes and services can be updated dynamically.

  • Images and containers

    • Adding labels to images
    • Overriding a container's labels at runtime
    • Inspecting labels on images or containers
    • Filtering images by label
    • Filtering containers by label
  • Local Docker daemons

    • Adding labels to a Docker daemon at runtime
    • Inspecting a Docker daemon's labels
  • Volumes

    • Adding labels to volumes
    • Inspecting a volume's labels
    • Filtering volumes by label
  • Networks

    • Adding labels to a network
    • Inspecting a network's labels
    • Filtering networks by label
  • Swarm nodes

    • Adding or updating a Swarm node's labels
    • Inspecting a Swarm node's labels
    • Filtering Swarm nodes by label
  • Swarm services

    • Adding labels when creating a Swarm service
    • Updating a Swarm service's labels
    • Inspecting a Swarm service's labels
    • Filtering Swarm services by label

 

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

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

相关文章

2025年正在重塑行业的10款AI代理工具

序言:本文的作者列出来的这10款AI代理工具是您认可的吗? 作为一名深入AI开发领域超过十年的开发者,我见过无数工具声称要颠覆我们构建AI代理的方式。有些工具确实实现了夸下的海口——但更多的则没有。 经过几个月的亲身测试以及与同行开发者的讨论,我整理出了一份2025年真…

[cause]: TypeError: e_.createContext is not a function (Next.js 15)

开发 Next.js 项目遇到报错: [cause]: TypeError: e_.createContext is not a function 出现这个报错的原因是在 Next.js 项目中,在 Server Component 中使用了MUI组件,但是MUI组件没有做 SSR 适配就会导致这个报错。 解决办法 解决办法就是在文件顶部添加 use client 声明…

golang自带的死锁检测并非银弹

网上总是能看到有人说go自带了死锁检测,只要有死锁发生runtime就能检测到并及时报错退出,因此go不会被死锁问题困扰。 这说明了口口相传知识的有效性是日常值得怀疑的,同时也再一次证明了没有银弹这句话的含金量。 这个说法的杀伤力在于它虽然不对,但也不是全错,真真假假很…

2025多校冲刺省选模拟赛2

2025多校冲刺省选模拟赛2\(T1\) A. aw \(10pts/20pts\)部分分\(10 \sim 20pts\) :枚举每一种定向方案,略带卡常。点击查看代码 const int p=998244353; struct node {int nxt,to; }e[200010]; int head[100010],dis[1010][1010],a[100010],b[100010],g[2][100010],cnt=0; b…

jamovi 2.6 (Linux, macOS, Windows) - 统计软件

jamovi 2.6 (Linux, macOS, Windows) - 统计软件jamovi 2.6 (Linux, macOS, Windows) - 统计软件 open statistical software 请访问原文直链:https://sysin.org/blog/jamovi/ 查看最新版。原创作品,转载请保留出处。 作者主页:sysin.orgjamovi适用于桌面和云的开放式统计软…

读数据保护:工作负载的可恢复性26商用数据备份方案

商用数据备份方案1. 备份简史 1.1. 20世纪80年代中期大家都还没有意识到,运行着商用UNIX操作系统的大型工作环境里,应该配备一款商用的备份软件或某种自动的磁带系统 1.2. 1993年备份工作全都是通过shell脚本与cron job形式的计划任务来实现的1.2.1. 脚本总是假定服务器中需要…

OpenCV和OpenVX有什么联系和区别

OpenCV和OpenVX有什么联系和区别 联系和区别是:OpenCV是一个基于Apache2.0许可(开源)发行的跨平台计算机视觉和机器学习软件库。OpenVX 实现了跨平台加速处理,OpenVX在嵌入式和实时性系统中,可以更好地发挥它的优势,在某些场合配合OpenCV的强大功能,可以实现更好的效果。…

SPIR-V生态系统概述

SPIR-V生态系统SPIR-V生态系统,如图1-42所示。图1-42 SPIR-V生态系统 1.4.2 OpenVX路线图OpenVX路线图,如图1-43所示。图1-43 OpenVX路线图 OpenVX跨供应商视觉与推理 基于图形的高级抽象实现可移植、高效的视觉处理。 1)处理器供应商创建、优化和发布优化OpenVX的驱动程序。…

4本书推荐《智能汽车传感器:原理设计应用》、《AI芯片开发核心技术详解》、《TVM编译器原理与实践》、《LLVM编译器原理与实践》

4本书推荐《AI芯片开发核心技术详解》、《智能汽车传感器:原理设计应用》、《TVM编译器原理与实践》、《LLVM编译器原理与实践》由清华大学出版社资深编辑赵佳霓老师策划编辑的新书《AI芯片开发核心技术详解》已经出版,京东、淘宝天猫、当当等网上,相应陆陆续续可以购买。该…

Easysearch 可搜索快照功能,看这篇就够了

可搜索快照功能改变了我们对备份数据的查询方式。以往要查询备份数据时,要先找到备份数据所在的快照,然后在一个合适的环境中恢复快照,最后再发起请求查询数据。这个处理路径很长,而且很消耗时间。可搜索快照功能将大大简化该处理路径,节约时间。 角色设置 相信你对节点角…

基本共射极放大电路的分析

静态分析利用直流通路求Q点(静态工作点)\[I_{BQ}=\frac{V_{BB}-V_{BEQ}}{R_{b}} \]一般硅管\(V_{BE}=0.7V\),锗管\(V_{BE}=0.2V\),\(\beta\)已知 \[I_{CQ}=\beta I_{BQ} \]\[V_{CEQ}=(\frac{V_{CC}-I_{CQ}}{R_{c}}-I_{CQ})R_L \]动态分析交流通路分析画小信号等效模型\[r_{…

使用扣子实现营销获客套电机器人-工作流+多维表格+飞书机器人

V+: llike620 就是利用扣子的工作流,实现简单的获取线索机器人,然后对接在抖音音私信上 主要用于某汽车贴膜产品的获客,先获取车型,再获取联系方式增加了状态机制,不能让对方跳过业务流程新线索存入飞书多维表格,并通过飞书机器人进行通知 十年开发经验程序员,离职全心…