收集metrics
通过本文档,我们预期达成以下目标:
- 部署grafana-agent到您的K8s集群中;
- 配置grafana-agent抓取kubelet和cAdvisor的metrics;
- 一个开启RBAC(role-based access control)的Kubernetes集群;
- 安装并配置好了kubectl命令行工具;
export NAMESPACE=default
export CLUSTER_NAME=kubernetes
export FC_REMOTE_WRITE_URL=http://10.206.0.16:8480/insert/0/prometheus/api/v1/write
#export FC_REMOTE_WRITE_URL=https://n9e-server:19000/prometheus/v1/write
#export FC_REMOTE_WRITE_USERNAME=fc_laiwei
#export FC_REMOTE_WRITE_PASSWORD=fc_laiweisecret
cat <<EOF |
kind: ConfigMap
metadata:
name: grafana-agent
apiVersion: v1
data:
agent.yaml: |
server:
http_listen_port: 12345
metrics:
wal_directory: /tmp/grafana-agent-wal
global:
scrape_timeout: 10s
cluster: ${CLUSTER_NAME}
configs:
- name: integrations
remote_write:
- url: ${FC_REMOTE_WRITE_URL}
basic_auth:
username: ${FC_REMOTE_WRITE_USERNAME}
password: ${FC_REMOTE_WRITE_PASSWORD}
scrape_configs:
- job_name: integrations/kubernetes/cadvisor
bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
kubernetes_sd_configs:
- role: node
metric_relabel_configs:
- action: drop
regex: container_([a-z_]+);
source_labels:
- __name__
- image
- action: drop
regex: container_(network_tcp_usage_total|network_udp_usage_total|tasks_state|cpu_load_average_10s)
source_labels:
- __name__
relabel_configs:
- replacement: kubernetes.default.svc:443
target_label: __address__
- regex: (.+)
replacement: /api/v1/nodes/\$1/proxy/metrics/cadvisor
- __meta_kubernetes_node_name
scheme: https
tls_config:
ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
insecure_skip_verify: false
server_name: kubernetes
- job_name: integrations/kubernetes/kubelet
bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
kubernetes_sd_configs:
- role: node
relabel_configs:
- replacement: kubernetes.default.svc:443
target_label: __address__
- regex: (.+)
replacement: /api/v1/nodes/\$1/proxy/metrics
source_labels:
- __meta_kubernetes_node_name
target_label: __metrics_path__
scheme: https
tls_config:
ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
insecure_skip_verify: false
server_name: kubernetes
EOF
envsubst | kubectl apply -n $NAMESPACE -f -
对于采集MySQLd_Exporter等需要运行多个grafana-agent实例的情况,推荐以deployment运行。
export NAMESPACE=default
MANIFEST_URL=https://raw.githubusercontent.com/flashcatcloud/fc-agent/fc-release/etc/k8s/agent-deployment.yaml
kubectl rollout restart deployment/grafana-agent
至此,我们已经完成了在K8s中部署grafana-agent并收集metrics,进一步,我们还可以配置grafana-agent来。