2

So I have a few services running using nodejs, in more than one kubernetes pod.

These services obviously need to talk to each other, and be able to find the database included in this kubernetes cluster.

In docker-compose I could just use the container name and it would automatically route me to the correct one, with kubernetes it does not seem to be the case.

How do I route to specific containers in different pods in Kubernetes? I tried service names, using the namespace set up by the pipeline's admin team (<POD>.<NAMESPACE>.svc.cluster.local), and an ExternalName, but every time I get the same error: Error: getaddrinfo ENOTFOUND

Any advice would be appreciated as I am new to kubernetes.

1 Answer 1

4

Normally, you access services using the name provided by a Service resource.

For example, if I have a Pod running Redis, like this:

apiVersion: v1
kind: Pod
metadata:
  labels:
    app: redis
  name: redis
spec:
  containers:
    - image: docker.io/redis:latest
      name: redis
      ports:
      - containerPort: 6379
        name: redis

I might have a service definition that looks like:

apiVersion: v1
kind: Service
metadata:
  name: redis
spec:
  selector:
    app: redis
  ports:
  - port: 6379
    targetPort: redis

With these in place, from another Pod I can refer to the Redis service using the hostname redis:

[root@client /]# redis-cli -h redis
redis:6379>

The name and port to which we connect is controlled by the Service object. If instead of the above example I had:

apiVersion: v1
kind: Service
metadata:
  name: bob
spec:
  selector:
    app: redis
  ports:
  - port: 2000
    targetPort: redis

I would need to connect like this:

[root@client /]# redis-cli -h bob -p 2000
bob:2000>

For more information:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.