Auto creating Services along with Pods

1/3/2019

I have a Kubernetes scenario where I will need to deploy 3 Redis servers (pods), and each needs to be exposed with a ClusterIP service. Something like;

  • RedisClusterIP1 -> RedisPod1
  • RedisClusterIP2 -> RedisPod2
  • RedisClusterIP3 -> RedisPod3

I have deployment plan like below, but as you can see it is creating ClusterIP service manually, and binds single of them to all pods.

Is there any way to set a deployment plan, there it will create same amount of services with Pods?

I checked deployments and replica sets but could not figure out if there is something already exists.

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: redisharedis
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: redisharedis
    spec:
      containers:
        - name: redisharedis
          image: aozdemir/redisharedis:v6
          resources:
            limits:
              cpu: "1"
              memory: "800Mi"
            requests:
              cpu: "0.1"
              memory: "200Mi"
          ports:
            - containerPort: 6379


---
apiVersion: v1
kind: Service
metadata:
  name: redisharedis-service
  labels:
    name: redisharedis-service
spec:
  ports:
    - port: 6379
      targetPort: 6379
      protocol: TCP
  selector:
    app: redisharedis
  type: ClusterIP
-- Teoman shipahi
kubernetes

1 Answer

1/3/2019

This can be done If you're deploying your redis cluster as Statefulset rather than deployment. 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"

You need to install MetaController in your kubernetes cluster to install one service per pod. Please refer following link for detailed instructions:

https://github.com/GoogleCloudPlatform/metacontroller/tree/master/examples/service-per-pod

-- Prafull Ladha
Source: StackOverflow