Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject node labels into Kubernetes pod

Tags:

kubernetes

I am looking to deploy services to a Kubernetes cluster running over multiple zones and would like to be able to inject the region/zone labels into my pods using environment variables.

I have looked into the downward API however this only seems to allow you to inject labels/metadata from the pod/service and not from the node you are running the pod on.

If there is no way to inject the node labels another solution I thought about was having the container query the kubernetes/AWS API to fetch this information however that would mean adding quite a lot of complexity to my containers.

like image 511
Daniel Cannon Avatar asked Apr 18 '16 09:04

Daniel Cannon


People also ask

How do you get a node name in Kubernetes?

Typically, the node name is given with "--hostname-override" option when starting the kubelet. So, the following command would give the node name if --hostname-override is set.

How labels work in Kubernetes?

Labels are key/value pairs that are attached to objects, such as pods. Labels are intended to be used to specify identifying attributes of objects that are meaningful and relevant to users, but do not directly imply semantics to the core system. Labels can be used to organize and to select subsets of objects.


1 Answers

I thought about was having the container query the kubernetes/AWS API to fetch this information however that would mean adding quite a lot of complexity to my containers.

This is currently the recommended approach for getting information not available in the downward API. To avoid the additional complexity in your containers, you could use a "sidecar" with a variation on Tobias's solution. The sidecar would be an additional container in the pod, which connects to the kubernetes API, queries the information you're looking for (node labels), and writes the output to a shared volume. This could be implemented as an init container, or a sidecar that continuously syncs with the API.

like image 108
Tim Allclair Avatar answered Sep 20 '22 01:09

Tim Allclair