kubernetes-handbook/concepts/custom-metrics-hpa.md

69 lines
6.1 KiB
Markdown
Raw Normal View History

2021-12-24 11:11:37 +08:00
# 自定义指标 HPA
2017-11-27 19:33:18 +08:00
2021-12-24 11:11:37 +08:00
Kubernetes 中不仅支持 CPU、内存为指标的 HPA还支持自定义指标的 HPA例如 QPS。
2017-12-13 23:01:11 +08:00
2021-12-24 11:11:37 +08:00
本文中使用的 yaml 文件见 [manifests/HPA](https://github.com/rootsongjc/kubernetes-handbook/tree/master/manifests/HPA)。
2017-11-27 19:33:18 +08:00
## 设置自定义指标
**kubernetes1.6**
2021-12-24 11:11:37 +08:00
> 在 kubernetes1.6 集群中配置自定义指标的 HPA 的说明已废弃。
2017-12-13 23:01:11 +08:00
2021-12-24 11:11:37 +08:00
在设置定义指标 HPA 之前需要先进行如下配置:
2017-11-27 19:33:18 +08:00
2021-12-24 11:11:37 +08:00
- 将 heapster 的启动参数 `--api-server` 设置为 true
2017-11-27 19:33:18 +08:00
2021-12-24 11:11:37 +08:00
- 启用 custom metric API
- 将 kube-controller-manager 的启动参数中 `--horizontal-pod-autoscaler-use-rest-clients` 设置为 true并指定 `--master` 为 API server 地址,如 `--master=http://172.20.0.113:8080`
2017-11-27 19:33:18 +08:00
2021-12-24 11:11:37 +08:00
在 kubernetes1.5 以前很容易设置,参考 [1.6 以前版本的 kubernetes 中开启自定义 HPA](https://medium.com/@marko.luksa/kubernetes-autoscaling-based-on-custom-metrics-without-using-a-host-port-b783ed6241ac),而在 1.6 中因为取消了原来的 annotation 方式设置 custom metric只能通过 API server 和 kube-aggregator 来获取 custom metric因为只有两种方式来设置了一是直接通过 API server 获取 heapster 的 metrics二是部署 [kube-aggragator](https://github.com/kubernetes/kube-aggregator) 来实现。
2017-11-27 19:33:18 +08:00
2021-12-24 11:11:37 +08:00
我们将在 kubernetes1.8 版本的 kubernetes 中,使用聚合的 API server 来实现自定义指标的 HPA。
2017-12-13 23:01:11 +08:00
**kuberentes1.7+**
2017-11-27 19:33:18 +08:00
2021-12-24 11:11:37 +08:00
确认您的 kubernetes 版本在 1.7 或以上,修改以下配置:
2017-12-13 23:01:11 +08:00
2021-12-24 11:11:37 +08:00
- 将 kube-controller-manager 的启动参数中 `--horizontal-pod-autoscaler-use-rest-clients` 设置为 true并指定 `--master` 为 API server 地址,如 `--master=http://172.20.0.113:8080`
- 修改 kube-apiserver 的配置文件 apiserver增加一条配置 `--requestheader-client-ca-file=/etc/kubernetes/ssl/ca.pem --requestheader-allowed-names=aggregator --requestheader-extra-headers-prefix=X-Remote-Extra- --requestheader-group-headers=X-Remote-Group --requestheader-username-headers=X-Remote-User --proxy-client-cert-file=/etc/kubernetes/ssl/kubernetes.pem --proxy-client-key-file=/etc/kubernetes/ssl/kubernetes-key.pem`,用来配置 aggregator 的 CA 证书。
2017-11-27 19:33:18 +08:00
2021-12-24 11:11:37 +08:00
已经内置了 `apiregistration.k8s.io/v1beta1` API可以直接定义 APIService
2017-11-27 19:33:18 +08:00
```yaml
2017-12-13 23:01:11 +08:00
apiVersion: apiregistration.k8s.io/v1
2017-11-27 19:33:18 +08:00
kind: APIService
metadata:
2017-12-13 23:01:11 +08:00
name: v1.custom-metrics.metrics.k8s.io
2017-11-27 19:33:18 +08:00
spec:
insecureSkipTLSVerify: true
group: custom-metrics.metrics.k8s.io
groupPriorityMinimum: 1000
versionPriority: 5
service:
name: api
namespace: custom-metrics
version: v1alpha1
```
2021-12-24 11:11:37 +08:00
**部署 Prometheus**
2017-12-13 23:01:11 +08:00
2021-12-24 11:11:37 +08:00
使用 [prometheus-operator.yaml](https://github.com/rootsongjc/kubernetes-handbook/blob/master/manifests/HPA/prometheus-operator.yaml) 文件部署 Prometheus operator。
2017-12-13 23:01:11 +08:00
2021-12-24 11:11:37 +08:00
**注意:** 将镜像修改为你自己的镜像仓库地址。
2017-12-13 23:01:11 +08:00
2021-12-24 11:11:37 +08:00
这产生一个自定义的 API<http://172.20.0.113:8080/apis/custom-metrics.metrics.k8s.io/v1alpha1>,可以通过浏览器访问,还可以使用下面的命令可以检查该 API
2017-12-13 23:01:11 +08:00
2017-12-14 19:36:04 +08:00
```bash
$ kubectl get --raw=apis/custom-metrics.metrics.k8s.io/v1alpha1
{"kind":"APIResourceList","apiVersion":"v1","groupVersion":"custom-metrics.metrics.k8s.io/v1alpha1","resources":[{"name":"jobs.batch/http_requests","singularName":"","namespaced":true,"kind":"MetricValueList","verbs":["get"]},{"name":"namespaces/http_requests","singularName":"","namespaced":false,"kind":"MetricValueList","verbs":["get"]},{"name":"jobs.batch/up","singularName":"","namespaced":true,"kind":"MetricValueList","verbs":["get"]},{"name":"pods/up","singularName":"","namespaced":true,"kind":"MetricValueList","verbs":["get"]},{"name":"services/scrape_samples_scraped","singularName":"","namespaced":true,"kind":"MetricValueList","verbs":["get"]},{"name":"namespaces/scrape_samples_scraped","singularName":"","namespaced":false,"kind":"MetricValueList","verbs":["get"]},{"name":"pods/scrape_duration_seconds","singularName":"","namespaced":true,"kind":"MetricValueList","verbs":["get"]},{"name":"services/scrape_duration_seconds","singularName":"","namespaced":true,"kind":"MetricValueList","verbs":["get"]},{"name":"pods/http_requests","singularName":"","namespaced":true,"kind":"MetricValueList","verbs":["get"]},{"name":"pods/scrape_samples_post_metric_relabeling","singularName":"","namespaced":true,"kind":"MetricValueList","verbs":["get"]},{"name":"jobs.batch/scrape_samples_scraped","singularName":"","namespaced":true,"kind":"MetricValueList","verbs":["get"]},{"name":"jobs.batch/scrape_duration_seconds","singularName":"","namespaced":true,"kind":"MetricValueList","verbs":["get"]},{"name":"namespaces/scrape_duration_seconds","singularName":"","namespaced":false,"kind":"MetricValueList","verbs":["get"]},{"name":"namespaces/scrape_samples_post_metric_relabeling","singularName":"","namespaced":false,"kind":"MetricValueList","verbs":["get"]},{"name":"services/scrape_samples_post_metric_relabeling","singularName":"","namespaced":true,"kind":"MetricValueList","verbs":["get"]},{"name":"services/up","singularName":"","namespaced":true,"kind":"MetricValueList","verbs":["get"]},{"name":"pods/scrape_samples_scraped","singularName":"","namespaced":true,"kind":"MetricValueList","verbs":["get"]},{"name":"services/http_requests","singularName":"","namespaced":true,"kind":"MetricValueList","verbs":["get"]},{"name":"jobs.batch/scrape_samples_post_metric_relabeling","singularName":"","namespaced":true,"kind":"MetricValueList","verbs":["get"]},{"name":"namespaces/up","singularName":"","namespaced":false,"kind":"MetricValueList","verbs":["get"]}]}
```
2017-12-13 23:01:11 +08:00
2017-11-27 19:33:18 +08:00
## 参考
2021-12-24 11:11:37 +08:00
- [1.6 以前版本的 kubernetes 中开启自定义 HPA - medium.com](https://medium.com/@marko.luksa/kubernetes-autoscaling-based-on-custom-metrics-without-using-a-host-port-b783ed6241ac)
- [Horizontal Pod Autoscaler Walkthrough - kubernetes.io](https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/)
- [Kubernetes 1.8: Now with 100% Daily Value of Custom Metrics - blog.openshift.com](https://blog.openshift.com/kubernetes-1-8-now-custom-metrics/)
- [Arbitrary/Custom Metrics in the Horizontal Pod Autoscaler#117 - github.com](https://github.com/kubernetes/features/issues/117)