Aggregate Total vCPU Capacity from a Tanzu Kubernetes Grid Installation

You can obtain vCPU capacity from a Tanzu Kubernetes Grid environment either manually or by running a Bash script.

Manual Collection

  1. Set the KUBECONFIG environment variable to the path to the kubeconfig file for the Tanzu Kubernetes Grid management cluster.

    The command tanzu management-cluster get should run successfully.

  2. Get all cluster names.

    This allows iteration over the management and all workload clusters

    kubectl get -A clusters -o json | jq -r '.items[].metadata.name'
    
  3. Record the cluster vCPU capacity on the management cluster.

    kubectl get -A nodes -o json | jq '[.items[].status.capacity.cpu | tonumber] | add'
    
  4. Record the cluster vCPU capacity for each workload cluster that the management cluster manages.

    Run the following commands on each workload cluster

    1. Switch context to a workload cluster.

      kubectl config use-context "<CLUSTER_NAME>-admin@<CLUSTER_NAME>"
      
    2. Record the cluster vCPU capacity for that cluster.

      kubectl get -A nodes -o json | jq '[.items[].status.capacity.cpu | tonumber] | add'
      
    3. Repeat the procedure for each workload cluster. After iterating through all workload clusters, you should have a list of cluster vCPU capacities.

  5. Sum up all of the workload cluster vCPU capacities and the management cluster vCPU capacity to get the total vCPUs for the Tanzu Kubernetes Grid installation.

  6. Set the context back to the management cluster.

    tanzu cluster kubeconfig get "<MANAGEMENT_CLUSTER_NAME>" --admin
    

Bash Script Collection

  1. Set the KUBECONFIG environment variable to the path to the kubeconf file for the Tanzu Kubernetes Grid management cluster.

    The command tanzu management-cluster get should run successfully.

  2. Copy and paste the script from the Script section below into a file named get_cpu_script.sh.

  3. Give the file permission to run.
    chmod +x /path/to/file/get_cpu_script.sh
    
  4. Optionally uncomment the following lines to view CPU information per node.

    #  kubectl get -A nodes -o json | jq '.items[] | [.metadata.name, .status.capacity.cpu]' >> /tmp/tkg_cpu_script_log.txt
    
    #printf "\n---- Log output ----\n"
    #cat /tmp/tkg_cpu_script_log.txt
    #rm /tmp/tkg_cpu_script_log.txt
    
  5. Run the script. /path/to/file/get_cpu_script.sh

Script

Copy this script into a text file when you run the Bash Script Collection procedure.

#!/bin/bash

## Remove the `&>/dev/null` for cluster switching message to be printed to stdout. 
## This will break up the output formatting.

printf "\nCollection Timestamp:\t%s\n" "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"

CLUSTER_NAMES=$(kubectl get -A clusters -o json | jq -r '.items[].metadata.name')
if [[ "$CLUSTER_NAMES" == "" ]];
then
  printf "\nNo management cluster found. Please target the TKG management cluster\n"
  exit
fi

TOTAL_AGGREGATED_VCPUS=0

echo "-----------------------------"
printf "vCPUs\tCluster Name\n"
for cluster in $CLUSTER_NAMES
do
  if ! [[ "$cluster" == *"mgmt"* ]];
    then
      tanzu cluster kubeconfig get "$cluster" --admin &>/dev/null
  fi

  kubectl config use-context "$cluster-admin@$cluster" &>/dev/null

  cluster_vcpu_count=$(kubectl get -A nodes -o json | jq '[.items[].status.capacity.cpu | tonumber] | add')

## Uncomment the below line and the commented lines at the end of this script to view CPU per node [node name, cpu].
## This is a good way to verify that all clusters and nodes have been iterated over.
#  kubectl get -A nodes -o json | jq '.items[] | [.metadata.name, .status.capacity.cpu]' >> /tmp/tkg_cpu_script_log.txt

  TOTAL_AGGREGATED_VCPUS=$((TOTAL_AGGREGATED_VCPUS + cluster_vcpu_count))

  printf "$cluster_vcpu_count\t$cluster\n"
done

echo "-----------------------------"
printf "Total TKG vCPUs:\t%s\n" "$TOTAL_AGGREGATED_VCPUS"

## Uncomment the below lines to view the [node name, cpu] output.
#printf "\n---- Log output ----\n"
#cat /tmp/tkg_cpu_script_log.txt
#rm /tmp/tkg_cpu_script_log.txt

Output

The output changes depending on whether you have uncommented the applicable lines in the script.

If you did not uncomment the CPU per node output lines, you see the following output.

Collection Timestamp:	2022-01-24T18:45:11Z
-----------------------------
vCPUs	Cluster Name
4	atest22
4	test20
4	test21
4	ztest23
4	tkg-mgmt-vc
-----------------------------
Total TKG vCPUs:        20

If you did uncomment the CPU per node output lines, you see the following output.

Collection Timestamp:   2022-01-24T23:43:22Z
-----------------------------
vCPUs   Cluster Name
4       atest22
4       test20
4       test21
4       ztest23
4       tkg-mgmt-vc
-----------------------------
Total TKG vCPUs:        20

---- Node name and CPU output ----
[
  "atest22-control-plane-fsqxl",
  "2"
]
[
  "atest22-md-0-687d8dcf5c-xggq9",
  "2"
]
[
  "test20-control-plane-sqg59",
  "2"
]
[
  "test20-md-0-757c4977c6-b2rh5",
  "2"
]
[
  "test21-control-plane-k2p5n",
  "2"
]
[
  "test21-md-0-66df4495f5-jj9q4",
  "2"
]
[
  "ztest23-control-plane-4p787",
  "2"
]
[
  "ztest23-md-0-5954667dc9-hrqbc",
  "2"
]
[
  "tkg-mgmt-vc-control-plane-fjw4r",
  "2"
]
[
  "tkg-mgmt-vc-md-0-b84cc64c4-hc6lc",
  "2"
]
check-circle-line exclamation-circle-line close-line
Scroll to top icon