DEV Community

Cover image for Setup InfuxDB 2.0 with HTTPS
alfchee
alfchee

Posted on • Edited on

Setup InfuxDB 2.0 with HTTPS

I'm new in the InfluxDB path, and I'm finding it very useful to track data and make analytics in realtime in a very easy way.

I started when the version 2.0.2 was being released, then I'm starting with the new flavors and improvements of InfluxDB, which also brings changes in the configurations, respecting the 1.x versions.

The way for the installation I chose for this service was by means Docker container in a VM which the simple docker command can be found at the Get Started, but this is only usefull when working on local for a dev mode, when working for something to work on production it is required to have more power about the configuration and the use of HTTPS.

As a matter of run InfluxDB with docker-compose we just need a few lines in our docker-compose.yml file

version: '3'

services:
  influxdb:
    container_name: influxdb
    image: quay.io/influxdb/influxdb:v2.0.2
    restart: always 
    ports:
      - '8086:8086'
    volumes:
      - './data:/root/.influxdbv2'
Enter fullscreen mode Exit fullscreen mode

In this version of InfluxDB the data and configurations of InfluxDB are stored on ~/.influxdbv2, then inside the container, the user is root and for so the path placed in the _volumes` section.

The most complicated part is to enable the HTTPS because of the management of the certificates and also handle the renew of them, for these reasons I'm gonna use Let's Encrypt which is going to be handling it, and to be able to use an HTTP server to which Let's Encrypt may verify I'll be using Nginx

Note: For some reason I wasn't able to proxy InfluxDB using Nginx and generate the certificates with Let's Encrypt, but due I need Grafana in my setup, I used Grafana for this dutty.

The image of Nginx to use is jwilder/nginx-proxy which is a proxy very configurable that works very well with the image of Let's Encrypt jrcs/letsencrypt-nginx-proxy-companion.

Then to makes work Grafana with the Nginx proxy, we need to set the environment variables VIRTUAL_HOST, VIRTUAL_PROTO and VIRTUAL_PORT, so it can be routed automatically by Nginx:

`

  grafana:
    image: grafana/grafana
    container_name: grafana
    restart: always
    environment:
      - VIRTUAL_HOST=influxdb.eulerr.app # adjust to match your domain name
      - VIRTUAL_PROTO=https
      - VIRTUAL_PORT=3000
   volumes:
      - grafana-data:/var/lib/grafana

  nginx-proxy:
    image: jwilder/nginx-proxy
    container_name: nginx
    restart: always
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - ./nginx/html:/usr/share/nginx/html
      - ./nginx/vhost.d:/etc/nginx/vhost.d
Enter fullscreen mode Exit fullscreen mode

But then, now let's mix up everything with Let's Encrypt, adding the definition of the service and sharing through a volume the certificates created. In the Grafana service is added the LETSENCRYPT_HOST and LETSENCRYPT_EMAIL can create a certificate for the Host declared for Grafana.

version: '3'

services:
  influxdb:
    container_name: influxdb
    image: quay.io/influxdb/influxdb:v2.0.2
    restart: always 
    ports:
      - '8086:8086'
    volumes:
      - './data:/root/.influxdbv2'
      - ./letsencrypt/certs:/etc/letsencrypt/certs:ro
    environment:
      - INFLUXD_TLS_CERT=/etc/letsencrypt/certs/YOUR.DOMAIN.COM.crt # adjust to match your domain name
      - INFLUXD_TLS_KEY=/etc/letsencrypt/certs/YOUR.DOMAIN.COM.key # adjust to match your domain name


  grafana:
    image: grafana/grafana
    container_name: grafana
    restart: always
    environment:
      - VIRTUAL_HOST=YOUR.DOMAIN.COM # adjust to match your domain name
      - VIRTUAL_PROTO=https
      - VIRTUAL_PORT=3000
      - LETSENCRYPT_HOST=YOUR.DOMAIN.COM # adjust to match your domain name
      - LETSENCRYPT_EMAIL=me@DOMAIN.COM # adjust to match your email

      - GF_SERVER_CERT_FILE=/etc/letsencrypt/certs/YOUR.DOMAIN.COM.crt # adjust to match your domain name
      - GF_SERVER_CERT_KEY=/etc/letsencrypt/certs/YOUR.DOMAIN.COM.key # adjust to match your domain name
      - GF_SERVER_PROTOCOL=https

      - GF_SERVER_DOMAIN=YOUR.DOMAIN.COM # adjust to match your domain name
      - GF_SECURITY_ADMIN_USER=USER # adjust to create Grafana admin account
      - GF_SECURITY_ADMIN_PASSWORD=SECRET_PASSWORD # adjust to set Grafana admin password
    volumes:
      - ./letsencrypt/certs:/etc/letsencrypt/certs:ro
      - grafana-data:/var/lib/grafana

  nginx-proxy:
    image: jwilder/nginx-proxy
    container_name: nginx
    restart: always
    labels:
      - com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - ./nginx/html:/usr/share/nginx/html
      - ./nginx/vhost.d:/etc/nginx/vhost.d
      - ./letsencrypt/certs:/etc/nginx/certs:ro

  letsencrypt:
    image: jrcs/letsencrypt-nginx-proxy-companion
    container_name: letsencrypt
    restart: always
    environment:
      - NGINX_PROXY_CONTAINER=nginx-proxy
      - REUSE_PRIVATE_KEYS=true
    volumes_from:
      - nginx-proxy
    volumes:
      - ./letsencrypt/certs:/etc/nginx/certs:rw
      - /var/run/docker.sock:/var/run/docker.sock:ro

volumes:
  grafana-data:
    driver: local
Enter fullscreen mode Exit fullscreen mode

Using this configuration, Grafana is reachable through YOUR.DOMAIN.COM because is proxied by Nginx, while InfluxDB is directly served by YOUR.DOMAIN.COM:8086, and _Let's Encrypt` will be checking if the certificates are about to expire and renew them.

I hope this install can serves you as much as me. :)

Top comments (0)