企业入门实战--k8s之容器资源限制
kubernetes容器资源限制
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为换算标准的。
内存限制
在server1上:
我们先讲需要的镜像上传到我们的私有仓库:
docket pull progrium/stress:latest
docker tag progrium/stress:latest reg.westos.org/library/stress:latest
docker push reg.westos.org/library/stress:latest
创建一个目录limit 用来存放文件:
mkdir limit/
cd limit
vim pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: memory-demo
spec:
containers:
- name: memory-demo
image: stress
args:
- --vm
- "1"
- --vm-bytes
- 200M
## 需要的内存
resources:
requests:
memory: 50Mi
limits:
memory: 100Mi ## 最大内存
执行清单pod.yaml,查看节点pod,查看日志
我们一猜它就不会执行成功,因为内存限制为50-100MI
但是却要求创建200M的内存
我们删除pod节点,修改上限内存为300M,再次执行清单,并查看pod节点信息
kubectl delete -f pod.yaml
vim pod.yaml
kubectl apply -f pod.yaml
kubectl get pod
apiVersion: v1
kind: Pod
metadata:
name: memory-demo
spec:
containers:
- name: memory-demo
image: stress
args:
- --vm- "1"
- --vm-bytes
- 200M
resources:
requests:
memory: 50Mi
limits:
memory: 300Mi
实验完成之后,将刚才的pod.yaml清除
kubectl delete -f pod.yaml
CPU限制
vim pod1.yaml
apiVersion: v1
kind: Pod
metadata:
name: cpu-demo
spec:
containers:
- name: cpu-demo
image: stress
resources:
limits:
cpu: "10"
requests:
cpu: "5"
args:
- -c
- "2"
执行清单pod1.yaml
kubectl apply -f pod1.yaml
kubectl get pod
kubectl describe pod cpu-demo ##显示调度失败
修改cpu限额
vim pod1.yaml
apiVersion: v1
kind: Pod
metadata:
name: cpu-demo
spec:
containers:
- name: cpu-demoimage: stress
resources:
limits:
cpu: "1"
requests:
cpu: "0.1"
args:
- -c
- "2"
拉起清单
kubectl apply -f pod1.yaml
kubectl get pod
kubectl describe pod cpu-demo
为namespace设置资源限制
vim limitrange.yaml
apiVersion: v1
kind: LimitRange
metadata:
name: limitrange-memory
spec:
limits:
- default:
cpu: 0.5
memory: 512Mi
defaultRequest:
cpu: 0.1
memory: 256Mi
max:
cpu: 1
memory: 1Gi
min:
cpu: 0.1
memory: 100Mi
type: Container
拉起清单
kubectl apply -f limitrange.yaml
kubectl get limitranges
kubectl describe limitranges
vim pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: memory-demo
spec:
containers:
- name: memory-demo
image: nginx
#
resources:#
requests:
#
memory: 100Mi
#
limits:
#
memory: 300Mi
拉起pod清单 没有分配内存限制
kubectl apply -f pod.yaml
kubectl get pod
kubectl describe pod memory-demo ## 默认
vim limitrange.yaml
apiVersion: v1
kind: LimitRange
metadata:
name: limitrange-memory
spec:
limits:
- default:
cpu: 0.5
memory: 512Mi
defaultRequest:
cpu: 0.1
memory: 256Mi
max:
cpu: 1
memory: 1Gi
min:
cpu: 0.1
memory: 100Mi
type: Container
---
apiVersion: v1
kind: ResourceQuota
metadata:
name: mem-cpu-demo
spec:
hard:
requests.cpu: "1"
requests.memory: 1Gi
limits.cpu: "2"
limits.memory: 2G
kubectl apply -f limitrange.yaml
kubectl delete limitranges limitrange-memory
kubectl get resourcequotas
vim pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: memory-demo
spec:
containers:
- name: memory-demo
image: nginx
resources:
requests:
cpu: 0.1
memory: 100Mi
limits:
cpu: 0.1
memory: 300M
拉起清单
kubectl apply -f pod.yaml
kubectl describe resourcequotas
设置Pod配额以限制可以在namespace中运行的Pod数量
vim pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: memory-demo-1
spec:
containers:
- name: memory-demo
image: nginx
resources:
requests:
cpu: 0.2
memory: 100Milimits:
cpu: 0.5
memory: 300Mi
---
apiVersion: v1
kind: Pod
metadata:
name: memory-demo-2
spec:
containers:
- name: memory-demo
image: nginx
resources:
requests:
cpu: 0.2
memory: 100Mi
limits:
cpu: 0.5
memory: 300Mi
---
apiVersion: v1
kind: Pod
metadata:
name: memory-demo-3
spec:
containers:
- name: memory-demo
image: nginx
resources:
requests:
cpu: 0.2
memory: 100Mi
limits:
cpu: 0.5
memory: 300Mi
拉起清单
kubectl apply -f pod.yaml