Influx DB and Grafana Installation with Docker

Install Docker

  1. Uninstall previous docker packages if installed
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done
  1. Setup Docker's apt repository
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
  1. Install Latest Docker Packages
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  1. Add User to Docker Group
sudo usermod -aG docker your_username

Install Grafana

Docker Compose

  1. Create a docker-compose.yml file with this content
version: '3.8'

services:
  grafana:
    image: grafana/grafana-oss
    container_name: grafana
    restart: unless-stopped
    ports:
      - '3000:3000'
    environment:
      - GF_SERVER_ROOT_URL=http://grafana.aadarshadhakal.com.np/
    volumes:
      - grafana-storage:/var/lib/grafana

volumes:
  grafana-storage: {}
  1. Run containers
    docker compose up -d

This runs the services defined inside docker-compose.yml file in detached mode.

Setup Nginx Reverse Proxy

  1. Install Nginx
    sudo apt update && sudo apt install nginx

  2. Install certbot for SSL certificatr
    sudo apt install certbot python3-certbot-nginx

  3. Create a new nginx web server /etc/nginx/sites-available/<YOURDOMAIN> with the following content.

server {

    server_name <YOURDOMAIN>;

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_buffering off;
        proxy_pass http://127.0.0.1:3000;
    }
}
  1. Enable the newly created nginx web server
    sudo ln -s /etc/nginx/sites-available/<YOURDOMAIN> /etc/nginx/sites-enabled/<YOURDOMAIN>

  2. Install SSL certificate using Certbot
    sudo certbot run -d <YOURDOMAIN> -d www.<YOURDOMAIN>

Default Username and Password is admin and admin

Install Influx DB

  1. Create a docker-compose.yml file with the following contents
version: '3.8'

services:
  influxdb2:
    image: influxdb:2
    restart: always
    ports:
      - 8086:8086
    environment:
      DOCKER_INFLUXDB_INIT_MODE: setup
      DOCKER_INFLUXDB_INIT_USERNAME_FILE: /run/secrets/influxdb2-admin-username
      DOCKER_INFLUXDB_INIT_PASSWORD_FILE: /run/secrets/influxdb2-admin-password 
      DOCKER_INFLUXDB_INIT_ADMIN_TOKEN_FILE: /run/secrets/influxdb2-admin-token
      DOCKER_INFLUXDB_INIT_ORG: <YOUR ORGANIZATIO NAME> 
      DOCKER_INFLUXDB_INIT_BUCKET: <BUCKET NAME>
    secrets:
      - influxdb2-admin-username
      - influxdb2-admin-password
      - influxdb2-admin-token
    volumes:
      - type: volume
        source: influxdb2-data
        target: /var/lib/influxdb2
      - type: volume
        source: influxdb2-config
        target: /etc/influxdb2
secrets:
  influxdb2-admin-username:
    file: .env.influxdb2-admin-username
  influxdb2-admin-password:
    file: .env.influxdb2-admin-password
  influxdb2-admin-token:
    file: .env.influxdb2-admin-token
volumes:
  influxdb2-data:
  influxdb2-config:
  1. Execute this command from the directory where your docker-compose.yml file is located.
echo "<USERNAME>" > .env.influxdb2-admin-username
echo "<PASSWORD>" > .env.influxdb2-admin-password
echo "<TOKEN>" > .env.influxdb2-admin-token

*Replace USERNAME, PASSWORD and TOKEN

  1. Start services
    docker compose up -d

Add Influx DB as Data Source in Grafana

  1. Login to grafana dashboard
  2. From the drawer menu go to Data Sources
  3. Click on Add New Data Source button and select InfluxDB
  4. Select FLUX as the Query Language
  5. Add InfluxDB server url and port in the URL text field
  6. Enable Skip TLS Verify
  7. Enter Organization Name, Bucket Name and Token and click Save and Test

To get API token, in InfluxDB dashboard go to Load Data > API Tokens > Generate API Token or You can use the token provided during InfluxDB installation