Kubernetes-ResourceQuota配额限制

本文介绍了Kubernetes通过ResourceQuota限制namespace下资源的方法。包括创建namespace和ResourceQuota,限制pod数量等操作示例。还说明了ResourceQuota可限制的配额类型,如pod的cpu/内存等。同时指出一个namespace可创建多个ResourceQuota,需手动创建且无默认配额,限制cpu/mem时创建pod需指定相关参数。
部署运行你感兴趣的模型镜像

Kubernetes通过ResourceQuota来限制一个namespace下面的资源,简单的使用:

创建一个namespace:

cat <<EOF > create-ns-test.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: test
EOF
 
kubectl create -f create-ns-test.yaml

创建一个ResourceQuota,限制创建的pod数量为1

cat <<EOF > create-quota-test.yaml
apiVersion: v1
kind: ResourceQuota
metadata:
  name: quota-ns
spec:
  hard:
    pods: "1"
EOF
 
kubectl create -f create-quota-test.yaml --namespace=test

查看该配额的信息

kubectl --namespace=test get quota quota-ns -ojson
 
{
    "apiVersion": "v1",
    "kind": "ResourceQuota",
    "metadata": {
        "creationTimestamp": "2018-01-05T09:02:36Z",
        "name": "quota-ns",
        "namespace": "test",
        "resourceVersion": "840722",
        "selfLink": "/api/v1/namespaces/test/resourcequotas/quota-ns",
        "uid": "2c8f062f-f1f7-11e7-a73c-fa163ea226e1"
    },
    "spec": {
        "hard": {
            "pods": "1"
        }
    },
    "status": {
        "hard": {
            "pods": "1"
        },
        "used": {
            "pods": "0"
        }
    }
}

先创建一个pod

cat <<EOF > create-pod-test.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-test1
spec:
  containers:
  - name: nginx
    image: 10.10.10.7:8090/library/nginx:1.11.13
    ports:
    - containerPort: 80
EOF
 
kubectl --namespace=test create -f create-pod-test.yaml

查看该配额的信息

kubectl --namespace=test get quota quota-ns -ojson
 
{
    "apiVersion": "v1",
    "kind": "ResourceQuota",
    "metadata": {
        "creationTimestamp": "2018-01-05T09:02:36Z",
        "name": "quota-ns",
        "namespace": "test",
        "resourceVersion": "841655",
        "selfLink": "/api/v1/namespaces/test/resourcequotas/quota-ns",
        "uid": "2c8f062f-f1f7-11e7-a73c-fa163ea226e1"
    },
    "spec": {
        "hard": {
            "pods": "1"
        }
    },
    "status": {
        "hard": {
            "pods": "1"
        },
        "used": {
            "pods": "1"
        }
    }
}

再创建一个pod

# 先修改下yaml文件中的name
kubectl --namespace=test create -f create-pod-test.yaml
Error from server (Forbidden): error when creating "create-pod-test.yaml": pods "nginx-test2" is forbidden: exceeded quota: quota-ns, requested: pods=1, used: pods=1, limited: pods=1

可以看到已经创建失败,配额限制

 

ResourceQuota可以限制的配额包括,pod的cpu/内存、pod数量、service数量、rc数量、pvc数量等

目前发现:

1. 一个namespace下面可以创建多个ResourceQuota(ResourceQuota本身也可以限制ResourceQuota的数量),常规的使用是把计算资源quota(cpu、mem等),存储资源quota(storage、pvc等),对象数量quota(pod、service等)分别创建在不同的ResourceQuota下面,但是各个ResourceQuota中的限制项是可以重复的,这时会取最小的值,如下

# 分别创建2个ResourceQuota, 其中
# quota-test1限制pod数量为1,service数量为2
# quota-test2限制pod数量为2,service数量为1
# 在当前namespace下面已经存在1个pod和1个service的情况下:
kubectl create -f create-pod.yaml --namespace=test
Error from server (Forbidden): error when creating "create-pod.yaml": pods "nginx-test1" is forbidden: exceeded quota: quota-test1, requested: pods=1, used: pods=1, limited: pods=1
# 创建pod时,超过quota-test1的配额限制
 
kubectl create -f create-service.yaml --namespace=test
Error from server (Forbidden): error when creating "create-service.yaml": services "my-nginx-svc1" is forbidden: exceeded quota: quota-test2, requested: services=1, used: services=1, limited: services=1
# 创建service时,超过quota-test2的配额限制

2. ResourceQuota是需要手动创建的,每个namespace没有默认配额,如果当前已经存在资源的情况下(比如存在6个pod)

    这时创建一个ResourceQuota限制pod数量为5,可以创建成功,并且已有的pod不会自动删除

3. 如果ResourceQuota中限制了cpu/mem,则创建pod时就必须指定cpu和mem,否则创建失败

kubectl --namespace=test create -f create-pod-test.yaml
Error from server (Forbidden): error when creating "create-pod-test.yaml": pods "nginx-test" is forbidden: failed quota: quota-ns: must specify limits.cpu,limits.memory,requests.cpu,requests.memory

 

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

### 在 Kubernetes 中设置资源配额限制的最佳实践 在 Kubernetes 中,合理配置资源配额限制是确保集群稳定性和资源高效利用的关键。以下详细说明了如何实现这一目标。 #### 1. 使用 LimitRange 设置 Pod 和容器的默认资源限制 LimitRange 是一种资源对象,用于为命名空间中的 Pod 和容器定义默认的资源请求和限制。通过设置最小和最大资源值,可以防止用户创建超出范围的资源消耗型 Pod[^1]。例如: ```yaml apiVersion: v1 kind: LimitRange metadata: name: example-limitrange namespace: default spec: limits: - default: memory: 512Mi cpu: "500m" defaultRequest: memory: 256Mi cpu: "250m" type: Container ``` 上述配置为每个容器设置了默认的 CPU 和内存请求及限制。如果用户未指定这些值,Kubernetes 将自动应用默认值[^3]。 #### 2. 应用 ResourceQuota 管理命名空间级别的资源使用 ResourceQuota 允许管理员为命名空间设置整体资源使用上限,包括 CPU、内存、Pod 数量等。这有助于防止单个命名空间占用过多资源,影响其他租户的正常运行[^2]。示例配置如下: ```yaml apiVersion: v1 kind: ResourceQuota metadata: name: quota-example namespace: quota-example003 spec: hard: pods: "5" requests.cpu: "0.5" requests.memory: "512Mi" limits.cpu: "2" limits.memory: "2Gi" ``` 此配置限制了命名空间中最多可创建 5 个 Pod,并对 CPU 和内存的请求与限制进行了约束[^4]。 #### 3. 结合使用 LimitRange 和 ResourceQuota 单独使用 LimitRange 或 ResourceQuota 可能无法完全满足复杂的资源管理需求。建议将两者结合使用,以实现更精细的控制。例如,通过 LimitRange 设置容器级别的默认资源限制,同时通过 ResourceQuota 控制整个命名空间的资源消耗上限[^1]。 #### 4. 监控资源使用情况并动态调整配额 即使设置了初始配额限制,也应定期监控实际资源使用情况,并根据需要进行调整。Kubernetes 提供了多种工具(如 Metrics Server 和 Prometheus)来帮助管理员分析资源利用率,从而优化资源配置[^3]。 #### 5. 配置策略以处理资源不足的情况 当资源接近配额上限时,应制定明确的策略来处理潜在问题,例如扩展集群节点或调整现有配额。此外,可以通过 Horizontal Pod Autoscaler(HPA)实现基于负载的自动扩展,以应对突发流量[^5]。 ```yaml apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata: name: example-hpa namespace: default spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: example-deployment minReplicas: 2 maxReplicas: 10 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 50 ``` 上述 HPA 配置会根据 CPU 利用率动态调整副本数量,确保应用始终拥有足够的资源支持[^5]。 ### 注意事项 - 确保所有资源限制配额符合实际工作负载需求,避免因限制过严导致性能下降。 - 在修改现有配额限制时,需谨慎评估对已有工作负载的影响[^1]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值