Pass environment variable through service in Kubernetes?

8/25/2015

Is there a way to pass environment variables through the services in Kubernetes?

I tried passing it in to my service yaml like this:

apiVersion: v1
kind: Service
metadata:
  labels:
    name: kafka
  name: kafka
spec:
  ports:
    - port: 9092
    selector:
      name: kafka
  env:
    - name: BROKER_ID
      value: "1"

The service is being consumed by kubectl, and is created.

I've confirmed the service is connected to my container through env | grep KAFKA and the output of variables greatly increase, as expected when my service is up.

However, I would like to pass in custom environment-variables that have to be different depending on which instance of the container it is in.

Is this possible?

-- NegatioN
cluster-computing
kubernetes

2 Answers

9/1/2015

The way that Kubernetes is designed has Services decoupled from Pods. You can not inject a Secret or an env var into a running Pod. What you want is to configure the Pod to use the env var or Secret.

-- Tim Hockin
Source: StackOverflow

8/25/2015

This is the best way I've found so far: (reading required) https://github.com/kubernetes/kubernetes/issues/4710

Roughly, create a secret in a file that's mounted and source it before you execute your script.

-- JuanIsFree
Source: StackOverflow