3

For for a StateFul set, I can access its pods via a headless service internally.

I think, it would make sense, to have an easy way to expose also single pods externally (since the pods usually have a state and therefore loadbalancing over them makes no sense).

So far, I found no straight forward way to do that. Even doing kubectl expose pod pod-1 --type NodePort gives me a service which balances over all pods. Is there a reason why this is like this or is there a nice way to access single pods.

2 Answers2

4

You can expose a specific Pod in a StatefulSet externally by matching on the statefulset.kubernetes.io/pod-name label.

For example, if your StatefulSet is named app and you want to expose port 80 from the first Pod as a Service:

apiVersion: v1
kind: Service
metadata:
  name: app-0
spec:
  type: LoadBalancer
  selector:
    statefulset.kubernetes.io/pod-name: app-0
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
jbg
  • 4,903
  • 1
  • 27
  • 30
-1

I don’t see they way to distribute the load to single pod. If you want to client keep this session with one pod you can define service sessionAffinity: ClientIP. this will send established client traffic same pod.

sfgroups
  • 18,151
  • 28
  • 132
  • 204
  • From inside my Cluster, i can access each pod individually. So, I don't get the point why this makes no sense for access from outside the Cluster. – Christof Tinnes Aug 24 '17 at 09:26
  • I am assuming Inside the cluster your using POD IP to access the pods correct? or your using service IP access the pod? – sfgroups Aug 24 '17 at 12:05
  • Actually, I am accessing the pods over the headless Service by their DNS names. So in the end, this should be the Service IP, I guess? – Christof Tinnes Aug 28 '17 at 08:39