项目作者: kubernetes-sigs

项目描述 :
Framework for implementing custom metrics support for Kubernetes
高级语言: Go
项目地址: git://github.com/kubernetes-sigs/custom-metrics-apiserver.git
创建时间: 2017-03-09T22:35:20Z
项目社区:https://github.com/kubernetes-sigs/custom-metrics-apiserver

开源协议:Apache License 2.0

下载


Custom Metrics Adapter Server Boilerplate

Go Reference

Purpose

This repository contains boilerplate code for setting up an implementation
of the Metrics APIs:

  • Custom metrics (k8s.io/metrics/pkg/apis/custom_metrics)
  • External metrics (k8s.io/metrics/pkg/apis/external_metrics)

It includes the necessary boilerplate for setting up an implementation
(generic API server setup, registration of resources, etc), plus an
implementation for testing that allows setting custom metric values over HTTP.

How to use this repository

This repository is designed to be used as a library. First, implement one
or more of the metrics provider interfaces in pkg/provider (for example,
CustomMetricsProvider), depending on which APIs you want to support.

Then, use the AdapterBase in pkg/cmd to initialize the necessary flags
and set up the API server, passing in your providers.

More information can be found in the getting started
guide
, and the testing implementation can be
found in the test-adapter directory.

Prerequisites

Go: this library requires the same version of
Go as Kubernetes.

Test Adapter

There is a test adapter in this repository that can be used for testing
changes to the repository, as a mock implementation of the APIs for
automated unit tests, and also as an example implementation.

Note that this adapter should not be used for production. It’s for
writing automated e2e tests and serving as a sample only.

To build it:

  1. # build the test-adapter container as $REGISTRY/k8s-test-metrics-adapter-amd64
  2. export REGISTRY=<some-prefix>
  3. make test-adapter-container

To test it locally, install kind,
then create a cluster, and start the tests:

  1. kind create cluster
  2. make test-kind

To test it in an arbitraty Kubernetes cluster:

  1. # push the container up to a registry
  2. docker push $REGISTRY/k8s-test-metrics-adapter-amd64
  3. # launch the adapter using the test adapter deployment manifest
  4. kubectl apply -f test-adapter-deploy/testing-adapter.yaml

When the deployment is ready, you can define new metrics on the test adapter
by querying the write endpoint:

  1. # set up a proxy to the API server so we can access write endpoints
  2. # of the testing adapter directly
  3. kubectl proxy &
  4. # write a sample metric -- the write paths match the same URL structure
  5. # as the read paths, but at the /write-metrics base path.
  6. # data needs to be in json, so we also need to set the content-type header
  7. curl -X POST \
  8. -H 'Content-Type: application/json' \
  9. http://localhost:8001/api/v1/namespaces/custom-metrics/services/custom-metrics-apiserver:http/proxy/write-metrics/namespaces/default/services/kubernetes/test-metric \
  10. --data-raw '"300m"'
  1. # you can pipe to `jq .` to pretty-print the output, if it's installed
  2. # (otherwise, it's not necessary)
  3. kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta2" | jq .
  4. # fetching certain custom metrics of namespaced resources
  5. kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta2/namespaces/default/services/kubernetes/test-metric" | jq .

If you wanted to target a simple nginx-deployment and then use this as an HPA scaler metric, something like this would work following the previous curl command:

  1. apiVersion: autoscaling/v2
  2. kind: HorizontalPodAutoscaler
  3. metadata:
  4. name: nginx-deployment
  5. namespace: default
  6. spec:
  7. scaleTargetRef:
  8. apiVersion: apps/v1
  9. kind: Deployment
  10. name: nginx-deployment
  11. minReplicas: 1
  12. maxReplicas: 10
  13. metrics:
  14. - type: Object
  15. object:
  16. metric:
  17. name: test-metric
  18. describedObject:
  19. apiVersion: v1
  20. kind: Service
  21. name: kubernetes
  22. target:
  23. type: Value
  24. value: 300m

You can also query the external metrics:

  1. kubectl get --raw "/apis/external.metrics.k8s.io/v1beta1" | jq .
  2. # fetching certain custom metrics of namespaced resources
  3. kubectl get --raw "/apis/external.metrics.k8s.io/v1beta1/namespaces/default/my-external-metric" | jq .

Compatibility

The APIs in this repository follow the standard guarantees for Kubernetes
APIs, and will follow Kubernetes releases.

Community, discussion, contribution, and support

Learn how to engage with the Kubernetes community on the
community page.

You can reach the maintainers of this repository at:

Code of Conduct

Participation in the Kubernetes community is governed by the Kubernetes
Code of Conduct
.

Contribution Guidelines

See CONTRIBUTING.md for more information.