Answers: <p>The above answer is incorrect. You can use pod name labels, cf: <a href="https://stackoverflow.com/questions/68123751/kubernetes-service-for-a-subset-of-statefulset-pods">https://stackoverflow.com/questions/68123751/kubernetes-service-for-a-subset-of-statefulset-pods</a></p>
  <p>For example:</p>
  <pre><code>apiVersion: v1
  kind: Service
  metadata:
    name: my-sts-1
  spec:
    ...
    selector:
      statefulset.kubernetes.io/pod-name: my-sts-1
    ...
  </code></pre>
  <p>You will have to manually create the services when you scale your statefulset up or down. Alternatively, you could create a helm chart or a CRD + Operator.</p>
  
Answers: CC BY-SA 4.0
Answers: 2023-02-15T14:25:49.430Z
Answers: 2023-02-15T14:25:49.430Z
Answers: cmcnealy
Answers: <p>Just to clarify if I got you right.</p>
  <p>Do you want to have a separate service for every pod/replica in your statefulSet?</p>
  <p>If so - this is not how K8s sees services being used. The service is used for grouping and load balancing purposes.</p>
  <p>In your case, if you want to have 1-on-1 connection between an external client and a pod, I suggest deploying your statefulSet a number of times with <code>replicas=1</code> and exposing a service (as NodePort/LB) every time.</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-07-15T12:08:43.580Z
Answers: How else can I communicate with each pod statefully from the outside world? In your soultion you suggest creating copies of the statefulset manually? Because its not a deployment..
Answers: flowerProgrammer
Answers: CC BY-SA 4.0
Answers: 2020-07-17T07:42:53.243Z
Answers: The logic is like this:
  In order for a pod to be available from outside it needs to be exposed via a service (NodePort/LB). Service is applied to a group of pods with the same selectors.
  Since you want to open **each** pod (a replica of a statefulset in your case), you need to create 1 service per 1 pod. You either create a number of STS with services pointing at them, or can manually label each STS pod and point services to them accordingly. The second option is very dirt though.
  
  Are you sure you need exactly this?
Answers: Alex
Answers: CC BY-SA 4.0
Answers: 2020-07-17T11:17:20.470Z
Answers: The reason I need this is that every pod has an app that communicates with an outside app on specific ports. That app has to receive an ip and then it communicates with that app according to saved actions, therefore statefully.
Answers: flowerProgrammer
Answers: CC BY-SA 4.0
Answers: 2020-07-17T11:19:11.133Z
Answers: Therefore the only way I see is to create service for each pod that exposes those ports and then I use the pod's ip adress to give the outside app.
Answers: flowerProgrammer
Answers: CC BY-SA 4.0
Answers: 2020-07-15T09:39:59.380Z
Answers: 2020-07-15T09:39:59.380Z
Answers: Alex
Body: <p>I have a statefulset app which can have any number of replicas. The pods produced should be able to communicate to external comptures. Those pods must act statefully when clients communicate with them.
  I tried to create headless service and nodeport services, but it still has only one service to communicate with and also couldn't make it to communicate externally.
  How can I make it allocate external ip and port per each port?</p>
  
Tags: <kubernetes><openshift>
Title: Create service per pod in statefulset
UserDisplayName: flowerProgrammer
ThreadText: # Create service per pod in statefulset
  
  (Asked by: username_0 on 2020-07-14)
  
  I have a statefulset app which can have any number of replicas. The pods produced should be able to communicate to external comptures. Those pods must act statefully when clients communicate with them.
  I tried to create headless service and nodeport services, but it still has only one service to communicate with and also couldn't make it to communicate externally.
  How can I make it allocate external ip and port per each port?
  
  **Tags:** kubernetes, openshift
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2023-02-15. Score: 6
  
  The above answer is incorrect. You can use pod name labels, cf: <https://stackoverflow.com/questions/68123751/kubernetes-service-for-a-subset-of-statefulset-pods>
  
  For example:
  
  ```
  apiVersion: v1
  kind: Service
  metadata:
    name: my-sts-1
  spec:
    ...
    selector:
      statefulset.kubernetes.io/pod-name: my-sts-1
    ...
  
  ```
  
  You will have to manually create the services when you scale your statefulset up or down. Alternatively, you could create a helm chart or a CRD + Operator.
  
  
  ---
  
  ### Answer by username_2 on 2020-07-15. Score: 2
  
  Just to clarify if I got you right.
  
  Do you want to have a separate service for every pod/replica in your statefulSet?
  
  If so - this is not how K8s sees services being used. The service is used for grouping and load balancing purposes.
  
  In your case, if you want to have 1-on-1 connection between an external client and a pod, I suggest deploying your statefulSet a number of times with `replicas=1` and exposing a service (as NodePort/LB) every time.
  
  > **Comments:**
  > **username_0**: How else can I communicate with each pod statefully from the outside world? In your soultion you suggest creating copies of the statefulset manually? Because its not a deployment..  
  > **username_2**: The logic is like this: In order for a pod to be available from outside it needs to be exposed via a service (NodePort/LB). Service is applied to a group of pods with the same selectors. Since you want to open **each** pod (a replica of a statefulset in your case), you need to create 1 service per 1 pod. You either create a number of STS with services pointing at them, or can manually label each STS pod and point services to them accordingly. The second option is very dirt though.  Are you sure you need exactly this?  
  > **username_0**: The reason I need this is that every pod has an app that communicates with an outside app on specific ports. That app has to receive an ip and then it communicates with that app according to saved actions, therefore statefully.  
  > **username_0**: Therefore the only way I see is to create service for each pod that exposes those ports and then I use the pod's ip adress to give the outside app.  
  
  ---
  
  