Linux Networking & TCP/IP for Developers · 课时

Kubernetes 中的服务发现与 DNS

了解容器如何互相发现:集群 DNS、Service 对象,以及使微服务能够通过名称寻址的解析流程。

第 4 / 4 课13 个步骤

Kubernetes 中的服务发现与 DNS 是 CoddyKit 上的免费 Linux Networking & TCP/IP for Developers 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Networking & TCP/IP for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Networking & TCP/IP for Developers 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

The Discovery Problem

Container IPs are ephemeral — pods restart and get new addresses constantly. Hardcoding IPs is impossible.

Service discovery lets workloads find each other by stable names instead of changing IPs.

The Service Abstraction

A Kubernetes Service provides a stable virtual IP (ClusterIP) and DNS name that load-balances across a set of pods selected by labels.

apiVersion: v1
kind: Service
metadata:
  name: orders
spec:
  selector:
    app: orders
  ports:
    - port: 80

Cluster DNS

Every cluster runs a DNS server (CoreDNS). It automatically creates records for each Service so workloads can resolve them by name.

The DNS Naming Scheme

Services follow a predictable FQDN pattern:

service.namespace.svc.cluster.local

Within the same namespace, just orders works. Across namespaces use orders.payments.

curl http://orders.payments.svc.cluster.local

How a Pod Resolves Names

Each pod's /etc/resolv.conf points at the cluster DNS and includes search domains, so short names expand to full Service FQDNs automatically.

cat /etc/resolv.conf

ClusterIP Load Balancing

A ClusterIP is virtual — kube-proxy programs iptables or IPVS rules so traffic to it is distributed across the healthy backing pods. Clients never see individual pod IPs.

Headless Services

Set clusterIP: None for a headless Service. DNS then returns the individual pod IPs directly, which is ideal for stateful sets and client-side load balancing.

spec:
  clusterIP: None
  selector:
    app: db

Endpoints and EndpointSlices

Behind each Service, Kubernetes maintains EndpointSlices listing the ready pod IPs. As pods come and go, these update so DNS and routing stay current.

kubectl get endpointslices

Resolving from Inside a Pod

You can debug discovery by running nslookup from a pod to verify a Service name resolves to its ClusterIP.

kubectl exec -it shell -- nslookup orders

External Name Services

An ExternalName Service maps a cluster DNS name to an outside hostname via a CNAME, letting you reference external systems with cluster-internal names.

spec:
  type: ExternalName
  externalName: api.example.com

Common Discovery Failures

When name resolution fails, check:

  • Label selector mismatch (no endpoints)
  • Wrong namespace in the FQDN
  • CoreDNS pods unhealthy
  • NetworkPolicy blocking DNS (port 53)

Quick Check

Test your service discovery knowledge.

Recap

You now understand container service discovery:

  • Services give stable ClusterIPs and DNS names
  • CoreDNS resolves service.namespace.svc.cluster.local
  • EndpointSlices track ready pods
  • Headless and ExternalName Services for special cases
  • Debug with nslookup from inside pods

This builds on your Docker network modes and Kubernetes networking basics.

免费开始

用 AI 导师学习 Linux Networking & TCP/IP for Developers — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「Kubernetes 中的服务发现与 DNS」课时是免费的吗?

是的 — 「Kubernetes 中的服务发现与 DNS」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Networking & TCP/IP for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Linux Networking & TCP/IP for Developers 课程共包含 4 节课。

「Kubernetes 中的服务发现与 DNS」这节课中我会学到什么?

了解容器如何互相发现:集群 DNS、Service 对象,以及使微服务能够通过名称寻址的解析流程。 你通过在浏览器中直接运行的动手代码来练习 Linux Networking & TCP/IP for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Linux Networking & TCP/IP for Developers 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Linux Networking & TCP/IP for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「Kubernetes 中的服务发现与 DNS」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Linux Networking & TCP/IP for Developers 课中编写并运行代码吗?

能。每节 Linux Networking & TCP/IP for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Docker 网络模式
  2. Kubernetes 网络基础
  3. 容器中的网络策略
  4. Kubernetes 中的服务发现与 DNS
← 返回 Linux Networking & TCP/IP for Developers