k3d Documentation

repository·main·Indexed 27 days ago

https://github.com/k3d-io/k3d

k3d is a lightweight Kubernetes (k3s) distribution that runs in Docker containers, enabling the creation of multi-node k3s clusters on a single machine for development and testing. It provides a CLI for managing cluster lifecycles, including creating, starting, stopping, restarting, and deleting clusters, as well as configuring node counts, port mappings, volume mounts, and resource limits. k3d v5.x.x requires Docker v20.10.5 or higher.

Tokens
27.6K
Snippets
73
Records
202
Agent score
91%

What's inside k3d

  1. Understand k3d cluster anatomy

    main

    A k3d cluster is composed of several containerized components:

    1. Loadbalancer (Optional/Default)

      • Image: ghcr.io/k3d-io/k3d-proxy
      • Purpose: Proxies and load balances requests from the host to the cluster. By default, it proxies Kubernetes API traffic to port 6443 on server nodes. It supports multiple port-mappings that can be added or removed by recreating the proxy without affecting cluster state.
    2. Primary Server Node (Required)

      • Image: rancher/k3s
      • Purpose: The initializing server node. It runs the K3s executable (k3s server) which includes containerd, the Kubernetes API Server, and etcd/sqlite. In multi-server setups, it initializes the cluster with an embedded etcd database using the --cluster-init flag.
    3. Secondary Server Node(s) (Optional)

      • Image: rancher/k3s
      • Purpose: Additional server nodes for high availability.
    4. Agent Node(s) (Optional)

      • Image: rancher/k3s
      • Purpose: Runs the K3s agent process (e.g., kubelet) via k3s agent.
  2. Configure Podman network for DNS

    main

    The default podman network has DNS disabled. To allow k3d cluster nodes to communicate via DNS, create a new Podman network (e.g., named k3d) where DNS is enabled.

    podman network create k3d
    # Verify DNS is enabled
    podman network inspect k3d -f '{{ .DNSEnabled }}'
  3. Configure the Docker network for a k3d cluster

    main
    By default, k3d creates a new Docker network for every cluster it manages. To connect a cluster to an existing Docker network, use the --network flag during cluster creation. Note that k3d will not manage the lifecycle of existing networks; they will not be automatically deleted when the cluster is removed.
  4. Choose a k3d image importing mode

    main

    When importing images into a k3d cluster, you can choose between three modes depending on your environment and performance requirements:

    1. Auto: The default mode. k3d automatically selects between direct and tools-node. It selects tools-node for remote container runtimes to reduce network overhead, and direct for local runtimes.
    2. Direct: Loads images directly into the k3s nodes. This mode is efficient as it does not spawn a separate container or write intermediate files.
    3. Tools Node: Spawns a k3d-tools container within the container runtime, copies the images to that runtime, and then loads them into the k3s nodes from within that container.
  5. Basic k3d workflow: Create and use a cluster

    main

    Follow these steps to create a single-node k3s cluster, connect to it with kubectl, and then delete it.

    1. Create the cluster: k3d cluster create <CLUSTER_NAME>
    2. (Optional) Merge the kubeconfig to switch context automatically: k3d kubeconfig merge <CLUSTER_NAME> --kubeconfig-switch-context
    3. Use kubectl to interact with the cluster.
    4. Delete the cluster: k3d cluster delete <CLUSTER_NAME>
  6. Use a non-k3d-managed local registry

    main

    To use a custom Docker registry container (not managed by k3d):

    1. Run your registry container (e.g., registry:2).
    2. Add the registry to your registries.yaml configuration.
    3. Connect the registry container to the k3d network so the cluster nodes can reach it: docker network connect k3d-k3s-default <registry-container-name>.
    docker volume create local_registry
    docker container run -d --name registry.localhost -v local_registry:/var/lib/registry --restart always -p 12345:5000 registry:2
    
    # Connect to k3d network
    docker network connect k3d-k3s-default registry.localhost
  7. Expose services via NodePort

    main

    To expose services using a NodePort, you map a specific port from a specific agent node to your host.

    1. Create the cluster mapping a host port to a specific agent node. For example, mapping host port 8082 to port 30080 on agent-0: k3d cluster create mycluster -p "8082:30080@agent:0" --agents 2

    2. Create a NodePort service using a manifest that specifies the nodePort within the Kubernetes default range (30000-32767).

    3. Access the service via curl localhost:8082/.

    Warning: You can attempt to expose the entire NodePort range (e.g., -p "30000-32767:30000-32767@server:0"), but because Docker creates iptable entries and a proxy process per port-mapping, this may take a very long time or freeze your system.

  8. Pass additional arguments to k3s components

    main

    You can pass flags to specific Kubernetes components (like kube-apiserver, kube-scheduler, or kubelet) using the --k3s-arg flag.

    Important Syntax Rules:

    • The k3s flag itself must include dashes (e.g., --kube-apiserver-arg).
    • The internal Kubernetes flags (e.g., feature-gates) should not include dashes, as k3s adds them internally.
    • Use the @role:* suffix to target specific node roles (e.g., @server:* or @agent:*).
    k3d cluster create \
      --k3s-arg '--kube-apiserver-arg=feature-gates=EphemeralContainers=true@server:*' \
      --k3s-arg '--kube-scheduler-arg=feature-gates=EphemeralContainers=true@server:*' \
      --k3s-arg '--kubelet-arg=feature-gates=EphemeralContainers=true@agent:*'
  9. Create a pull-through registry proxy

    main

    You can create a pull-through registry to mirror a remote registry (like Docker Hub) and cache images locally. This speeds up image pulls for subsequent clusters.

    1. Create the registry container: Use k3d registry create with the --proxy-remote-url flag.
    2. Configure K3s mirrors: Create a registry.yml file to tell K3s to use your new registry as a mirror for specific domains.
    3. Create a cluster using the proxy: Use the --registry-use and --registry-config flags during cluster creation.
  10. Generate the k3d command tree using docgen

    main

    The docgen tool is used exclusively to generate the command tree documentation for the k3d usage commands. The output files are written to ../docs/usage/commands/.

    Note: You must execute the script from within the docgen directory because the relative path to the docs/ directory is hardcoded in the tool.

    # ensure that you're in the docgen dir, as the relative path to the docs/ dir is hardcoded
    cd docgen
    
    # run
    ./run.sh
  11. Requirements for running k3d

    main

    To use k3d, you must have the following installed:

    1. docker: k3d v5.x.x requires at least Docker v20.10.5 (runc >= v1.0.0-rc93).
    2. kubectl: Required to interact with the Kubernetes clusters created by k3d.