k8s helm安装VictoriaMetrics介绍

 

以下内容由ai生成

 

一、VictoriaMetrics 介绍

 
VictoriaMetrics 是一款高性能、高压缩率、易扩展的开源时序数据库(TSDB),专为云原生监控、APM、IoT 等大规模时序数据场景设计,是 Prometheus 主流的高性能替代 / 长期存储方案。
 
image
 
 

1. 核心优势

 
  • 极致性能
    • 写入:单机支持 数百万指标 / 秒 写入VictoriaMetrics
    • 查询:复杂查询比 Prometheus 快 2~10 倍,大时间范围查询优势更明显
    • 内存占用:比 Prometheus 低 3~4 倍
     
  • 超高压缩
    • 自研高效压缩算法,相同数据磁盘占用仅为 Prometheus 的 1/7~1/10VictoriaMetrics
     
  • 架构简洁
    • 单二进制文件,无任何外部依赖(无需 ZooKeeper、Etcd 等)
    • 数据存储在单一目录,备份 / 迁移 / 运维极简
     
  • 完美兼容 Prometheus
    • 完全兼容 PromQL(扩展为 MetricsQL)
    • 兼容 Prometheus 远程读写、服务发现、AlertManager、Grafana 面板
    • 可无缝替代 Prometheus 或作为其远程长期存储
     
  • 两种部署模式
    • 单机版(VMSingle):中小规模,一键部署,高可用靠备份
    • 集群版(Cluster):大规模场景,无共享分布式架构(SN 架构),组件独立伸缩VictoriaMetrics
     
 

2. 集群版核心组件VictoriaMetrics

 
  • vmstorage:有状态存储节点,负责数据存储与查询,节点间无通信、不共享数据
  • vminsert:无状态写入代理,接收指标并哈希分片到多个 vmstorage
  • vmselect:无状态查询代理,分发查询到所有 vmstorage 并聚合结果

vmagent:轻量采集器,替代 Prometheus 采集,支持 PULL/PUSH,可远程写入

 
image
 
 
 

二、K8s 环境部署 VictoriaMetrics(推荐方式二)

推荐两种主流方式:Helm 部署集群版(生产推荐)、Operator 部署(CRD 化管理)。

方式一:Helm 部署 VictoriaMetrics 集群(生产标准)

前提
  • Kubernetes 1.21+
  • Helm 3.10+
  • kubectl 有权限
1. 添加 Helm 仓库
helm repo add vm https://victoriametrics.github.io/helm-charts/
helm repo update
2. 部署集群(3 副本高可用)
bash
运行
helm install victoria-metrics vm/victoria-metrics-cluster \
  --namespace monitoring \
  --create-namespace \
  --set vmstorage.replicaCount=3 \
  --set vminsert.replicaCount=2 \
  --set vmselect.replicaCount=2 \
  --set vmstorage.persistentVolume.size=50Gi \
  --set vmstorage.persistentVolume.storageClassName=gp2
3. 验证部署
bash
运行
kubectl get pods -n monitoring -l app=victoria-metrics-cluster
4. 服务访问(内部)
  • 写入地址:http://victoria-metrics-cluster-vminsert.monitoring.svc:8480/insert/0/prometheus/
  • 查询地址:http://victoria-metrics-cluster-vmselect.monitoring.svc:8481/select/0/prometheus/
5. 暴露服务(可选,NodePort/Ingress)
bash
运行
# NodePort 暴露查询
kubectl expose svc victoria-metrics-cluster-vmselect \
  --name vmselect-nodeport \
  --type NodePort \
  --port 8481 \
  --namespace monitoring

方式二:Operator 部署(CRD 化,灵活管理)

1. 安装 Operator
helm repo add vm https://victoriametrics.github.io/helm-charts/
helm repo update

 

helm install vm-operator vm/victoria-metrics-operator \
--namespace monitoring \
--set operator.replicaCount=1 \
--set tolerations[0].key=app \
--set tolerations[0].operator=Equal \
--set tolerations[0].value=vmstack-saas \
--set tolerations[0].effect=NoSchedule
(可选)指定节点组
helm install vm-operator vm/victoria-metrics-operator \
  --namespace monitoring \
  --set operator.replicaCount=1 \
  --set tolerations[0].key=app \
  --set tolerations[0].operator=Equal \
  --set tolerations[0].value=vmstack-saas \
  --set tolerations[0].effect=NoSchedule \
  --set operator.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms[0].matchExpressions[0].key=eks.amazonaws.com/nodegroup \
  --set operator.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms[0].matchExpressions[0].operator=In \
  --set operator.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms[0].matchExpressions[0].values[0]=eks-prod-paimatix-shared-critical-Az2 \
  --set operator.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms[0].matchExpressions[0].values[1]=eks-prod-paimatix-shared-critical-Az3

 

2. 安装 victoria组件
部署单机版(VMSingle)示例
yaml
# vm-single.yaml
apiVersion: operator.victoriametrics.com/v1beta1
kind: VMSingle
metadata:
  name: vm-single
  namespace: monitoring
spec:
  retentionPeriod: "30d"
  storage:
    volumeClaimTemplate:
      spec:
        resources:
          requests:
            storage: 50Gi
        storageClassName: local-path
  resources:
    requests:
      cpu: "1"
      memory: "2Gi"
 
kubectl apply -f vm-single.yaml
部署集群版(VMCluster)示例
cat > vmcluster.yaml << EOF
apiVersion: operator.victoriametrics.com/v1beta1
kind: VMCluster
metadata:
  name: vm
  namespace: monitoring
spec:
  retentionPeriod: "365d"
  vmstorage:
    replicaCount: 3
    storage:
      volumeClaimTemplate:
        spec:
          storageClassName: alicloud-disk-ssd
          resources:
            requests:
              storage: 50Gi
    tolerations:
    - key: "app"
      operator: "Equal"
      value: "vmstack-saas"
      effect: "NoSchedule"
  vminsert:
    replicaCount: 2
    tolerations:
    - key: "app"
      operator: "Equal"
      value: "vmstack-saas"
      effect: "NoSchedule"
  vmselect:
    replicaCount: 2
    tolerations:
    - key: "app"
      operator: "Equal"
      value: "vmstack-saas"
      effect: "NoSchedule"
EOF

 

(选做)部署集群版(VMCluster),配置指定节点组示例
cat > vmcluster.yaml << EOF
apiVersion: operator.victoriametrics.com/v1beta1
kind: VMCluster
metadata:
  name: vm
  namespace: monitoring
spec:
  retentionPeriod: "365d"
  vmstorage:
    replicaCount: 3
    storage:
      volumeClaimTemplate:
        spec:
          storageClassName: alicloud-disk-ssd
          resources:
            requests:
              storage: 50Gi
    tolerations:
    - key: "app"
      operator: "Equal"
      value: "vmstack-saas"
      effect: "NoSchedule"
    # 添加节点亲和性
    affinity:
      nodeAffinity:
        requiredDuringSchedulingIgnoredDuringExecution:
          nodeSelectorTerms:
          - matchExpressions:
            - key: eks.amazonaws.com/nodegroup
              operator: In
              values:
              - eks-prod-paimatix-shared-critical-Az2
              - eks-prod-paimatix-shared-critical-Az3
  vminsert:
    replicaCount: 2
    tolerations:
    - key: "app"
      operator: "Equal"
      value: "vmstack-saas"
      effect: "NoSchedule"
    # 添加节点亲和性
    affinity:
      nodeAffinity:
        requiredDuringSchedulingIgnoredDuringExecution:
          nodeSelectorTerms:
          - matchExpressions:
            - key: eks.amazonaws.com/nodegroup
              operator: In
              values:
              - eks-prod-paimatix-shared-critical-Az2
              - eks-prod-paimatix-shared-critical-Az3
  vmselect:
    replicaCount: 2
    tolerations:
    - key: "app"
      operator: "Equal"
      value: "vmstack-saas"
      effect: "NoSchedule"
    # 添加节点亲和性
    affinity:
      nodeAffinity:
        requiredDuringSchedulingIgnoredDuringExecution:
          nodeSelectorTerms:
          - matchExpressions:
            - key: eks.amazonaws.com/nodegroup
              operator: In
              values:
              - eks-prod-paimatix-shared-critical-Az2
              - eks-prod-paimatix-shared-critical-Az3
EOF

 

 

运行
kubectl apply -f vmcluster.yaml

 

部署完成后有如下信息,(方式一举例)

[ec2-user@paimatix-dev-kubectl ~]$ helm install victoria-metrics vm/victoria-metrics-cluster \
  --namespace monitoring \
  --create-namespace \
  --set vmstorage.replicaCount=3 \
  --set vminsert.replicaCount=2 \
  --set vmselect.replicaCount=2 \
  --set vmstorage.persistentVolume.size=50Gi \
  --set vmstorage.persistentVolume.storageClass=gp2
NAME: victoria-metrics
LAST DEPLOYED: Tue Apr 14 08:03:16 2026
NAMESPACE: monitoring
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
Write API:

The Victoria Metrics write api can be accessed via port 8480 with the following DNS name from within your cluster:
victoria-metrics-victoria-metrics-cluster-vminsert.monitoring.svc.cluster.local.

Get the Victoria Metrics insert service URL by running these commands in the same shell:
  export POD_NAME=$(kubectl get pods --namespace monitoring -l "app=vminsert" -l "app.kubernetes.io/instance=victoria-metrics" -o jsonpath="{.items[0].metadata.name}")
  kubectl --namespace monitoring port-forward $POD_NAME 8480

You need to update your Prometheus configuration file and add the following lines to it:

prometheus.yml

    remote_write:
      - url: "http://<insert-service>/insert/0/prometheus/"

for example -  inside the Kubernetes cluster:

    remote_write:
      - url: http://victoria-metrics-victoria-metrics-cluster-vminsert.monitoring.svc.cluster.local.:8480/insert/0/prometheus/
Read API:

The VictoriaMetrics read api can be accessed via port 8481 with the following DNS name from within your cluster:
victoria-metrics-victoria-metrics-cluster-vmselect.monitoring.svc.cluster.local.

Get the VictoriaMetrics select service URL by running these commands in the same shell:
  export POD_NAME=$(kubectl get pods --namespace monitoring -l "app=vmselect" -l "app.kubernetes.io/instance=victoria-metrics" -o jsonpath="{.items[0].metadata.name}")
  kubectl --namespace monitoring port-forward $POD_NAME 8481

You need to specify select service URL into your Grafana:
 NOTE: you need to use the Prometheus Data Source

Input this URL field into Grafana

    http://<select-service>/select/0/prometheus/


for example - inside the Kubernetes cluster:

    http://victoria-metrics-victoria-metrics-cluster-vmselect.monitoring.svc.cluster.local.:8481/select/0/prometheus/

 

三、安装vmagent

方式一:Helm 部署 VictoriaMetrics 集群

例子:监控kafka

1、安装vmagent

cat > vmagent.yaml << 'EOF'
apiVersion: v1
kind: ConfigMap
metadata:
  name: vmagent-config
  namespace: monitoring
data:
  prometheus.yml: |
    global:
      scrape_interval: 15s

    # 专门采集 kafka-exporter → job 名正确
    scrape_configs:
      - job_name: 'kafka-exporter'
        static_configs:
          - targets:
            - 'kafka-exporter.monitoring.svc:9308'

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: vmagent
  namespace: monitoring
spec:
  replicas: 1
  selector:
    matchLabels:
      app: vmagent
  template:
    metadata:
      labels:
        app: vmagent
    spec:
      serviceAccountName: vmagent
      containers:
      - name: vmagent
        image: victoriametrics/vmagent:latest
        args:
        - --promscrape.config=/etc/prometheus/prometheus.yml
        - --remoteWrite.url=http://victoria-metrics-victoria-metrics-cluster-vminsert.monitoring.svc:8480/insert/0/prometheus/
        volumeMounts:
        - name: config
          mountPath: /etc/prometheus
      volumes:
      - name: config
        configMap:
          name: vmagent-config
EOF

 

2、给vmagent权限,不然vmagent它没有权限monitoring 命名空间 查看 Pod

cat > vmagent-rbac.yaml << EOF
apiVersion: v1
kind: ServiceAccount
metadata:
  name: vmagent
  namespace: monitoring
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: vmagent
rules:
- apiGroups: [""]
  resources:
  - nodes
  - nodes/proxy
  - services
  - endpoints
  - pods
  verbs: ["get", "list", "watch"]
- apiGroups:
  - extensions
  - networking.k8s.io
  resources:
  - ingresses
  verbs: ["get", "list", "watch"]
- nonResourceURLs: ["/metrics"]
  verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: vmagent
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: vmagent
subjects:
- kind: ServiceAccount
  name: vmagent
  namespace: monitoring
EOF

3、安装vmagent

kubectl apply -f  vmagent-rbac.yaml
kubectl apply -f  vmagent.yaml

 

4、安装kafka exporter

vim kafka-exporter.yaml

 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: kafka-exporter
  namespace: monitoring
  labels:
    app: kafka-exporter
spec:
  replicas: 1
  selector:
    matchLabels:
      app: kafka-exporter
  template:
    metadata:
      labels:
        app: kafka-exporter
    spec:
      containers:
      - name: kafka-exporter
        image: danielqsj/kafka-exporter:latest
        args:
        - "--kafka.server=alikafka-pre-cn-dew4qq0ut001-1-vpc.alikafka.aliyuncs.com:9092"
        - "--kafka.server=alikafka-pre-cn-dew4qq0ut001-2-vpc.alikafka.aliyuncs.com:9092"
        - "--kafka.server=alikafka-pre-cn-dew4qq0ut001-3-vpc.alikafka.aliyuncs.com:9092"
        - "--web.listen-address=:9308"
        ports:
        - containerPort: 9308
          name: metrics
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
          limits:
            cpu: 2000m
            memory: 2000Mi
---
# Service 带正确标签!
apiVersion: v1
kind: Service
metadata:
  name: kafka-exporter
  namespace: monitoring
  labels:
    app: kafka-exporter  # 关键标签!VMServiceScrape 靠它发现
spec:
  ports:
  - port: 9308
    name: metrics
    targetPort: 9308
  selector:
    app: kafka-exporter

 

kc apply -f kafka-exporter.yaml

 

5、验证:起一个终端

kubectl port-forward -n monitoring svc/victoria-metrics-victoria-metrics-cluster-vmselect 8481

6、验证:另外起一个终端运行

curl 'http://127.0.0.1:8481/select/0/prometheus/api/v1/query?query=kafka_brokers'
{"status":"success","isPartial":false,"data":{"resultType":"vector","result":[{"metric":{"__name__":"kafka_brokers","instance":"kafka-exporter.monitoring.svc:9308","job":"kafka-exporter"},"value":[1776158258,"3"]},{"metric":{"__name__":"kafka_brokers","instance":"10.52.99.85:9308","job":"kafka-exporter"},"value":[1776158258,"3"]}]},"stats":{"seriesFetched": "2","executionTimeMsec":3}}

 

 

方式二:Operator 部署

例子:监控kafka

 

1、安装vmagent,给vmagent权限,不然vmagent它没有权限去 monitoring 命名空间 查看 Pod

 

vim vmagent-rbac.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: vmagent
  namespace: monitoring
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: vmagent
rules:
- apiGroups: [""]
  resources:
  - nodes
  - services
  - endpoints
  - pods
  - secrets      # 关键:加上这个!
  verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: vmagent
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: vmagent
subjects:
- kind: ServiceAccount
  name: vmagent
  namespace: monitoring

 

vim vmagent.yaml

 

apiVersion: operator.victoriametrics.com/v1beta1
kind: VMAgent
metadata:
  name: vmagent
  namespace: monitoring
spec:
  replicaCount: 1
  serviceAccountName: vmagent

  # 👇 正确:选择哪些命名空间里的 VMServiceScrape
  serviceScrapeNamespaceSelector:
    matchNames:
      - monitoring
      - internal
      - production
      - stagging
  # 为 VMStaticScrape 添加命名空间选择器,让它去 internal 命名空间里找
  staticScrapeNamespaceSelector:
    matchNames:
      - monitoring
      - internal
      - production
      - stagging
  podScrapeNamespaceSelector:
    matchNames:
      - monitoring
      - internal
      - production
      - stagging
  # 👇 正确:选择所有 VMServiceScrape(空选择器 + selectAllByDefault)
  selectAllByDefault: true
  serviceScrapeSelector: {}

  podScrapeSelector: {}
  staticScrapeSelector: {}

  remoteWrite:
  - url: "http://vminsert-vm.monitoring.svc:8480/insert/0/prometheus/"

 

kubectl apply -f vmagent-rbac.yaml
kubectl apply -f vmagent.yaml

 

2、创建 VMServiceScrape

cat > vmservicescrape-kafka.yaml << EOF
apiVersion: operator.victoriametrics.com/v1beta1
kind: VMServiceScrape
metadata:
  name: kafka-exporter
  namespace: monitoring
spec:
  # 你要的:job 名称
  jobLabel: kafka-exporter
  
  # 自动发现 Service
  selector:
    matchLabels:
      app: kafka-exporter

  # 采集端口
  endpoints:
  - port: 9308
    interval: 15s
    path: /metrics
EOF

 

3、安装kafka exporter

vim kafka-exporter.yaml

 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: kafka-exporter
  namespace: monitoring
  labels:
    app: kafka-exporter
spec:
  replicas: 1
  selector:
    matchLabels:
      app: kafka-exporter
  template:
    metadata:
      labels:
        app: kafka-exporter
    spec:
      containers:
      - name: kafka-exporter
        image: danielqsj/kafka-exporter:latest
        args:
        - "--kafka.server=alikafka-pre-cn-dew4qq0ut001-1-vpc.alikafka.aliyuncs.com:9092"
        - "--kafka.server=alikafka-pre-cn-dew4qq0ut001-2-vpc.alikafka.aliyuncs.com:9092"
        - "--kafka.server=alikafka-pre-cn-dew4qq0ut001-3-vpc.alikafka.aliyuncs.com:9092"
        - "--web.listen-address=:9308"
        ports:
        - containerPort: 9308
          name: metrics
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
          limits:
            cpu: 2000m
            memory: 2000Mi
---
# Service 带正确标签!
apiVersion: v1
kind: Service
metadata:
  name: kafka-exporter
  namespace: monitoring
  labels:
    app: kafka-exporter  # 关键标签!VMServiceScrape 靠它发现
spec:
  ports:
  - port: 9308
    name: metrics
    targetPort: 9308
  selector:
    app: kafka-exporter

 

kc apply -f kafka-exporter.yaml

 

4、创建kafka的VMServiceScrape(生产级自动采集)

vim vmservicescrape-kafka.yaml

 

apiVersion: operator.victoriametrics.com/v1beta1
kind: VMServiceScrape
metadata:
  name: kafka-exporter
  namespace: monitoring
spec:
  jobName: "kafka-exporter" 
  # 自动发现 Service
  selector:
    matchLabels:
      app: kafka-exporter

  # 采集端口
  endpoints:
  - port: "metrics"
    interval: 15s
    path: /metrics

 

kc apply -f  vmservicescrape-kafka.yaml

 

5、验证:起一个端口监听

kubectl port-forward -n monitoring svc/vmselect-vm 8481

6、验证:另外起一个终端访问

[ec2-user@paimatix-dev-kubectl VictoriaMetrics-operator]$  curl 'http://127.0.0.1:8481/select/0/prometheus/api/v1/query?query=kafka_brokers'
{"status":"success","isPartial":false,"data":{"resultType":"vector","result":[{"metric":{"__name__":"kafka_brokers","container":"kafka-exporter","instance":"10.52.98.189:9308","job":"kafka-exporter","namespace":"monitoring","pod":"kafka-exporter-5b7567f9d5-2p9md","prometheus":"monitoring/vmagent","service":"kafka-exporter"},"value":[1776164088,"3"]}]},"stats":{"seriesFetched": "1","executionTimeMsec":3}}

 

例外:查有哪些jobs

curl 'http://127.0.0.1:8481/select/0/prometheus/api/v1/label/job/values'

 

四、对接 Prometheus/Grafana

1. vmselect配置ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    alb.ingress.kubernetes.io/listen-ports: '[{"HTTP": 80}]'
    alb.ingress.kubernetes.io/scheme: internet-facing
    alb.ingress.kubernetes.io/target-type: ip
    kubernetes.io/ingress.class: alb
  name: vmselect-alb
  namespace: monitoring
spec:
  rules:
  - host: vmselect-paimatix.shoplazza.site
    http:
      paths:
      - backend:
          service:
            name: vmselect-vm
            port:
              number: 8481
        path: /
        pathType: Prefix
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    alb.ingress.kubernetes.io/listen-ports: '[{"HTTP": 80}]'
    alb.ingress.kubernetes.io/scheme: internet-facing
    alb.ingress.kubernetes.io/target-type: ip
    kubernetes.io/ingress.class: alb
  name: vmselect-alb
  namespace: monitoring
spec:
  rules:
  - host: vmselect-paimatix.shoplazza.site
    http:
      paths:
      - backend:
          service:
            name: vmselect-vm
            port:
              number: 8481
        path: /
        pathType: Prefix

 

2. Prometheus 远程读入(选做)
remote_write:
  - url: "http://victoria-metrics-cluster-vminsert.monitoring.svc:8480/insert/0/prometheus/api/v1/write"
3. Grafana 配置数据源
  • Type:Prometheus
  • URL:http://vmselect.monitoring.svc:8481/select/0/prometheus(这是k8s内部地址,如果grafana和vmselect在不同k8s集群,则写alb地址)
  • Access:Server(推荐)
posted @ 2026-04-14 16:21  苦逼yw  阅读(133)  评论(0)    收藏  举报