Access pods in minikube cluster via service name does not work

12/18/2018

I got the following service defined:

apiVersion: v1
kind: Service
metadata:
  name: customerservice
spec:
  type: NodePort
  selector:
    app: customerapp
  ports:
  - protocol: TCP
    port: 31004
    nodePort: 31004
    targetPort: 8080

Current situation: I am able to hit the pod via the service IP. Now my goal is to reach the customerservice via the name of the service, which does not work right now. So I would simply type http://customerservice:31004 instead of http://<IP>:31004.

-- elp
kubernetes

2 Answers

12/18/2018

Normal Services are assigned a DNS A record for a name of the form my-svc.my-namespace.svc.cluster.local. This resolves to the cluster IP of the Service. This DNS entry is present inside the kubernetes cluster only and hence you're able to access the service by name from inside the kubernetes pod.

Now, if you want to access your kubernetes service by name from one of the node you need to modify the /etc/resolve.conf of your node with <svc_name>.<namespace>.svc.cluster.local, please have a look at following /etc/resolve.conf

search ec2.internal default.svc.cluster.local svc.cluster.local cluster.local
nameserver 10.96.0.10

nameserver is clusterIP of kube-dns service, you can find it using kubectl get svc kube-dns -n kube-system

Now you will be able to curl your service as curl ui.default.svc.cluster.local:80

-- Prafull Ladha
Source: StackOverflow

12/18/2018

DNS resolution of services is ONLY available within the cluster, provided by CoreDNS/KubeDNS.

Should you wish to have access to this locally on your machine, you'd need to use another tool. One such tool is kubefwd:

https://github.com/txn2/kubefwd

A slightly simpler solution, is to use port-forward; which is a very simple way to access a single service locally.

kubectl port-forward --namespace=whatever svs/service-name port

EDIT:// I've made the assumption that you want to use the service DNS locally, as I'm assuming by saying:

I would simply type http://customerservice:31004

is in the context of your web browser.

-- Rawkode
Source: StackOverflow