<p>I would like to deploy a sidecar container that is measuring the memory usage (and potentially also CPU usage) of the main container in the pod and then send this data to an endpoint.</p>

<p>I was looking at cAdvisor, but Google Kubernetes Engine has hardcoded 10s measuring interval, and I need 1s granularity. Deploying another cAdvisor is an option, but I need those metrics only for a subset of pods, so it would be wasteful. </p>

<p>Is it possible to write a sidecar container that monitors the main container metrics? If so, what tools could the sidecar use to gather the data? </p>

Jan Matas
<p>That one second granularity will be probably the main showstopper for many monitoring tools. In theory you can script it on your own. You can use Docker stats API and read stats stream only for main pod. You will need to mount /var/run/docker.sock to the sidecar container. Curl example:</p>

<pre><code>curl -N --unix-socket /var/run/docker.sock http:/containers/&lt;container-id&gt;/stats
</code></pre>

<p>Another option is to read metric from cgroups. But you will need more calculations in this case. Mounting of croups to the sidecar container will be required. See some examples of cgroup pseudo-files on <a href="https://docs.docker.com/config/containers/runmetrics/" rel="nofollow noreferrer">https://docs.docker.com/config/containers/runmetrics/</a></p>
