Skip to content

Commit a3b30be

Browse files
authored
Merge pull request #31558 from chrismetz09/metzResourceMetrics
update resource metrics pipeline section
2 parents d202485 + 3f74c2a commit a3b30be

File tree

2 files changed

+179
-46
lines changed

2 files changed

+179
-46
lines changed

content/en/docs/concepts/configuration/manage-resources-containers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ The kubelet reports the resource usage of a Pod as part of the Pod
231231

232232
If optional [tools for monitoring](/docs/tasks/debug-application-cluster/resource-usage-monitoring/)
233233
are available in your cluster, then Pod resource usage can be retrieved either
234-
from the [Metrics API](/docs/tasks/debug-application-cluster/resource-metrics-pipeline/#the-metrics-api)
234+
from the [Metrics API](/docs/tasks/debug-application-cluster/resource-metrics-pipeline/#metrics-api)
235235
directly or from your monitoring tools.
236236

237237
## Local ephemeral storage

content/en/docs/tasks/debug-application-cluster/resource-metrics-pipeline.md

Lines changed: 178 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,208 @@
22
reviewers:
33
- fgrzadkowski
44
- piosz
5-
title: Resource metrics pipeline
5+
title: Resource metrics pipeline
66
content_type: concept
77
---
88

99
<!-- overview -->
1010

11-
Resource usage metrics, such as container CPU and memory usage,
12-
are available in Kubernetes through the Metrics API. These metrics can be accessed either directly
13-
by the user with the `kubectl top` command, or by a controller in the cluster, for example
14-
Horizontal Pod Autoscaler, to make decisions.
11+
For Kubernetes, the _Metrics API_ offers a basic set of metrics to support automatic scaling and similar use cases.
12+
This API makes information available about resource usage for node and pod, including metrics for CPU and memory.
13+
If you deploy the Metrics API into your cluster, clients of the Kubernetes API can then query for this information, and
14+
you can use Kubernetes' access control mechanisms to manage permissions to do so.
1515

16-
<!-- body -->
16+
The [HorizontalPodAutoscaler](/docs/tasks/run-application/horizontal-pod-autoscale/) (HPA) and [VerticalPodAutoscaler](https://github.com/kubernetes/autoscaler/tree/master/vertical-pod-autoscaler#readme) (VPA) use data from the metrics API to adjust workload replicas and resources to meet customer demand.
1717

18-
## The Metrics API
18+
You can also view the resource metrics using the [`kubectl top`](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#top) command.
1919

20-
Through the Metrics API, you can get the amount of resource currently used
21-
by a given node or a given pod. This API doesn't store the metric values,
22-
so it's not possible, for example, to get the amount of resources used by a
23-
given node 10 minutes ago.
20+
{{< note >}}
21+
The Metrics API, and the metrics pipeline that it enables, only offers the minimum
22+
CPU and memory metrics to enable automatic scaling using HPA and / or VPA.
23+
If you would like to provide a more complete set of metrics, you can complement
24+
the simpler Metrics API by deploying a second
25+
[metrics pipeline](/docs/tasks/debug-application-cluster/resource-usage-monitoring/#full-metrics-pipeline)
26+
that uses the _Custom Metrics API_.
27+
{{< /note >}}
2428

25-
The API is no different from any other API:
2629

27-
- it is discoverable through the same endpoint as the other Kubernetes APIs under the path: `/apis/metrics.k8s.io/`
28-
- it offers the same security, scalability, and reliability guarantees
30+
Figure 1 illustrates the architecture of the resource metrics pipeline.
31+
32+
{{< mermaid >}}
33+
flowchart RL
34+
subgraph cluster[Cluster]
35+
direction RL
36+
S[ <br><br> ]
37+
A[Metrics-<br>Server]
38+
subgraph B[Nodes]
39+
direction TB
40+
D[cAdvisor] --> C[kubelet]
41+
E[Container<br>runtime] --> D
42+
E1[Container<br>runtime] --> D
43+
P[pod data] -.- C
44+
end
45+
L[API<br>server]
46+
W[HPA]
47+
C ---->|Summary<br>API| A -->|metrics<br>API| L --> W
48+
end
49+
L ---> K[kubectl<br>top]
50+
classDef box fill:#fff,stroke:#000,stroke-width:1px,color:#000;
51+
class W,B,P,K,cluster,D,E,E1 box
52+
classDef spacewhite fill:#ffffff,stroke:#fff,stroke-width:0px,color:#000
53+
class S spacewhite
54+
classDef k8s fill:#326ce5,stroke:#fff,stroke-width:1px,color:#fff;
55+
class A,L,C k8s
56+
{{< /mermaid >}}
57+
58+
Figure 1. Resource Metrics Pipeline
59+
60+
The architecture components, from right to left in the figure, consist of the following:
61+
62+
* [cAdvisor](https://github.com/google/cadvisor): Daemon for collecting, aggregating and exposing container metrics included in Kubelet.
63+
* [kubelet](/docs/concepts/overview/components/#kubelet): Node agent for managing container resources. Resource metrics are accessible using the `/metrics/resource` and `/stats` kubelet API endpoints.
64+
* [Summary API](#summary-api-source): API provided by the kubelet for discovering and retrieving per-node summarized stats available through the `/stats` endpoint.
65+
* [metrics-server](#metrics-server): Cluster addon component that collects and aggregates resource metrics pulled from each kubelet. The API server serves Metrics API for use by HPA, VPA, and by the `kubectl top` command. Metrics Server is a reference implementation of the Metrics API.
66+
* [Metrics API](#metrics-api): Kubernetes API supporting access to CPU and memory used for workload autoscaling. To make this work in your cluster, you need an API extension server that provides the Metrics API.
67+
68+
{{< note >}}
69+
cAdvisor supports reading metrics from cgroups, which works with typical container runtimes on Linux.
70+
If you use a container runtime that uses another resource isolation mechanism, for example virtualization, then that container runtime must support [CRI Container Metrics](https://github.com/kubernetes/community/blob/master/contributors/devel/sig-node/cri-container-stats.md) in order for metrics to be available to the kubelet.
71+
{{< /note >}}
72+
73+
74+
<!-- body -->
2975

30-
The API is defined in [k8s.io/metrics](https://github.com/kubernetes/metrics/blob/master/pkg/apis/metrics/v1beta1/types.go)
31-
repository. You can find more information about the API there.
76+
## Metrics API
77+
78+
The metrics-server implements the Metrics API. This API allows you to access CPU and memory usage for the nodes and pods in your cluster. Its primary role is to feed resource usage metrics to K8s autoscaler components.
79+
80+
Here is an example of the Metrics API request for a `minikube` node piped through `jq` for easier reading:
81+
```shell
82+
kubectl get --raw "/apis/metrics.k8s.io/v1beta1/nodes/minikube" | jq '.'
83+
```
84+
85+
Here is the same API call using `curl`:
86+
```shell
87+
curl http://localhost:8080/apis/metrics.k8s.io/v1beta1/nodes/minikube
88+
```
89+
Sample reply:
90+
```json
91+
{
92+
"kind": "NodeMetrics",
93+
"apiVersion": "metrics.k8s.io/v1beta1",
94+
"metadata": {
95+
"name": "minikube",
96+
"selfLink": "/apis/metrics.k8s.io/v1beta1/nodes/minikube",
97+
"creationTimestamp": "2022-01-27T18:48:43Z"
98+
},
99+
"timestamp": "2022-01-27T18:48:33Z",
100+
"window": "30s",
101+
"usage": {
102+
"cpu": "487558164n",
103+
"memory": "732212Ki"
104+
}
105+
}
106+
```
107+
Here is an example of the Metrics API request for a `kube-scheduler-minikube` pod contained in the `kube-system` namespace and piped through `jq` for easier reading:
108+
109+
```shell
110+
kubectl get --raw "/apis/metrics.k8s.io/v1beta1/namespaces/kube-system/pods/kube-scheduler-minikube" | jq '.'
111+
```
112+
Here is the same API call using `curl`:
113+
```shell
114+
curl http://localhost:8080/apis/metrics.k8s.io/v1beta1/namespaces/kube-system/pods/kube-scheduler-minikube
115+
```
116+
Sample reply:
117+
```json
118+
{
119+
"kind": "PodMetrics",
120+
"apiVersion": "metrics.k8s.io/v1beta1",
121+
"metadata": {
122+
"name": "kube-scheduler-minikube",
123+
"namespace": "kube-system",
124+
"selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/kube-system/pods/kube-scheduler-minikube",
125+
"creationTimestamp": "2022-01-27T19:25:00Z"
126+
},
127+
"timestamp": "2022-01-27T19:24:31Z",
128+
"window": "30s",
129+
"containers": [
130+
{
131+
"name": "kube-scheduler",
132+
"usage": {
133+
"cpu": "9559630n",
134+
"memory": "22244Ki"
135+
}
136+
}
137+
]
138+
}
139+
```
140+
141+
The Metrics API is defined in the [k8s.io/metrics](https://github.com/kubernetes/metrics) repository. You must enable the [API aggregation layer](/docs/tasks/extend-kubernetes/configure-aggregation-layer/) and register an [APIService](/docs/reference/kubernetes-api/cluster-resources/api-service-v1/) for the `metrics.k8s.io` API.
142+
143+
To learn more about the Metrics API, see [resource metrics API design](https://github.com/kubernetes/design-proposals-archive/blob/main/instrumentation/resource-metrics-api.md), the [metrics-server repository](https://github.com/kubernetes-sigs/metrics-server) and the [resource metrics API](https://github.com/kubernetes/metrics#resource-metrics-api).
144+
145+
146+
{{< note >}} You must deploy the metrics-server or alternative adapter that serves the Metrics API to be able to access it. {{< /note >}}
147+
148+
## Measuring resource usage
32149

33-
{{< note >}}
34-
The API requires the metrics server to be deployed in the cluster. Otherwise it will be not available.
35-
{{< /note >}}
150+
### CPU
36151

37-
## Measuring Resource Usage
152+
CPU is reported as the average core usage measured in cpu units. One cpu, in Kubernetes, is equivalent to 1 vCPU/Core for cloud providers, and 1 hyper-thread on bare-metal Intel processors.
38153

39-
### CPU
154+
This value is derived by taking a rate over a cumulative CPU counter provided by the kernel (in both Linux and Windows kernels). The time window used to calculate CPU is shown under window field in Metrics API.
40155

41-
CPU is reported as the average usage, in
42-
[CPU cores](/docs/concepts/configuration/manage-resources-containers/#meaning-of-cpu),
43-
over a period of time. This value is derived by taking a rate over a cumulative CPU counter
44-
provided by the kernel (in both Linux and Windows kernels).
45-
The kubelet chooses the window for the rate calculation.
156+
To learn more about how Kubernetes allocates and measures CPU resources, see [meaning of CPU](/docs/concepts/configuration/manage-compute-resources-container/#meaning-of-cpu).
46157

47158
### Memory
48159

49-
Memory is reported as the working set, in bytes, at the instant the metric was collected.
50-
In an ideal world, the "working set" is the amount of memory in-use that cannot be freed under memory pressure.
51-
However, calculation of the working set varies by host OS, and generally makes heavy use of heuristics to produce an estimate.
52-
It includes all anonymous (non-file-backed) memory since Kubernetes does not support swap.
53-
The metric typically also includes some cached (file-backed) memory, because the host OS cannot always reclaim such pages.
160+
Memory is reported as the working set, measured in bytes, at the instant the metric was collected.
161+
162+
In an ideal world, the "working set" is the amount of memory in-use that cannot be freed under memory pressure. However, calculation of the working set varies by host OS, and generally makes heavy use of heuristics to produce an estimate.
163+
164+
The Kubernetes model for a container's working set expects that the container runtime counts anonymous memory associated with the container in question. The working set metric typically also includes some cached (file-backed) memory, because the host OS cannot always reclaim pages.
165+
166+
To learn more about how Kubernetes allocates and measures memory resources, see [meaning of memory](/docs/concepts/configuration/manage-compute-resources-container/#meaning-of-memory).
54167

55168
## Metrics Server
56169

57-
[Metrics Server](https://github.com/kubernetes-sigs/metrics-server) is a cluster-wide aggregator of resource usage data.
58-
By default, it is deployed in clusters created by `kube-up.sh` script
59-
as a Deployment object. If you use a different Kubernetes setup mechanism, you can deploy it using the provided
60-
[deployment components.yaml](https://github.com/kubernetes-sigs/metrics-server/releases) file.
170+
The metrics-server fetches resource metrics from the kubelets and exposes them in the Kubernetes API server through the Metrics API for use by the HPA and VPA. You can also view these metrics using the `kubectl top` command.
171+
172+
The metrics-server uses the Kubernetes API to track nodes and pods in your cluster. The metrics-server queries each node over HTTP to fetch metrics. The metrics-server also builds an internal view of pod metadata, and keeps a cache of pod health. That cached pod health information is available via the extension API that the metrics-server makes available.
173+
174+
For example with an HPA query, the metrics-server needs to identify which pods fulfill the label selectors in the deployment.
175+
176+
The metrics-server calls the [kubelet](/docs/reference/command-line-tools-reference/kubelet/) API to collect metrics from each node. Depending on the metrics-server version it uses:
177+
* Metrics resource endpoint `/metrics/resource` in version v0.6.0+ or
178+
* Summary API endpoint `/stats/summary` in older versions
179+
61180

62-
Metrics Server collects metrics from the Summary API, exposed by
63-
[Kubelet](/docs/reference/command-line-tools-reference/kubelet/) on each node, and is registered with the main API server via
64-
[Kubernetes aggregator](/docs/concepts/extend-kubernetes/api-extension/apiserver-aggregation/).
181+
To learn more about the metrics-server, see the [metrics-server repository](https://github.com/kubernetes-sigs/metrics-server).
65182

66-
Learn more about the metrics server in
67-
[the design doc](https://github.com/kubernetes/community/blob/master/contributors/design-proposals/instrumentation/metrics-server.md).
183+
You can also check out the following:
68184

69-
### Summary API Source
70-
The [Kubelet](/docs/reference/command-line-tools-reference/kubelet/) gathers stats at node, volume, pod and container level, and emits their statistics in
185+
* [metrics-server design](https://github.com/kubernetes/community/blob/master/contributors/design-proposals/instrumentation/metrics-server.md)
186+
* [metrics-server FAQ](https://github.com/kubernetes-sigs/metrics-server/blob/master/FAQ.md)
187+
* [metrics-server known issues](https://github.com/kubernetes-sigs/metrics-server/blob/master/KNOWN_ISSUES.md)
188+
* [metrics-server releases](https://github.com/kubernetes-sigs/metrics-server/releases)
189+
* [Horizontal Pod Autoscaling](/docs/tasks/run-application/horizontal-pod-autoscale/)
190+
191+
### Summary API source
192+
193+
The [kubelet](/docs/reference/command-line-tools-reference/kubelet/) gathers stats at the node, volume, pod and container level, and emits this information in
71194
the [Summary API](https://github.com/kubernetes/kubernetes/blob/7d309e0104fedb57280b261e5677d919cb2a0e2d/staging/src/k8s.io/kubelet/pkg/apis/stats/v1alpha1/types.go)
72195
for consumers to read.
73196

74-
Pre-1.23, these resources have been primarily gathered from [cAdvisor](https://github.com/google/cadvisor). However, in 1.23 with the
75-
introduction of the `PodAndContainerStatsFromCRI` FeatureGate, container and pod level stats can be gathered by the CRI implementation.
76-
Note: this also requires support from the CRI implementations (containerd >= 1.6.0, CRI-O >= 1.23.0).
197+
Here is an example of a Summary API request for a `minikube` node:
198+
199+
200+
```shell
201+
kubectl get --raw "/api/v1/nodes/minikube/proxy/stats/summary"
202+
```
203+
Here is the same API call using `curl`:
204+
```shell
205+
curl http://localhost:8080/api/v1/nodes/minikube/proxy/stats/summary
206+
```
207+
{{< note >}}
208+
The summary API `/stats/summary` endpoint will be replaced by the `/metrics/resource` endpoint beginning with metrics-server 0.6.x.
209+
{{< /note >}}

0 commit comments

Comments
 (0)