Skip to content

Instantly share code, notes, and snippets.

@BondAnthony
Last active October 10, 2019 13:03
Show Gist options
  • Save BondAnthony/f35b7d909ba624606f57c2d3f0eebe73 to your computer and use it in GitHub Desktop.
Save BondAnthony/f35b7d909ba624606f57c2d3f0eebe73 to your computer and use it in GitHub Desktop.
Kubernetes deployment for Telegraf Webhook Input

Telegraf Kubernetes Deployment Github Events

This deployment will run Telegraf on Kubernetes. This will create a single pod configured to receive webhook events from configured repositories.

Dependencies:

  • You must be running nginx ingress controller within your cluster.
  • Your cluster should be running external dns with a configured domain and zone. This allows for DNS records to be created automatically.
  • Certmanager should be used to handle creating TLS certificates.
  • You should already or can create the required credentials for your Telegraf input and output endpoints.

Namespace

Create the Telegraf namespace if you don't have one already.

# Namespace
---
apiVersion: v1
kind: Namespace
metadata:
  name: telegraf

Secret

For everything to work correctly you must create a secret that contains your Github token and your InfluxDB 2.0 cloud credentials. Below is an example that can be updated with your credentials. The deployment will mount this secret into your pod populating the environment variables used in the Telegraf configmap.

# Serect
---
apiVersion: v1
kind: Secret
metadata:
  name: telegraf-github
  namespace: telegraf
type: Opaque
stringData:
  ENV: test
  INFLUXDB_BUCKET: telegraf
  INFLUXDB_CLOUD: https://us-west-2-1.aws.cloud2.influxdata.com
  INFLUXDB_ORG: 91134ece19ne4gh
  INFLUXDB_TOKEN: jNtE1j0HHAJM2_pLl9RFx6FZqGkVoT7_E2YpFcmKwcMpA==

ConfigMap

Update your configmap with the correct Github webhook configurations based on your needs.

# ConfigMap
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: telegraf-config-github
  namespace: telegraf
data:
  telegraf.conf: |+
    [global_tags]
      env = "$ENV"
      hostname = "$HOSTNAME"
      nodename = "$NODENAME"
      role = "github"
    [[inputs.webhooks]]
      service_address = ":1619"

      [inputs.webhooks.github]
        path = "/github"
        secret = "$GITHUB_TOKEN"
        ## Add your webhook events configuration https://github.com/influxdata/telegraf/tree/master/plugins/inputs/webhooks/github 

    [[outputs.influxdb_v2]]
      urls = ["$INFLUXDB_CLOUD"]
      token = "$INFLUXDB_TOKEN"
      organization = "$INFLUXDB_ORG"
      bucket = "$INFLUXDB_BUCKET"
      timeout = "5s"
    [[inputs.internal]]

Deployment

Create your deployment along with the service and ingress.

# Deployment
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: telegraf-github
  namespace: telegraf
  labels:
    app: telegraf-github
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: telegraf-github
  template:
    metadata:
      labels:
        app: telegraf
    spec:
      volumes:
        - name: telegraf-config
          configMap:
            name: telegraf-config-github
      containers:
        - name: telegraf
          image: docker.io/library/telegraf:1.12
          volumeMounts:
            - name: telegraf-config
              mountPath: /etc/telegraf
          resources:
            limits:
              cpu: 500m
              memory: 500Mi
            requests:
              cpu: 100m
              memory: 500Mi
          envFrom:
            - secretRef:
                name: telegraf-github
          env:
            - name: NODENAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
# Sevice
---
apiVersion: v1
kind: Service
metadata:
  name: telegraf-github
  namespace: telegraf
spec:
  ports:
    - name: telegraf-github
      port: 1619
      targetPort: 1619
  selector:
    app: telegraf-github
  type: ClusterIP

# Ingress
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: public
    kubernetes.io/tls-acme: "true"
  name: telegraf-github-public
  namespace: telegraf
spec:
  rules:
    - host: github.domain.com # Replace this domain with your own domain name
      http:
        paths:
          - backend:
              serviceName: telegraf-github
              servicePort: 1619
            path: /github
  tls:
    - hosts:
        - github.domain.com # Replace this domain with your own domain name
      secretName: telegraf-github-tls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment