This section explains how to allocate memory requests and make memory restrictions on a container. A container is guaranteed to have enough memory to handle the request, but it is also not allowed to use more than the limit of memory.
Before you begin
Need to have a k8s cluster
A kubectl tool needs to be installed and able to communicate with the cluster.
If you are not ready, you can use Minikube or use the following practice space.
Https://www.katacoda.com/courses/kubernetes/playground
http://labs.play-with-k8s.com/
You can use the following command to detect the version
Kubectl version
Each node in the cluster has at least 300MB of memory.
Some of the steps in this article require that you have a service running Metrics-server in your cluster, and you can skip these steps if you are not running.
If you are using Minikube, you can use the following command to enable:
Minikube Addons Enable Metrics-server
To check if Metrics-server is running, or to provide Metrics API resources (Metrics.k8s.io), you can use the following command
Kubectl Get Apiservices
If the Metrics API resource is available, it responds to the following:
NAME
V1beta1.metrics.k8s.io
Create a Namespace
Create namespaces to ensure isolation of the current test environment and other environments of the cluster.
Define a memory request and memory limit
Define a Memory Request:
Resources:requests
Define a memory limit:
Resources:limits
In this exercise, you create a pod that contains a container, the container requests 100MB of memory, and the 200MB memory limit is set.
Apiversion:v1
Kind:pod
Metadata
Name:memory-demo
Namespace:mem-example
Spec
Containers
Name:memory-demo-ctr
Image:polinux/stress
Resources
Limits
Memory: "200Mi"
Requests:
Memory: "100Mi"
Command: ["stress"]
args: ["--VM", "1", "--vm-bytes", "150M", "--vm-hang", "1"]
Args is the parameter that provides the container to start.
"--vm-bytes", "150M" tells the container how much memory to allocate
Create a Pod
Kubectl create-f Https://k8s.io/examples/pods/resource/memory-request-limit.yaml--namespace=mem-example
Verify that the pod is running
Kubectl Get pod Memory-demo--namespace=mem-example
View pod details and output to Yaml
Kubectl get pod Memory-demo--output=yaml--namespace=mem-example
Get the technical specifications for Pods
Kubectl Top pod Memory-demo--namespace=mem-example
The memory size shown is 162,900,000 bytes, approximately 150MB, which is greater than the allocated 100MB memory and less than 200MB of memory limit.
NAME CPU (cores) MEMORY (bytes)
Memory-demo <something> 162856960
Delete pod
Kubectl Delete pod Memory-demo--namespace=mem-example
1-1 allocating memory resources to containers and pods