Skip to content

Commit

Permalink
Merge pull request #178 from helight/zh-translation-reference-metrics
Browse files Browse the repository at this point in the history
zh-translation:/reference/metrics.md
  • Loading branch information
mergify[bot] authored Aug 12, 2020
2 parents eceab5f + 7cca038 commit 4eec33a
Showing 1 changed file with 20 additions and 41 deletions.
61 changes: 20 additions & 41 deletions docs/book/src/reference/metrics.md
Original file line number Diff line number Diff line change
@@ -1,68 +1,49 @@
# Metrics
# 指标

By default, controller-runtime builds a global prometheus registry and
publishes a collection of performance metrics for each controller.
默认情况下,controller-runtime 会构建一个全局 prometheus 注册,并且会为每个控制器发布一系列性能指标。

## Protecting the Metrics
## 指标保护

These metrics are protected by [kube-auth-proxy](https://github.com/brancz/kube-rbac-proxy)
by default if using kubebuilder. Kubebuilder v2.2.0+ scaffold a clusterrole which
can be found at `config/rbac/auth_proxy_client_clusterrole.yaml`.
如果使用了 kubebuilder [kube-auth-proxy](https://github.com/brancz/kube-rbac-proxy) 默认会保护这些指标。Kubebuilder v2.2.0+ 会创建一个集群角色,它在 `config/rbac/auth_proxy_client_clusterrole.yaml` 文件中配置。

You will need to grant permissions to your Prometheus server so that it can
scrape the protected metrics. To achieve that, you can create a
`clusterRoleBinding` to bind the `clusterRole` to the service account that your
Prometheus server uses.
你需要给你所有的 Prometheus 服务授权,以便它可以拿到这些被保护的指标。为实现授权,你可以创建一个 `clusterRoleBinding``clusterRole` 绑定到一个你的 Prometheus 服务使用的账户上。

You can run the following kubectl command to create it. If using kubebuilder
`<project-prefix>` is the `namePrefix` field in `config/default/kustomization.yaml`.
可以运行下面的 kubectl 命令来创建它。如果你使用 kubebuilder,在`config/default/kustomization.yaml` 文件中 `namePrefix` 字段是 `<project-prefix>`

```bash
kubectl create clusterrolebinding metrics --clusterrole=<project-prefix>-metrics-reader --serviceaccount=<namespace>:<service-account-name>
```

## Exporting Metrics for Prometheus
## 给 Prometheus 导出指标
按照下面的步骤来用 Prometheus Operator 导出指标:

Follow the steps below to export the metrics using the Prometheus Operator:

1. Install Prometheus and Prometheus Operator.
We recommend using [kube-prometheus](https://github.com/coreos/kube-prometheus#installing)
in production if you don't have your own monitoring system.
If you are just experimenting, you can only install Prometheus and Prometheus Operator.
2. Uncomment the line `- ../prometheus` in the `config/default/kustomization.yaml`.
It creates the `ServiceMonitor` resource which enables exporting the metrics.
1. 安装 Prometheus 和 Prometheus Operator。如果没有自己的监控系统,在生产环境上我们推荐使用 [kube-prometheus](https://github.com/coreos/kube-prometheus#installing)。如果你只是做实验,那么可以只安装 Prometheus 和 Prometheus Operator。
2.`config/default/kustomization.yaml` 配置文件中取消 `- ../prometheus` 这一行的注释。它会创建可以导出指标的 `ServiceMonitor` 资源。

```yaml
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
# [PROMETHEUS] 用于启用 prometheus 监控, 取消所有带 'PROMETHEUS' 部分的注释。
- ../prometheus
```
Note that, when you install your project in the cluster, it will create the
`ServiceMonitor` to export the metrics. To check the ServiceMonitor,
run `kubectl get ServiceMonitor -n <project>-system`. See an example:
注意,当你在集群中安装你的项目时,它会创建一个 `ServiceMonitor` 来导出指标。为了检查 ServiceMonitor,可以运行 `kubectl get ServiceMonitor -n <project>-system`。看下面的例子:

```
$ kubectl get ServiceMonitor -n monitor-system
$ kubectl get ServiceMonitor -n monitor-system
NAME AGE
monitor-controller-manager-metrics-monitor 2m8s
```
Also, notice that the metrics are exported by default through port `8443`. In this way,
you are able to check the Prometheus metrics in its dashboard. To verify it, search
for the metrics exported from the namespace where the project is running
`{namespace="<project>-system"}`. See an example:
同样,要注意默认情况下是通过 `8443` 端口导出指标的。这种情况下,你可以在自己的 dashboard 中检查 Prometheus metrics。要检查这些指标,在项目运行的 `{namespace="<project>-system"}` 命名空间下搜索导出的指标。看下面的例子:
<img width="1680" alt="Screenshot 2019-10-02 at 13 07 13" src="https://user-images.githubusercontent.com/7708031/66042888-a497da80-e515-11e9-9d77-d8a9fc1159a5.png">
<img width="1680" alt="Screenshot 2019-10-02 at 13 07 13" src="https://user-images.githubusercontent.com/7708031/66042888-a497da80-e515-11e9-9d77-d8a9fc1159a5.png">
## Publishing Additional Metrics
## 发布额外的指标:
If you wish to publish additional metrics from your controllers, this
can be easily achieved by using the global registry from
`controller-runtime/pkg/metrics`.
如果你想从你的控制器发布额外的指标,可以通过在 `conoller-runtime/pkg/metrics` 中使用全局注册的方式来轻松做到。
One way to achieve this is to declare your collectors as global variables and then register them using `init()`.
实现发布额外指标的一种方式是把收集器声明为全局变量,并且使用 `init()` 来注册。
For example:
例如:
```go
import (
Expand Down Expand Up @@ -91,6 +72,4 @@ func init() {
}
```

You may then record metrics to those collectors from any part of your
reconcile loop, and those metrics will be available for prometheus or
other openmetrics systems to scrape.
然后就可以从你的接收循环部分中记录这些指标到收集器中了,并且这些指标就可以被 prometheus 或者其它开放指标系统来抓取了。

0 comments on commit 4eec33a

Please sign in to comment.