kubernetes - 如何解决Prometheus图表Ingres/Service映射?

标签 kubernetes prometheus kubernetes-helm nginx-ingress

刚刚安装了稳定/普罗米修斯图表,其值如下,我可以从 Pod 访问服务器前端,但不能从主机的 Web 浏览器访问。

我的values.yaml:

alertmanager:
  ingress:
    enabled: true
    annotations:
      kubernetes.io/ingress.class: nginx
    hosts:
      - localhost/alerts

server:
  ingress:
    enabled: true
    annotations:
      kubernetes.io/ingress.class: nginx
    hosts:
      - localhost/prom

pushgateway:
  ingress:
    enabled: true
    annotations:
      kubernetes.io/ingress.class: nginx
    hosts:
      - localhost/push

我使用 nginx 入口并创建入口,但由于某些未知原因,它似乎没有映射到服务。

一些数据:

我可以通过默认名称和 DNS 服务名称从入口 Pod(以及所有其他 Pod)访问服务器:

kubectl exec -it nginx-ingress-controller-5cb489cd48-t4dgv -- sh
/etc/nginx $ curl prometheus-server.default.svc.cluster.local
<a href="/graph">Found</a>

/etc/nginx $ curl prometheus-server
<a href="/graph">Found</a>

图表创建的事件入口列表:

kubectl get ingress
NAME                      HOSTS       ADDRESS     PORTS   AGE
nginx-ingress             localhost   localhost   80      37h
prometheus-alertmanager   localhost   localhost   80      43m
prometheus-pushgateway    localhost   localhost   80      43m
prometheus-server         localhost   localhost   80      43m

活跃服务资源列表:

kubectl get svc
NAME                            TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
kubernetes                      ClusterIP      10.96.0.1        <none>        443/TCP                      37h
nginx-deployment                ClusterIP      10.100.1.167     <none>        80/TCP                       37h
nginx-ingress-controller        LoadBalancer   10.109.57.131    localhost     80:32382/TCP,443:30669/TCP   36h
nginx-ingress-default-backend   ClusterIP      10.107.91.35     <none>        80/TCP                       36h
php-deployment                  ClusterIP      10.105.73.26     <none>        9000/TCP                     37h
prometheus-alertmanager         ClusterIP      10.97.89.149     <none>        80/TCP                       44m
prometheus-kube-state-metrics   ClusterIP      None             <none>        80/TCP,81/TCP                44m
prometheus-node-exporter        ClusterIP      None             <none>        9100/TCP                     44m
prometheus-pushgateway          ClusterIP      10.105.81.111    <none>        9091/TCP                     44m
prometheus-server               ClusterIP      10.108.225.187   <none>        80/TCP                       44m

另一方面,如果我将子域声明为入口主机,则可以访问 Prometheus:

alertmanager:
  ingress:
    enabled: true
    annotations:
      kubernetes.io/ingress.class: nginx
    hosts:
      - alerts.localhost

server:
  ingress:
    enabled: true
    annotations:
      kubernetes.io/ingress.class: nginx
    hosts:
      - prom.localhost

pushgateway:
  ingress:
    enabled: true
    annotations:
      kubernetes.io/ingress.class: nginx
    hosts:
      - push.localhost

我做错了什么还是有什么问题? 有什么建议吗?

提前致谢!

Helm 和 Kubernetes 的版本: Helm 3.0.3/Kubernetes 1.15.5(Mac 版 Docker,MacOS Catalina)

最佳答案

我重现了您的场景,并通过运行一些测试,我了解到这不会按照您希望的方式工作。这不是正确的实现方式。

让我们深入探讨一下。

您可以在入口中添加nginx.ingress.kubernetes.io/rewrite-target,如下例所示:

$ kubectl get ingresses. myprom-prometheus-pushgateway -oyaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
  creationTimestamp: "2020-02-18T09:51:32Z"
  generation: 1
  labels:
    app: prometheus
    chart: prometheus-10.4.0
    component: pushgateway
    heritage: Helm
    release: myprom
  name: myprom-prometheus-pushgateway
  namespace: default
  resourceVersion: "3239"
  selfLink: /apis/extensions/v1beta1/namespaces/default/ingresses/myprom-prometheus-pushgateway
  uid: 499372f4-52b1-4b37-982c-b52e70657d37
spec:
  rules:
  - host: localhost
    http:
      paths:
      - backend:
          serviceName: myprom-prometheus-pushgateway
          servicePort: 9091
        path: /push
status:
  loadBalancer:
    ingress:
    - ip: 192.168.39.251

添加此内容后,您将能够按照您的意愿进行访问。不幸的是,完成此操作后,您将面临一个新问题。如果我们检查curl命令的html输出,我们可以看到:

<!DOCTYPE html>

<html lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="robots" content="noindex,nofollow">
    <title>Prometheus Pushgateway</title>

    <link rel="shortcut icon" href="/static/favicon.ico?v=793293bdadd51fdaca69de5bb25637b0f93b656b">

    <script src="/static/jquery-3.4.1.min.js?v=793293bdadd51fdaca69de5bb25637b0f93b656b"></script>
    <script src="/static/bootstrap-4.3.1-dist/js/bootstrap.min.js?v=793293bdadd51fdaca69de5bb25637b0f93b656b"></script>
    <script src="/static/functions.js?v=793293bdadd51fdaca69de5bb25637b0f93b656b"></script>

    <link type="text/css" rel="stylesheet" href="/static/bootstrap-4.3.1-dist/css/bootstrap.min.css?v=793293bdadd51fdaca69de5bb25637b0f93b656b">
    <link type="text/css" rel="stylesheet" href="/static/prometheus.css?v=793293bdadd51fdaca69de5bb25637b0f93b656b">
    <link type="text/css" rel="stylesheet" href="/static/bootstrap4-glyphicons/css/bootstrap-glyphicons.min.css?v=793293bdadd51fdaca69de5bb25637b0f93b656b">
  </head>

可以看出,例如,我们有 /static/jquery-3.4.1.min.js,因此这会将您的浏览器重定向到不同的位置。问题是你没有这个位置。

这就是为什么我建议您避免使用路径并坚持使用涉及使用子域的辅助解决方案。

alertmanager:
  ingress:
    enabled: true
    annotations:
      kubernetes.io/ingress.class: nginx
    hosts:
      - alerts.localhost

server:
  ingress:
    enabled: true
    annotations:
      kubernetes.io/ingress.class: nginx
    hosts:
      - prom.localhost

pushgateway:
  ingress:
    enabled: true
    annotations:
      kubernetes.io/ingress.class: nginx
    hosts:
      - push.localhost

关于kubernetes - 如何解决Prometheus图表Ingres/Service映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60232693/

相关文章:

prometheus - 在普罗米修斯中显示任意@Timed指标

prometheus - 如何使用带有 prometheus 的 blackbox_exporter ping 目标

kubernetes - 使用 Kubernetes Configmap 将文件保存到容器时如何将 YAML 转换为 JSON

kubernetes - 使用Helm图表在MutatingWebhookConfiguration中设置caBundle

kubernetes - 为什么许多官方 Helm 图表在Values.yaml中都包含密码/凭据?

git - kubernetes仪表板无法启动

kubernetes - 不同 kubernetes 命名空间中的多个 Traefik 实例

kubernetes - 与 etcd 直接通信的 Kubernetes 模块是什么

json - 如何使用 client_golang 在 prometheus 中提取指标

amazon-web-services - 使用 kops 在 AWS 上的 kubernetes 集群中自定义 SSL 证书,身份验证失败