2017-08-29 00:19:25 +03:00
# Deploying the DNS Cluster Add-on
2018-09-30 22:35:05 +03:00
In this lab you will deploy the [DNS add-on ](https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/ ) which provides DNS based service discovery, backed by [CoreDNS ](https://coredns.io/ ), to applications running inside the Kubernetes cluster.
2017-08-29 00:19:25 +03:00
## The DNS Cluster Add-on
2020-06-27 21:57:27 +03:00
Get the CoreDNS yaml:
```bash
wget https://storage.googleapis.com/kubernetes-the-hard-way/coredns.yaml
```
Edit the `coredns.yaml` file to change CoreDNS configuration to enable DNS resolution for external name:
```bash
sed '/.*prometheus :9153/a \ \ \ \ \ \ \ \ forward . /etc/resolv.conf' coredns.yaml
```
2018-09-30 22:35:05 +03:00
Deploy the `coredns` cluster add-on:
2017-08-29 00:19:25 +03:00
2020-06-20 10:24:03 +03:00
```bash
2020-06-27 21:57:27 +03:00
kubectl apply -f coredns.yaml
2017-08-29 00:19:25 +03:00
```
2020-06-20 16:46:33 +03:00
> Output:
2017-08-29 00:19:25 +03:00
2020-06-20 10:24:03 +03:00
```bash
2018-09-30 22:35:05 +03:00
serviceaccount/coredns created
clusterrole.rbac.authorization.k8s.io/system:coredns created
clusterrolebinding.rbac.authorization.k8s.io/system:coredns created
configmap/coredns created
deployment.extensions/coredns created
service/kube-dns created
2017-08-29 00:19:25 +03:00
```
List the pods created by the `kube-dns` deployment:
2020-06-20 10:24:03 +03:00
```bash
2017-08-29 00:19:25 +03:00
kubectl get pods -l k8s-app=kube-dns -n kube-system
```
2020-06-22 13:42:19 +03:00
> Output (you may need to wait a few seconds to see the pods "READY"):
2017-08-29 00:19:25 +03:00
2020-06-20 10:24:03 +03:00
```bash
2018-09-30 22:35:05 +03:00
NAME READY STATUS RESTARTS AGE
coredns-699f8ddd77-94qv9 1/1 Running 0 20s
coredns-699f8ddd77-gtcgb 1/1 Running 0 20s
2017-08-29 00:19:25 +03:00
```
## Verification
Create a `busybox` deployment:
2020-06-20 10:24:03 +03:00
```bash
2019-09-14 21:41:56 +03:00
kubectl run --generator=run-pod/v1 busybox --image=busybox:1.28 --command -- sleep 3600
2017-08-29 00:19:25 +03:00
```
List the pod created by the `busybox` deployment:
2020-06-20 10:24:03 +03:00
```bash
2017-08-29 00:19:25 +03:00
kubectl get pods -l run=busybox
```
2020-06-22 13:42:19 +03:00
> Output (you may need to wait a few seconds to see the pod "READY"):
2017-08-29 00:19:25 +03:00
2020-06-20 10:24:03 +03:00
```bash
2019-09-14 21:41:56 +03:00
NAME READY STATUS RESTARTS AGE
busybox 1/1 Running 0 3s
2017-08-29 00:19:25 +03:00
```
Retrieve the full name of the `busybox` pod:
2020-06-20 10:24:03 +03:00
```bash
2017-08-29 00:19:25 +03:00
POD_NAME=$(kubectl get pods -l run=busybox -o jsonpath="{.items[0].metadata.name}")
```
Execute a DNS lookup for the `kubernetes` service inside the `busybox` pod:
2020-06-20 10:24:03 +03:00
```bash
2017-08-29 00:19:25 +03:00
kubectl exec -ti $POD_NAME -- nslookup kubernetes
```
2020-06-20 16:46:49 +03:00
> Output:
2017-08-29 00:19:25 +03:00
2020-06-20 10:24:03 +03:00
```bash
2017-08-29 00:19:25 +03:00
Server: 10.32.0.10
Address 1: 10.32.0.10 kube-dns.kube-system.svc.cluster.local
Name: kubernetes
Address 1: 10.32.0.1 kubernetes.default.svc.cluster.local
```
Next: [Smoke Test ](13-smoke-test.md )