DEV Community

cosckoya
cosckoya

Posted on • Updated on

InfluxDB v2 on Kubernetes

Here is the thing, let's setup a InfluxDB v2 in Kubernetes. Fast and simple and just with 3 YAML files ;)

You will need a Kubernetes cluster. You could create a GKE, EKS or AKS, but if you're poor as I am; use Minikube :P

Install Minikube or Kind and start it:

Minikube:

CMD> minikube start
Enter fullscreen mode Exit fullscreen mode

Kind:

CMD> kind create cluster
Enter fullscreen mode Exit fullscreen mode

Then create a namespace called "monitoring"

CMD> kubectl create namespace monitoring
Enter fullscreen mode Exit fullscreen mode

Easy, dont you think? What now? Let's create a Service Account with a privileged permission scheme, this is mine:

apiVersion: v1
kind: ServiceAccount
metadata:
  name: cthulhu
  namespace: monitoring
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: cthulhu-clusterrole
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: cthulhu
  namespace: monitoring
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: cthulhu-role
  namespace: monitoring
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: cthulhu
  namespace: monitoring
Enter fullscreen mode Exit fullscreen mode

Take note that this one is just for testing purposes, NEVER use this RBAC on a production environment, you will need to touch here and there for a production environment. Did I told you that this will be fast as a shark?

Let's deploy our InfluxDB v2 server. Here is the YAML:

apiVersion: v1
kind: Service
metadata:
  name: influxdb
  namespace: monitoring 
spec:
  type: ClusterIP
  selector:
    app: influxdb
  ports:
  - name: api
    port: 9999
  - name: gui
    port: 8086
--------
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: influxdb
  namespace: monitoring   
spec:
  serviceName: "influxdb"
  selector:
    matchLabels:
      app: influxdb
  template:
    metadata:
      labels:
        app: influxdb
    spec:
      serviceAccount: cthulhu    
      containers:
      - name: influxdb
        image: quay.io/influxdb/influxdb:v2.0.3
        resources:
          limits:
            memory: "128Mi"
            cpu: 1
        ports:
        - name: api
          containerPort: 9999
        - name: gui
          containerPort: 8086
        volumeMounts:
        - name: data
          mountPath: /root/.influxdbv2
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes:
        - ReadWriteOnce
        resources:
          requests:
            storage: 1Gi
        volumeMode: Filesystem
Enter fullscreen mode Exit fullscreen mode

This is a Statefulset that will be enough to test new influxDB v2 features on your local environment, develop some alerting and dashboards with the new GUI and test some API things.

Now, we must configure the basic credentials. You could manage to create the admin user via GUI, but let's do it via YAML, because could be useful to create some pipelines with this monitoring monster.

So, here is the "config" YAML. Take note that I set up a simple token and password. Did I say that this is only for testing purposes?

apiVersion: batch/v1
kind: Job
metadata:
  name: influxdb-setup
  namespace: monitoring
spec:
  template:
    spec:
      restartPolicy: Never
      containers:
        - name: create-credentials
          image: quay.io/influxdb/influxdb:v2.0.3
          command:
            - influx
          args:
            - setup
            - --host
            - http://influxdb.monitoring:8086
            - --bucket
            - kubernetes
            - --org
            - InfluxData
            - --password
            - yogsothoth
            - --username
            - admin
            - --token
            - secret-token
            - --force
Enter fullscreen mode Exit fullscreen mode

So, so... what's next? I also have a Telegraf to send some data to that Influx, here it is:

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: telegraf
  namespace: monitoring  
spec:
  selector:
    matchLabels:
      app: telegraf
  template:
    metadata:
      labels:
        app: telegraf
    spec:
      serviceAccount: cthulhu
      volumes:
      - name: config
        configMap:
          name: telegraf
      containers:
      - name: telegraf
        image: telegraf:alpine
        imagePullPolicy: IfNotPresent
        volumeMounts:
        - name: config
          mountPath: /etc/telegraf/telegraf.conf
          subPath: telegraf.conf     
--------
apiVersion: v1
kind: ConfigMap
metadata:
  name: telegraf
  namespace: monitoring  
data:
  telegraf.conf: | 
    [global_tags]
      infra = "minikube"

    [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            = "telegraf"
      omit_hostname       = false

    [[outputs.influxdb_v2]]
      urls         = ["http://influxdb:8086"]

      organization = "InfluxData"
      bucket       = "kubernetes"
      token        = "secret-token"

    [[inputs.cpu]]  
      percpu           = true
      totalcpu         = true
      collect_cpu_time = false
      report_active    = false

    [[inputs.disk]]
       ignore_fs = ["rootfs","tmpfs", "devtmpfs", "devfs", "iso9660", "overlay", "aufs", "squashfs"]
Enter fullscreen mode Exit fullscreen mode

And then? Nothing more than port-forward InfluxDB GUI service and connect to http://localhost:8086

kubectl port-forward service/influxdb 8086:8086
Enter fullscreen mode Exit fullscreen mode

Enjoy!

Top comments (0)