【问题标题】:"Connect: Connection Refused" when Connecting Prometheus to Kubernetes将 Prometheus 连接到 Kubernetes 时出现“Connect: Connection Refused”
【发布时间】:2019-02-24 01:25:24
【问题描述】:

我是 Prometheus 的新手,对 kubernetes 还比较陌生,所以请多多包涵。我正在尝试测试 Prometheus 并尝试了两种不同的方法。

  1. 将 Prometheus 作为 Kubernetes 之外的 docker 容器运行。为此,我创建了这个 Dockerfile:

    FROM prom/prometheus
    ADD prometheus.yml /etc/prometheus/
    

    还有这个 yaml 文件:

    global:
      scrape_interval: 15s
      external_labels:
        monitor: 'codelab-monitor'
    scrape_configs:
    - job_name: 'kubernetes-apiservers'
      scheme: http
      tls_config:
        ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
      bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
      kubernetes_sd_configs:
      - role: endpoints
        api_server: localhost:443
    

    当我运行它时,我得到:

    Failed to list *v1.Pod: Get http://localhost:443/api/v1/pods?limit=500&resourceVersion=0: dial tcp 127.0.0.1:443: connect: connection refused"
    Failed to list *v1.Service: Get http://localhost:443/api/v1/pods?limit=500&resourceVersion=0: dial tcp 127.0.0.1:443: connect: connection refused"
    Failed to list *v1.Endpoints: Get http://localhost:443/api/v1/pods?limit=500&resourceVersion=0: dial tcp 127.0.0.1:443: connect: connection refused"
    

    在一个循环中。当我访问 localhost:9090 但没有数据时,Prometheus 会加载。

  2. 我认为将 Prometheus 部署为 Kubernetes 部署可能会有所帮助,所以我制作了这个 yaml 并进行了部署。

    kind: Deployment
    apiVersion: extensions/v1beta1
    metadata:
      name: prometheus-monitor
    spec:
      selector:
        matchLabels:
          app: prometheus
      template:
        metadata:
          labels:
            app: prometheus
        spec:
          containers:
          - name: prometheus-monitor
            image: prom/prometheus
            # args:
            #   - '-config.file=/etc/prometheus/prometheus.yaml'
            imagePullPolicy: IfNotPresent
            ports:
            - name: webui
              containerPort: 9090
    

    部署成功,但如果我转到 localhost:9090,我会得到“ERR_SOCKET_NOT_CONNECTED”。 (我的端口被转发)

谁能告诉我使用 Kubernetes 与使用 Kubernetes 的优势以及如何解决这些问题中的至少一个?

另外,我的配置文件被禁止显示,因为它给出了一个错误,一旦我能够加载 Prometheus,我将对此进行调查。

【问题讨论】:

    标签: docker kubernetes yaml prometheus


    【解决方案1】:

    当您部署容器时,Kubernetes 不会映射其集群之外的端口。

    您还必须创建一个服务(可以在同一个文件中)以使其可从您的工作站使用(将其附加到您的 prometheus yaml):

    ---
    apiVersion: v1
    kind: Service
    metadata:
        name: prometheus-web
        labels:
            app: prometheus
    spec:
        type: NodePort
        ports:
            - port: 9090
              protocol: TCP
              targetPort: 9090
              nodePort: 30090
              name: webui
        selector:
            app: prometheus
    

    NodePort 在您拥有的所有节点上打开给定端口。你应该可以看到http://localhost:30090/的前端

    默认情况下,kubernetes 允许端口 30000 到 32767 用于 NodePort 类型 (https://kubernetes.io/docs/concepts/services-networking/service/#nodeport)。

    请考虑阅读一般文档以了解有关 kubernetes 服务的更多信息:https://kubernetes.io/docs/concepts/services-networking/service/

    【讨论】:

      【解决方案2】:

      所以 2 个不同的问题。开:

      1. 您正在尝试连接到 Prometheus 正在运行的 localhost:443,它期望与 Kubernetes API 服务器通信。显然,没有在 localhost:443 上监听。您是否正在向您的 kube-apiserver 进行端口转发?

      2. 在这种情况下,您需要公开您的部署端口。像这样的东西:

         kubectl expose deployment prmetheus-web --type=LoadBalancer # or 
         kubectl expose deployment prmetheus-web --type=NodePort
        

        取决于您希望如何公开您的服务。 NodePort 将其暴露在映射到 Kubernetes 节点上的端口(IPAddress:Port)的服务中,LoadBalancer 使用外部负载均衡器公开部署,该负载均衡器可能因您使用的云(AWS、GCP、OpenStack、Azure 等)而异.更多关于暴露你的 Deployments 或 DaemonSets 或 StatefulSets here。更多关于服务here

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多