k8s集群外exporter怎么使用Prometheus监控

要在 A 的 Kubernetes 集群中运行 Prometheus 并监控 B 的 Kubernetes 集群,您可以通过以下步骤实现:

1. 设置网络连接

  • 跨集群网络连接: 确保 A 集群中的 Prometheus 可以通过网络访问 B 集群中的 Kubernetes API 和监控目标。通常,这需要配置跨集群的网络连接,如 VPN、VPC Peering、或通过公共 IP 和适当的防火墙规则。
  • API 访问权限: B 集群的 Kubernetes API 必须对 A 集群的 Prometheus 开放,并且要确保 Prometheus 有权限通过 Kubernetes API 进行服务发现和抓取数据。

2. 在 B 集群中设置 Prometheus 的 ServiceAccount 和 RBAC 权限

  • ServiceAccount: 在 B 集群中,创建一个专用的 ServiceAccount,以便 Prometheus 能够使用该身份访问 B 集群的资源。
  • RBAC(角色和角色绑定): 为这个 ServiceAccount 配置适当的 RBAC 权限,通常需要允许访问以下资源:
    • Pods
    • Services
    • Endpoints
    • Nodes
    • Namespaces

示例 RBAC 配置:

yaml 复制代码
yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: prometheus
  namespace: monitoring

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: prometheus
rules:
  - apiGroups: [""]
    resources:
      - nodes
      - nodes/proxy
      - services
      - endpoints
      - pods
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources:
      - configmaps
    verbs: ["get"]
  - nonResourceURLs: ["/metrics"]
    verbs: ["get"]

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: prometheus
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: prometheus
subjects:
  - kind: ServiceAccount
    name: prometheus
    namespace: monitoring

yaml

3. 在 A 集群的 Prometheus 配置中添加 B 集群的监控目标

配置 Kubernetes 服务发现

在 Prometheus 的配置文件 prometheus.yml 中,使用 kubernetes_sd_configs 来配置对 B 集群的服务发现。需要使用 B 集群的 Kubernetes API 地址,并配置相应的凭据(如 Bearer Token 或 TLS 证书)。

示例配置:

yaml 复制代码
yaml
scrape_configs:
  - job_name: 'kubernetes-b-cluster'
    kubernetes_sd_configs:
      - api_server: 'https://<B-cluster-api-server>'
        role: pod
        bearer_token_file: '/path/to/b-cluster-token'
        tls_config:
          ca_file: '/path/to/ca.crt'
          cert_file: '/path/to/prometheus.crt'
          key_file: '/path/to/prometheus.key'
    relabel_configs:
      - source_labels: [__meta_kubernetes_namespace]
        action: keep
        regex: monitoring  # 仅监控 'monitoring' 命名空间中的 Pod

  - job_name: 'kubernetes-b-cluster-services'
    kubernetes_sd_configs:
      - api_server: 'https://<B-cluster-api-server>'
        role: service
        bearer_token_file: '/path/to/b-cluster-token'
        tls_config:
          ca_file: '/path/to/ca.crt'
          cert_file: '/path/to/prometheus.crt'
          key_file: '/path/to/prometheus.key'

yaml
  • `:B 集群的 Kubernetes API 服务器的 URL。
  • bearer_token_file`:用于访问 B 集群的 Bearer Token 文件路径。
  • tls_config:用于安全连接的 TLS 证书和密钥文件路径。
选择合适的角色
  • role: pod 用于发现 Pod。
  • role: service 用于发现服务。

4. 生成并配置访问凭据

  • Bearer Token:在 B 集群中创建用于 Prometheus 访问的 ServiceAccount,并获取其 Bearer Token,将其配置到 A 集群中的 Prometheus。
  • TLS 证书:如果需要使用 TLS,生成并配置用于访问 B 集群的证书。

5. 验证连接

  • 测试访问 B 集群的 API:使用 curl 或其他工具从 A 集群中测试访问 B 集群的 Kubernetes API,确保网络和权限配置正确。
  • 检查 Prometheus Targets 页面:访问 Prometheus 的 Web 界面,检查 Targets 页面,确认 B 集群的监控目标是否正常显示并被采集数据。

6. 优化和扩展

  • 分区监控配置:根据需要将 B 集群的监控配置与 A 集群分开,使用不同的 job_name,方便管理和查看。
  • 跨集群聚合:如果监控多个集群,可以考虑使用 Thanos 等工具对多个集群的 Prometheus 实例进行统一查询和聚合。

7. 高可用性和容错

  • 多集群配置:为防止网络故障或集群故障,考虑在多个 Prometheus 实例中配置对 B 集群的监控,并使用工具如 Thanos 聚合监控数据。
  • 负载均衡:如果 B 集群有多个 Kubernetes API 服务器节点,配置负载均衡以确保对 API 的访问高可用。

通过以上步骤,您可以在 A 的 Kubernetes 集群中运行 Prometheus,并成功监控位于 B 的 Kubernetes 集群中的服务和应用。这种方法在跨多个集群进行集中监控时非常有用。

相关推荐
三十八度的风1 小时前
Mac上搭建k8s环境——Minikube
macos·kubernetes·minikube·k8s安装
风车带走过往1 小时前
k8s常见面试题1
云原生·容器·kubernetes
NiNg_1_2342 小时前
Docker最佳实践:安装Nacos
运维·docker·容器
timerring2 小时前
Docker 101
运维·docker·容器
bruce1283 小时前
pushgateway指标聚合问题
prometheus·pushgateway
学Linux的语莫4 小时前
k8s中,一.pod污点,二.pod容器污点容忍策略,三.pod优先级(PriorityClass类)
linux·docker·容器·kubernetes
不懂说话的猿7 小时前
基于Docker搭建ES集群,并设置冷热数据节点
elasticsearch·docker·容器·集群·冷热数据
mpb8 小时前
docker-compose 配置nginx
nginx·docker·容器
景天科技苑10 小时前
【Prometheus】如何通过golang生成prometheus格式数据
开发语言·golang·prometheus·prometheus数据