Introduction

Kubernetes has become a standard in orchestrating containerized applications, and managing its costs effectively is crucial for optimizing resources and budgeting. Kubecost provides a real-time cost monitoring and management solution for Kubernetes, offering visibility into your Kubernetes usage and helping to reduce costs. In this tutorial, we will walk through the process of deploying Kubecost on a Kubernetes cluster managed by k3s, a lightweight Kubernetes distribution ideal for edge, IoT, and CI/CD environments.

What is K3s?

K3s is a highly available, certified Kubernetes distribution designed for production workloads in unattended, resource-constrained, remote locations or inside IoT appliances. It simplifies the operation of Kubernetes clusters and reduces the resource overhead required to run them. k3s removes many non-essential features to minimize its footprint.

What is Kubecost?

Kubecost provides cost visibility and insights for Kubernetes workloads, helping teams monitor and manage the costs associated with their Kubernetes resources. It integrates with your Kubernetes cluster and provides detailed information about resource usage and cost efficiencies.

Prerequisites

Before we start, ensure you have the following:

  1. A machine with at least 2GB RAM and 20GB of available disk space.
  2. Basic familiarity with command-line interfaces and Kubernetes concepts.
  3. A clean installation of Ubuntu 20.04 LTS or similar Linux distribution.

Before we begin the process, it is essential to update and upgrade the Ubuntu package index to ensure we have the latest package information and software versions. To accomplish this, execute the following command in your terminal:

sudo apt update && sudo apt upgrade
How to Deploy Kubecost on Kubernetes Using K3s

Step 1: Installing K3s

First, you need to install K3s. This process is straightforward thanks to the installation script provided by Rancher.

  1. Open your terminal.
  2. Run the following command to download and execute the k3s installation script:
curl -sfL https://get.k3s.io | sh -
Installing K3s on Ubuntu
sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
sudo chown $(id -u):$(id -g) ~/.kube/config
  1. Once the installation is complete, check if k3s is running:
sudo k3s kubectl get node

This command should show your node in a “Ready” state.

check if k3s is running

Step 2: Installing Helm

Kubecost is installed via Helm, a package manager for Kubernetes. Here’s how to install Helm:

  1. Download Helm:
curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
Installing Helm
  1. Verify the installation:
helm version
version.BuildInfo{Version:"v3.14.4", GitCommit:"81c902a123462fd4052bc5e9aa9c513c4c8fc142", GitTreeState:"clean", GoVersion:"go1.21.9"}

Step 3: Adding the Kubecost Helm Repository

Now, add the Kubecost repository to Helm:

helm repo add kubecost https://kubecost.github.io/cost-analyzer/
helm repo update
Adding the Kubecost Helm Repository

Step 4: Deploying Kubecost

With the repository added, you can now deploy Kubecost using Helm:

Install Kubecost using Helm:

    To begin with Kubecost, visit their official website and click on the “Get Started” option available at this link and get your token: Kubecost Installation Guide.

    Replace "YOUR_KUBECOST_TOKEN" with the actual token which you can obtain from the Kubecost website.

    helm install kubecost cost-analyzer \
    --repo https://kubecost.github.io/cost-analyzer/ \
    --namespace kubecost --create-namespace \
    --set kubecostToken="YOUR_KUBECOST_TOKEN"
    Deploying Kubecost on Kubernetes

    Step 5: Accessing Kubecost

    After the installation, Kubecost’s services will be running in your kubecost namespace. To access the Kubecost dashboard:

    1. Forward the port from the Kubecost service to your local machine:
    kubectl port-forward --namespace kubecost deployment/kubecost-cost-analyzer 9090

    Open your web browser and go to http://localhost:9090. This will load the Kubecost dashboard, where you can start monitoring costs.

    Kubecost dashboard

    Congratulations! You have successfully installed K3s, set up Helm, and deployed Kubecost in your Kubernetes cluster. You now have a powerful tool at your disposal to help manage and optimize the costs associated with your Kubernetes resources.

    Understanding and managing costs is crucial in any production environment. Kubecost provides detailed insights that allow you to see which components are driving up costs and where you can optimize to save money. With this setup, you are well on your way to maintaining a cost-effective and efficient Kubernetes environment.

    Additional Resources

    To further enhance your understanding and capabilities in managing Kubernetes, consider exploring the following resources:

    • Kubernetes official documentation
    • K3s documentation
    • Helm charts and their configurations

    By leveraging these tools and resources, you will enhance your Kubernetes administration skills and ensure your deployments are both robust and cost-efficient.