Common kind Commands Explained
About 595 words
kind (Kubernetes in Docker) runs a local Kubernetes cluster using Docker containers as "nodes". It is primarily used for local development, testing, and CI/CD environments.
Installing kind
# Install with Homebrew (macOS)
brew install kind
# Install with curl
curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.20.0/kind-linux-amd64
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind
# Install with Go
go install sigs.k8s.io/[email protected]Cluster Management Commands
Creating a Cluster
# Create the default cluster
kind create cluster
# Create a named cluster
kind create cluster --name my-cluster
# Create a cluster from a configuration file
kind create cluster --config kind-config.yaml
# Create a multi-node cluster
kind create cluster --config multi-node-config.yamlCreating a Cluster in One Command
# Create a simple cluster in one command
kind create cluster --name dev-cluster --config - <<EOF
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
- role: worker
EOFDeleting Clusters
# Delete the default cluster
kind delete cluster
# Delete a named cluster
kind delete cluster --name my-cluster
# Delete all clusters
kind delete clusters --allInspecting Cluster Information
# List all clusters
kind get clusters
# View detailed cluster configuration
kind get kubeconfig --name my-cluster
# View cluster nodes
docker ps --filter "label=io.x-k8s.kind.cluster"Image Management Commands
Loading Images into a Cluster
# Load a local image into the default cluster
kind load docker-image my-app:latest
# Load an image into a named cluster
kind load docker-image my-app:latest --name my-cluster
# Load images from a tar archive
kind load image-archive my-images.tar
# Load multiple images
kind load docker-image app1:latest app2:latest --name my-clusterBuilding Images
# Build an image and load it into the cluster
kind build node-image --image my-custom-node:latest
# Build an image from source
kind build node-image --source /path/to/kubernetes/sourceCommon Operations
Accessing and Switching Clusters
Method 1: Switch with Environment Variables
# Get the kubeconfig and set the environment variable
export KUBECONFIG=$(kind get kubeconfig --name my-cluster)
kubectl get nodes
# Switch to another cluster
export KUBECONFIG=$(kind get kubeconfig --name another-cluster)
kubectl get nodes
# Show the current cluster
kubectl config current-contextMethod 2: Use the --kubeconfig Flag
# Retrieve kubeconfig files
kind get kubeconfig --name my-cluster > my-cluster-kubeconfig
kind get kubeconfig --name dev-cluster > dev-cluster-kubeconfig
# Use a specific kubeconfig file
kubectl --kubeconfig my-cluster-kubeconfig get nodes
kubectl --kubeconfig dev-cluster-kubeconfig get nodes
# View resources in different clusters
kubectl --kubeconfig my-cluster-kubeconfig get pods --all-namespaces
kubectl --kubeconfig dev-cluster-kubeconfig get pods --all-namespacesMethod 3: Merge Multiple kubeconfig Files
# Retrieve the kubeconfig for each kind cluster
kind get kubeconfig --name my-cluster > ~/.kube/kind-my-cluster
kind get kubeconfig --name dev-cluster > ~/.kube/kind-dev-cluster
kind get kubeconfig --name test-cluster > ~/.kube/kind-test-cluster
# Merge into the default kubeconfig
KUBECONFIG=~/.kube/config:~/.kube/kind-my-cluster:~/.kube/kind-dev-cluster:~/.kube/kind-test-cluster kubectl config view --flatten > ~/.kube/config
# Show all available contexts
kubectl config get-contexts
# Switch to a specific cluster
kubectl config use-context kind-my-cluster
kubectl config use-context kind-dev-cluster
kubectl config use-context kind-test-clusterMethod 4: Use kubectx (Recommended)
# Install kubectx
# macOS
brew install kubectx
# Linux
sudo git clone https://github.com/ahmetb/kubectx /opt/kubectx
sudo ln -s /opt/kubectx/kubectx /usr/local/bin/kubectx
sudo ln -s /opt/kubectx/kubens /usr/local/bin/kubens
# Show all contexts
kubectx
# Quickly switch clusters
kubectx kind-my-cluster
kubectx kind-dev-cluster
kubectx kind-test-cluster
# Switch back to the previous cluster
kubectx -