一、默认的requests和limits¶
说明:配置的默认requests和limits作用层面仅限于pod,对deployment等高级调度资源不生效
1.定义一个yaml文件
[root@k8s-master01 study]# vim limitrange.yaml
apiVersion: v1
kind: LimitRange
metadata:
name: cpu-mem-limit-range
spec:
limits:
- default:
cpu: 1
memory: 512Mi
defaultRequest:
cpu: 0.5
memory: 256Mi
type: Container
#default:默认limits配置
#defaultRequest:默认requests配置
2.开始创建
[root@k8s-master01 study]# kubectl create ns rq-test
[root@k8s-master01 study]# kubectl create -f limitrange.yaml -n rq-test
3.创建deployment
[root@k8s-master01 study]# kubectl create deploy test-1 --image=registry.cn-hangzhou.aliyuncs.com/zq-demo/nginx:1.14.2 --replicas=3 -n rq-test
4.查看deployment创建情况
[root@k8s-master01 study]# kubectl get deployment -n rq-test
NAME READY UP-TO-DATE AVAILABLE AGE
test-1 3/3 3 3 8m11s
5.在线编辑查看resources是否已配置,观察到没有配置
[root@k8s-master01 study]# kubectl edit deployment test-1 -n rq-test
...
...
name: nginx
resources: {}
...
...
6.查看pod生成的默认参数。观察到已成功添加到默认参数,同时说明limitrange对deployment不生效,而对pod生效
[root@k8s-master01 study]# kubectl get po -n rq-test
NAME READY STATUS RESTARTS AGE
test-1-74795ddfbc-6zxft 1/1 Running 0 13m
test-1-74795ddfbc-hhsnd 1/1 Running 0 13m
test-1-74795ddfbc-lxpjh 1/1 Running 0 6s
[root@k8s-master01 study]# kubectl get po test-1-74795ddfbc-lxpjh -n rq-test -oyaml
...
...
imagePullPolicy: IfNotPresent
name: nginx
resources:
limits:
cpu: "1"
memory: 512Mi
requests:
cpu: 500m
memory: 256Mi
...
...
二、限制容器可以使用的最大和最小资源¶
1.在上面基础修改yaml文件
[root@k8s-master01 study]# vim limitrange.yaml
apiVersion: v1
kind: LimitRange
metadata:
name: cpu-mem-limit-range
spec:
limits:
- default:
cpu: 1
memory: 512Mi
defaultRequest:
cpu: 0.5
memory: 256Mi
max:
cpu: "2"
memory: 1Gi
min:
cpu: "10m"
memory: 128Mi
type: Container
max:内存CPU的最大配置
min:内存CPU的最小配置
2.重新部署
[root@k8s-master01 study]# kubectl create ns rq-test
[root@k8s-master01 study]# kubectl replace -f limitrange.yaml -n rq-test
3.结果验证,观察到参数已经配置完成
[root@k8s-master01 study]# kubectl get limitrange -n rq-test -oyaml
apiVersion: v1
items:
- apiVersion: v1
kind: LimitRange
metadata:
creationTimestamp: "2022-12-11T02:11:12Z"
name: cpu-mem-limit-range
namespace: rq-test
resourceVersion: "31880"
uid: 3a3f1870-890c-4b67-aaf4-343b49bc6d75
spec:
limits:
- default:
cpu: "1"
memory: 512Mi
defaultRequest:
cpu: 500m
memory: 256Mi
max:
cpu: "2"
memory: 1Gi
min:
cpu: 10m
memory: 128Mi
type: Container
kind: List
metadata:
resourceVersion: ""
selfLink: ""
4.在线编辑deployment,修改内存以及CPU参数小于最小值
[root@k8s-master01 study]# kubectl edit deploy test-1 -n rq-test
...
...
resources:
requests:
cpu: 1m
memory: 1Mi
...
...
5.查看pod状态,观察到没有生成新的pod
[root@k8s-master01 study]# kubectl get po -n rq-test
NAME READY STATUS RESTARTS AGE
test-1-74795ddfbc-6zxft 1/1 Running 0 58m
test-1-74795ddfbc-hhsnd 1/1 Running 0 58m
test-1-74795ddfbc-lxpjh 1/1 Running 0 45m