120

I have a local kubernetes cluster on my local docker desktop.

This is how my kubernetes service looks like when I do a kubectl describe service

Name:              helloworldsvc
Namespace:         test
Labels:            app=helloworldsvc
Annotations:       kubectl.kubernetes.io/last-applied-configuration:
                     {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"helloworldsvc"},"name":"helloworldsvc","namespace":"test...
Selector:          app=helloworldapp
Type:              ClusterIP
IP:                10.108.182.240
Port:              http  9111/TCP
TargetPort:        80/TCP
Endpoints:         10.1.0.28:80
Session Affinity:  None
Events:            <none>

This service is pointing to a deployment with a web app.

My question how to I find the url for this service? I already tried http://localhost:9111/ and that did not work.

I verified that the pod that this service points to is up and running.

8 Answers 8

213

URL of service is in the below format:

<service-name>.<namespace>.svc.cluster.local:<service-port>

In your case it is:

helloworldsvc.test.svc.cluster.local:9111
7
  • Thank you for the answer, is there a way to avoid the <namespace> in the URL? I'm running a cron, than should make a request to the service in the same namespace.
    – Kostanos
    Commented Feb 8, 2021 at 21:06
  • 13
    if both services are in same namespace then just use service name. rest can be ignored Commented Feb 10, 2021 at 3:29
  • 1
    I have seen at some places this format <pod-name>.<service-name>.<namespace>.svc.cluster.local:<service-port> being used. Is this incorrect? If it's correct, when do we use this?
    – Ram Patra
    Commented Aug 26, 2021 at 12:15
  • 1
    @RamPatra You can find the details on DNS queries for pod name under this section in Kubernetes documentation. kubernetes.io/docs/concepts/services-networking/dns-pod-service/…
    – redzack
    Commented Nov 3, 2021 at 10:53
  • just helloworldsvc.test is right?
    – tanteng
    Commented Dec 22, 2022 at 15:12
31

Get the service name: kubectl get service -n test

URL to a kubernetes service is service-name.namespace.svc.cluster.local:service-port where cluster.local is the kubernetes cluster name.

To get the cluster name: kubectl config get-contexts | awk {'print $2'}

URL to service in your case will be helloworldsvc.test.svc.cluster.local:9111

The way you are trying to do won't work as to make it available on your localhost you need to make the service available at nodeport or using port-forward or using kubectl proxy.

However, if you want dont want a node port and to check if inside the container everything works fine then follow these steps to get inside the container if it has a shell.

kubectl exec -it container-name -n its-namespace-name sh

then do a

curl localhost:80 or curl helloworldsvc.test.svc.cluster.local:9111 or curl 10.1.0.28:80

but both curl commands will work only inside Kubernetes pod and not on your localhost machine.

To access on your host machine kubectl port-forward svc/helloworldsvc 80:9111 -n test

15

The following url variations worked for me when in the same cluster and on the same namespace (namespace: default; though all but first should still work when services are on different namespaces):

http://helloworldsvc
http://helloworldsvc.default
http://helloworldsvc.default.svc
http://helloworldsvc.default.svc.cluster.local
http://helloworldsvc.default.svc.cluster.local:80

//

using HttpClient client = new();
string result = await client.GetStringAsync(url);

Notes:

  • I happen to be calling to and from an ASP.NET 6 application using HttpClient
  • That client I think just sets port to 80 by default, so no 80 port needs to be explicitly set to work. But I did verify for all of these it can be added or removed from the url
  • http only (not https, unless you configured it specially)
  • namespace can only be omitted in the first case (i.e. when domain / 'authority' is just the service name alone). So helloworldsvc.svc.cluster.local:80 fails with exception "Name or service not known (helloworldsvc.svc.cluster.local:80)"
14

The service you have created is of type ClusterIP which is only accessible from inside the cluster. You have two ways to access it from your desktop:

  1. Create a nodeport type service and then access it via nodeip:nodeport

  2. Use Kubectl port forward and then access it via localhost:forwardedport

1
  • 1
    Never use IP if you can use DNS, unless you know exactly what you are doing. Commented Feb 10, 2021 at 20:46
6

If you are working with minikube , you can run the code below

minikube service --all

enter image description here

for specific service


minikube service service-name --url

enter image description here

6

Here is another way to get the URL of service

Enter one pod through kubectl exec

kubectl exec -it podName -n namespace -- /bin/sh

Then execute nslookup IP of service such as 172.20.2.213 in the pod

/ # nslookup 172.20.2.213
nslookup: can't resolve '(null)': Name does not resolve

Name:      172.20.2.213
Address 1: 172.20.2.213 172-20-2-213.servicename.namespace.svc.cluster.local

Or execute nslookup IP of serviceName in the pod

/ # nslookup servicename
nslookup: can't resolve '(null)': Name does not resolve

Name:      172.20.2.213
Address 1: 172.20.2.213 172-20-2-213.servicename.namespace.svc.cluster.local

Now the service URL is servicename.namespace.svc.cluster.local attached with the service port after removing IP for the output of nslookup.

0

just issue kubectl get all That will give you something similar to this

enter image description here

Paste it in the browser

-1

Usually docker-based containers that would be run in K8s environments are stripped off any excessive tools, but if you enter your pod:

kubectl exec -it <pod-name> -n <namespace> -- /bin/sh

and after that use dnsdomainname like so:

kafka-0:/$ dnsdomainname
kafka-headless.kafka.svc.cluster.local

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.