Foo
Foo

Reputation: 4606

How to find the url of a service in kubernetes?

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.

Upvotes: 120

Views: 230064

Answers (8)

Krzysztof Waclawski
Krzysztof Waclawski

Reputation: 21

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

enter image description here

Paste it in the browser

Upvotes: 0

Lev Vanyan
Lev Vanyan

Reputation: 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

Upvotes: -1

zangw
zangw

Reputation: 48396

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.

Upvotes: 6

Nicholas Petersen
Nicholas Petersen

Reputation: 9558

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)"

Upvotes: 15

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

Upvotes: 6

P Ekambaram
P Ekambaram

Reputation: 17621

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

Upvotes: 211

redzack
redzack

Reputation: 1711

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

Upvotes: 31

Arghya Sadhu
Arghya Sadhu

Reputation: 44569

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

Upvotes: 14

Related Questions