kubernetes之配置namespace中Pod的总数
简介
限制namespace中运行的Pod个数,设置Pod配额(Quota)。
备注:此文档参考官方文档,并加以自己的理解。如有误导性的内容,请批评指正。
namepace的Pod配置示例
创建namespace
# kubectl create namespace quota-pod-example
创建一个ResourceQuota
对象。文件名:quota-pod.yaml
apiVersion: v1
kind: ResourceQuota
metadata:
name: pod-demo
spec:
hard:
pods: "2"
# kubectl apply -f /root/k8s-example/namespace/quota-pod.yaml --namespace=quota-pod-example
查看输出结果
# kubectl get resourcequota pod-demo --namespace=quota-pod-example --output=yaml
从结果中可以看到,Pod配额为2个
,使用了0个
spec:
hard:
pods: "2"
status:
hard:
pods: "2"
used:
pods: "0"
创建一个Deployment
资源,启动一个3副本的Pod
。文件名:quota-pod-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: pod-quota-demo
spec:
selector:
matchLabels:
purpose: quota-demo
replicas: 3
template:
metadata:
labels:
purpose: quota-demo
spec:
containers:
- name: pod-quota-demo
image: nginx
# kubectl apply -f /root/k8s-example/namespace/quota-pod-deployment.yaml --namespace=quota-pod-example
查看Deployment
资源详情
# kubectl get deployment pod-quota-demo --namespace=quota-pod-example --output=yaml
从输出结果可以看出,已创建2个Pod,但是由于该namespace的Pod配额是2个,所以第3个无法被创建
spec:
...
replicas: 3
...
status:
availableReplicas: 2
...
lastUpdateTime: 2017-07-07T20:57:05Z
message: 'unable to create pods: pods "pod-quota-demo-1650323038-" is forbidden:
exceeded quota: pod-demo, requested: pods=1, used: pods=2, limited: pods=2'
查看正在运行的Pod
# kubectl get po -n quota-pod-example
NAME READY STATUS RESTARTS AGE
pod-quota-demo-56d6f96f68-8c9px 1/1 Running 0 6m43s
pod-quota-demo-56d6f96f68-zq6v6 1/1 Running 0 6m43s
实验完毕,删除Pod
# kubectl delete namespace quota-pod-example
总结
1、为了充分利用资源,可以给namespace定义Pod配置,当超过该Pod配额值时,Pod不会被创建。