Kubernetes - 监控系统statsd+influxdb+grafana

1. 概述

  • StatsD 是一个使用 Node.js 开发的简单的网络守护进程,通过 UDP 或者 TCP 方式侦听各种统计信息,包括计数器和定时器,并发送聚合信息到后端服务,例如 Graphite、ElasticSearch、InfluxDB 等等。
  • Grafana 是一个使用 Go 开发的开源的、功能齐全的、好看的仪表盘和图表的编辑器,可用来做日志的分析与展示曲线图(如 api 的请求日志),支持多种 backend,如 ElasticSearch、InfluxDB、OpenTSDB 等等。
  • InfluxDB 是一个使用 Go 语言开发的开源分布式时序、事件和指标数据库,无需外部依赖,其设计目标是实现分布式和水平伸缩扩展。

ref

  • StatsD支持的backend
    https://github.com/etsy/statsd/blob/master/docs/backend.md
  • grafana github
    https://github.com/grafana/grafana
  • grafana demo
    https://play.grafana.org
  • influxdb github
    https://github.com/influxdata/influxdb

docker image

  • https://hub.docker.com/r/samuelebistoletti/docker-statsd-influxdb-grafana
  • https://github.com/samuelebistoletti/docker-statsd-influxdb-grafana

2. 本地启动statsd+influxdb+grafana

docker run

$ docker run --ulimit nofile=66000:66000 \
  -d \
  --name docker-statsd-influxdb-grafana \
  -p 3003:3003 \
  -p 3004:8888 \
  -p 8086:8086 \
  -p 22022:22 \
  -p 8125:8125/udp \
  samuelebistoletti/docker-statsd-influxdb-grafana:2.2.0

$ docker ps
CONTAINER ID        IMAGE                                                    COMMAND                  CREATED             STATUS              PORTS                                                                                                                   NAMES
1e405d343f6c        samuelebistoletti/docker-statsd-influxdb-grafana:2.2.0   "/usr/bin/supervisord"   4 seconds ago       Up 3 seconds        0.0.0.0:3003->3003/tcp, 0.0.0.0:8086->8086/tcp, 0.0.0.0:8125->8125/udp, 0.0.0.0:22022->22/tcp, 0.0.0.0:3004->8888/tcp   docker-statsd-influxdb-grafana

SSH

ssh root@localhost -p 22022
密码:root

grafana portal

地址:http://localhost:3003
账号密码:root/root

image.png
  • Add data source on Grafana

Url: http://localhost:8086
Database: telegraf
User: telegraf
Password: telegraf

influxdb portal

URL: http://localhost:3004
Username: root
Password: root
Port: 8086

image.png

push data to statsd

  • push_data.py
import statsd
from time import sleep, time
from random import random

STATSD_HOST = 'localhost'
client = statsd.StatsClient(host=STATSD_HOST, port= 8125, prefix='test.statsd')


def push_data_to_statsd():
    service_id = 'abcd1234'
    total_time = random() * 100.0
    res = client.incr('service.%s.success' % service_id)
    client.timing('service.%s.total_time' % service_id, total_time)
    with client.timer('service.%s.calling_time' % service_id):
        sleep(random())


if __name__ == '__main__':
    while True:
        push_data_to_statsd()
        sleep(1)
  • 通过Grafana配置Panel
image.png

3. K8S启动statsd+influxdb+grafana

  • deploy.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: statsd-influxdb-grafana-deployment
  namespace: shuzhang
spec:
  replicas: 1
  selector:
    matchLabels:
      app: statsd-influxdb-grafana
  template:
    metadata:
      labels:
        app: statsd-influxdb-grafana
    spec:
      containers:
      - name: statsd-influxdb-grafana
        image: samuelebistoletti/docker-statsd-influxdb-grafana:2.2.0
        ports:
        - name: grafana
          containerPort: 3003
        - name: influxdb-admin
          containerPort: 8888
        - name: influxdb
          containerPort: 8086
        - name: statsd
          containerPort: 8125
          protocol: UDP
---
apiVersion: v1
kind: Service
metadata:
  name: statsd-influxdb-grafana-svc
  namespace: shuzhang
spec:
  ports:
  - name: grafana
    port: 3003
    targetPort: 3003
  - name: influxdb-admin
    port: 3004
    targetPort: 8888
  - name: influxdb
    port: 8086
    targetPort: 8086
  - name: statsd
    port: 8125
    targetPort: 8125
    protocol: UDP
  selector:
    app: statsd-influxdb-grafana
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: statsd-influxdb-grafana-ing
  namespace: shuzhang
  # annotations:
  #   kubernetes.io/ingress.class: nginx
  #   nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - host: grafana.shuzhang.io
      http:
        paths:
        - path: /
          backend:
            serviceName: statsd-influxdb-grafana-svc
            servicePort: 3003
    - host: influxdb.shuzhang.io
      http:
        paths:
        - path: /
          backend:
            serviceName: statsd-influxdb-grafana-svc
            servicePort: 3004
  • 本地配置dns后,可访问grafana.shuzhang.io和influxdb.shuzhang.io
172.17.8.102 grafana.shuzhang.io
172.17.8.102 influxdb.shuzhang.io
  • 备注:
    a. k8s貌似不能直接代理tcp/udp端口,需要创建ingress-controller,完成8125/udp端口的映射,才能被cluster外部访问
    b. k8s内部访问statsd,可以直接通过service name访问statsd,如果跨namespace,也OK

4. References

  • 使用 StatsD + Grafana + InfluxDB 搭建 Node.js 监控系统
    https://juejin.im/entry/58561a87ac502e0067eb0113

你可能感兴趣的:(Kubernetes - 监控系统statsd+influxdb+grafana)