# Prometheus and Grafana installation

本教程将向您展示如何安装 [Prometheus](https://prometheus.io/) 和 [Grafana](https://grafana.com/) 通过nginx ingress controller收取指标.

!!! important 这个例子对Prometheus和Grafana使用`emptyDir` volume。 这意味着，一旦吊舱终止，您将丢失所有数据。.

## Before You Begin

nginx Ingress控制器应该已经根据部署说明进行了部署 [here](/ingress-nginx-docs-cn/docs/deploy/index.md).

请注意，本教程中使用的Kustomize 基于存储在 [deploy](https://github.com/kubernetes/ingress-nginx/tree/master/deploy) 目录，仓库为 [kubernetes/ingress-nginx](https://github.com/kubernetes/ingress-nginx).

## Deploy and configure Prometheus Server

必须配置Prometheus服务器，以便它可以发现服务的端点。 如果Prometheus服务器已经在集群中运行，并且以可以找到 ingress controller容器的方式进行了配置，则不需要额外的配置。

如果没有现有的Prometheus服务器正在运行，本教程的其余部分将指导您完成部署正确配置的Prometheus服务器所需的步骤。

运行以下命令可在Kubernetes中部署Prometheus:

```
kubectl apply --kustomize github.com/kubernetes/ingress-nginx/deploy/prometheus/
serviceaccount/prometheus-server created
role.rbac.authorization.k8s.io/prometheus-server created
rolebinding.rbac.authorization.k8s.io/prometheus-server created
configmap/prometheus-configuration-bc6bcg7b65 created
service/prometheus-server created
deployment.apps/prometheus-server created
```

### Prometheus Dashboard

在网络浏览器中打开Prometheus仪表板:

```
kubectl get svc -n ingress-nginx
NAME                   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                                      AGE
default-http-backend   ClusterIP   10.103.59.201   <none>        80/TCP                                       3d
ingress-nginx NodePort    10.97.44.72     <none>        80:30100/TCP,443:30154/TCP,10254:32049/TCP   5h
prometheus-server      NodePort    10.98.233.86    <none>        9090:32630/TCP                               1m
```

获取正在运行的集群中节点的IP地址:

```
kubectl get nodes -o wide
```

在某些情况下，该节点只有内部IP地址，我们需要执行:

```
kubectl get nodes --selector=kubernetes.io/role!=master -o jsonpath={.items[*].status.addresses[?\(@.type==\"InternalIP\"\)].address}
10.192.0.2 10.192.0.3 10.192.0.4
```

打开浏览器并访问以下URL: <http://{node> *IP address}:{prometheus-svc-nodeport}* 加载 Prometheus Dashboard.

根据上面的示例，此网址为 <http://10.192.0.3:32630>

![Dashboard](/files/-Lt99LJuIUVrmjuSAEh-)

### Grafana

```
kubectl apply --kustomize github.com/kubernetes/ingress-nginx/deploy/grafana/
```

```
kubectl get svc -n ingress-nginx
NAME                   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                                      AGE
default-http-backend   ClusterIP   10.103.59.201   <none>        80/TCP                                       3d
ingress-nginx NodePort    10.97.44.72     <none>        80:30100/TCP,443:30154/TCP,10254:32049/TCP   5h
prometheus-server      NodePort    10.98.233.86    <none>        9090:32630/TCP                               10m
grafana                NodePort    10.98.233.87    <none>        3000:31086/TCP                               10m
```

打开浏览器并访问以下URL: <http://{node> *IP address}:{grafana-svc-nodeport}* 加载Grafana Dashboard. 根据上面的示例，此网址为 <http://10.192.0.3:31086>

用户名密码为 `admin`

登录后，您可以从以下位置导入Grafana仪表板 <https://github.com/kubernetes/ingress-nginx/tree/master/deploy/grafana/dashboards>

![Dashboard](/files/-Lt99LJwr30wlc98rW5L)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://rocdu.gitbook.io/ingress-nginx-docs-cn/docs/user-guide/monitoring.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
