Skip to main content

VictoriaLogs Docker Deployment

Prerequisites

If you have not rotated MongoDB logs yet, please first read MongoDB Log Rotation. The Docker version already includes Grafana and the slow query dashboard, so it can be used directly after deployment.

Prerequisites

  • docker is installed.
  • docker-compose is installed.

Deploy VictoriaLogs + Grafana

  1. Download the image

    docker pull registry.cn-hangzhou.aliyuncs.com/hap-mdy/hap-monitor-victorialogs-grafana-amd64:1.0.0
  2. Create the configuration and data directories

    mkdir -p /usr/local/victoria-logs-linux
    mkdir -p /data/victorialogs
    mkdir -p /data/grafana
  3. Create the configuration file

    cat > /usr/local/victoria-logs-linux/hap-monitor-logs.yaml << 'EOF'
    services:
    hap-monitor-logs:
    image: registry.cn-hangzhou.aliyuncs.com/hap-mdy/hap-monitor-victorialogs-grafana-amd64:1.0.0
    container_name: hap-monitor-logs
    restart: always
    ports:
    # VictoriaLogs HTTP API
    - "9428:9428"
    # Grafana Web UI
    - "3000:3000"
    volumes:
    # VictoriaLogs data directory
    - /data/victorialogs:/data/victorialogs
    # Grafana data directory
    - /data/grafana:/usr/local/grafana/data
    environment:
    # Time zone
    TZ: Asia/Shanghai
    # Go Runtime heap limit, recommended to keep <= mem_limit
    GOMEMLIMIT: 2GiB
    #####################################################################
    # VictoriaLogs
    #####################################################################
    # Data directory
    VL_STORAGE_DATA_PATH: /data/victorialogs
    # HTTP listen address
    VL_HTTP_LISTEN_ADDR: ":9428"
    # Basic Auth
    VL_HTTP_AUTH_USERNAME: myuser
    VL_HTTP_AUTH_PASSWORD: mypassword
    # Data retention period
    VL_RETENTION_PERIOD: 30d
    # Maximum disk usage
    VL_RETENTION_DISK_USAGE: 50GiB
    # Internal cache percentage
    VL_MEMORY_ALLOWED_PERCENT: "2"
    # Write concurrency
    VL_MAX_CONCURRENT_INSERTS: "4"
    # Query concurrency
    VL_MAX_CONCURRENT_REQUESTS: "2"
    # Query timeout
    VL_SEARCH_MAX_QUERY_DURATION: 30s
    # Maximum query time range
    VL_SEARCH_MAX_QUERY_TIMERANGE: 24h
    # Query queue timeout
    VL_SEARCH_MAX_QUEUE_DURATION: 5s
    # Slow query log threshold
    VL_SEARCH_LOG_SLOW_QUERY_DURATION: 3s
    # Maximum line size
    VL_INSERT_MAX_LINE_SIZE_BYTES: "262144"
    # Maximum number of fields per line
    VL_INSERT_MAX_FIELDS_PER_LINE: "300"
    # syslog extra fields
    VL_SYSLOG_EXTRA_FIELDS_TCP: '{"job":"rsyslog"}'
    # Log level
    VL_LOGGER_LEVEL: INFO
    #####################################################################
    # Grafana
    #####################################################################
    # Grafana sub-path
    GRAFANA_SERVER_ROOT_URL: "%(protocol)s://%(domain)s:%(http_port)s/privatedeploy/mdy/monitor/victorialogs-grafana/"
    # Enable serve from sub-path mode
    GRAFANA_SERVER_SERVE_FROM_SUB_PATH: "true"
    # Grafana default admin account
    GRAFANA_ADMIN_USER: admin
    # Grafana default admin password
    GRAFANA_ADMIN_PASSWORD: admin@123456
    # Allow loading unsigned plugins
    GRAFANA_ALLOW_UNSIGNED_PLUGINS: victoriametrics-logs-datasource
    #####################################################################
    # VictoriaLogs Grafana Datasource
    #####################################################################
    # VictoriaLogs address
    GRAFANA_VL_DATASOURCE_URL: http://127.0.0.1:9428
    # VictoriaLogs Basic Auth username
    GRAFANA_VL_BASIC_AUTH_USER: myuser
    # VictoriaLogs Basic Auth password
    GRAFANA_VL_BASIC_AUTH_PASSWORD: mypassword
    mem_limit: 3g
    memswap_limit: 3g
    cpus: 2.0
    ulimits:
    nofile:
    soft: 102400
    hard: 102400
    logging:
    driver: json-file
    options:
    # Single log file size
    max-size: "100m"
    # Number of retained log files
    max-file: "3"
    EOF

    Variable notes:

    • VL_HTTP_AUTH_USERNAME and VL_HTTP_AUTH_PASSWORD are used for VictoriaLogs Basic Auth. It is recommended to replace them with a more complex username and password according to your local standards instead of using the example values directly.
    • VL_RETENTION_PERIOD is used to set the log retention period. For example, 30d means logs are retained for 30 days.
    • VL_RETENTION_DISK_USAGE is used to set the disk usage upper limit for VictoriaLogs. For example, 50GiB means VictoriaLogs can use up to 50 GiB of disk space.
    • If the log volume is large, it is recommended to tune these two parameters together to prevent disk growth from getting out of control.
    • GRAFANA_VL_DATASOURCE_URL is used to specify the VictoriaLogs address that Grafana connects to. If VictoriaLogs is not running on the same host, change it to an actually reachable address.
    • GRAFANA_VL_BASIC_AUTH_USER and GRAFANA_VL_BASIC_AUTH_PASSWORD are used for Grafana's Basic Auth access to VictoriaLogs, and must match the VictoriaLogs authentication credentials.
  4. Start and stop the service

    # Start
    docker-compose -f /usr/local/victoria-logs-linux/hap-monitor-logs.yaml up -d
    # Stop
    docker-compose -f /usr/local/victoria-logs-linux/hap-monitor-logs.yaml down

Deploy the Log Collector Promtail

Promtail must be deployed on every MongoDB node. It reads the local mongodb.log file directly and pushes the slow query logs to VictoriaLogs.

Notes:

  • PROMTAIL_PUSH_URL must be changed to the actual VictoriaLogs address.
  • PROMTAIL_USERNAME and PROMTAIL_PASSWORD must be changed to the real VictoriaLogs authentication credentials.
  • PROMTAIL_JOB_NAME must remain unique on each MongoDB node so that log sources can be distinguished.
  • PROMTAIL_LOG_PATH must be changed to the absolute path of the MongoDB log file on the current node.
  • If the log path differs between MongoDB nodes, adjust the corresponding configuration on each node separately.
  1. Download the image

    docker pull registry.cn-hangzhou.aliyuncs.com/hap-mdy/hap-promtail-vlogs-mongodb-amd64:1.0.0
  2. Create the configuration and data directories

    mkdir -p /usr/local/promtail-vlogs
    mkdir -p /usr/local/promtail-vlogs/promtail-data
  3. Create the configuration file

    cat > /usr/local/promtail-vlogs/promtail-vlogs-mongodb.yaml << 'EOF'
    services:
    promtail-vlogs-mongodb:
    image: registry.cn-hangzhou.aliyuncs.com/hap-mdy/hap-promtail-vlogs-mongodb-amd64:1.0.0
    container_name: promtail-vlogs-mongodb
    restart: always
    network_mode: host
    environment:
    PROMTAIL_PUSH_URL: http://172.17.30.241:9428/insert/loki/api/v1/push
    PROMTAIL_USERNAME: myuser
    PROMTAIL_PASSWORD: mypassword
    PROMTAIL_JOB_NAME: mongodb-01
    PROMTAIL_LOG_PATH: /data/logs/mongodb/mongodb.log
    volumes:
    - /data/logs/mongodb/mongodb.log:/data/logs/mongodb/mongodb.log:ro
    - ./promtail-data:/data/promtail
    deploy:
    resources:
    limits:
    cpus: '1'
    memory: 1G
    EOF

    cat > /usr/local/promtail-vlogs/promtail-data/positions.yaml << 'EOF'
    positions: {}
    EOF
  4. Start and stop the service

    # Start
    docker-compose -f /usr/local/promtail-vlogs/promtail-vlogs-mongodb.yaml up -d
    # Stop
    docker-compose -f /usr/local/promtail-vlogs/promtail-vlogs-mongodb.yaml down

Reverse Proxy Configuration for Grafana

upstream grafana {
server 192.168.1.10:3000;
}

map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}

server {
listen 80;
server_name hap.domain.com;
access_log /data/logs/weblogs/grafana.log main;
error_log /data/logs/weblogs/grafana.mingdao.net.error.log;

location /privatedeploy/mdy/monitor/victorialogs-grafana/ {
#allow 1.1.1.1;
#deny all;
proxy_hide_header X-Frame-Options;
proxy_set_header X-Frame-Options ALLOWALL;
proxy_set_header Host $http_host;
proxy_pass http://grafana;
proxy_redirect http://localhost:3000 http://hap.domain.com:80/privatedeploy/mdy/monitor/victorialogs-grafana;
}

location /privatedeploy/mdy/monitor/victorialogs-grafana/api/live {
rewrite ^/(.*) /$1 break;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $http_host;
proxy_pass http://grafana;
}
}

After the reverse proxy is configured, you can access the Grafana page with the following URL:

http://hap.domain.com/privatedeploy/mdy/monitor/victorialogs-grafana

Combined Deployment Reference

  • VictoriaLogs and Grafana are already deployed in the same Docker bundle, so Grafana does not need to be installed separately.
  • Promtail must be deployed on every MongoDB node so that each node can collect its own local logs.
  • If the MongoDB log directory differs between nodes, modify PROMTAIL_LOG_PATH on the corresponding node accordingly.

Access Notes

  • VictoriaLogs listens on port 9428 by default.
  • Grafana listens on port 3000 by default and is exposed externally through a reverse proxy sub-path.
  • If you need to integrate with an existing Nginx or gateway, make sure the sub-path matches GRAFANA_SERVER_ROOT_URL.