Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Using Kubernetes namespaces to manage memory and CPU resources (2)

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Manifesto of the Lady

It is well known that Kubernetes allows you to specify requests and restrictions for CPU and RAM, which is useful for resource consumption management of separate pod. In this series of articles, we will show you three strategies for efficient management of cluster resources.

However, if you are a Kubernetes cluster administrator, you may also want to control the global consumption of resources in the cluster and / or configure the default resource requirements for all containers.

Happily, Kubernetes supports cluster resource management at the namespace level. As you may already know, Kubernetes's namespace provides a range of names and resource quotas, which allows cluster resources to be effectively divided among multiple users, projects, and teams. In Kubernetes, you can define default resource requests and restrictions, resource constraints (minimum and maximum resource requests and limits), and resource quotas for all containers running in a given namespace. These features enable applications in the cluster to utilize resources efficiently and allocate resources effectively among different teams. For example, resource constraints that use namespaces allow you to control how production and development workloads use resources, thus allowing them to consume a fair share of limited cluster resources. This can be achieved by creating separate namespaces for production and development workloads and assigning them different resource constraints.

In this series of articles, we will show you three strategies for efficient management of cluster resources:

Set default resource requests and container limits

Define minimum and maximum resource constraints

Set resource quotas for all containers in the namespace

These strategies will help you solve a variety of use cases, taking advantage of the full capabilities of Kubernetes namespaces and resource management.

Set minimum and maximum resource constraints for namespaces

In this example, we will create a resource constraint for the namespace. These constraints are essentially the minimum and maximum amount of resources that the container can use in resource requests and restrictions. Let's see how it works!

As in the previous example, first create the namespace:

Kubectl create namespace resource-constraints-demo namespace "resource-constraints-demo" created

Next, we will create a scope for this namespace:

ApiVersion: v1

Kind: LimitRange

Metadata: name: resource-constraints-lr

Spec: limits:

-max:

Memory: 1Gi

Cpu: 0.8

Min:

Memory: 500Mi

Cpu: 0.3

Type: Container

Save LimitRange as limit-range-2.yaml and create it:

Kubectl create-f limit-range-2.yaml-- namespace resource-constraints-demo

Limitrange "resource-constraints-lr" created

After creating the scope, let's see if our minimum and maximum resource constraints are applied to the namespace:

Kubectl get limitrange resource-constraints-lr-namespace resource-constraints-demo-output=yaml

The response is as follows:

Spec:

Limits:

-default:

Cpu: 800m

Memory: 1Gi

DefaultRequest:

Cpu: 800m

Memory: 1Gi

Max:

Cpu: 800m

Memory: 1Gi

Min:

Cpu: 300m

Memory: 500Mi

Type: Container

As you can see, the default resource requests and restrictions for your namespace are automatically set to the maximum resource constraints specified in LimitRange. Now, when we create a container in the resource-constraints-demo namespace, the following rules apply automatically:

If the container does not specify its resource requests and restrictions, the default resource requests and restrictions are applied.

All containers in the namespace need to have resource requests greater than or equal to 300 million for CPU and 500 Mi memory.

All containers in the namespace require a resource limit of less than or equal to 800 million for CPU and 1 Gi memory.

Let's create a pod to show how to apply namespace resource constraints to the container:

ApiVersion: v1

Kind: Pod

Metadata:

Name: resource-constraints-pod

Spec:

Containers:

-name: resource-constraints-ctr

Image: httpd:2.4

Resources:

Limits:

Memory: "900Mi"

Cpu: 0.7

Requests:

Memory: "600Mi"

Cpu: 0.4

The specification requests 600 Mi RAM and 0.4 CPU and sets limits of 900 Mi RAM and 0.7 CPU for the httpd container in this pod. These resource requirements meet the minimum and maximum constraints of the namespace.

We save it as default-resources-demo-pod-3.yaml and create a pod in our namespace:

Kubectl create-f resource-constraints-pod.yaml-- namespace resource-constraints-demo

Pod "resource-constraints-pod" created

Next, check the resources allocated to the container in pod:

Kubectl get pod resource-constraints-pod-namespace resource-constraints-demo-output=yaml

You should get the following output:

Containers:

-image: httpd:2.4 imagePullPolicy: IfNotPresent name: resource-constraints-ctr resources:

Limits:

Cpu: 700m memory: 900Mi requests:

Cpu: 400m memory: 600Mi

The pod is created successfully because the container's requests and restrictions are within the minimum and maximum constraints of the namespace.

Now, let's see what happens if we specify requests and limits that exceed the minimum and maximum values defined for the namespace. Let's create a new pod with new requests and restrictions:

ApiVersion: v1

Kind: Pod

Metadata: name: resource-constraints-pod-2

Spec: containers:-name: resource-constraints-ctr-2 image: httpd:2.4 resources: limits: memory: "1200Mi" cpu: 1.2 requests: memory: "200Mi" cpu: 0.2

We save it as resource-constraints-pod-2.yaml and create a pod in our namespace:

Kubectl create-f resource-constraints-pod-2.yaml-- namespace resource-constraints-demo

Pod "resource-constraints-pod-2" created

Because the resource request is lower than the minimum LimitRange and the resource limit exceeds the maximum value of this namespace, the pod will not be created as expected:

Error from server (Forbidden): error when creating "resource-constraints-pod-2.yaml": pods "resource-constraints-pod-2" is forbidden: [minimum memory usage per Container is 500Mi, but request is 200Mi.minimum cpu usage per Container is 300m, but request is 200m.maximum cpu usage per Container is 800m, but limit is 1200m.maximum memory usage per Container is 1Gi, but limit is 1200Mi.]

Clear

After this example is complete, let's clean it up.

Delete the namespace:

Kubectl delete namespace resource-constraints-demo namespace "resource-constraints-demo" deleted

Summary

In this article, we will show you how to set minimum and maximum resource constraints for namespaces. The next series of articles will continue to show you how to set resource quotas for all containers in the namespace.

Discussion on HULK first-line Technology

The official account of technology sharing created by the 360Cloud platform team covers many technical areas, such as cloud computing, database, big data, monitoring, pan-front-end, automated testing, and so on. Through the accumulation of solid technology and rich first-line practical experience, to bring you the best technology sharing.

Original link: https://mp.weixin.qq.com/s/NXLNIL1rbItpzjAJmp9WUQ

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report