kubernetes / minikube

Run Kubernetes locally
https://minikube.sigs.k8s.io/
Apache License 2.0
29.55k stars 4.89k forks source link

Name resolution isn't working #1274

Closed gheibia closed 7 years ago

gheibia commented 7 years ago

This a BUG REPORT.

Minikube version: v0.17.1 Environment:

What happened: After creating a service, from within cluster I can access it by its VIP, but not its name.

$ kubectl get svc
NAME                  CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
userdeployment   10.0.0.14        <nodes>       8090:30061/TCP        9h
$
$ curl -v -H "Content-Type: application/json" http://10.0.0.14:8090/an_api
< HTTP/1.1 200
$ curl -v -H "Content-Type: application/json" http:////userdeployment:8090/an_api
* Couldn't resolve host 'userdeployment'

What you expected to happen: To be able to access the service through its name. How to reproduce it: Use the following Yaml file to create a deployment and its service, ssh into the cluster and hit the service by its name.

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: userdeployment
spec:
  template: # create pods using pod definition in this template
    metadata:
      labels:
        app: usermanagement
    spec:
      containers:
      - name: usr
        image: 192.168.42.22:80/usermanagement:latest
        args: ["-port", "8090", "-configfile", "/var/containerdata/config", "-log_dir", "/var/containerdata/logs"]
        ports:
        - containerPort: 8090
        volumeMounts:
        - mountPath: /var/containerdata
          name: vlm
      volumes:
      - name: vlm
        hostPath:
          path: '/data/vlm'
---
apiVersion: v1
kind: Service
metadata:
  name: userdeployment
spec:
  selector:
    app: usermanagement
  ports:
  - protocol: TCP
    port: 8090
    nodePort: 30061
  type: NodePort

Anything else do we need to know: The image in that file is being pulled from a private registry. I can't upload it elsewhere, but this is how I build it.

CGO_ENABLED=0 GOOS=linux go build private.git.repo/services/user_management/
docker build -t usermanagement:latest -f dcokerfile .

and the dockerfile is:

FROM scratch
ADD user_management /
ENTRYPOINT ["/user_management"] 

the image then is tagged and pushed to the private registry.

gheibia commented 7 years ago

Testing this with a busybox image has the same problem.

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: bbd
spec:
  template: # create pods using pod definition in this template
    metadata:
      labels:
        app: bba
    spec:
      containers:
      - name: bb
        image: busybox:latest
        command:
        - sleep
        - "3600"
---
apiVersion: v1
kind: Service
metadata:
  name: bbs
spec:
  selector:
    app: bba
  ports:
  - protocol: TCP
    port: 80
    nodePort: 30067
  type: NodePort

Confirming the creation went ok:

$ kubectl get po
NAME                                 READY     STATUS    RESTARTS   AGE
bbd-318937977-c6sxg                  1/1       Running   0          7m

$ kubectl get svc
NAME                CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
bbs                 10.0.0.133   <nodes>       80:30067/TCP     7m

$ kubectl get ep
NAME                ENDPOINTS              AGE
bbs                 172.17.0.8:80          7m

And making sure the DNS is up and running:

$ kubectl get rc --namespace=kube-system
NAME                   DESIRED   CURRENT   READY     AGE
kube-dns-v20           1         1         1         14h
kubernetes-dashboard   1         1         1         14h

Now testing:

$ minikube ssh
$ curl -v http://10.0.0.133
> GET / HTTP/1.1
> Host: 10.0.0.133
> User-Agent: curl/7.50.1
> Accept: */*
> 
< HTTP/1.0 404 Not Found
< Content-type: text/html
< Date: Wed, 22 Mar 2017 20:12:52 GMT
< Connection: close
< 
<HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD>
<BODY><H1>404 Not Found</H1>
The requested URL was not found
</BODY></HTML>

$ curl -v http://bbs       
* Couldn't resolve host 'bbs'
curl: (6) Couldn't resolve host 'bbs'
$ 
gtirloni commented 7 years ago

Could you try running kubectl exec <pod> -- nslookup bbs and see if resolution works inside the pod?

gheibia commented 7 years ago

It does:

$ kubectl exec bbd-318937977-c6sxg -- nslookup bbs
Server:    10.0.0.10
Address 1: 10.0.0.10 kube-dns.kube-system.svc.cluster.local

Name:      bbs
Address 1: 10.0.0.133 bbs.default.svc.cluster.local
r2d4 commented 7 years ago

You won't be able to resolve services through their cluster VIPs. Instead you will only be able to access the services through the node ports in minikube if you're outside the cluster.

In this case, you could either do

curl $(minikube service userdeployment --url) or curl $(minikube ip):30061

which are both equivalent.

r2d4 commented 7 years ago

Some users have been using https://docs.traefik.io/user-guide/kubernetes/ to manage name resolution, or other options are discussed #38

gheibia commented 7 years ago

Thanks. So what you're saying is that I can either use the NodePort from outside or use service name from inside pods, right?

r2d4 commented 7 years ago

Thats correct

gheibia commented 7 years ago

Thanks! Closing since this isn't a bug, but a misunderstanding.