Telegraf on Kubernetes: Error: Service "telegraf" is invalid: spec.ports: Required value

6/23/2021

what do I want to do?

I'm trying to deploy telegraf in my Kubernetes cluster so that I can use Telegraf's Prometheus input plugin to read the data (metrics) from a particular URL and write the metrics in a file using telegraf's output file plugin.

what did I do?

I used the telegraf helm chart to deploy telegraf on kubernetes. I changed the following config changes. The original telegraf yaml file:

config:
  agent:
    interval: "10s"
    round_interval: true
    metric_batch_size: 1000
    metric_buffer_limit: 10000
    collection_jitter: "0s"
    flush_interval: "10s"
    flush_jitter: "0s"
    precision: ""
    debug: false
    quiet: false
    logfile: ""
    hostname: "$HOSTNAME"
    omit_hostname: false
  processors:
    - enum:
        mapping:
          field: "status"
          dest: "status_code"
          value_mappings:
            healthy: 1
            problem: 2
            critical: 3
  outputs:
    - influxdb:
        urls:
          - "http://influxdb.monitoring.svc:8086"
        database: "telegraf"
  inputs:
    - statsd:
        service_address: ":8125"
        percentiles:
          - 50
          - 95
          - 99
        metric_separator: "_"
        allowed_pending_messages: 10000
        percentile_limit: 1000

The changes I made to it:

config:
  outputs:
    - file:
        files:
          - "stdout"
          - "metrics.out" 
        data_format: influx  
  inputs:
    - prometheus:
        - urls:
            url: "http://ipaddr:80/metrics"

And when I applied the helm chart along with the changes I got Error: Service "telegraf" is invalid: spec.ports: Required value and my deployment failed.

chandhana@Azure:~/clouddrive/PromExpose$ helm install telegraf influxdata/telegraf -f telegraf-values.yaml
Error: Service "telegraf" is invalid: spec.ports: Required value

Please do help me if I'm making any mistakes on the changed YAML configuration since I didn't find any resource for yaml format of telegraf's input and output plugin. Additional link for reference: telegraf .conf file

-- Chandhana
kubernetes
kubernetes-helm
prometheus
telegraf
telegraf-plugins

1 Answer

6/25/2021

You forgot to enable metrics in values.yaml, its disabled by default. Correct part is

metrics:
  health:
    enabled: true
  collect_memstats: false

Change your telegraf-values.yaml to

config:
  agent:
    interval: "10s"
    round_interval: true
    metric_batch_size: 1000
    metric_buffer_limit: 10000
    collection_jitter: "0s"
    flush_interval: "10s"
    flush_jitter: "0s"
    precision: ""
    debug: false
    quiet: false
    logfile: ""
    hostname: "$HOSTNAME"
    omit_hostname: false
  processors:
    - enum:
        mapping:
          field: "status"
          dest: "status_code"
          value_mappings:
            healthy: 1
            problem: 2
            critical: 3
  outputs:
    - file:
        files:
          - "stdout"
          - "metrics.out" 
        data_format: influx 
  inputs:
    - prometheus:
        - urls:
            url: "http://ipaddr:80/metrics"

metrics:
  health:
    enabled: true
  collect_memstats: false

Result:

helm install telegraf influxdata/telegraf -f values.yaml
NAME: telegraf
LAST DEPLOYED: Fri Jun 25 10:35:05 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
To open a shell session in the container running Telegraf run the following:

  kubectl exec -i -t --namespace default $(kubectl get pods --namespace default -l app.kubernetes.io/name=telegraf -o jsonpath='{.items[0].metadata.name}') /bin/sh

To view the logs for a Telegraf pod, run the following:

  kubectl logs -f --namespace default $(kubectl get pods --namespace default -l app.kubernetes.io/name=telegraf -o jsonpath='{ .items[0].metadata.name }')
-- Vit
Source: StackOverflow