Service Discovery und DNS in K8s
Erkunden Sie, wie Kubernetes Service Discovery und DNS-Auflösung für die Kommunikation zwischen Services verwaltet.
Service Discovery und DNS in K8s ist eine kostenlose Docker & Kubernetes for Developers-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Docker & Kubernetes for Developers-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Docker & Kubernetes for Developers-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Intro to Service Discovery
Welcome to Service Discovery & DNS in K8s! In a dynamic Kubernetes cluster, Pods are constantly created, destroyed, and moved, leading to ever-changing IP addresses.
How do applications running in one Pod find and communicate with applications in another Pod or Service?
This is where Service Discovery comes in. It's how services find each other without needing to know their specific, ephemeral IP addresses.
The Role of DNS in K8s
You might already know about DNS (Domain Name System) on the internet. It translates human-readable names like google.com into IP addresses.
Kubernetes uses a similar concept internally. Instead of relying on unstable Pod IPs, K8s assigns stable DNS names to its Services.
This allows your applications to connect to other services using simple, consistent names.
CoreDNS: The K8s DNS Server
Every Kubernetes cluster comes with its own DNS server, typically CoreDNS (or sometimes kube-dns in older versions).
CoreDNS runs as a Pod within your cluster and is responsible for resolving all internal Kubernetes service names to their corresponding cluster IP addresses.
It's automatically configured for you when you set up your cluster.
How Pods Get DNS Config
When a Pod starts, Kubernetes automatically injects DNS configuration into it.
- Each Pod's
/etc/resolv.conffile is updated to point to the cluster's CoreDNS Service IP. - It also includes search paths, allowing you to use shorter service names.
This means any application inside a Pod can immediately use the cluster's DNS for name resolution.
Service DNS Names
When you create a Kubernetes Service (e.g., a ClusterIP Service), CoreDNS automatically creates a DNS record for it.
The most common format for a Service's DNS name within its own namespace is simply: <service-name>.
For example, a service named my-web-app in the default namespace can be reached by other Pods in the same namespace using just my-web-app.
Example: Same-Namespace Access
Let's say you have a deployment hello-app and a service hello-service in the default namespace. Any other Pod in default can reach it via hello-service.
Here's a simple example of a Deployment and Service:
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-app
spec:
selector:
matchLabels:
app: hello
replicas: 1
template:
metadata:
labels:
app: hello
spec:
containers:
- name: hello-container
image: busybox
command: ["sh", "-c", "while true; do echo Hello K8s; sleep 5; done"]
---
apiVersion: v1
kind: Service
metadata:
name: hello-service
spec:
selector:
app: hello
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIPTesting Same-Namespace DNS
After applying the YAML from the previous scene, you can test service discovery. First, create a temporary Pod (e.g., debug-pod) in the same namespace.
Then, exec into debug-pod and try to ping the service:
kubectl run debug-pod --image=busybox --restart=Never --rm -it --command -- sh- Inside the pod:
ping hello-service
You should see the hello-service ClusterIP being resolved!
Cross-Namespace DNS (FQDN)
What if your services are in different namespaces? Kubernetes uses a Fully Qualified Domain Name (FQDN) format:
<service-name>.<namespace-name>.svc.cluster.local
For convenience, if the Pod's namespace is in its DNS search path (which it usually is), you can often use a shorter form:
<service-name>.<namespace-name>
This allows clear and unambiguous communication across different parts of your application.
Example: Cross-Namespace Access
Let's imagine a backend-service in the prod namespace. From a Pod in the default namespace, you'd access it like this:
ping backend-service.prodKubernetes DNS handles the resolution, directing traffic to the correct service, even across namespaces.
This provides strong isolation while still enabling communication where needed.
Quick Check
Which of the following statements are TRUE regarding Kubernetes Service Discovery and DNS?
Recap: Service Discovery & DNS
Great job! In this lesson, you learned how Kubernetes handles service discovery and DNS resolution for internal communication:
- Pods have ephemeral IPs, requiring a stable discovery mechanism.
- CoreDNS is the cluster's internal DNS server.
- Services get stable DNS names, resolvable by other Pods.
- Pods are automatically configured to use the cluster's DNS.
- You can access services in the same namespace by name or in other namespaces using FQDNs.
This powerful system ensures your applications can always find each other reliably!
Lerne Docker & Kubernetes for Developers mit einem KI-Tutor — kostenlos
Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.
- Kurse
- 12
- Lektionen
- 48
Häufig gestellte Fragen
Ist die Lektion „Service Discovery und DNS in K8s“ kostenlos?
Ja — der vollständige Text von „Service Discovery und DNS in K8s“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Docker & Kubernetes for Developers-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Docker & Kubernetes for Developers-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Service Discovery und DNS in K8s“?
Erkunden Sie, wie Kubernetes Service Discovery und DNS-Auflösung für die Kommunikation zwischen Services verwaltet. Du übst Docker & Kubernetes for Developers mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Docker & Kubernetes for Developers zu starten?
Keine Vorkenntnisse erforderlich. Docker & Kubernetes for Developers auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.
Wie lange dauert die Lektion „Service Discovery und DNS in K8s“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Docker & Kubernetes for Developers-Lektion Code schreiben und ausführen?
Ja. Jede Docker & Kubernetes for Developers-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Kubernetes-Ingress und Routing
- Netzwerkrichtlinien implementieren
- Service Discovery und DNS in K8s
- TLS-Terminierung und Ingress mit HTTPS absichern