Skip to content

Deploying VictoriaMetrics cluster mode on Ubuntu 22.04 (vminsert + vmstorage + vmselect)

Stand up a self-hosted VictoriaMetrics cluster on Ubuntu 22.04 — sharded vmstorage, vminsert + vmselect with replication factor, retention, and a vmagent ingest path.

Your Prometheus single-node has crossed the threshold where it goes through TSDB compaction during business hours and the dashboards stop responding for 60 seconds at a time. The simple answer is “use VictoriaMetrics”, and the simple answer is correct — but the single-node binary won’t help you, because that’s just trading one single-node for another. You want cluster mode: vminsert, vmstorage, vmselect, sharded and replicated, sitting behind a vmagent ingest pipeline that can buffer if storage goes down. The hard part is wiring it correctly the first time so the replication factor actually does what you think it does.

What’s happening

VictoriaMetrics cluster mode splits metrics ingestion into three roles. vmstorage nodes are stateful — they hold the actual time-series data, sharded by series ID across all storage nodes. vminsert is stateless and routes incoming writes to vmstorage nodes; with a replication factor of N, each sample is sent to N storage nodes. vmselect is stateless and fans queries out to all storage nodes, then deduplicates results based on the replication factor. vmagent sits in front of vminsert and provides a durable buffer (-remoteWrite.tmpDataPath) so that brief vminsert outages don’t lose samples — this is the piece most deployments forget.

The replication contract is subtle. If you have 4 vmstorage nodes and -replicationFactor=2, each sample is written to 2 of the 4 storage nodes. You can lose any single storage node without data loss, but you cannot lose two simultaneously. vmselect must be started with the same -dedup.minScrapeInterval setting on both sides for replication-aware deduplication to work; mismatched flags here are how you end up with double-counted rates that look like a sudden traffic spike.

How to verify

Check that vminsert sees all storage nodes:

curl -fs http://vminsert-01:8480/-/healthy
curl -fs http://vminsert-01:8480/internal/force_flush | jq .
curl -fs http://vminsert-01:8480/metrics | grep vm_rpc_dial

Check that vmselect can reach all storage nodes:

curl -fs "http://vmselect-01:8481/select/0/prometheus/api/v1/query?query=up" | jq .
curl -fs http://vmselect-01:8481/metrics | grep vm_rpc_connections

Verify replication actually works:

# pick a storage node, gracefully stop it, observe that vminsert continues
# and vmselect returns the same data via the replica
systemctl stop vmstorage
curl -fs "http://vmselect-01:8481/select/0/prometheus/api/v1/query_range?query=up&start=$(date -d '-1 hour' +%s)&end=$(date +%s)&step=60s" \
  | jq '.data.result | length'

The count should match what you saw before stopping the storage node. If it dropped, your replication factor is set wrong (or vmselect is running with -replicationFactor=1).

The fix

The installer below deploys a 6-node VictoriaMetrics cluster on Ubuntu 22.04: 2 vminsert, 2 vmselect, and 4 vmstorage with -replicationFactor=2. Roles are selected by the ROLE variable. The vmagent ingest path is configured on a separate node (or co-located with your Prometheus servers) using the vmagent role.

#!/usr/bin/env bash
set -euo pipefail

### VictoriaMetrics cluster installer for Ubuntu 22.04
### Run on each node after editing the variables below.

if [[ "$(id -u)" -ne 0 ]]; then
  echo "Run as root."
  exit 1
fi

# === EDIT PER NODE ===
ROLE="vmstorage"              # vminsert | vmstorage | vmselect | vmagent
NODE_NAME="vmstorage-01"
NODE_IP="10.40.0.41"
CLUSTER_NETWORK="10.40.0.0/24"

# Storage nodes (full list, every role needs to know them)
STORAGE_HOSTS=("10.40.0.41:8400" "10.40.0.42:8400" "10.40.0.43:8400" "10.40.0.44:8400")
INSERT_HOSTS=("10.40.0.31" "10.40.0.32")
SELECT_HOSTS=("10.40.0.21" "10.40.0.22")

REPLICATION_FACTOR=2
RETENTION_PERIOD="13months"

VM_VERSION="v1.105.0"
# =====================

echo
echo "========================================"
echo "VictoriaMetrics ${ROLE} on ${NODE_NAME} (${NODE_IP})"
echo "========================================"
echo

echo "==> Base packages"
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get -y install \
  curl wget ca-certificates jq socat ufw chrony fail2ban \
  unattended-upgrades vim lsof tcpdump net-tools iproute2

systemctl enable --now chrony
systemctl enable --now fail2ban

echo "==> Kernel/sysctl tuning"
cat > /etc/sysctl.d/99-victoriametrics.conf <<'SYSCTL'
vm.swappiness = 1
fs.file-max = 2097152
net.core.somaxconn = 4096
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_syncookies = 1
net.ipv4.ip_local_port_range = 10240 65535
kernel.dmesg_restrict = 1
SYSCTL
sysctl --system

cat > /etc/security/limits.d/99-victoriametrics.conf <<'LIMITS'
victoriametrics soft nofile 200000
victoriametrics hard nofile 200000
LIMITS

echo "==> SSH hardening"
sed -i 's|^[#[:space:]]*PermitRootLogin.*|PermitRootLogin prohibit-password|' /etc/ssh/sshd_config
sed -i 's|^[#[:space:]]*PasswordAuthentication.*|PasswordAuthentication no|' /etc/ssh/sshd_config
sshd -t && systemctl restart ssh

echo "==> UFW"
ufw default deny incoming
ufw default allow outgoing
ufw allow 22/tcp
case "${ROLE}" in
  vminsert)
    ufw allow from "${CLUSTER_NETWORK}" to any port 8480 proto tcp ;;
  vmselect)
    ufw allow from "${CLUSTER_NETWORK}" to any port 8481 proto tcp ;;
  vmstorage)
    ufw allow from "${CLUSTER_NETWORK}" to any port 8400 proto tcp
    ufw allow from "${CLUSTER_NETWORK}" to any port 8401 proto tcp
    ufw allow from "${CLUSTER_NETWORK}" to any port 8482 proto tcp ;;
  vmagent)
    ufw allow from "${CLUSTER_NETWORK}" to any port 8429 proto tcp ;;
esac
ufw --force enable

echo "==> Creating victoriametrics user + dirs"
id -u victoriametrics >/dev/null 2>&1 || \
  useradd --system --no-create-home --shell /sbin/nologin victoriametrics

install -d -o victoriametrics -g victoriametrics -m 0750 /var/lib/victoriametrics
install -d -o victoriametrics -g victoriametrics -m 0750 /etc/victoriametrics
install -d -o victoriametrics -g victoriametrics -m 0750 /var/log/victoriametrics

echo "==> Downloading VictoriaMetrics ${VM_VERSION}"
TMP=$(mktemp -d)
cd "${TMP}"
case "${ROLE}" in
  vmstorage|vminsert|vmselect)
    PKG="victoria-metrics-linux-amd64-${VM_VERSION}-cluster.tar.gz"
    URL="https://github.com/VictoriaMetrics/VictoriaMetrics/releases/download/${VM_VERSION}/${PKG}"
    wget -q "${URL}" -O cluster.tgz
    tar xzf cluster.tgz
    install -m 0755 -o root -g root ${ROLE}-prod /usr/local/bin/${ROLE}
    ;;
  vmagent)
    PKG="vmutils-linux-amd64-${VM_VERSION}.tar.gz"
    URL="https://github.com/VictoriaMetrics/VictoriaMetrics/releases/download/${VM_VERSION}/${PKG}"
    wget -q "${URL}" -O vmutils.tgz
    tar xzf vmutils.tgz
    install -m 0755 -o root -g root vmagent-prod /usr/local/bin/vmagent
    ;;
esac
rm -rf "${TMP}"

echo "==> Writing flags + systemd unit"

case "${ROLE}" in
  vmstorage)
    cat > /etc/victoriametrics/${ROLE}.flags <<EOF
-storageDataPath=/var/lib/victoriametrics
-retentionPeriod=${RETENTION_PERIOD}
-vminsertAddr=:8400
-vmselectAddr=:8401
-httpListenAddr=:8482
-dedup.minScrapeInterval=15s
EOF
    ;;
  vminsert)
    STORAGE_LIST=$(IFS=,; echo "${STORAGE_HOSTS[*]}")
    cat > /etc/victoriametrics/${ROLE}.flags <<EOF
-httpListenAddr=:8480
-storageNode=${STORAGE_LIST}
-replicationFactor=${REPLICATION_FACTOR}
EOF
    ;;
  vmselect)
    STORAGE_LIST=$(IFS=,; echo "${STORAGE_HOSTS[*]/%/}")
    # vmselect dials vmselectAddr (:8401) on each storage; format host:8401
    STORAGE_LIST=$(printf ",%s:8401" "${STORAGE_HOSTS[@]%:*}")
    STORAGE_LIST=${STORAGE_LIST:1}
    cat > /etc/victoriametrics/${ROLE}.flags <<EOF
-httpListenAddr=:8481
-storageNode=${STORAGE_LIST}
-replicationFactor=${REPLICATION_FACTOR}
-dedup.minScrapeInterval=15s
-search.maxQueryDuration=120s
EOF
    ;;
  vmagent)
    REMOTE_URLS=""
    for h in "${INSERT_HOSTS[@]}"; do
      REMOTE_URLS+=" -remoteWrite.url=http://${h}:8480/insert/0/prometheus/api/v1/write"
    done
    cat > /etc/victoriametrics/${ROLE}.flags <<EOF
-promscrape.config=/etc/victoriametrics/scrape.yml
-remoteWrite.tmpDataPath=/var/lib/victoriametrics/vmagent-buffer
-remoteWrite.maxDiskUsagePerURL=10GB
-httpListenAddr=:8429
${REMOTE_URLS}
EOF

    cat > /etc/victoriametrics/scrape.yml <<'SCRAPE'
global:
  scrape_interval: 15s
scrape_configs:
  - job_name: 'self'
    static_configs:
      - targets: ['localhost:8429']
SCRAPE
    ;;
esac

chown victoriametrics:victoriametrics /etc/victoriametrics/${ROLE}.flags
chmod 640 /etc/victoriametrics/${ROLE}.flags

cat > /etc/systemd/system/${ROLE}.service <<EOF
[Unit]
Description=VictoriaMetrics ${ROLE}
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=victoriametrics
Group=victoriametrics
ExecStart=/usr/local/bin/${ROLE} \$(cat /etc/victoriametrics/${ROLE}.flags | xargs)
Restart=on-failure
RestartSec=5
LimitNOFILE=200000
TimeoutStopSec=60

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable --now ${ROLE}

echo "==> Waiting for ${ROLE} listener"
case "${ROLE}" in
  vmstorage) PORT=8482 ;;
  vminsert)  PORT=8480 ;;
  vmselect)  PORT=8481 ;;
  vmagent)   PORT=8429 ;;
esac

for i in {1..60}; do
  if ss -lnt "sport = :${PORT}" | grep -q LISTEN; then break; fi
  sleep 2
done

echo
echo "========================================"
echo "DONE — ${NODE_NAME} (${ROLE})"
echo "========================================"
echo
ss -lntp | grep -E ':(8400|8401|8429|8480|8481|8482)\b' || true
echo
curl -fs "http://127.0.0.1:${PORT}/-/healthy" 2>/dev/null || \
curl -fs "http://127.0.0.1:${PORT}/health" 2>/dev/null || true
echo
echo "systemctl status ${ROLE}:"
systemctl --no-pager status ${ROLE} | head -15 || true

Make executable and run on each node — start with vmstorage nodes, then vminsert, then vmselect, then vmagent last:

chmod 700 /root/setup-victoriametrics-node.sh
/root/setup-victoriametrics-node.sh

Once everything is up, confirm cluster health:

# vminsert sees all storage nodes
curl -fs http://10.40.0.31:8480/metrics | grep vm_rpc_connection_status

# vmselect can fan out
curl -fs "http://10.40.0.21:8481/select/0/prometheus/api/v1/query?query=up" | jq .

# Push 10k synthetic series via vmagent's remote_write
cat > /tmp/load.sh <<'LOAD'
for i in {1..10000}; do
  ts=$(date +%s)
  echo "load_test{instance=\"node-$((i % 100))\",job=\"synthetic\"} $((RANDOM % 1000)) ${ts}000"
done | curl -fs -XPOST \
  --data-binary @- \
  "http://10.40.0.31:8480/insert/0/prometheus/api/v1/import/prometheus"
LOAD
bash /tmp/load.sh

sleep 5
curl -fs -G "http://10.40.0.21:8481/select/0/prometheus/api/v1/query" \
  --data-urlencode 'query=count(load_test)' | jq .

The query should return 10000. If it returns a smaller number, the vmagent buffer is still draining — check vm_persistentqueue_blocks{path=~".*buffer.*"} on vmagent. If it returns a larger number, your -dedup.minScrapeInterval on vmselect doesn’t match what vmagent is producing, and you’re double-counting.

For production-grade VictoriaMetrics clusters we deploy and operate — sharding strategy, vmagent buffer alerting, storage-tier sizing — see Cloud Compute under Managed Cloud Ops, or book a discovery session if you’re outgrowing single-node Prometheus.