CodexBloom - Programming Q&A Platform

Kubernetes Service Not Resolving DNS for Headless Services in Cluster v1.22.0

👀 Views: 89 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-16
kubernetes dns coredns yaml

I'm testing a new approach and I'm working on a personal project and Could someone explain I'm experiencing issues with DNS resolution for a headless service in my Kubernetes cluster running version v1.22.0... I've defined a headless service for my StatefulSet, but the pods want to resolve the DNS names of other pods associated with this service. Here's the YAML configuration for the headless service I created: ```yaml apiVersion: v1 kind: Service metadata: name: my-headless-service spec: clusterIP: None ports: - port: 80 name: http selector: app: my-app ``` I have verified that the pods are indeed being created and labeled properly. The StatefulSet YAML is as follows: ```yaml apiVersion: apps/v1 kind: StatefulSet metadata: name: my-statefulset spec: serviceName: "my-headless-service" replicas: 3 selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: containers: - name: my-container image: my-image:latest ports: - containerPort: 80 ``` When I try to ping one of the pods using its DNS name, like `my-headless-service-0.my-headless-service.default.svc.cluster.local`, I get the following behavior message: ``` ping: my-headless-service-0.my-headless-service.default.svc.cluster.local: Name does not resolve ``` I've checked the CoreDNS logs and they appear to be functioning correctly, with no obvious errors. I also confirmed that CoreDNS is running and healthy in the kube-system namespace. Furthermore, I attempted to resolve the DNS manually using `nslookup`, which produced the same failure. I've tried flushing the DNS cache on my pods using `kubectl exec` and restarting CoreDNS, but nothing seems to change. Has anyone encountered a similar scenario with headless services and can provide insight on what might be going wrong or how to troubleshoot further? Could someone point me to the right documentation? This is for a web app running on Ubuntu 20.04. Thanks in advance! I've been using Yaml for about a year now.