How to find the url of a service in kubernetes?

1/2/2020

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.

-- Foo
kubernetes
kubernetes-service

3 Answers

1/2/2020

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
-- P Ekambaram
Source: StackOverflow

1/2/2020

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

-- redzack
Source: StackOverflow

1/2/2020

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

-- Arghya Sadhu
Source: StackOverflow