Skip to main content

Kubectl cost queries

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 & efficiency for any Kubernetes workload.

Installation instructions: kubectl cost:

kubectl-cost usage

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

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 \
--show-efficiency=false

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 \
--show-efficiency=true

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.