<p>Assuming I have set <code>resource.limits.ephemeral-storage</code> for containers in a Kubernetes cluster (using Docker), and the following Docker daemon.json logging configuration on the worker nodes:</p>
<pre><code>{
  &quot;log-driver&quot;: &quot;json-file&quot;,
  &quot;log-opts&quot;: {
    &quot;max-size&quot;: &quot;100m&quot;,
    &quot;max-file&quot;: &quot;10&quot;,
  }
}
</code></pre>
<p>My understanding is that all log files (even the rotated log files) will count towards this ephemeral storage limit. This means that to determine the value for <code>resource.limits.ephemeral-storage</code>, I have to consider the maximum allowed log size (here 10*100MB) to the calculation.</p>
<p>Is there a way to &quot;exclude&quot; log files from counting towards the container's ephemeral-storage limit?</p>
<p>Since log handling is done &quot;outside&quot; of Kubernetes, I want to avoid that the resource limits for Kubernetes workloads depend on the Docker log configuration. Otherwise any change to the rotation settings (e.g. increase to 10*200MB) could cause pods to be evicted, if one would forget to adjust the limit for each and every container.</p>

<p>Based on the function <a href="https://github.com/kubernetes/kubernetes/blob/d88fadbd65c5e8bde22630d251766a634c7613b0/pkg/kubelet/stats/helper.go#L344" rel="nofollow noreferrer">calcEphemeralStorage</a> from <a href="https://github.com/kubernetes/kubernetes/tree/d88fadbd65c5e8bde22630d251766a634c7613b0" rel="nofollow noreferrer">release 1.17.16 source code</a>, if you want to exclude logs from calculation you can comment or remove those lines and rebuild kubelet:</p>
<pre><code>if podLogStats != nil {
        result.UsedBytes = addUsage(result.UsedBytes, podLogStats.UsedBytes)
        result.InodesUsed = addUsage(result.InodesUsed, podLogStats.InodesUsed)
        result.Time = maxUpdateTime(&amp;result.Time, &amp;podLogStats.Time)
    }
</code></pre>
<p>This part of the code is responsible for counting ephemeral storage usage for logs. But removing that part of code may also require to adjust some test files which expect that logs amount is calculated.
All statistics are instead counted in <a href="https://github.com/kubernetes/kubernetes/blob/ebcb4a2d88c83096e6068aa56e9a5281976e1fec/pkg/kubelet/stats/cri_stats_provider.go#L392" rel="nofollow noreferrer">this function</a>:</p>
<pre><code>func (p *criStatsProvider) makePodStorageStats(s *statsapi.PodStats, rootFsInfo *cadvisorapiv2.FsInfo) {
    podNs := s.PodRef.Namespace
    podName := s.PodRef.Name
    podUID := types.UID(s.PodRef.UID)
    vstats, found := p.resourceAnalyzer.GetPodVolumeStats(podUID)
    if !found {
        return
    }
    logStats, err := p.hostStatsProvider.getPodLogStats(podNs, podName, podUID, rootFsInfo)
    if err != nil {
        klog.ErrorS(err, &quot;Unable to fetch pod log stats&quot;, &quot;pod&quot;, klog.KRef(podNs, podName))
        // If people do in-place upgrade, there might be pods still using
        // the old log path. For those pods, no pod log stats is returned.
        // We should continue generating other stats in that case.
        // calcEphemeralStorage tolerants logStats == nil.
    }
    etcHostsStats, err := p.hostStatsProvider.getPodEtcHostsStats(podUID, rootFsInfo)
    if err != nil {
        klog.ErrorS(err, &quot;Unable to fetch pod etc hosts stats&quot;, &quot;pod&quot;, klog.KRef(podNs, podName))
    }
    ephemeralStats := make([]statsapi.VolumeStats, len(vstats.EphemeralVolumes))
    copy(ephemeralStats, vstats.EphemeralVolumes)
    s.VolumeStats = append(append([]statsapi.VolumeStats{}, vstats.EphemeralVolumes...), vstats.PersistentVolumes...)
    s.EphemeralStorage = calcEphemeralStorage(s.Containers, ephemeralStats, rootFsInfo, logStats, etcHostsStats, true)
}
</code></pre>
<p>In the last line you can find a usage of <code>calcEphemeralStorage</code>.</p>
<p>In the recent version the <a href="https://github.com/kubernetes/kubernetes/blob/ebcb4a2d88c83096e6068aa56e9a5281976e1fec/pkg/kubelet/stats/helper.go#L374" rel="nofollow noreferrer">mentioned code</a> include the same log calculation section, so the solution should work for the <a href="https://github.com/kubernetes/kubernetes/tree/092fbfbf53427de67cac1e9fa54aaa09a28371d7" rel="nofollow noreferrer">latest release</a> too.</p>
<p>See also:</p>
<ul>
<li><a href="https://github.com/kubernetes/kubernetes/blob/d88fadbd65c5e8bde22630d251766a634c7613b0/pkg/kubelet/eviction/eviction_manager.go#L504" rel="nofollow noreferrer">variable with total usage of ephemeral storage</a></li>
<li><a href="https://github.com/kubernetes/kubernetes/tree/d88fadbd65c5e8bde22630d251766a634c7613b0" rel="nofollow noreferrer">commit of your version</a></li>
<li><a href="https://developer.ibm.com/components/kubernetes/articles/setup-guide-for-kubernetes-developers/" rel="nofollow noreferrer">Setup guide for Kubernetes developers</a></li>
<li><a href="https://learn.microsoft.com/en-us/virtualization/windowscontainers/kubernetes/compiling-kubernetes-binaries" rel="nofollow noreferrer">Compiling Kubernetes binaries</a></li>
</ul>
