目录
基本概念
内存限制
CPU 限制
配置 namespace 资源
为 namespace 设置资源限制:
为 namespace 设置资源配额:
数量配置: 为 Namespace 配置 Pod 配额
基本概念
在 K8s 中定义 Pod 中运行容器有两个维度的限制:
资源需求: 即运行 Pod 的节点必须满足运行 Pod 的最基本需求才能运行 Pod. 如: Pod 运行至少需要 2G 内存, 1 核 CPU
资源限额: 即运行 Pod 期间, 可能内存使用量会增加, 那最多能使用多少内存, 这就是资源限额.
Kubernetes 采用 request 和 limit 两种限制类型来对资源进行分配:
request(资源需求): 即运行 Pod 的节点必须满足运行 Pod 的最基本需求才能运行 Pod.
limit(资源限额): 即运行 Pod 期间, 可能内存使用量会增加, 那最多能使用多少内存, 这就是资源限额.
资源类型:
CPU 的单位是核心数, 内存的单位是字节.
一个容器申请 0.5 个 CPU, 就相当于申请 1 个 CPU 的一半, 你也可以加个后缀 m 表示千分之一的概念. 比如说 100m 的 CPU,100 豪的 CPU 和 0.1 个 CPU 都是一样的.
内存单位:
- K,M,G,T,P,E #通常是以 1000 为换算标准的.
- Ki,Mi,Gi,Ti,Pi,Ei #通常是以 1024 为换算标准的.
内存限制
如果容器超过其内存限制, 则会被终止. 如果可重新启动, 则与所有其他类型的运行时故障一样, kubelet 将重新启动它.
如果一个容器超过其内存请求, 那么当节点内存不足时, 它的 Pod 可能被逐出
真实主机将 stress.tar 发送给 server1
- docker load -i stress.tar
- docker push reg.westos.org/library/stress:latest
server2:
VIM demo.YAML
设置容器内存为 200M. 但是限制其可使用的内存资源为 100M
- kubectl apply -f demo.YAML
- kubectl get pod
- kubectl delete -f pod/deploy.YAML
- kubectl get pod
- kubectl describe pod memory-demo
- kubectl logs memory-demo
报错: 直接被 kill 了, 因为超出了内存需求
VIM demo.YAML
[限制最多不能超过 300]
- kubectl delete -f demo.YAML
- kubectl apply -f demo.YAML
- kubectl get pod
可以看到状态已经就绪
kubectl delete -f demo.YAML
CPU 限制
VIM demo.YAML
将上下限分别修改为 2 和 0.5, 使用两个 CPU
- kubectl apply -f demo.YAML
- kubectl get pod
状态是 running
kubectl delete -f demo.YAML
VIM demo.YAML
- kubectl apply -f demo.YAML
- kubectl get pod
状态是 pending(节点上需求无法满足)
kubectl delete -f demo.YAML
配置 namespace 资源
为 namespace 设置资源限制:
VIM demo.YAML
- kubectl apply -f demo.YAML
- kubectl get limitranges
- kubectl describe limitranges
- kubectl run demo --image=nginx
创建一个 pod
- kubectl get pod
- kubectl describe pod demo
可以看到刚才没有加资源限制. 但是 Limits 和 Requests 都有限制
- kubectl describe limitranges
- kubectl delete pod demo
- mkdir limit
- cd limit/
- mv ../demo.YAML .
VIM pod.YAML
kubectl apply -f pod.YAML
创建报错 (因为最大 CPU 使用是 1, 但是这里 limit 设置的是 2)
kubectl describe limitranges
VIM pod.YAML
kubectl apply -f pod.YAML
都使其满足要求后创建成功
- kubectl get pod
- kubectl delete -f pod.YAML
为 namespace 设置资源配额:
VIM demo.YAML
- kubectl apply -f demo.YAML
- kubectl get resourcequotas
- kubectl run demo1 --image=nginx
- kubectl get pod
- kubectl get resourcequotas
可以看到资源配额已经变了 (会时时计算这个 pod 内所有的 pod)
- kubectl run demo2 --image=nginx
- kubectl get resourcequotas
- kubectl run demo3 --image=nginx
- kubectl get resourcequotas
- kubectl run demo4 --image=nginx
- kubectl get resourcequotas
这个时候资源上限刚刚好, 没有超, 不会影响到 pod
kubectl get pod
kubectl run demo5 --image=nginx
再次创建 demo5 就会报错, 因为配额到上限了
kubectl delete pod --all
回收掉所有的 pod
- kubectl get limitranges
- kubectl delete limitranges limitrange-memory
- kubectl get resourcequotas
现在只剩配额没有资源限制
kubectl run demo1 --image=nginx
创建 pod 报错 (必须要指定资源限制, 不然不能计算资源配额)
- kubectl apply -f demo.YAML
- kubectl get limitranges
创建之后资源限制又有了
kubectl run demo1 --image=nginx
创建 pod 就没有报错了
- kubectl describe pod demo1
- kubectl get pod
数量配置: 为 Namespace 配置 Pod 配额
VIM demo.YAML
- kubectl apply -f demo.YAML
- kubectl describe resourcequotas
- kubectl run demo2 --image=nginx
- kubectl describe resourcequotas
- kubectl run demo3 --image=nginx
报错原因: 资源有, 但是 pod 数量上限了
- kubectl delete -f demo.YAML
- kubectl delete pod --all
来源: https://blog.csdn.net/m0_62341392/article/details/122550522