user31986
user31986

Reputation: 1656

k8s: Communicating between pods of same deployment

What is the recommended way to communicate between the pods belonging to the same replica-set (deployment)? Is it possible to lookup what are the urls of other pods from a given pod?

Or is replica-set not a right approach to follow for that?

Looking for a right k8s way to do this. Thanks!

Upvotes: 3

Views: 1752

Answers (1)

Howard_Roark
Howard_Roark

Reputation: 4326

You can use a StatefulSet in conjunction with a Headless Service. One of the features of a StatefulSet is a unique consistent naming convention:

For a StatefulSet with N replicas, each Pod in the StatefulSet will be assigned an integer ordinal, from 0 up through N-1, that is unique over the Set.

So, if you have three copies of MyPod, you know the names will be MyPod-0, MyPod-1, MyPod-2. Then if you tie them to a Headless Service called MyHeadlessService, you will be able to reach your pods via:

MyPod-0.MyHeadlessService
MyPod-1.MyHeadlessService
MyPod-2.MyHeadlessService

To see this you can exec into MyPod-0

kubectl exec -it MyPod-0 /bin/bash

And then ping MyPod-1

ping MyPod-1.MyHeadlessService

There's lots of examples online of this pattern, and you can decide if it fits your use case. As an anecdote, Cluster related technologies like ElasticSearch and Vault use this pattern for inter-node communication.

Upvotes: 8

Related Questions