I am trying to expose a service in a simple kubernetes cluster composed of a single worker and one master. In particular, I am using the descriptor below:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb-deployment
labels:
app: mongodb
spec:
replicas: 1
selector:
matchLabels:
app: mongodb
template:
metadata:
labels:
app: mongodb
spec:
containers:
- name: mongodb
image: mongo
ports:
- containerPort: 27017
---
apiVersion: v1
kind: Service
metadata:
name: mongodb-service
spec:
selector:
app: mongodb
ports:
- protocol: TCP
port: 27017
targetPort: 27017
Then I try to use this service from another pod:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongo-express
labels:
app: mongo-express
spec:
replicas: 1
selector:
matchLabels:
app: mongo-express
template:
metadata:
labels:
app: mongo-express
spec:
containers:
- name: mongo-express
image: mongo-express
ports:
- containerPort: 8081
env:
- name: ME_CONFIG_MONGODB_SERVER
value: mongodb-service
However, what I get in the other mongo-express pod is that mongodb-service cannot be resolved. In fact if I spin up a pod and I try a simple wget this is the output:
$ wget http://mongodb-service/ -O-
--2021-06-23 13:31:08-- http://mongodb-service/
Resolving mongodb-service (mongodb-service)... failed: Name or service not known.
wget: unable to resolve host address 'mongodb-service'
Instead nslookup mongodb-service works fine:
$ nslookup mongodb-service
Server: 10.96.0.10
Address: 10.96.0.10#53
However, if I try with netcat I get this:
$nc mongodb-service 27017
nc: getaddrinfo for host "mongodb-service" port 27017: Name or service not known
So it seems that it is getaddrinfo that is failing.
How could I debug the problem?