<p>I'm trying to implement a <a href="https://kubernetes.io/docs/concepts/cluster-administration/logging/#streaming-sidecar-container" rel="nofollow noreferrer">Streaming Sidecar Container</a> logging architecture in Kubernetes using Fluentd.</p>

<p>In a single pod I have:</p>

<ul>
<li><a href="https://kubernetes.io/docs/concepts/storage/volumes/#emptydir" rel="nofollow noreferrer">emptyDir</a> Volume (as log storage)</li>
<li>Application container</li>
<li>Fluent log-forwarder container</li>
</ul>

<p>Basically, the Application container logs are stored in the shared emptyDir volume. Fluentd log-forwarder container tails this log file in the shared emptyDir volume and forwards it an external log-aggregator.</p>

<p>The Fluentd log-forwarder container uses the following config in <code>td-agent.conf</code>:</p>

<pre><code>&lt;source&gt;
  @type tail
  tag "#{ENV['TAG_VALUE']}"
  path (path to log file in volume)
  pos_file /var/log/td-agent/tmp/access.log.pos
  format json
  time_key time
  time_format %iso8601
  keep_time_key true
&lt;/source&gt;

&lt;match *.*&gt;
  @type forward
  @id forward_tail
  heartbeat_type tcp
  &lt;server&gt;
    host (server-host-address)
  &lt;/server&gt;
&lt;/match&gt;
</code></pre>

<p>I'm using an environment variable to set the <code>tag</code> value so I can change it dynamically e.g. when I have to use this container side-by-side with a different Application container, I don't have to modify this config and rebuild this image again.</p>

<p>Now, I set the environment variable value during pod creation in Kubernetes:</p>

<pre><code>    .
    .
    spec:
      containers:
      - name: application-pod
        image: application-image:1.0
        ports:
        - containerPort: 1234
        volumeMounts:
        - name: logvolume
          mountPath: /var/log/app
      - name: log-forwarder
        image: log-forwarder-image:1.0
        env:
        - name: "TAG_VALUE"
          value: "app.service01"
        volumeMounts:
        - name: logvolume
          mountPath: /var/log/app
      volumes:
      - name: logvolume
        emptyDir: {}
</code></pre>

<p>After deploying the pod, I found that the tag value in the Fluentd log-forwarder container comes out empty (expected value: "app.service01"). I imagine it's because Fluentd's td-agent initializes first before the <code>TAG_VALUE</code> environment variable gets assigned.</p>

<p>So, the main question is...<br>
<strong>How can I dynamically set the td-agent's tag value?</strong></p>

<p>But really, what I'm wondering is:<br>
<strong>Is it possible to assign an environment variable before a container's initialization in Kubernetes?</strong></p>

<p>You can use the combo <a href="https://github.com/fabric8io/fluent-plugin-kubernetes_metadata_filter" rel="nofollow noreferrer">fluent-plugin-kubernetes_metadata_filter</a> and <a href="https://github.com/fluent/fluent-plugin-rewrite-tag-filter" rel="nofollow noreferrer">fluent-plugin-rewrite-tag-filter</a> to set container name or something to the tag.</p>
