Skip to main content

kubectl cost

kubectl-cost is a kubectl plugin that provides easy CLI access to Kubernetes cost allocation metrics via the OpenCost API. It allows developers, devops, and others to quickly determine the cost and efficiency for any Kubernetes workload.

Installation instructions:

If you have Krew, the kubectl plugin manager, installed:

kubectl krew install cost

Otherwise, you can run the following commands:

os=$(uname | tr '[:upper:]' '[:lower:]') && \
arch=$(uname -m | tr '[:upper:]' '[:lower:]' | sed -e s/x86_64/amd64/) && \
curl -s -L$os-$arch.tar.gz | tar xz -C /tmp && \
chmod +x /tmp/kubectl-cost && \
sudo mv /tmp/kubectl-cost /usr/local/bin/kubectl-cost

For more information on installation please access kubectl cost.

kubectl-cost usage

kubectl cost will need to be pointed at the OpenCost service. The below example assumes you followed the installation instructions.

Potentially set an alias:

alias kcac='kubectl cost --service-port 9003 --service-name opencost --kubecost-namespace opencost --allocation-path /allocation/compute'

Find the cost of each namespace based on the last five days:

kubectl cost --service-port 9003 --service-name opencost --kubecost-namespace opencost --allocation-path /allocation/compute  \
namespace \
--historical \
--window 5d \
--show-cpu \
--show-memory \
--show-pv \

Find the total projected monthly costs based on the last two hours:

kubectl cost --service-port 9003 --service-name opencost --kubecost-namespace opencost --allocation-path /allocation/compute  \
namespace \
--window 2h \

Using the alias, show the total costs of all resources with the label app:

Note: Unallocated represents resources not using the label

kcac label --historical -l app --window 5d
Documentation Distributed under CC BY 4.0.  The Linux Foundation® (TLF) has registered trademarks and uses trademarks. For a list of TLF trademarks, see: Trademark Usage.