-->

DEVOPSZONES

  • Recent blogs

    calculate the number of required nodes when moving a microservice to another node group in Kubernetes

     Here’s a Python script to calculate the number of required nodes when moving a microservice to another node group in Kubernetes.

    Script: calculate_nodes.py

    python
    def calculate_required_nodes(cpu_request_per_pod, memory_request_per_pod, num_pods,
    cpu_per_node, memory_per_node, allocatable_percentage=0.9): """ Calculate the number of nodes required when migrating a microservice to another node group. Args: cpu_request_per_pod (float): CPU requested per pod (in cores, e.g., 0.5 for 500m). memory_request_per_pod (float): Memory requested per pod (in GiB). num_pods (int): Number of pods to be deployed. cpu_per_node (float): Total CPU capacity of a node (in cores). memory_per_node (float): Total Memory capacity of a node (in GiB). allocatable_percentage (float): Percentage of node resources available for workloads (default: 90%). Returns: int: Required number of nodes. """ # Compute total resource requests total_cpu = cpu_request_per_pod * num_pods total_memory = memory_request_per_pod * num_pods # Compute available resources per node available_cpu = cpu_per_node * allocatable_percentage available_memory = memory_per_node * allocatable_percentage # Compute required nodes based on CPU and Memory nodes_cpu = total_cpu / available_cpu nodes_memory = total_memory / available_memory # Take the maximum value and round up required_nodes = max(nodes_cpu, nodes_memory) return int(required_nodes) if required_nodes.is_integer() else int(required_nodes) + 1 # Example Usage if __name__ == "__main__": cpu_request_per_pod = 0.5 # Each pod requires 0.5 CPU cores memory_request_per_pod = 1 # Each pod requires 1 GiB memory num_pods = 10 # Number of pods to deploy cpu_per_node = 4 # Each node has 4 CPU cores memory_per_node = 16 # Each node has 16 GiB memory required_nodes = calculate_required_nodes(cpu_request_per_pod, memory_request_per_pod, num_pods, cpu_per_node, memory_per_node) print(f"Required Nodes: {required_nodes}")

    How to Use?

    1. Save this script as calculate_nodes.py
    2. Run the script in a terminal:
      python calculate_nodes.py
    3. Output Example (for 10 pods, 0.5 CPU & 1GiB per pod):
      Required Nodes: 2

    Key Features

    Handles both CPU and memory constraints
    Considers Kubernetes node allocatable resources (default 90%)
    Rounds up to ensure sufficient resources
    Easily customizable for different workloads


    Key Considerations

    • If the node auto-scaler is enabled, nodes will scale dynamically.
    • Affinity/Anti-affinity rules may affect pod distribution.
    • If using vertical pod autoscaler (VPA), pod resource requests can change dynamically.
    • Overhead from networking, daemon sets, or system pods can affect node availability.




    No comments