Pods can communicate with each other and with services via their IP addresses, once networking has been set up in a Kubernetes cluster. But it is easier to locate other pods and services by hostname, since IP addresses can change. In order to do this, the cluster requires a DNS service. In this learning activity, you will learn how to deploy *kube-dns* to a cluster in order to provide DNS service to pods.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Deploy kube-dns to the cluster.
You can deploy kube-dns to the cluster like so:
kubectl create -f https://storage.googleapis.com/kubernetes-the-hard-way/kube-dns.yaml
Verify that the kube-dns pod starts up correctly:
kubectl get pods -l k8s-app=kube-dns -n kube-system
You should get output showing the kube-dns pod. It should look something like this:
NAME READY STATUS RESTARTS AGE kube-dns-598d7bf7d4-spbmj 3/3 Running 0 36s
Make sure that 3/3 containers are ready and that the pod has a status of
Running
. It may take a moment for the pod to be fully up and running, so if READY is not 3/3 at first, check again after a few moments.- Test the DNS by creating a service, and perform a DNS lookup for service from another pod using the service name
Test the DNS by creating a service, and access the service from another pod using the service name.
Create a simple service like so:
kubectl run nginx --image=nginx
kubectl expose deployment nginx --port 80
Get a list of services:
kubectl get svc
You should see the
nginx
service listed.Spin up a busybox pod for testing and get the pod name:
kubectl run busybox --image=busybox:1.28 --command -- sleep 3600
POD_NAME=$(kubectl get pods -l run=busybox -o jsonpath="{.items[0].metadata.name}")
Perform a DNS lookup of the
nginx
service from within the busybox pod to verify that DNS is working:kubectl exec $POD_NAME -- nslookup nginx
You should get output that looks like this:
Server: 10.32.0.10 Address 1: 10.32.0.10 kube-dns.kube-system.svc.cluster.local Name: nginx Address 1: 10.32.0.248 nginx.default.svc.cluster.local