Home > Blockchain >  How to select a specific pod instance for a service in Kubernetes
How to select a specific pod instance for a service in Kubernetes

Time:10-29

I have a Kubernetes cluster with 3 nodes. On this cluster, there is one Kubernetes StatefulSet that is responsible for receiving messages, persisting the messages, and scheduling the received messages and an application processor of type Deployment that has 3 instances (replicas), ​Here the App Processor POD is receiving multiple kinds of messages from Message Processor POD using REST calls.
So finally I have

message-processor-0

and

app-processor-12345
app-processor-23456
app-processor-45567

Here my requirement is, I want to send particular types of messages to​ only one dedicated instance of the App Processor.

CodePudding user response:

Since you are using a deployment, that specific POD has no reliable network identity. A POD's name and IP address will change whenever it is killed and recreated.

You have two options. One is to create a brand-new deployment and service for that particular pod, so you can communicate with that pod using the newly created service.

Otherwise StatefulSets are another choice, as they have a persistent network identity regardless of whether the POD is killed or restarted.

The StatefulSet pods get fixed ordered names, which will be in the format $(statefulset name)-$(ordinal).

For example if your yaml looks like this:

---
apiVersion: v1
kind: Service
metadata:
  name: testservice
  namespace: testnamespace
  labels:
    app: testapp
spec:
  ports:
    - port: 80
      name: http
  type: ClusterIP
  selector:
    app: myapp
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: testapp
  namespace: testnamespace
spec:
  serviceName: testservice
  replicas: 5
  selector:
    matchLabels:
      app: testapp

The names of Pods will be testapp-0, testapp-1 and so on.

Then you can use:

testapp-0.testservice.testnamespace.svc.cluster.local for accessing Pod 0 of testapp StatetefulSet

For more information refer to: StatefulSet Docs

CodePudding user response:

What you could use here is a Statefulset for your app-processor.

Statefulsets have the capability of having unique names for each of its replicas, persisted on the restarts in the form of:

  • app-processor-0
  • app-processor-1
  • app-processor-2

So, they keep a sticky identity that you could use to send specific messages to one dedicated instance.

  • Related