In k8s, I want my server to listen on podIP rather than 127.0.0.1 or 0.0.0.0. But the podIP is dynamically allocated, so I want to do something like:
ip := getIPFromNic("eth0")
net.Listen(ip)
But I don’t know how to ensure the nic in pod is called eth0 rather than en0 or others. Should I do something in Dockerfile or should I do something in deployment.yaml?
I tried to search on google, but didn’t find any related resources about the nic in docker. Most of the blogs are talking about the veth and docker bridge
3
Answers
After digging into this problem, I found that the network interface name is controlled by the kubelet, it's a cluster wide configuration that can't be changed by the users. By default, the nic name is eth0, and if you're not sure, you can check any pod in the cluster to confirm that.
In practice, it's recommended to use downward api to pass the PodIP as an env var to the pod, thus you can get the PodIP easily. But my case is a little special, the application's config file(mounted as a configmap) need a listen addr, which must be either an ip address or a nic name. That's why I need a fixed value.
/proc/1/task/1/net/fib_trie
file
mine is like:
and 10.0.0.45 is my pod ip address
curl -s -H "Authorization: Bearer $YOUR_TOKEN" https://$KUBERNETES_SERVICE_HOST/api/v1/namespaces/$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace)/pods/$(hostname)/ | grep "podIP" | awk '{print $2}' | tr -d '",'
unfortunately default privileges of
cat /var/run/secrets/kubernetes.io/serviceaccount/token
is not enough for this request.You can get the pod ip from Kubernetes rather than trying to look it up from the interface (or files in
/proc
).You can expose information about a Pod as environment variables; the documentation includes this example; look in particular at how
MY_POD_IP
is set:You can an example of this in use in the
node_exporter
manifests from the kube-prometheus project.