# 让 Discourse 能够看到 Prometheus 服务器的 IP

**URL:** https://meta.discourse.org/t/getting-discourse-to-see-the-prometheus-server-ip/265015
**Category:** Self-hosting
**Tags:** prometheus
**Created:** [2020年六月26日 20:29 UTC](https://meta.discourse.org/t/getting-discourse-to-see-the-prometheus-server-ip/265015 "2020-06-26T20:29:07Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![ishan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ishan/32/182322_2.png) [@ishan](https://meta.discourse.org/u/ishan)
#### Post date: [2020年六月26日 20:29 UTC](https://meta.discourse.org/t/getting-discourse-to-see-the-prometheus-server-ip/265015/1 "2020-06-26T20:29:07Z")

</div>

大家好！

到目前为止，如果我的理解正确，要确保 Prometheus 实例能够从 `/metrics` 端点抓取数据，我们必须通过编辑 `DISCOURSE_PROMETHEUS_TRUSTED_IP_WHITELIST_REGEX` 将该机器的 IP 地址添加到允许列表中。

还有其他方法吗？我们的 Discourse 实例位于 Cloudflare 的 DNS 代理之后，无法看到最终用户的实际 IP 地址。我真的很希望能找到一种使用 API 密钥的方法，尽管由于 Prometheus 摄取数据的限制，这似乎不太可能。

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2020年六月26日 21:05 UTC](https://meta.discourse.org/t/getting-discourse-to-see-the-prometheus-server-ip/265015/2 "2020-06-26T21:05:57Z")

</div>

> [@ishan](#):
>
> 我们的 Discourse 实例位于 Cloudflare 的 DNS 代理之后，无法看到最终用户的实际 IP 地址。

那么 Discourse 就无法正常工作，您需要添加 Cloudflare 模板。

> [@ishan](#):
>
> 我真的很希望我们能找到一种使用 API 密钥的方法来实现这一点？

我认为这样可以。您尝试过了吗？哦，但我不认为您可以让 Grafana 将密钥放入请求头中，这也是我最初添加此功能的原因。

---

<div class="post-metadata">

### Author: ![ishan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ishan/32/182322_2.png) [@ishan](https://meta.discourse.org/u/ishan)
#### Post date: [2020年六月27日 06:21 UTC](https://meta.discourse.org/t/getting-discourse-to-see-the-prometheus-server-ip/265015/3 "2020-06-27T06:21:26Z")

</div>

> 那么 Discourse 就出问题了，你需要添加 Cloudflare 模板。

啊，你说的“Cloudflare 模板”是什么意思？

> 我想那样应该能行。你试过了吗？哦，但我认为你无法让 Grafana 将密钥放入请求头，这也是我最初添加此功能的原因。

是的，我试过了。Prometheus 仍然不允许在抓取配置中使用自定义请求头。因此，如果无法将 Prometheus 服务器的 IP 地址加入白名单（在我的情况下这是不可能的），我就无法在 Prometheus 中接收 Discourse 的指标。

可能还有其他人在 Cloudflare 等代理后面运行他们的 Discourse 实例。考虑到这一点，我相信我们需要对导出器进行一些调整。

Discourse 的 API 使用其自己的 `Api-Key` 和 `Api-Username` 请求头，而不是某种“相对标准化”的方式（例如 HTTP 基本认证，而 Prometheus 的抓取子系统支持该方式）。因此，在我的设置中，我真的无法使用这种方式。

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2020年六月27日 06:43 UTC](https://meta.discourse.org/t/getting-discourse-to-see-the-prometheus-server-ip/265015/4 "2020-06-27T06:43:41Z")

</div>

> [@ishan](#):
>
> “Cloudflare 模板”是什么意思？

您需要进行搜索。不过，您可以在 templates 目录中找到它，并将其添加到您的 app.yml 中。

---

<div class="post-metadata">

### Author: ![ishan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ishan/32/182322_2.png) [@ishan](https://meta.discourse.org/u/ishan)
#### Post date: [2020年六月27日 07:27 UTC](https://meta.discourse.org/t/getting-discourse-to-see-the-prometheus-server-ip/265015/5 "2020-06-27T07:27:42Z")

</div>

谢谢！

已启用 `cloudflare.template.yml`。🙂 但据我理解，那只是为了禁用来自 Cloudflare IP 地址的流量速率限制。

目前，我的 Prometheus 配置如下：

```plaintext
scrape_configs:
        - job_name: forum
          scrape_interval: 5s
          scheme: https
          static_configs:
                  - targets:
                          - forum-behind-cloudflare-dns-proxy.com

```

现在我面临的问题是，Discourse 无法看到请求的实际 IP 地址。因此，为了让白名单方法生效，我尝试了以下操作：

1. 将我的 Prometheus 服务器的 IPv6 地址添加到 `app.yml` 中的 `DISCOURSE_PROMETHEUS_TRUSTED_IP_WHITELIST_REGEX` 环境变量中。

2. 在 Prometheus 服务器的 `/etc/hosts` 文件中硬编码了论坛的实际 IPv6 地址。现在，论坛可以看到我的 Prometheus 服务器的 IP 地址并允许访问。🙂

我还有其他一些问题。例如，我在容器中运行 Prometheus。宿主机的 `/etc/hosts` 并未在容器内共享。因此，它持续将论坛地址解析为 Cloudflare IP，并在认证过程中失败。

我可以在启动 Prometheus 时通过 `-v /etc/hosts:/etc/hosts` 将宿主机的 `/etc/hosts` 共享到 Docker 容器中，但这会导致如下错误：

```plaintext
Get "https://forum-behind-cloudflare-dns-proxy.com:443/metrics": dial tcp [<ipv6-address>]:443: connect: cannot assign requested address

```

现在我需要解决这个具体问题。

编辑 #1：这个问题也解决了。Docker 容器内禁用了 IPv6。我可以通过在 Docker 容器中启用 IPv6 或使用 `--net=host` 来修复它。

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2020年六月27日 12:00 UTC](https://meta.discourse.org/t/getting-discourse-to-see-the-prometheus-server-ip/265015/6 "2020-06-27T12:00:09Z")

</div>

您是否位于反向代理之后？您需要配置 nginx，以便让真实的远程 IP 能够访问 Discourse。我认为这篇帖子 [在同一台机器上运行其他网站与 Discourse](https://meta.discourse.org/t/running-other-websites-on-the-same-machine-as-discourse/17247) 可能会为您提供一些解决思路。

---

<div class="post-metadata">

### Author: ![ishan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ishan/32/182322_2.png) [@ishan](https://meta.discourse.org/u/ishan)
#### Post date: [2020年六月28日 07:51 UTC](https://meta.discourse.org/t/getting-discourse-to-see-the-prometheus-server-ip/265015/7 "2020-06-28T07:51:52Z")

</div>

该 Discourse 实例确实运行在反向代理之后。该反向代理由 Cloudflare 提供，我们目前仅使用 Pro 套餐，无法将真实的客户端 IP 从 Cloudflare 传递给 Discourse 实例。不过，我的问题已经解决了。🙂

Grafana 仪表板现在可能也需要更新。随着时间的推移，Prometheus exporter 已更新，不再发送某些指标，但 Grafana 仪表板仍配置为读取这些（现已不存在的）指标的值。
