Unable to access a service through a service name. in kubernetes

7/15/2021

I am new to Kubernetes and I'm experimenting with different things. This is my deployment file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hellok8s
spec:
  replicas: 2
  selector:
    matchLabels:
      app: hellok8s
  template:
    metadata:
      labels:
        app: hellok8s
    spec:
      containers:
      - image: brianstorti/hellok8s:v3
        name: hellok8s-container

and below is my service file.

apiVersion: v1
kind: Service
metadata:
  name: hellok8s-svc
spec:
  type: NodePort
  selector:
    app: hellok8s
  ports:
  - port: 4567
    nodePort: 30001

I'm using the exec command and from inside the container, I'm trying to access the service using the service name. When I access this using the cluster IP, it works fine but when I try to access it using the service name it doesn't work. what might be the problem?

-- nimramubashir
kubernetes
kubernetes-pod
kubernetes-service

2 Answers

7/15/2021

The service DNS: hellok8s-svc.default.svc is only accessible inside the cluster.

Service DNS: <service-name>.<namespace>.svc

Since you're using service of type NodePort, you will be able to access it at NODE_IP:NODT_PORT both from inside and outside of the cluster. But the service DNS address won't work from outside the cluster. This is the expected behavior.

Update:

While using curl with service DNS, make sure you're using the complete DNS address:

  • <service-name>.<namespace>.svc.cluster.local

Try it from inside the container:

$ curl http://hellok8s-svc.default.svc.cluster.local:4567

N.B.:

Say, you don't remember the DNS naming format, you can still get the complete address by nslookup CLI.

# nslookup service-name.namespace
$ nslookup es-all.demo
Server:    10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name:      es-all.demo
Address 1: 10.96.97.198 es-all.demo.svc.cluster.local
-- Kamol Hasan
Source: StackOverflow

7/27/2021

Posted as community wiki, to better visibility. As nimramubashir mentioned in the comment:

I have debugged deeper and there seems to be some problem with the internal pod-pod communication. I'm creating a cluster through kubeadm and there seems to be some problem with it that Is causing this problem. I'm asking a new question for that. I have tried deploying it on the cloud and this is working fine.

-- Mikołaj Głodziak
Source: StackOverflow