Optimize Kubernetes CPU Resources with Ansible Playbooks
By Luca Berton · Published 2024-01-01 · Category: troubleshooting
Learn to assign CPU resources to Kubernetes and OpenShift pods using Ansible. Streamline your container management with effective resource configuration.

How to Assign CPU Resources to Kubernetes (K8s) or OpenShift (OCP) Containers and Pods with Ansible
Welcome to another episode of Ansible Pilot! I'm Luca Berton, and today I'll show you how to manage CPU resource allocation for containers and pods in Kubernetes (K8s) and OpenShift (OCP) using Ansible.
In Kubernetes and OpenShift, containers cannot exceed their configured CPU limits. If there's available CPU time, a container is guaranteed as much CPU as it requests. You can control this behavior using the resources field in your container's manifest. Specifically, resources:requests sets the amount of CPU the container is guaranteed, while resources:limits specifies the maximum amount of CPU the container can use.
See also: Deploy Kubernetes Resources with Ansible Playbook
Using Ansible for Kubernetes and OpenShift
Introduction to the k8s Module
Ansible provides the kubernetes.core.k8s module to manage Kubernetes (K8s) and OpenShift (OCP) resources. This module allows you to create, update, and delete various Kubernetes objects.
Key Parameters
• name: The name of the Kubernetes object. • namespace: The namespace in which the object resides. • api_version: The API version of the object (e.g., "v1"). • kind: The type of the object (e.g., Pod, Namespace). • state: Desired state of the object (e.g.,present, absent, patched).
• definition: A YAML or JSON definition of the object.
• src: Path to a file containing the YAML or JSON definition.
• template: A YAML or JSON template for the object.
• validate: How to validate the resource definition against Kubernetes schema.
For detailed documentation, visit: • kubernetes.core.k8s • Kubernetes CPU Resource Management
Example Playbook
This playbook demonstrates how to create a namespace and a pod with CPU resource requests and limits. We will use the vish/stress image to simulate CPU load.
Playbook Code
Save the following YAML as ansible_playbook.yml:
---
- name: k8s CPU Resource Management Playbook
hosts: localhost
gather_facts: false
connection: local
vars:
myproject: "cpu-example"
tasks:
- name: Create namespace
kubernetes.core.k8s:
kind: Namespace
name: "{{ myproject }}"
state: present
api_version: v1
- name: Create Pod with CPU resources
kubernetes.core.k8s:
state: present
definition:
apiVersion: v1
kind: Pod
metadata:
name: cpu-Playbook
namespace: "{{ myproject }}"
spec:
containers:
- name: cpu-Playbook-ctr
image: vish/stress
resources:
limits:
cpu: "1"
requests:
cpu: "0.5"
args:
- -cpus
- "2"
Execution
Run the playbook using the following command:
ansible-playbook ansible_playbook.yml
Output
After running the playbook, you should see output similar to:
PLAY [k8s CPU Resource Management Playbook] ************************************************************************************
TASK [Create namespace] *******************************************************************************************************
changed: [localhost]
TASK [Create Pod with CPU resources] *******************************************************************************************
changed: [localhost]
PLAY RECAP ********************************************************************************************************************
localhost : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Idempotency
Running the playbook again should yield:
PLAY [k8s CPU Resource Management Playbook] ************************************************************************************
TASK [Create namespace] *******************************************************************************************************
ok: [localhost]
TASK [Create Pod with CPU resources] *******************************************************************************************
ok: [localhost]
PLAY RECAP ********************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Verification
• Before ExecutionCheck if the namespace exists:
kubectl get namespace | grep cpu
For OpenShift:
oc get namespace | grep cpu
• After Execution
Verify the namespace and pod:
kubectl get namespace cpu-example
kubectl get pod cpu-Playbook --namespace=cpu-example
kubectl get pod cpu-Playbook --namespace=cpu-example --output=yaml
For OpenShift:
oc get namespace cpu-example
oc get pods --namespace=cpu-example
oc get pod cpu-Playbook --namespace=cpu-example --output=yaml
Logs
To verify the pod's behavior, view its logs:
kubectl logs cpu-Playbook --namespace=cpu-example
See also: Assign Memory to Kubernetes Pods with Ansible
Conclusion
You've now learned how to use Ansible to manage CPU resources for containers and pods in Kubernetes and OpenShift. This method allows you to ensure efficient resource allocation and management, enhancing the performance and stability of your applications.
Feel free to explore additional configurations and options to tailor your resource management to your specific needs. Thanks for joining this episode of Ansible Pilot, and happy automating!
Related Articles
• Jinja2 templating in AnsibleCategory: troubleshooting
Watch the video: Optimize Kubernetes CPU Resources with Ansible Playbooks — Video Tutorial