# Pod数量限额

参考文档:Configure a Pod Quota for a Namespace (opens new window)

本文通过实例演示了如何通过ResourceQuota为名称空间配置最多可以运行多少个Pod。演示的步骤如下:

# 创建名称空间

为本次演示创建名称空间:

kubectl create namespace quota-pod-example
1

# 创建ResourceQuota

为本次演示创建 ResourceQuota 对象,yaml文件如下所示:







 

apiVersion: v1
kind: ResourceQuota
metadata:
  name: pod-demo
spec:
  hard:
    pods: "2"
1
2
3
4
5
6
7

执行命令创建该 ResourceQuota

kubectl apply -f https://kuboard.cn/statics/learning/policy/rq-pod-quota.yaml --namespace=quota-pod-example
1

执行如下命令查看已创建的 ResourceQuota

kubectl get resourcequota pod-demo --namespace=quota-pod-example --output=yaml
1

输出结果中显示了该名称空间的配额限定了只能创建两个 Pod,当前没有任何 Pod 被创建:



 


 

 

spec:
  hard:
    pods: "2"
status:
  hard:
    pods: "2"
  used:
    pods: "0"
1
2
3
4
5
6
7
8

# 创建Pod

创建如下 Deployment

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

该 Deployment 的副本数为 3 replicas: 3,执行命令以创建该 Deployment:

kubectl apply -f https://kuboard.cn/statics/learning/policy/rq-pod-deployment.yaml --namespace=quota-pod-example
1

执行命令以查看 Deployment 的详细信息

kubectl get deployment pod-quota-demo --namespace=quota-pod-example --output=yaml
1

尽管 Deployment 期望的副本数是 3,但是由于名称空间通过 ResourceQuota 限定了最大的 Pod 数量,因此,最终只有两个 Pod 被创建成功。输出结果如下所示:



 


 


 


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'
1
2
3
4
5
6
7
8
9
10

# 清理

删除名称空间可清理本次演示创建的对象:

kubectl delete namespace quota-pod-example
1
更新时间: 2019-10-22 21:37:01