Optimize Kubernetes CPU Resources with Ansible Playbooks — Video Tutorial
Learn to assign CPU resources to Kubernetes and OpenShift pods using Ansible. Streamline your container management with effective resource configuration.
Watch Video
Watch "Optimize Kubernetes CPU Resources with Ansible Playbooks" on YouTube
What You'll Learn
- How to Assign CPU Resources to Kubernetes (K8s) or OpenShift (OCP) Containers and Pods with Ansible
- Using Ansible for Kubernetes and OpenShift
- Introduction to the `k8s` Module
- Example Playbook
- Playbook Code
- Execution
- Output
- Idempotency
- Verification
- Logs
Full Tutorial Content
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.
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](https://docs.ansible.com/ansible/latest/collections/kubernetes/core/k8s_module.html)
- [Kubernetes CPU Resource Management](https://kubernetes.io/docs/tasks/configure-pod-container/assign-cpu-resource/)
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`:
```yaml
---
- 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
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 4 min
- Category: troubleshooting
Read the full written article: Optimize Kubernetes CPU Resources with Ansible Playbooks
Topics Covered
Related Video Tutorials
- Deploy Kubernetes Resources with Ansible Playbook — Learn how to deploy Kubernetes resources using Ansible. Follow this guide to create namespaces, pods, and services with an Ansible playbook.
- Assign Memory to Kubernetes Pods with Ansible — Learn how to assign memory resources to Kubernetes or OpenShift containers and pods using Ansible. Follow our live Playbook and simple code examples.
- Configure a Pod to Use a Volume for Storage on Kubernetes or OpenShift with Ansible — Learn how to configure a Kubernetes or OpenShift Pod to use a volume for persistent storage with Ansible. This guide includes a live Playbook example, Ansible module details, and execution steps for managing storage efficiently.
- Create Kubernetes K8s or OpenShift OCP Pod - nginx - Ansible module k8s — How to automate the creation of "nginx" Pod in namespace "example" of Kubernetes K8s or OpenShift OCP with Ansible module k8s.
- Ansible troubleshooting - Kubernetes K8s or OpenShift OCP 401 Unauthorized — Explore troubleshooting steps for Kubernetes 401 Unauthorized errors in Ansible when interacting with Kubernetes or OpenShift clusters.
- Create Kubernetes K8s or OpenShift OCP Secret - Ansible module k8s — How to automate the creation of “mysecret” secret in namespace “default” Kubernetes K8s or OpenShift OCP with Ansible module k8s.