patternkubernetesMinor
Kubernetes container networking between pods
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 (`
Any advice would be appreciated as I am new to kubernetes.
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
For example, if I have a
I might have a service definition that looks like:
With these in place, from another
service using the hostname
The name and port to which we connect is controlled by the
object. If instead of the above example I had:
I would need to connect like this:
For more information:
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: redisI might have a service definition that looks like:
apiVersion: v1
kind: Service
metadata:
name: redis
spec:
selector:
app: redis
ports:
- port: 6379
targetPort: redisWith these in place, from another
Pod I can refer to the Redisservice using the hostname
redis:[root@client /]# redis-cli -h redis
redis:6379>The name and port to which we connect is controlled by the
Serviceobject. If instead of the above example I had:
apiVersion: v1
kind: Service
metadata:
name: bob
spec:
selector:
app: redis
ports:
- port: 2000
targetPort: redisI 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: redisapiVersion: 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.