Kubernetes: Send a Pod Id and Deployment Id to Doc

2019-08-03 07:21发布

问题:

My Kube deployment

Consider a cluster with a few Kubernetes deployments of Docker containers, one per each campaign. Each deployment scales quite frequently.

For logging purposes, I want each pod to write to a separate directory on the host, which includes both its unique Pod Id and the shared deployment id, e.g.:

 /var/logs/campaigns/campaign1/pod1/
 /var/logs/campaigns/campaign1/pod2/
 /var/logs/campaigns/campaign2/pod3/
 /var/logs/campaigns/campaign2/pod4/

This way no pod can access logs of another campaign or another pod. As a side note, I use FileBeat to collect the logs from the base directory and send them to ElasticSearch.

My question

How can Kubernetes pass the Pod Id and Deployment Id to Docker so that it can create and mount a directory on the host containing these values?

回答1:

For deployment you do not need anything inside kube, all you need to do is define proper ENV variable in your deployment manifest (ie. identical to deployment name).

As for Pod, there is a downward API.