什么是无头服务,它做什么/完成什么,它有哪些合法用例? [英] What is a headless service, what does it do/accomplish, and what are some legitimate use cases for it?

查看:19
本文介绍了什么是无头服务,它做什么/完成什么,它有哪些合法用例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了一些关于 Kubernetes 的书籍以及 文档中关于无头服务的页面.但我仍然不确定它实际上做了什么以及为什么有人会使用它.有没有人对它有很好的理解,它的作用是什么,为什么有人会使用它?

I've read a couple of passages from some books written on Kubernetes as well as the page on headless services in the docs. But I'm still unsure what it really actually does and why someone would use it. Does anyone have a good understanding of it, what it accomplishes, and why someone would use it?

推荐答案

好吧,我认为您需要一些理论.整个互联网上有很多解释(包括官方文档),但我认为 Marco Luksa 做得最好:

Well, I think you need some theory. There are many explanations (including the official docs) across the whole internet, but I think Marco Luksa did it the best:

服务的每个连接都转发给随机选择的一个支持豆荚.但是如果客户端需要连接到所有这些呢?豆荚?如果支持 Pod 本身需要每个连接到所有其他支持豆荚.通过服务连接显然不是这样做的方法.什么是?

Each connection to the service is forwarded to one randomly selected backing pod. But what if the client needs to connect to all of those pods? What if the backing pods themselves need to each connect to all the other backing pods. Connecting through the service clearly isn’t the way to do this. What is?

一个客户端要连接到所有的 Pod,它需要弄清楚 IP每个单独的豆荚.一种选择是让客户端调用Kubernetes API 服务器并获取 pod 列表及其 IP 地址通过 API 调用,但因为您应该始终努力保持您的应用与 Kubernetes 无关,使用 API 服务器并不理想

For a client to connect to all pods, it needs to figure out the the IP of each individual pod. One option is to have the client call the Kubernetes API server and get the list of pods and their IP addresses through an API call, but because you should always strive to keep your apps Kubernetes-agnostic, using the API server isn’t ideal

幸运的是,Kubernetes 允许客户端通过 DNS 发现 pod IP查找.通常,当您对服务执行 DNS 查找时,DNS服务器返回单个 IP——服务的集群 IP.但是如果你告诉Kubernetes 你的服务不需要集群 IP(你这样做通过在服务规范中将 clusterIP 字段设置为 None ),DNS 服务器将返回 pod IP 而不是单个服务知识产权.DNS 服务器不会返回单个 DNS A 记录,而是将为服务返回多条A记录,每条记录指向的IP在那一刻支持服务的单个 pod.客户可以因此做一个简单的 DNS A 记录查找并获取所有的 IP属于服务的 pod.然后客户端可以使用它信息以连接到其中一个、多个或全部.

Luckily, Kubernetes allows clients to discover pod IPs through DNS lookups. Usually, when you perform a DNS lookup for a service, the DNS server returns a single IP — the service’s cluster IP. But if you tell Kubernetes you don’t need a cluster IP for your service (you do this by setting the clusterIP field to None in the service specification ), the DNS server will return the pod IPs instead of the single service IP. Instead of returning a single DNS A record, the DNS server will return multiple A records for the service, each pointing to the IP of an individual pod backing the service at that moment. Clients can therefore do a simple DNS A record lookup and get the IPs of all the pods that are part of the service. The client can then use that information to connect to one, many, or all of them.

将服务规范中的 clusterIP 字段设置为 None 会使无头服务,因为 Kubernetes 不会通过以下方式为其分配集群 IP哪些客户端可以连接到支持它的 Pod.

Setting the clusterIP field in a service spec to None makes the service headless, as Kubernetes won’t assign it a cluster IP through which clients could connect to the pods backing it.

Marco Luksa 的Kubernetes in Action"

"Kubernetes in Action" by Marco Luksa

这篇关于什么是无头服务,它做什么/完成什么,它有哪些合法用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆