需求:集群管理为每一个pod配置Requests和Limits限制是繁琐的,
解决方案: kubernetes提供了LimitRange机制对pod和容器的Requests和Limits作出限制
LimitRange几种限制方式: 最大最小范围, Requests或Limits的默认值, limits与Requests的最大比例上限
1.创建namespace
kubectl create namespace limitrange-example
2.LimitRange资源对象
apiVersion: v1
kind: LimitRange
metadata:
name: mylimits
namespace: limitrange-example
spec:
limits:
- max:
cpu: "2"
memory: 4Gi
min:
cpu: 200m
memory: 6Mi
maxLimitRequestRatio:
cpu: 2
memory: 2
type: Pod
- default:
cpu: 300m
memory: 200Mi
defaultRequest:
cpu: 200m
memory: 100Mi
min:
cpu: 100m
memory: 3Mi
max:
cpu: "2"
memory: 1Gi
maxLimitRequestRatio:
cpu: 5
memory: 4
type: Container
3.查看limitrange对象信息
kubectl describe limitrange mylimits -n limitrange-example
说明
type为Pod:
min表示是pod中所有容器的requests值的总和下限
max表示pod中所有容器的limits值的总和上限
maxLimitRequestRatio表示是pod中所有容器的Limits值总和与Requests值总和的比例上限
type为Requests:
min表示是pod中的容器的requests值的下限
max表示pod中容器的limits值的上限
defaultRequest: pod中未指定Requests值的容器的默认Requests值
default表示pod中未指定Limits值的容器的默认Limits值
maxLimitRequestRatio表示是pod中所有容器的Limits值总和与Requests值比例上限
4.创建pod
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: limitrange-example
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.11
ports:
- containerPort: 80
说明: 由于该pod未配置Requests和Limits, 所以使用默认定义的Requests和Limits值
5.配置超过资源限制的pod, 结果是无法创建的
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: limitrange-example
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.11
ports:
- containerPort: 80
resources:
limits:
cpu: "3"
memory: 2Gi