devops icon indicating copy to clipboard operation
devops copied to clipboard

k8s Prometheus 提供了多种服务发现(Service Discovery)机制,每种机制如何配置与使用

Open heidsoft opened this issue 2 years ago • 0 comments

Prometheus是一个开源的监控系统,它提供了多种服务发现(Service Discovery)机制,包括静态配置、文件服务发现、Kubernetes服务发现、Consul服务发现等等。

  1. 静态配置:这是最基础也是最直接的方式。在Prometheus的配置文件中,通过static_configs指定所有的服务实例。

    scrape_configs:
      - job_name: 'prometheus'
        static_configs:
          - targets: ['localhost:9090']
    
  2. 文件服务发现:Prometheus可以定期地从文件中读取服务实例。这对于那些不能直接集成Prometheus服务发现的环境非常有用。

    scrape_configs:
      - job_name: 'my_job'
        file_sd_configs:
          - files:
            - '/path/to/json/file.json'
    
  3. Kubernetes服务发现:Prometheus可以发现Kubernetes集群中的服务实例。它支持多种资源类型,包括pod、service、endpoints等。

    scrape_configs:
      - job_name: 'kubernetes-pods'
        kubernetes_sd_configs:
          - role: pod
    
  4. Consul服务发现:Prometheus可以发现Consul中注册的服务实例。

    scrape_configs:
      - job_name: 'consul'
        consul_sd_configs:
          - server: 'localhost:8500'
            services: ['web', 'db']
    
  5. EC2服务发现:Prometheus可以发现AWS EC2中的服务实例。

    scrape_configs:
      - job_name: 'ec2'
        ec2_sd_configs:
          - region: us-east-1
            access_key: 'access key'
            secret_key: 'secret key'
            role_arn: 'role arn'
    

在配置完服务发现之后,你需要重启Prometheus来使新的配置生效。服务发现会定期刷新,新发现的服务实例会被自动添加到监控目标中,不再存在的服务实例会被自动移除。

heidsoft avatar Oct 16 '23 10:10 heidsoft