Reputation: 4606
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
Reputation: 21
just issue kubectl get all That will give you something similar to this
Paste it in the browser
Upvotes: 0
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
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
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:
HttpClient
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 urlhttp
only (not https
, unless you configured it specially)helloworldsvc.svc.cluster.local:80
fails with exception "Name or service not known (helloworldsvc.svc.cluster.local:80)"
Upvotes: 15
Reputation: 4211
If you are working with minikube
, you can run the code below
minikube service --all
for specific service
minikube service service-name --url
Upvotes: 6
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
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
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:
Create a nodeport type service and then access it via nodeip:nodeport
Use Kubectl port forward and then access it via localhost:forwardedport
Upvotes: 14