When creating 3 replica StatefulSet, get/use IP/hostname from 1st replica as parameter for other 2

1/13/2022

I am creating a StatefulSet following this article on Kubernetes.io.

What I want to achieve is the following:

  1. I have a custom service that is run on 3 instances (A, B and C)
  2. The first instance (A) starts, and is assigned an ip or hostname.
  3. Instances B and C start later, and they get either some environment variable or some command argument containing the ip or hostname of instance A.

So far I've got the following k8s YAML:

apiVersion: apps/v1
kind: StatefulSet
metadata:
   name: myservice
spec:
   serviceName: myservice
   replicas: 3
   selector:
     matchLabels:
       app: myservice
   template:
     metadata:
       labels:
         app: myservice
     spec:
       containers:
       - name: myservice
         image: myservice/myservice:version
         args: ['-root="myservice-0"']
         ports:
               - containerPort: 2222

Following the K8s article, I thought that the hostname 'myservice-0' would be actually visible to the other pods (as per the "usable stable network identities" section). But this doesn't work.

The command I am executing to run this service is:

kubectl create -f myservice.yml

How could I achieve this in Kubernetes?

-- obaqueiro
kubernetes

0 Answers