收集metrics

    通过本文档,我们预期达成以下目标:

    1. 部署grafana-agent到您的K8s集群中;
    2. 配置grafana-agent抓取kubelet和cAdvisor的metrics;
    1. 一个开启RBAC(role-based access control)的Kubernetes集群;
    2. 安装并配置好了kubectl命令行工具;
    1. export NAMESPACE=default
    2. export CLUSTER_NAME=kubernetes
    3. export FC_REMOTE_WRITE_URL=http://10.206.0.16:8480/insert/0/prometheus/api/v1/write
    4. #export FC_REMOTE_WRITE_URL=https://n9e-server:19000/prometheus/v1/write
    5. #export FC_REMOTE_WRITE_USERNAME=fc_laiwei
    6. #export FC_REMOTE_WRITE_PASSWORD=fc_laiweisecret
    7. cat <<EOF |
    8. kind: ConfigMap
    9. metadata:
    10. name: grafana-agent
    11. apiVersion: v1
    12. data:
    13. agent.yaml: |
    14. server:
    15. http_listen_port: 12345
    16. metrics:
    17. wal_directory: /tmp/grafana-agent-wal
    18. global:
    19. scrape_timeout: 10s
    20. cluster: ${CLUSTER_NAME}
    21. configs:
    22. - name: integrations
    23. remote_write:
    24. - url: ${FC_REMOTE_WRITE_URL}
    25. basic_auth:
    26. username: ${FC_REMOTE_WRITE_USERNAME}
    27. password: ${FC_REMOTE_WRITE_PASSWORD}
    28. scrape_configs:
    29. - job_name: integrations/kubernetes/cadvisor
    30. bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
    31. kubernetes_sd_configs:
    32. - role: node
    33. metric_relabel_configs:
    34. - action: drop
    35. regex: container_([a-z_]+);
    36. source_labels:
    37. - __name__
    38. - image
    39. - action: drop
    40. regex: container_(network_tcp_usage_total|network_udp_usage_total|tasks_state|cpu_load_average_10s)
    41. source_labels:
    42. - __name__
    43. relabel_configs:
    44. - replacement: kubernetes.default.svc:443
    45. target_label: __address__
    46. - regex: (.+)
    47. replacement: /api/v1/nodes/\$1/proxy/metrics/cadvisor
    48. - __meta_kubernetes_node_name
    49. scheme: https
    50. tls_config:
    51. ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
    52. insecure_skip_verify: false
    53. server_name: kubernetes
    54. - job_name: integrations/kubernetes/kubelet
    55. bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
    56. kubernetes_sd_configs:
    57. - role: node
    58. relabel_configs:
    59. - replacement: kubernetes.default.svc:443
    60. target_label: __address__
    61. - regex: (.+)
    62. replacement: /api/v1/nodes/\$1/proxy/metrics
    63. source_labels:
    64. - __meta_kubernetes_node_name
    65. target_label: __metrics_path__
    66. scheme: https
    67. tls_config:
    68. ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
    69. insecure_skip_verify: false
    70. server_name: kubernetes
    71. EOF
    72. envsubst | kubectl apply -n $NAMESPACE -f -

    对于采集MySQLd_Exporter等需要运行多个grafana-agent实例的情况,推荐以deployment运行。

    1. export NAMESPACE=default
    2. MANIFEST_URL=https://raw.githubusercontent.com/flashcatcloud/fc-agent/fc-release/etc/k8s/agent-deployment.yaml
    1. kubectl rollout restart deployment/grafana-agent

    至此,我们已经完成了在K8s中部署grafana-agent并收集metrics,进一步,我们还可以配置grafana-agent来。