Contents

Kubernetes in Docker

KinD

This article provides a guide on how to install KinD and configure a multi-node K8S cluster.

Introduction

KinD stands for Kubernetes in Docker and as the name implies this tool helps in creating local Kubernetes clusters using docker containers as nodes.

Info
KinD can be installed by following the instructions in the  Quick Start Guide

On Linux:

curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.14.0/kind-linux-amd64
chmod +x ./kind
mv ./kind /some-dir-in-your-PATH/kind     # add this location to your path

Creating a cluster

The K8S cluster is built using prebuilt container images called as node image. The version of the node image determines the K8S version that gets installed. Looking at the release notes you can figure out the mapping between the Kind version and the node image (K8S Version) that it supports.

A K8S cluster can be created by executing the kind create cluster command

$ kind create cluster --help
Creates a local Kubernetes cluster using Docker container 'nodes'

Usage:
  kind create cluster [flags]

Flags:
      --config string       path to a kind config file
  -h, --help                help for cluster
      --image string        node docker image to use for booting the cluster
      --kubeconfig string   sets kubeconfig path instead of $KUBECONFIG or $HOME/.kube/config
      --name string         cluster name, overrides KIND_CLUSTER_NAME, config (default kind)
      --retain              retain nodes for debugging when cluster creation fails
      --wait duration       wait for control plane node to be ready (default 0s)

Global Flags:
      --loglevel string   DEPRECATED: see -v instead
  -q, --quiet             silence all stderr output
  -v, --verbosity int32   info log verbosity
  • --config flag can be used to provide a config file in which we can define the topology and configuration for the multi node cluster.
  • --name flag can be used to give a name for the K8S cluster. By default it is set to kind.
  • by default the kubeconfig is picked up from the $HOME/.kube/config path but you can specify your custom location using the --kubeconfig flag.

Creating a multi-node K8S cluster

You can create a multi-node cluster by creating a config file and using it while creating the cluster.

Define the below configurations in kind-config.yaml file and then create a K8S cluster using the command:

kind create cluster --config kind-config.yaml

A three node cluster having one master and two worker nodes can be defined as

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker

A cluster with control-plane HA can be defined as

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: control-plane
- role: control-plane
- role: worker
- role: worker
- role: worker

Docker port mapping can be achieved using extraPortMapping parameter

Tip
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  extraPortMappings:
  - containerPort: 80
    hostPort: 80
    listenAddress: "0.0.0.0" # Optional, defaults to "0.0.0.0"
    protocol: udp # Optional, defaults to tcp

This can be useful if using NodePort services or daemonsets exposing host ports.

A specific K8S version can be chosen by choosing the corresponding node image version

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  image: kindest/node:v1.16.4@sha256:b91a2c2317a000f3a783489dfb755064177dbc3a0b2f4147d50f04825d016f55
- role: worker
  image: kindest/node:v1.16.4@sha256:b91a2c2317a000f3a783489dfb755064177dbc3a0b2f4147d50f04825d016f55

Kind Commands

The below are some kind commands that can be used

List K8S clusters

  • kind get clusters

Switch between clusters

  • kubectl culster-info –context kind-cluster_name

Delete a cluster

  • kind delete cluster –name cluster_name

Export logs

$ kind export logs ./desination_dir

The structure of the logs looks like below:

.
├── docker-info.txt
└── kind-control-plane/
    ├── containers
    ├── docker.log
    ├── inspect.json
    ├── journal.log
    ├── kubelet.log
    ├── kubernetes-version.txt
    └── pods/