How to use multiple config files in a Kubernetes Telegraf pod

Hi there,

I am trying to include multiple configuration files in a telegraf pod in Kubernetes.
This is my current yaml file with one config file to deploy telegraf on Kubernetes and it works just fine.
But when I try to map multiple config files into the telegraf pod nothing works anymore.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: telegraf
spec:
  selector:
    matchLabels:
      app: telegraf
  minReadySeconds: 5
  template:
    metadata:
      labels:
        app: telegraf
    spec:
      containers:
        - image: telegraf:1.21.0
          name: telegraf
          volumeMounts:
            - mountPath: /etc/telegraf/telegraf.conf
              name: telegraf-config
              subPath: telegraf.conf
              readOnly: true
          envFrom:
            - secretRef:
                name: telegraf-secrets
      volumes:
        - name: telegraf-config
          configMap:
            name: telegraf-config

I created a configmap with “kubectl create configmap --from-file=/etc/telegraf/telegraf.d” and extended the yaml file above like this:

          volumeMounts:
            - name: telegraf-dir
              mountPath: /etc/telegraf/telegraf.d
              readOnly: true
      volumes:
        - name: telegraf-dir
          projected:
            sources:
            - configMap:
                name: telegraf-configs-dir

In fact the files get mapped into the pod but telegraf doesn’t use them.
I’d like to know if there is a way to map multiple config files into the telegraf pod.

Thanks for your help.

Hi,

The telegraf container does not use the /etc/telegraf/telegraf.d directory by default. You would need to add that as an option to your telegraf command to include --config-directory /etc/telegraf/telegraf.d

Let me know if that helps or if you are somehow running Telegraf differently such that it is already trying to include that folder.

Hi,

my yaml file looks like this, after I included the command:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: telegraf
spec:
  selector:
    matchLabels:
      app: telegraf
  minReadySeconds: 5
  template:
    metadata:
      labels:
        app: telegraf
    spec:
      containers:
        - image: telegraf:1.21
          name: telegraf
          args: ["--config-directory /etc/telegraf/telegraf.d"]
          volumeMounts:
            - mountPath: /etc/telegraf/telegraf.conf
              name: telegraf-config
              subPath: telegraf.conf
              readOnly: true
          volumeMounts:
            - name: telegraf-dir
              mountPath: /etc/telegraf/telegraf.d
              readOnly: true
          envFrom:
            - secretRef:
                name: telegraf-secrets
      volumes:
        - name: telegraf-config
          configMap:
            name: telegraf-config
      volumes:
        - name: telegraf-dir
          projected:
            sources:
            - configMap:
                name: telegraf-configs-dir

I tried to include it only with the args: lable and also with the command and the args lable.
In both scenarios the pod directly goes into completed state and after a few minutes it goes to CrashLoopBackOff.

When I don’t use the --config-directory flag the pod starts and keeps running but won’t send metrics to influx. Not even the main file will work, although it worked before I mapped the other config files into the config-folder.

I don’t know if it is possible to run telegraf in a pod with multiple config files or if my yaml file is just wrong.

You probably want to include the config file as well in your args.

Can you please get the logs from the container?

The logs from the container look like this:

Hi,

I found the problem and now it is working.
The problem was, that I had to pass the flag --config-directory as two arguments and you can use the keywords volumes and volumeMounts only once but you can specify more sources than one.

See the full yaml-file below:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: telegraf
spec:
  selector:
    matchLabels:
      app: telegraf
  minReadySeconds: 5
  template:
    metadata:
      labels:
        app: telegraf
    spec:
      containers:
        - image: telegraf:1.21.0
          name: telegraf
          args: ["--config-directory", "/etc/telegraf/telegraf.d"]
          volumeMounts:
            - mountPath: /etc/telegraf/telegraf.conf
              name: telegraf-config
              subPath: telegraf.conf
              readOnly: true
            - name:  telegraf-dir
              mountPath:  /etc/telegraf/telegraf.d
          envFrom:
            - secretRef:
                name: telegraf-secrets
      volumes:
        - name: telegraf-config
          configMap:
            name: telegraf-config
        - name: telegraf-dir
          configMap:
            name: telegraf-configs-dir