K8S中pod和container的资源管理:CPU和Memory
K8S中创建pod时,可以显示地指明包含的container的资源需求(resouce request和resource limit),通常是CPU和Memory(RAM).
kube-scheduler将用这些container的资源请求(resource request)汇总成该pod的需求,来决定在哪个node上部署这个pod;而node上的kubelet则保留相应的资源给container使用,以及根据这些container的资源限制(resource limit)来执行,不允许其使用的资源超过设置的limit。
【requests和limits】
如果pod所在的node有足够的资源,可以允许container使用比request更多的资源,但不能超过limit的限制。
例如:一个container的memory request是256MiB,limit是4GiB,而所在node的RAM是8GiB,则该container使用的RAM可以大于256MiB,但不能多于4GiB。如果该process尝试使用更多的内存,system kernel将终止该process(注意:是该container所属的pod将被终止),并且报出out of memory (OOM)的错误信息。
(参考文档1:If a container exceeds its memory request and the node that it runs on becomes short of memory overall, it is likely that the Pod the container belongs to will be evicted)
> 如果container只是设置了memory limit,而没有设置memory request,则kubernetes会自动认为memory request等于memory limit;
> 如果container只是设置了cpu limit,而没有设置cpu request,则kubernetes会自动认为cpu request等于cpu limit;
> 如果container只是设置了memory request,而没有设置memory limit,则以下场景的其中一个会被应用:
- container可以无上限地使用memory,例如使用所在node的所有可用memory,这样可能会导致OOM Killer。
- container所属的namespace有default memory limit,则container使用这个limit。
> 如果container只是设置了cpu request,而没有设置cpu limit,则以下场景的其中一个会被应用:
- container可以无上限地使用cpu,例如使用所在node的所有可用cpu。
- container所属的namespace有default cpu limit,则container使用这个limit。
> container可能被允许或不被允许在较长时间内超过其CPU限制。然而container runtimes不会因CPU过度使用而终止该POD或container。
注:查看配置时看到cpu/memory limit是0(或者0%)时,表示没有limit。
container与资源request/limit相关的参数如下:
spec.containers[].resources.limits.cpuspec.containers[].resources.limits.memoryspec.containers[].resources.limits.hugepages-<size>spec.containers[].resources.requests.cpuspec.containers[].resources.requests.memoryspec.containers[].resources.requests.hugepages-<size>
注:与cpu,memory不一样,hugepage资源不能超配(overcommit)。
pod的资源request/limit就是所包含的container对应的资源request/limit的总和。
【资源计量单位(resource units)】
CPU Resource Units:
CPU resource用cpu unit来计量,在kubernetes中1个CPU Unit等于1个物理CPU core或者1个虚拟CPU core(1 physical CPU core, or 1 virtual core),取决于这个node是物理主机还是虚拟机。
CPU resource的计量可以使用小数,例如一个容器的spec.containers[].resources.requests.cpu=0.5,表示申请占用一半的cpu time。
为避免使用小数,millicpu(或者millicores) 被引入:1millicpu(简写成1m)=0.001cpu unit。这样0.1cpu unit就可以用100m表示。
kubernetes中1m是最小的cpu resource计量单位了,小于1m是不被允许的。
CPU resource总是一个绝对值,不是相对值;不管这个container是运行在哪种CPU架构上:single-core, dual-core, or 48-core,500m CPU表示相同的计算资源消耗。
Memory Resource Units:
内存用bytes来计量。可以将内存表示为一个正整数,或者使用以下数量后缀之一来表示:E、P、T、G、M、k。也可以使用计算机字节数:Ei、Pi、Ti、Gi、Mi、Ki。例如,以下值大致相同:128974848, 129M, 128974848000m,123Mi。注意:400m表示0.4byte,400Mi表示400M byte。
下面是一个container资源需求的YAML文件示例:
---
apiVersion: v1
kind: Pod
metadata:
name: frontend
spec:
containers:
- name: app
image: images.my-company.example/app:v4
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
- name: log-aggregator
image: images.my-company.example/log-aggregator:v6
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
【资源使用率的查看和排序】
简单快速查看实时资源使用情况可以用kubectl top指令:
1)查看node资源使用情况:
kubectl top node #资源实际使用情况
kubectl describe node #node的资源capacity,可用的资源情况(Allocatable),以及各pod已分配的(Allocated)资源配置情况(request和limit)
| master-0:> kubectl top node W0421 13:48:16.185235 2602 top_node.go:119] Using json format to get metrics. NAME CPU(cores) CPU% MEMORY(bytes) MEMORY% worker-0 518m 6% 10094Mi 63% master-0 515m 25% 2367Mi 61% |
上面两个node的cpu unit都等于500m左右,但是CPU使用率却不同,这是因为worker-0配置了8个vcpu(对应0.5/8=6%),而master-0配置了2个vcpu(对应0.5/2=25%)。
2)查看pod资源使用情况:
# Show metrics for all pods in the default namespace
kubectl top pod
# Show metrics for all pods in the given namespace
kubectl top pod --namespace=NAMESPACE
# Show metrics for a given pod and its containers
kubectl top pod POD_NAME --containers
# Show metrics for the pods defined by label name=myLabel
kubectl top pod -l name=myLabel --sort-by=''
//If non-empty, sort pods list using specified field. The field can be either 'cpu' or 'memory'.
kubectl top pod -A --sort-by=cpu
kubectl top pod -A --sort-by=memory
可以对node上pod的资源使用情况进行排序:
#按memory使用排序
kubectl get po -A -owide | grep <NODE_NAME> | awk '{print $1, $2}' | xargs -n2 kubectl top pod --no-headers -n $1 | sort --key 3 -nr | column -t
#按cpu使用排序
kubectl get po -A -owide | grep <NODE_NAME> | awk '{print $1, $2}' | xargs -n2 kubectl top pod --no-headers -n $1 | sort --key 2 -nr | column -t
//awk指令打印出namespace和pod信息;xargs指令用-n2表示输入pod和namespace两个参数;sort指令用key来选定memory或cpu列来排序,-nr表示按数字大小降序排列;column指令-t表示用空格做分隔符
【其它】
pod的使用量等于其所有业务容器的总和,不包括 pause 容器,值等于 cadvisr中的 container_memory_working_set_bytes 指标。
node 的值并不等于该 node 上所有 pod 值的总和,也不等于直接在机器上运行 top 或 free 看到的值。
Process ID (PID) limits表示kubelet限制pod消耗的PIDs数量。
资源使用情况的统计以及相关数据的来源,不一致等问题可以参见以下文档。
【参考】
- Resource Management for Pods and Containers | Kubernetes
- Assign CPU Resources to Containers and Pods | Kubernetes
- Assign Memory Resources to Containers and Pods | Kubernetes
- Process ID Limits And Reservations | Kubernetes
- kubectl top 命令解析 - 云+社区 - 腾讯云 (tencent.com)
- 一次关于k8s kubectl top 和 contained ps 不一致的问题探究 - 文章详情 (itpub.net)
- https://ops.tips/blog/why-top-inside-container-wrong-memory/
- How to read metrics `kubectl top nodes/pods`? · Issue #193 · kubernetes-sigs/metrics-server · GitHub
- https://www.ibm.com/support/pages/kubectl-top-pods-and-docker-stats-show-different-memory-statistics
- K8S 中虚拟机的资源管理与Pod调度 (iswbm.com)

K8S中pod和container的资源管理:CPU和Memory的更多相关文章
- k8s 中 Pod 的控制器
k8s 中 Pod 的控制器 前言 Replication Controller ReplicaSet Deployment 更新 Deployment 回滚 deployment StatefulS ...
- K8s中Pod健康检查源代码分析
了解k8s中的Liveness和Readiness Liveness: 表明是否容器正在运行.如果liveness探测为fail,则kubelet会kill掉容器,并且会触发restart设置的策略. ...
- k8s中pod的yaml文件全面解读
apiVersion: v1 #必选,版本号,例如v1,版本号必须可以用 kubectl api-versions 查询到 . kind: Pod #必选,Pod metadata: #必选,元数据 ...
- k8s中pod内dns无法解析的问题
用k8s创建了pod,然后进入pod后,发现在pod中无法解析www.baidu.com,也就是出现了无法解析外面的域名的问题.经过高人指点,做个小总结.操作如下. 一,将CoreDNS 的Confi ...
- 延申三大问题中的第二个问题处理---收集查看k8s中pod的控制台日志
1.不使用logstash 2.步骤: 2.1 先获取一个文件的日志 2.2 再获取多个文件的日志 2.3 批量获取文件日志 pod日志文件路径 [root@worker hkd-eureka]# p ...
- Jenkins和Gitlab CI/CD自动更新k8s中pod使用的镜像说明
Jenkins 使用Jenkins的话,完成的工作主要有如下步骤: 1.从Gogs或Gitlab仓库上拉取代码 2.使用Maven编译代码,打包成jar文件 3.根据jar文件使用相对应的Docker ...
- K8S中POD节点状态ContainerCreating原因排查
现象: # kubectl get pods -n kube-system |grep dashboard kubernetes-dashboard-6685cb584f-dqkwk 0/1 Cont ...
- k8s中pod的容器日志查看命令
如果容器已经崩溃停止,您可以仍然使用 kubectl logs --previous 获取该容器的日志,只不过需要添加参数 --previous. 如果 Pod 中包含多个容器,而您想要看其中某一个容 ...
- Windows中查看进程的资源消耗(cpu, Disk,Memory,NetWork)
1.通过Windows Task Manager 的 Performance Tab 可以看到总体的性能消耗情况. 2.如果想看系统中每个进程的资源消耗,可以点击 下面的 Open Resource ...
- k8s 中的 Pod 细节了解
k8s中Pod的理解 基本概念 k8s 为什么使用 Pod 作为最小的管理单元 如何使用 Pod 1.自主式 Pod 2.控制器管理的 Pod 静态 Pod Pod的生命周期 Pod 如何直接暴露服务 ...
随机推荐
- Oracle中的sql脚本语言中的循环语句介绍
--sql脚本语言的循环介绍:--1.goto循环点.declare x number;begin x:=0;--变量初始化: <<repeat_loop>>--设置循环 ...
- LeetCode-688 骑士在棋盘上的概率
来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/knight-probability-in-chessboard 题目描述 在一个 n x n 的 ...
- 通过docker 安装部署sentry
一.下载项目 git clone https://github.com/getsentry/self-hosted 二.安装 1.安装docker和docker-compose 2.运行sentry ...
- Centos7 MyCat2 安装部署
部署MyCat2 之前需要搭建好数据库的主从,详看文档:mysql 主次数据库搭建 官网:http://www.mycat.org.cn/ 官方文档: https://www.yuque.com/cc ...
- 记一次SpringBoot整合WebSocket 找不到ServerEndpointExporter类的问题
package com.mengxiangnongfu.cms.framework.configure; import org.springframework.context.annotation.B ...
- 阶梯场景jp@gc - Stepping Thread Group (deprecated)
1.新建线程,添加配置元件.监听器 由上可见: 需要启动100个线程, 然后间隔30s就持续5s去启动10个线程, 那么就需要这样重复操作10次,才能100个线程全部启动. 最后整体100个线程持续运 ...
- String、StringBuffer、StringBuilder他们的区别
String.StringBuffer.StringBuilder他们的区别 String: String的值是不可变的,这就导致每次对String的操作都会生成新的String对象,然后将指针新的对 ...
- nginx客户端真实IP配置
https://www.cnblogs.com/kevingrace/p/8269955.html include mime.types; default_type application/octet ...
- 使用.Net工具安装某种程序
使用.Net开发的一个程序,安装时需要使用.net的工具. Emmm... 好长时间不用,有点忘了,偶尔翻到,记录一下 @echo off setlocal chcp 65001 set U_PATH ...
- CTF学习笔记(二)
二.常见的HTML知识 (一)rorbts协议 robots协议也称爬虫协议.爬虫规则等,是指网站可建立一个robots.txt文件来告诉搜索引擎哪些页面可以抓取,哪些页面不能抓取,而搜索引擎则通过读 ...