|
| 1 | +# Deploying the DNS Cluster Add-on |
| 2 | + |
| 3 | +Up to this point the cluster is fully functional except for the DNS resolution inside the pods, i.e accessing services exposed inside the cluster by pods. It also applies to DNS resolution to external hosts. |
| 4 | + |
| 5 | +## Deploy CoreDNS |
| 6 | + |
| 7 | +This yaml file contains the deployment of [CoreDNS](https://coredns.io/) along with some other kubernetes objects to connect with the cluster, such as `Role`, `RoleBinding`, `ConfigMap`. |
| 8 | + |
| 9 | +```shell |
| 10 | +kubectl apply -f https://storage.googleapis.com/kubernetes-the-hard-way/coredns-1.7.0.yaml |
| 11 | +serviceaccount/coredns created |
| 12 | +clusterrole.rbac.authorization.k8s.io/system:coredns created |
| 13 | +clusterrolebinding.rbac.authorization.k8s.io/system:coredns created |
| 14 | +configmap/coredns created |
| 15 | +deployment.apps/coredns created |
| 16 | +service/kube-dns created |
| 17 | +``` |
| 18 | + |
| 19 | +Wait a few seconds and the get the coredns pods |
| 20 | + |
| 21 | +```shell |
| 22 | +kubectl get pods -l k8s-app=kube-dns -n kube-system |
| 23 | +NAME READY STATUS RESTARTS AGE |
| 24 | +coredns-5677dc4cdb-l7qhl 1/1 Running 0 55s |
| 25 | +coredns-5677dc4cdb-tmnnr 1/1 Running 0 55s |
| 26 | +``` |
| 27 | + |
| 28 | +Edit the configuration map to include the forwarding to our external DNS. |
| 29 | + |
| 30 | +```shell |
| 31 | +kubectl edit -n kube-system configmaps coredns |
| 32 | +``` |
| 33 | + |
| 34 | +In this case my home router has the IP address `192.168.1.254`. Add the following line after the `kubernetes` block. |
| 35 | + |
| 36 | +`forward . 192.168.1.254` |
| 37 | + |
| 38 | +Optionally you can also add `log` to help in troubleshooting. |
| 39 | + |
| 40 | +It should read |
| 41 | + |
| 42 | +``` |
| 43 | +... |
| 44 | +Corefile: | |
| 45 | + .:53 { |
| 46 | + errors |
| 47 | + health |
| 48 | + ready |
| 49 | + kubernetes cluster.local in-addr.arpa ip6.arpa { |
| 50 | + pods insecure |
| 51 | + fallthrough in-addr.arpa ip6.arpa |
| 52 | + } |
| 53 | + prometheus :9153 |
| 54 | + cache 30 |
| 55 | + loop |
| 56 | + reload |
| 57 | + loadbalance |
| 58 | + log |
| 59 | + forward . 192.168.1.254 |
| 60 | + } |
| 61 | +... |
| 62 | +``` |
| 63 | + |
| 64 | +## Verification |
| 65 | + |
| 66 | +The original guide suggests to use `busybox` image, however I found many issues when trying it for the DNS resolution tests. Instead, I used a plain `Debian` and installed `dnsutils` package on it to achieve the same results. |
| 67 | + |
| 68 | +### Prepare the Test Pod |
| 69 | + |
| 70 | +```shell |
| 71 | +kubectl run debian --image=arm32v5/debian --command -- sleep 7200 |
| 72 | +pod/debian created |
| 73 | +``` |
| 74 | + |
| 75 | +```shell |
| 76 | +kubectl get pods -l run=debian -w |
| 77 | +NAME READY STATUS RESTARTS AGE |
| 78 | +debian 0/1 ContainerCreating 0 116s |
| 79 | +debian 1/1 Running 0 3m10s |
| 80 | +``` |
| 81 | + |
| 82 | +```shell |
| 83 | +kubectl exec debian -- apt update |
| 84 | +kubectl exec debian -- apt install -y dnsutils |
| 85 | +``` |
| 86 | + |
| 87 | +### Test Resolving `kubernetes` |
| 88 | + |
| 89 | +```shell |
| 90 | +kubectl exec debian -- nslookup kubernetes |
| 91 | +Server: 10.32.0.10 |
| 92 | +Address: 10.32.0.10#53 |
| 93 | + |
| 94 | +Name: kubernetes.default.svc.cluster.local |
| 95 | +Address: 10.32.0.1 |
| 96 | +``` |
| 97 | + |
| 98 | +### Test Resolving `nginx` Pod |
| 99 | + |
| 100 | +```shell |
| 101 | +kubectl create deployment nginx --image=arm32v5/nginx |
| 102 | +deployment.apps/nginx created |
| 103 | +``` |
| 104 | + |
| 105 | +```shell |
| 106 | +kubectl get pods -l app=nginx -w |
| 107 | +NAME READY STATUS RESTARTS AGE |
| 108 | +nginx-54cb54645d-88k7c 0/1 ContainerCreating 0 53s |
| 109 | +nginx-54cb54645d-88k7c 1/1 Running 0 76s |
| 110 | +``` |
| 111 | + |
| 112 | +Resolve nginx pod using short name `nginx` |
| 113 | + |
| 114 | +```shell |
| 115 | +kubectl exec debian -- nslookup nginx |
| 116 | +Server: 10.32.0.10 |
| 117 | +Address: 10.32.0.10#53 |
| 118 | + |
| 119 | +Name: nginx.default.svc.cluster.local |
| 120 | +Address: 10.32.0.110 |
| 121 | +``` |
| 122 | + |
| 123 | +Resolve nginx pod using long name `nginx.default.svc.cluster.local` |
| 124 | + |
| 125 | +```shell |
| 126 | +kubectl exec debian -- nslookup nginx.default.svc.cluster.local |
| 127 | +Server: 10.32.0.10 |
| 128 | +Address: 10.32.0.10#53 |
| 129 | + |
| 130 | +Name: nginx.default.svc.cluster.local |
| 131 | +Address: 10.32.0.110 |
| 132 | +``` |
0 commit comments