Connection refused when trying to access a kubernetes pod via kubernetes DNS
What is the difference when accessing a port via k8s-dns?
When you call a Pod IP address, you directly connect to a pod, not to the service.
When you call to DNS name of your service, it resolves to a Service IP address, which forward your request to actual pods using Selectors as a filter to find a destination, so it is 2 different ways of how to access pods.
Also, you cal call Service IP address directly instead of using DNS, it will works the same way. Moreover, Service IP address, unlike Pod IPs, is static, so you can use it all the time if you want.
For in-cluster communication you are using ClusterIP service mode, which is default and you set it, so everything is OK here.
Current endpoints where your service forwards requests you can get by kubectl get service $servicename -o wide
in an "endpoint" column.
What about your current problems with connection, I can recommend you:
Check endpoint of your service (there should be one or more IP addresses of pods),
Set
targetPort
parameter for each of service ports, e.g:apiVersion: v1kind: Servicemetadata: labels: service: hadoop name: hadoopspec: ports: - name: hadoop port: 9000 targetPort: 9000 # here is - name: ssh port: 22 targetPort: 22 # here is - name: hadoop-ui port: 50070 targetPort: 50070 # here is selector: service: hadoop type: ClusterIP
P.S. Here is a nice topic with explanation about how Service works. Also, you can check official documentation.