How do I use minikube's DNS?

11/21/2019

How do I use minikube's (cluster's) DNS? I want to receive all IP addresses associated with all pods for selected headless service? I don’t want to expose it outside the cluster. I am currently creating back-end layer.

As stated in the following answer: What exactly is a headless service, what does it do/accomplish, and what are some legitimate use cases for it?

„Instead of returning a single DNS A record, the DNS server will return multiple A records for the service, each pointing to the IP of an individual pod backing the service at that moment.”

Thus the pods in back-end layer can communicate to each other.

I can’t use dig command. It is not installed in minikube. Eventually how do I install it? There is no apt available.

I hope this explains more accurately what I want to achieve.

-- hal
dns
kubernetes
minikube

2 Answers

11/21/2019

As illustrated in kubernetes/minikube issue 4397

Containers don't have an IP address by default.
You'll want to use minikube service or minikube tunnel to get endpoint information.

See "hello-minikube/ Create a service":

By default, the Pod is only accessible by its internal IP address within the Kubernetes cluster.

To make the hello-node Container accessible from outside the Kubernetes virtual network, you have to expose the Pod as a Kubernetes Service.

Expose the Pod to the public internet using the kubectl expose command:

kubectl expose deployment hello-node --type=LoadBalancer --port=8080

The --type=LoadBalancer flag indicates that you want to expose your Service outside of the cluster.

View the Service you just created:

kubectl get services

NAME         TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
hello-node   LoadBalancer   10.108.144.78   <pending>     8080:30369/TCP   21s
kubernetes   ClusterIP      10.96.0.1       <none>        443/TCP      

On Minikube, the LoadBalancer type makes the Service accessible through the minikube service command.

Run the following command:

minikube service hello-node
-- VonC
Source: StackOverflow

11/22/2019

You mentioned that you want to receive IP addresses associated with pods for selected service name for testing how does headless service work.

For only testing purposes you can use port-forwarding. You can forward traffic from your local machine to dns pod in your cluster. To do this, you need to run:

kubectl port-forward svc/kube-dns -n kube-system 5353:53

and it will expose kubs-dns service on your host. Then all you need is to use dig command (or alternative) to query the dns server.

dig @127.0.0.1 -p 5353 +tcp +short <service>.<namespace>.svc.cluster.local

You can also test your dns from inside of cluster e.g. by running a pod with interactive shell:

kubectl run --image tutum/dnsutils dns -it --rm -- bash
root@dns:/# dig +search <service>

Let me know it it helped.

-- HelloWorld
Source: StackOverflow