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

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

Read the full written article: Optimize Kubernetes CPU Resources with Ansible Playbooks

Topics Covered

Related Video Tutorials