Generate k8s diagrams of your cluster using D2
README.md

k8s-d2

k8s-d2 #

Go Report Card License Release

A command-line tool that generates D2 diagram files from Kubernetes cluster topology. Visualize your cluster's namespaces, workloads, services, and their relationships as code.

Note: This README tracks the main branch. If you are using the latest stable release, check the matching Git tag or the release notes for the shipped CLI behavior and flags.

k8s-d2 Example Diagram

Features #

  • Generate D2 diagrams from live Kubernetes clusters
  • Visualize workloads (Deployments, StatefulSets, DaemonSets) with distinct icons
  • Map service-to-workload relationships with port information
  • Visualize ingress and external service entrypoints
  • Filter by namespace or view entire cluster
  • Track ConfigMaps and Secrets per namespace
  • Use D2 automatic layout for cleaner topology diagrams
  • Output to file or stdout for pipeline integration
  • Generate SVG images directly via Kroki API (no local D2 installation required)

Installation #

Homebrew (macOS/Linux) #

brew install vieitesss/tap/k8s-d2

Go Install #

go install github.com/vieitesss/k8s-d2@latest

Pre-compiled Binaries #

Download the latest release for your platform from the releases page, then extract the archive and move the k8sdd binary to your PATH:

tar -xzf k8s-d2_*_*.tar.gz
sudo mv k8sdd /usr/local/bin/  # Linux/macOS

For Windows, extract the .zip file and add the binary to your PATH.

Build from Source #

git clone https://github.com/vieitesss/k8s-d2.git
cd k8s-d2
go build -o k8sdd .

Quick Start #

Generate a diagram from your current Kubernetes context:

k8sdd diagram -o cluster.d2

Render the diagram with D2:

d2 cluster.d2 cluster.svg

Or generate an SVG directly (no D2 installation required):

k8sdd diagram -i cluster.svg

By default, SVG generation uses https://kroki.io with a 30s timeout.

Use a private or self-hosted Kroki deployment when needed:

k8sdd diagram -i cluster.svg --kroki-base-url https://kroki.internal --kroki-timeout 60s

Flags #

Flag Short Default Description
--kubeconfig ~/.kube/config Path to kubeconfig file
--namespace -n Filter by specific namespaces (repeat or comma-separated)
--all-namespaces -A false Include system namespaces
--output -o stdout Output D2 file path
--image -i Output SVG image file (uses Kroki API)
--kroki-base-url https://kroki.io Kroki base URL for SVG generation
--kroki-timeout 30s Kroki request timeout for SVG generation
--include-storage false Include PVCs with storage class labels
--quiet -q false Suppress progress indicators and log messages

Note: --output and --image are mutually exclusive.

--kroki-base-url and --kroki-timeout are only used with --image. When set, the Kroki base URL must not be empty and the timeout must be greater than 0.

--include-storage adds PVC nodes to the diagram and shows each PVC's storage class as label metadata when available. It does not render StorageClass objects as separate nodes.

Requirements #

  • Kubernetes cluster access via kubeconfig
  • Valid KUBECONFIG or ~/.kube/config file
  • D2 for rendering diagrams locally (optional - you can use --image flag to generate SVG via Kroki API instead)

Contributing #

Contributions are welcome! Please open an issue or submit a pull request.

Acknowledgments #

Built with:

  • Cobra - CLI framework
  • client-go - Kubernetes API client
  • D2 - Declarative diagramming language
  • Kroki - Diagram rendering API