DEV Community

Cover image for Deploy Kubernetes Dashboard with Metrics Server using Terraform and Helm on Docker Desktop
Garis Space
Garis Space

Posted on • Updated on

Deploy Kubernetes Dashboard with Metrics Server using Terraform and Helm on Docker Desktop

This is a simple example of how to deploy the Kubernetes Dashboard and Metrics Server using Terraform and Helm on Docker Desktop.


Step 1 - Start the local Kubernetes cluster from Docker Desktop

Go to Docker Desktop > Preferences > Kubernetes > Enable Kubernetes and click Apply & Restart

Docker Desktop Settings


Optional Step 2 - Set the kubectl context to Docker Desktop

If you have multiple kubectl contexts, you can set the current context to Docker Desktop using the following command:



kubectl config use-context docker-desktop


Enter fullscreen mode Exit fullscreen mode

Step 3 - Install Terraform for macOs (if not already installed)

Download and install Terraform from https://www.terraform.io/downloads.html



brew tap hashicorp/tap && brew install hashicorp/tap/terraform


Enter fullscreen mode Exit fullscreen mode

Step 4 - Clone repository



git clone https://github.com/garis-space/tf-helm-kubernetes-dashboard-and-metrics-server.git
cd tf-helm-kubernetes-dashboard-and-metrics-server


Enter fullscreen mode Exit fullscreen mode

In the root directory of this repository, you will find a main.tf file which contains the Terraform configuration to deploy the Kubernetes Dashboard and Metrics Server.



terraform {
  required_providers {
    # Kubernetes provider
    kubernetes = {
      source  = "hashicorp/kubernetes"
    }
    # Helm provider
    helm = {
      source  = "hashicorp/helm"
    }
  }
}

# Path to config file for the Kubernetes provider as variable
variable "kubeconfig" {
  type = string
  # Load the kubeconfig from your home directory (default location for Docker Desktop Kubernetes)
  default = "~/.kube/config"
}

# Kubernetes provider configuration
provider "kubernetes" {
  config_path = var.kubeconfig
}

# Helm provider configuration
provider "helm" {
  # Local Kubernetes cluster from Docker Desktop
  kubernetes {
    # Load the kubeconfig from your home directory
    config_path = var.kubeconfig
  }
}

###
# Create a new Kubernetes namespace for the application deployment
###
resource "kubernetes_namespace" "kubernetes_dashboard" {
  metadata {
    name = "kubernetes-dashboard"
  }
}

###
# Install the Kubernetes Dashboard using the Helm provider
###
resource "helm_release" "kubernetes_dashboard" {
  # Name of the release in the cluster
  name       = "kubernetes-dashboard"

  # Name of the chart to install
  repository = "https://kubernetes.github.io/dashboard/"

  # Version of the chart to use
  chart      = "kubernetes-dashboard"

  # Wait for the Kubernetes namespace to be created
  depends_on = [kubernetes_namespace.kubernetes_dashboard]

  # Set the namespace to install the release into
  namespace  = kubernetes_namespace.kubernetes_dashboard.metadata[0].name

  # Set service type to LoadBalancer
  set {
    name  = "service.type"
    value = "LoadBalancer"
  }

  # Set service external port to 9080
  set {
    name  = "service.externalPort"
    value = "9080"
  }

  # Set protocol to HTTP (not HTTPS)
  set {
    name  = "protocolHttp"
    value = "true"
  }

  # Enable insecure login (no authentication)
  set {
    name  = "enableInsecureLogin"
    value = "true"
  }

  # Enable cluster read only role (no write access) for the dashboard user
  set {
    name  = "rbac.clusterReadOnlyRole"
    value = "true"
  }

  # Enable metrics scraper (required for the CPU and memory usage graphs)
  set {
    name  = "metricsScraper.enabled"
    value = "true"
  }

  # Wait for the release to be deployed
  wait = true
}

###
# Install the Metrics Server using the Helm provider
###
resource "helm_release" "metrics_server" {
  # Name of the release in the cluster
  name       = "metrics-server"

  # Name of the chart to install
  repository = "https://kubernetes-sigs.github.io/metrics-server/"

  # Version of the chart to use
  chart      = "metrics-server"

  # Wait for the Kubernetes Dashboard and Kubernetes namespace to be created
  depends_on = [helm_release.kubernetes_dashboard, kubernetes_namespace.kubernetes_dashboard]

  # Set the namespace to install the release into
  namespace  = kubernetes_namespace.kubernetes_dashboard.metadata[0].name

  # Recent updates to the Metrics Server do not work with self-signed certificates by default.
  # Since Docker For Desktop uses such certificates, you’ll need to allow insecure TLS
  set {
    name  = "args"
    value = "{--kubelet-insecure-tls=true}"
  }

  # Wait for the release to be deployed
  wait = true
}

# Output metadata of the Kubernetes Dashboard release
output "kubernetes_dashboard_service_metadata" {
  value = helm_release.kubernetes_dashboard.metadata
}

# Output metadata of the Metrics Server release
output "metrics_server_service_metadata" {
  value = helm_release.metrics_server.metadata
}

# Output the URL of the Kubernetes Dashboard
output "kubernetes_dashboard_url" {
  value = "http://localhost:9080"
}


Enter fullscreen mode Exit fullscreen mode

Step 5 - Initialize Terraform



terraform init


Enter fullscreen mode Exit fullscreen mode

Step 6 - Deploy the Kubernetes Dashboard and Metrics Server



terraform apply


Enter fullscreen mode Exit fullscreen mode

Step 7 - Access the Kubernetes Dashboard



# Open the Kubernetes Dashboard (please wait a few minutes for the Kubernetes Dashboard to start and the Metrics Server to collect data)
open https://localhost:9080


Enter fullscreen mode Exit fullscreen mode

Kubernetes Dashboard with Metrics Server


Optional Step 8 - Clean up



terraform destroy


Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
sloan profile image
Sloan the DEV Moderator

Heyo!

This is an awesome post! 🔥 In fact, the topic of your post would also work really well in The Ops Community too!

The Ops Community is a place for cloud engineers to share tips & tricks, tutorials, and career insights. Folks there commonly share information about DevOps and SecOps topics amongst other things.

Would you consider posting this article there too? Because The Ops Community is built on the same platform as DEV (Forem) you can fairly easily copy the Markdown and post your article there as well.

Really hope that you'll share your post with the community there and consider browsing the other Forem communities out there!