kubernetes loadbalancer with multiple backend ports

1/1/2017

I have an application with three different backend types. Each of them is listening on different ports (eg 8080, 8180, 8280). Now I would like to access them by using http://example.com:{8080,8180,8280}. To be safe there should be running two pods of each service.

  1. How should the yaml file look like with multiple backends, each of them having a different port?
  2. Can I include the definition for replicas in the same file? Or is there some kind of main file in kubernetes where I can include other files?
-- Denny Crane
docker
kubernetes

1 Answer

1/2/2017

To do this you would create multiple services, see http://kubernetes.io/docs/user-guide/services/ for details.

An example could look like (using yaml, you can also use json):

apiVersion: v1
kind: Service
metadata:
  name: yourservice_1
  namespace: default
  labels:
    component: yourcomponent
spec:
  type: NodePort
  selector:
    component: yourcomponent
  ports:
  - name: http
    port: 8080
    protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
  name: yourservice_2
  namespace: default
  labels:
    component: yourcomponent
spec:
  type: NodePort
  selector:
    component: yourcomponent
  ports:
  - name: http
    port: 8081
    protocol: TCP
---
Etc.

And to answer the second part of your question:

The --- is to have multiple definitions (can be services, can be any kubernetes yaml), in a single file.

I however advice a separate file for services since they in general tend to be loaded just once, and a separate file for your pod definitions (since that will change more often).

One thing to be aware of with the multiple service definition: In (at least Kubernetes 1.3.5/1.3.6, versions which I am using), there is a runaway pod issue in which certain combinations of selectors lead to kubernetes starting just as many pods as it can. To prevent this: Test and experiment. It does work as long as you avoid this condition.

A single service entry point can be also be used and can be defined as:

apiVersion: v1
kind: Service
metadata:
  name: yourservice_1
  namespace: default
  labels:
    component: yourcomponent
spec:
  type: NodePort
  selector:
    component: yourcomponent
  ports:
  - name: http
    port: 8080
    protocol: TCP
  - name: http2
    port: 8081
    protocol: TCP
  - name: http2
    port: 8082
    protocol: TCP
-- Norbert van Nobelen
Source: StackOverflow