Skip to content
Branch: master
Find file History
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
hooks examples/service-per-pod: Use finalizer for cleanup. Sep 27, 2018
README.md examples/service-per-pod: Use finalizer for cleanup. Sep 27, 2018
my-statefulset.yaml DecoratorController: Add service-per-pod example. Mar 2, 2018
service-per-pod.yaml examples/service-per-pod: Use finalizer for cleanup. Sep 27, 2018
test.sh

README.md

Service-Per-Pod Decorator

This is an example DecoratorController that adds a Service for each Pod in a StatefulSet, for any StatefulSet that requests this by adding an annotation that specifies the name of the label containing the Pod name.

In Kubernetes 1.9+, StatefulSet automatically adds the Pod name as a label on each of its Pods, so you can enable Service-Per-Pod like this:

apiVersion: apps/v1beta2
kind: StatefulSet
metadata:
  annotations:
    service-per-pod-label: "statefulset.kubernetes.io/pod-name"
    service-per-pod-ports: "80:8080"
...

For earlier versions, this example also contains a second DecoratorController that adds the Pod name label since StatefulSet previously didn't do it.

The Pod name label is only added to Pods that request it with an annotation, which you can add in the StatefulSet's Pod template:

apiVersion: apps/v1beta2
kind: StatefulSet
metadata:
  annotations:
    service-per-pod-label: "pod-name"
    service-per-pod-ports: "80:8080"
...
spec:
  template:
    metadata:
      annotations:
        pod-name-label: "pod-name"
...

If the StatefulSet is then deleted, or if the service-per-pod-label annotation is removed to opt out of the decorator, any Services created will be cleaned up.

Prerequisites

  • Kubernetes 1.8+ is recommended for its improved CRD support, especially garbage collection.
  • Install Metacontroller.

Deploy the DecoratorControllers

kubectl create configmap service-per-pod-hooks -n metacontroller --from-file=hooks
kubectl apply -f service-per-pod.yaml

Create an Example StatefulSet

kubectl apply -f my-statefulset.yaml

Watch for the Services to get created:

kubectl get services --watch

Check that the StatefulSet's Pods can be selected by pod-name label:

kubectl get pod -l pod-name=nginx-0
kubectl get pod -l pod-name=nginx-1
kubectl get pod -l pod-name=nginx-2

Check that the per-Pod Services get cleaned up when the StatefulSet is deleted:

kubectl delete -f my-statefulset.yaml
kubectl get services
You can’t perform that action at this time.