"Connect: Connection Refused" 将 Prometheus 连接到 Kubernetes 时

"Connect: Connection Refused" when Connecting Prometheus to Kubernetes

我是 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,我就会调查一下。

当您部署容器时,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 个不同的问题。在:

  1. 您正在尝试连接到 localhost:443,其中 Prometheus 运行 并且它期望与 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. More about services here

希望对您有所帮助。