HiveBrain v1.2.0
Get Started
← Back to all entries
patternkubernetesMinor

Kubernetes container networking between pods

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
networkingcontainerkubernetespodsbetween

Problem

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 (`..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.

Solution

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:

  • Service

Code Snippets

apiVersion: v1
kind: Pod
metadata:
  labels:
    app: redis
  name: redis
spec:
  containers:
    - image: docker.io/redis:latest
      name: redis
      ports:
      - containerPort: 6379
        name: redis
apiVersion: v1
kind: Service
metadata:
  name: redis
spec:
  selector:
    app: redis
  ports:
  - port: 6379
    targetPort: redis
[root@client /]# redis-cli -h redis
redis:6379>
apiVersion: v1
kind: Service
metadata:
  name: bob
spec:
  selector:
    app: redis
  ports:
  - port: 2000
    targetPort: redis
[root@client /]# redis-cli -h bob -p 2000
bob:2000>

Context

StackExchange DevOps Q#15842, answer score: 4

Revisions (0)

No revisions yet.