Kubernetes add pod IPs into environment variable

3/11/2019

I have a Kafka deployment and service deployed via Kubernetes. Each its pods have its internal IP and with a command like this

kubectl describe services broker --namespace=kafka | grep Endpoints | awk '{print $2}'

I can get them all: 10.244.1.11:9092,10.244.2.15:9092,10.244.2.16:9092

I have another service deployed with Kubernetes, after my Kafka, that needs the result of that command as an environment variable KAFKA_BOOTSTRAP_SERVERS.

How can I get the result of that command into an environment variable in my service kubernetes YML file?

-- Lobo
environment-variables
kubernetes
service
yaml

2 Answers

3/11/2019

Kubernetes allow you to use environmental variables. Here is the documentation.

You can also use HELM to use templates which also allow the use of the environmental variables.

In your case, you can get the result in an env variable like as below:

SOME_ENV_VARIABLE=$( command... )

-- Vishrant
Source: StackOverflow

3/11/2019

You should develop a client program in python or go and using the service account that gets mounted in each container, hit the api server endpoint and retrieve Kafka endpoints. Parse the Json file output abd grab the actual broker ip addresses

-- P Ekambaram
Source: StackOverflow