This guide provides common tasks and exercises to understand and work with CoreDNS, the default DNS provider in Kubernetes. It now includes an example of creating an ExternalName Service.
- Understand how CoreDNS works in Kubernetes.
- Test DNS resolution for Services and Pods.
- Configure and troubleshoot CoreDNS.
- Resolve external DNS using ExternalName Services.
- A running Kubernetes cluster (e.g., Minikube, K3s, or a cloud provider).
kubectl
is installed and configured.
-
List CoreDNS Pods:
kubectl get pods -n kube-system -l k8s-app=kube-dns
-
Check CoreDNS logs:
kubectl logs -n kube-system -l k8s-app=kube-dns
-
Describe the CoreDNS Deployment:
kubectl describe deployment -n kube-system coredns
-
Deploy a Service: Save the following YAML as
my-service.yaml
:apiVersion: v1 kind: Service metadata: name: my-service spec: selector: app: my-app ports: - protocol: TCP port: 80 targetPort: 80
Apply the YAML:
kubectl apply -f my-service.yaml
-
Deploy a Pod: Save the following YAML as
test-pod.yaml
:apiVersion: v1 kind: Pod metadata: name: test-pod spec: containers: - name: test-container image: busybox command: ["sleep", "3600"]
Apply the YAML:
kubectl apply -f test-pod.yaml
-
Test DNS resolution from the Pod:
kubectl exec -it test-pod -- nslookup my-service kubectl exec -it test-pod -- ping my-service
-
Create a new namespace and deploy a Service:
kubectl create namespace test-ns kubectl apply -f my-service.yaml -n test-ns
-
Query the Service from the
default
namespace:kubectl exec -it test-pod -- nslookup my-service.test-ns.svc.cluster.local
-
Edit the CoreDNS ConfigMap:
kubectl edit configmap -n kube-system coredns
-
Add the following block to enable external domain forwarding:
forward . 8.8.8.8
-
Save the ConfigMap and restart CoreDNS Pods:
kubectl rollout restart deployment -n kube-system coredns
-
Test external domain resolution:
kubectl exec -it test-pod -- nslookup example.com
-
Create an ExternalName Service to resolve
example.com
: Save the following YAML asexternal-service.yaml
:apiVersion: v1 kind: Service metadata: name: external-service spec: type: ExternalName externalName: example.com
Apply the YAML:
kubectl apply -f external-service.yaml
-
Test the ExternalName Service:
kubectl exec -it test-pod -- nslookup external-service kubectl exec -it test-pod -- curl external-service
-
Verify that the Service resolves to the external domain
example.com
.
-
Check the DNS Policy of a Pod:
kubectl get pod test-pod -o yaml | grep dnsPolicy
-
Test DNS resolution using
dig
(if installed):kubectl exec -it test-pod -- dig my-service
-
Check CoreDNS metrics (if enabled):
kubectl port-forward -n kube-system svc/coredns 9153:9153 curl http://localhost:9153/metrics
To remove all resources created during the tasks:
kubectl delete -f my-service.yaml
kubectl delete -f test-pod.yaml
kubectl delete -f external-service.yaml
kubectl delete namespace test-ns
- CoreDNS resolves internal Kubernetes Services using DNS names.
- ExternalName Services map Kubernetes Service names to external DNS domains.
- Cross-namespace resolution requires fully qualified domain names.
- External DNS resolution can be configured in the CoreDNS ConfigMap.