Kubernetes_installation
Chapter 17: Kubernetes Installation - Setting Up Your Cluster
Section titled “Chapter 17: Kubernetes Installation - Setting Up Your Cluster”Table of Contents
Section titled “Table of Contents”- Introduction to Kubernetes Installation
- Installation Options
- Minikube
- Kind (Kubernetes in Docker)
- k3s - Lightweight Kubernetes
- Production Clusters
- kubectl Installation
- Verifying Your Installation
- Hands-on Lab
- Summary
Introduction to Kubernetes Installation
Section titled “Introduction to Kubernetes Installation”What We’ll Cover
Section titled “What We’ll Cover”Setting up Kubernetes can range from a simple single-node local cluster to a complex multi-node production environment. In this chapter, we’ll explore various installation methods and help you choose the right one for your needs.
┌─────────────────────────────────────────────────────────────────────────────┐│ KUBERNETES INSTALLATION OPTIONS │├─────────────────────────────────────────────────────────────────────────────┤│ ││ Local Development Production Clusters ││ ┌─────────────────┐ ┌─────────────────┐ ││ │ Minikube │ │ Managed K8s │ ││ │ - Single node │ │ - EKS │ ││ │ - VM-based │ │ - GKE │ ││ │ - Easy setup │ │ - AKS │ ││ └─────────────────┘ │ - DigitalOcean │ ││ └─────────────────┘ ││ ┌─────────────────┐ ┌─────────────────┐ ││ │ Kind │ │ Self-Managed │ ││ │ - Docker-based│ │ - kubeadm │ ││ │ - Fast │ │ - Kubespray │ ││ │ - No VM │ │ - RKE │ ││ └─────────────────┘ └─────────────────┘ ││ ││ ┌─────────────────┐ ││ │ k3s │ ││ │ - Lightweight │ ││ │ - Edge/IoT │ ││ │ - Quick start│ ││ └─────────────────┘ ││ │└─────────────────────────────────────────────────────────────────────────────┘Installation Options
Section titled “Installation Options”Comparison Table
Section titled “Comparison Table”┌─────────────────────────────────────────────────────────────────────────────┐│ INSTALLATION OPTIONS COMPARISON │├─────────────────────────────────────────────────────────────────────────────┤│ ││ Option │ Pros │ Cons │ Best For ││ ─────────────┼───────────────────┼─────────────────┼───────────────── ││ Minikube │ Full K8s features│ VM overhead │ Learning, dev ││ Kind │ No VM, fast │ Docker required │ CI/CD, testing ││ k3s │ Very lightweight │ Limited support │ Edge, IoT, dev ││ kubeadm │ Production-ready │ Manual setup │ Self-hosted prod ││ EKS/GKE/AKS │ Managed, scalable │ Cloud cost │ Production ││ ││ Requirements: ││ ──────────── ││ • Minikube: 2+ CPU, 2GB RAM, VirtualBox/Hypervisor ││ • Kind: Docker, 2GB RAM ││ • k3s: 512MB RAM (1GB recommended) ││ • kubeadm: 2+ nodes, 2GB RAM each ││ │└─────────────────────────────────────────────────────────────────────────────┘Minikube
Section titled “Minikube”What is Minikube?
Section titled “What is Minikube?”Minikube is a tool that lets you run Kubernetes locally. It creates a single-node cluster inside a VM, providing a full Kubernetes experience for learning and development.
Installation
Section titled “Installation”# Install Minikube on Linuxcurl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64sudo install minikube-linux-amd64 /usr/local/bin/minikube
# Install on macOSbrew install minikube
# Install on Windows# Download from: https://github.com/kubernetes/minikube/releasesStarting Minikube
Section titled “Starting Minikube”# Start with default settingsminikube start
# Specify VM driverminikube start --driver=virtualboxminikube start --driver=dockerminikube start --driver=hyperkit
# Specify Kubernetes versionminikube start --kubernetes-version=v1.28.0
# Allocate resourcesminikube start --cpus=4 --memory=8192
# Use existing Kubernetes clusterminikube start --driver=none
# Start with specific container runtimeminikube start --container-runtime=containerdMinikube Commands
Section titled “Minikube Commands”# Check statusminikube status
# Stop clusterminikube stop
# Delete clusterminikube delete
# SSH into nodeminikube ssh
# Get IP addressminikube ip
# Access dashboardminikube dashboard
# Addons managementminikube addons listminikube addons enable ingressminikube addons enable metrics-serverUsing Minikube with Docker Driver
Section titled “Using Minikube with Docker Driver”┌─────────────────────────────────────────────────────────────────────────────┐│ MINIKUBE WITH DOCKER DRIVER │├─────────────────────────────────────────────────────────────────────────────┤│ ││ Traditional (VM) Docker Driver ││ ┌─────────────────┐ ┌─────────────────┐ ││ │ Host │ │ Host │ ││ │ │ │ │ ││ │ ┌───────────┐ │ │ ┌───────────┐ │ ││ │ │ VM │ │ │ │ Docker │ │ ││ │ │ ┌───────┐ │ │ │ │ ┌───────┐ │ │ ││ │ │ │ K8s │ │ │ │ │ │ K8s │ │ │ ││ │ │ │ Node │ │ │ │ │ │ Node │ │ │ ││ │ │ └───────┘ │ │ │ │ └───────┘ │ │ ││ │ └───────────┘ │ │ └───────────┘ │ ││ └────────┬────────┘ └────────┬────────┘ ││ │ │ ││ ▼ ▼ ││ ┌─────────────────┐ ┌─────────────────┐ ││ │ Hypervisor │ │ Direct Docker │ ││ │ (VirtualBox, │ │ Integration │ ││ │ Hyper-V, etc)│ │ │ ││ └─────────────────┘ └─────────────────┘ ││ ││ Benefits: ││ • No VM required ││ • Faster startup ││ • Less resource overhead ││ • Native Docker CLI integration ││ │└─────────────────────────────────────────────────────────────────────────────┘Kind (Kubernetes in Docker)
Section titled “Kind (Kubernetes in Docker)”What is Kind?
Section titled “What is Kind?”Kind creates Kubernetes clusters using Docker containers as nodes. It’s designed for testing Kubernetes itself and is excellent for CI/CD pipelines.
Installation
Section titled “Installation”# Install Kind on Linuxcurl -Lo kind https://kind.sigs.k8s.io/dl/v0.20.0/kind-linux-amd64chmod +x kindsudo mv kind /usr/local/bin/
# Install on macOSbrew install kind
# Install on Windows# Using Chocolateychoco install kind# Or using Gogo install sigs.k8s.io/kind@v0.20.0Creating a Cluster
Section titled “Creating a Cluster”# Create a simple clusterkind create cluster
# Create with a specific namekind create cluster --name my-cluster
# Create with a specific Kubernetes versionkind create cluster --image kindest/node:v1.28.0
# Create multi-node clusterkind create cluster --config kind-config.yamlKind Configuration
Section titled “Kind Configuration”kind: ClusterapiVersion: kind.x-k8s.io/v1alpha4nodes: - role: control-plane extraPortMappings: - containerPort: 80 hostPort: 80 protocol: TCP - containerPort: 443 hostPort: 443 protocol: TCP - role: worker - role: worker# Create cluster with configkind create cluster --config kind-config.yamlKind Commands
Section titled “Kind Commands”# List clusterskind get clusters
# Delete clusterkind delete clusterkind delete cluster --name my-cluster
# Load image into clusterkind load docker-image myapp:latest
# Export logskind export logsk3s - Lightweight Kubernetes
Section titled “k3s - Lightweight Kubernetes”What is k3s?
Section titled “What is k3s?”k3s is a highly available, certified Kubernetes distribution designed for production workloads in resource-constrained environments. It’s a binary under 100MB that bundles everything needed.
Installation
Section titled “Installation”# Install k3s (single node)curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="server" sh -
# Check statuskubectl get nodessystemctl status k3s
# Get node token (for adding nodes)cat /var/lib/rancher/k3s/server/node-tokenk3s as Single Node
Section titled “k3s as Single Node”# Quick startcurl -sfL https://get.k3s.io | sh -
# With specific versioncurl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=v1.28.0 sh -
# Access kubectlkubectl get nodessudo kubectl get nodesk3s Server (Multi-Node)
Section titled “k3s Server (Multi-Node)”# First server nodecurl -sfL https://get.k3s.io | \ INSTALL_K3S_EXEC="server --cluster-init" \ K3S_TOKEN=<node-token> sh -
# Additional server nodescurl -sfL https://get.k3s.io | \ INSTALL_K3S_EXEC="server --server https://<server-ip>:6443" \ K3S_TOKEN=<node-token> sh -
# Worker nodescurl -sfL https://get.k3s.io | \ INSTALL_K3S_EXEC="agent" \ K3S_URL=https://<server-ip>:6443 \ K3S_TOKEN=<node-token> sh -k3s vs Standard Kubernetes
Section titled “k3s vs Standard Kubernetes”┌─────────────────────────────────────────────────────────────────────────────┐│ K3S vs STANDARD KUBERNETES │├─────────────────────────────────────────────────────────────────────────────┤│ ││ Feature │ Standard K8s │ k3s ││ ──────────────────┼────────────────────┼────────────── ││ Size │ ~600MB+ │ ~100MB ││ Installation │ Complex (kubeadm) │ Simple script ││ Dependencies │ Many │ Bundled ││ Database │ etcd (external) │ SQLite (embedded) ││ Memory Usage │ High │ Low (~500MB) ││ Supported addons│ All │ Most (traefik, servicelb) ││ ││ What's removed in k3s: ││ • Legacy auth mechanisms ││ • In-tree cloud providers (can be added) ││ • Some alpha features ││ ││ What's added in k3s: ││ • Built-in ingress controller (Traefik) ││ • Built-in service load balancer (Servicelb) ││ • Built-in local storage provider ││ • Simplified containerd setup ││ │└─────────────────────────────────────────────────────────────────────────────┘Production Clusters
Section titled “Production Clusters”Managed Kubernetes Services
Section titled “Managed Kubernetes Services”┌─────────────────────────────────────────────────────────────────────────────┐│ MANAGED KUBERNETES SERVICES │├─────────────────────────────────────────────────────────────────────────────┤│ ││ ┌────────────────┐ ┌────────────────┐ ┌────────────────┐ ││ │ EKS │ │ GKE │ │ AKS │ ││ │ (AWS) │ │ (GCP) │ │ (Azure) │ ││ │ │ │ │ │ │ ││ │ • Fargate │ │ • Autopilot │ │ • Virtual │ ││ │ • Self-managed│ │ • Standard │ │ Nodes │ ││ │ • EKS Anywhere│ │ • GKE Hub │ │ • Azure CNI │ ││ └────────────────┘ └────────────────┘ └────────────────┘ ││ ││ ┌────────────────┐ ┌────────────────┐ ┌────────────────┐ ││ │ DO Kubernetes│ │ Linode LKE │ │ OKE │ ││ │ (DigitalOcean)│ │ (Akamai) │ │ (Oracle) │ ││ └────────────────┘ └────────────────┘ └────────────────┘ ││ ││ Benefits of Managed Services: ││ • Automatic upgrades ││ • High availability ││ • Integrated load balancing ││ • Security and compliance ││ • Monitoring and logging ││ │└─────────────────────────────────────────────────────────────────────────────┘Creating EKS Cluster
Section titled “Creating EKS Cluster”# Install eksctlcurl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmpsudo mv /tmp/eksctl /usr/local/bin/
# Create clustereksctl create cluster \ --name my-cluster \ --region us-west-2 \ --nodegroup-name standard-workers \ --node-type t3.medium \ --nodes 3
# Create with config fileeksctl create cluster -f cluster-config.yamlapiVersion: eksctl.io/v1alpha5kind: ClusterConfig
metadata: name: my-cluster region: us-west-2
nodeGroups: - name: standard-workers instanceType: t3.medium desiredCapacity: 3 minSize: 2 maxSize: 5Self-Managed with kubeadm
Section titled “Self-Managed with kubeadm”# Install prerequisitessudo apt-get updatesudo apt-get install -y apt-transport-https ca-certificates curl
# Add Kubernetes repositorycurl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpgecho 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
# Install componentssudo apt-get updatesudo apt-get install -y kubelet kubeadm kubectlsudo apt-mark hold kubelet kubeadm kubectl
# Initialize mastersudo kubeadm init --pod-network-cidr=10.244.0.0/16
# Setup kubectlmkdir -p $HOME/.kubesudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/configsudo chown $(id -u):$(id -g) $HOME/.kube/config
# Install CNI (Calico example)kubectl apply -f https://docs.projectcalico.org/manifests/calico.yamlkubectl Installation
Section titled “kubectl Installation”Installing kubectl
Section titled “Installing kubectl”# Linuxcurl -LO "https://dl.k8s.io/release/v1.28.0/bin/linux/amd64/kubectl"chmod +x kubectlsudo mv kubectl /usr/local/bin/kubectl
# macOSbrew install kubectl
# Verify installationkubectl version --clientConfiguring kubectl
Section titled “Configuring kubectl”# Get cluster credentialsaws eks update-kubeconfig --name my-cluster
# Or manually configurekubectl config set-cluster my-cluster \ --server=https://<endpoint> \ --certificate-authority=/path/to/ca.crt
kubectl config set-credentials admin \ --token=<bearer-token>
kubectl config use-context my-clusterkubectl Autocompletion
Section titled “kubectl Autocompletion”# Bashsource <(kubectl completion bash)echo "source <(kubectl completion bash)" >> ~/.bashrc
# Zshsource <(kubectl completion zsh)echo "source <(kubectl completion zsh)" >> ~/.zshrcVerifying Your Installation
Section titled “Verifying Your Installation”Checking Cluster Health
Section titled “Checking Cluster Health”# Get cluster infokubectl cluster-info
# Get nodeskubectl get nodes
# Get component statuseskubectl get componentstatuses# or (deprecated in v1.19+)kubectl get --raw='/apis/healthz.kubernetes.io/v1/healthz'
# Describe nodekubectl describe node <node-name>
# Get all namespaceskubectl get nsRunning a Test Pod
Section titled “Running a Test Pod”# Run a test podkubectl run nginx --image=nginx --port=80
# Check pod statuskubectl get pods
# Get pod detailskubectl describe pod nginx
# Delete test podkubectl delete pod nginxCommon Issues and Solutions
Section titled “Common Issues and Solutions”┌─────────────────────────────────────────────────────────────────────────────┐│ COMMON INSTALLATION ISSUES │├─────────────────────────────────────────────────────────────────────────────┤│ ││ Issue: "The connection to the server localhost:8080 was refused" ││ Solution: ││ • Ensure kubectl is configured: cat ~/.kube/config ││ • For minikube: minikube start ││ • For kind: kind get kubeconfig ││ ││ Issue: "Node NotReady" ││ Solution: ││ • Check CNI installation: kubectl get pods -n kube-system ││ • Check kubelet logs: journalctl -u kubelet ││ ││ Issue: "ImagePullBackOff" ││ Solution: ││ • Check image name is correct ││ • For private registry: Create image pull secret ││ ││ Issue: "No nodes available to schedule pods" ││ Solution: ││ • Check node status: kubectl get nodes ││ • Check resources: kubectl describe node <node> ││ │└─────────────────────────────────────────────────────────────────────────────┘Hands-on Lab
Section titled “Hands-on Lab”Lab: Setting Up Local Kubernetes with Minikube
Section titled “Lab: Setting Up Local Kubernetes with Minikube”In this hands-on lab, we’ll set up a local Kubernetes cluster using Minikube.
Prerequisites
Section titled “Prerequisites”- Docker installed
- VirtualBox or another hypervisor
- 4GB RAM available
Lab Steps
Section titled “Lab Steps”# Step 1: Install Minikube# Linuxcurl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64sudo install minikube-linux-amd64 /usr/local/bin/minikube
# Step 2: Install kubectlcurl -LO "https://dl.k8s.io/release/v1.28.0/bin/linux/amd64/kubectl"chmod +x kubectlsudo mv kubectl /usr/local/bin/kubectl
# Step 3: Start Minikube with Docker driverminikube start --driver=docker --cpus=2 --memory=4096
# Step 4: Verify installationkubectl cluster-infokubectl get nodes
# Step 5: Enable addonsminikube addons enable ingressminikube addons enable metrics-serverminikube addons list
# Step 6: Deploy a test applicationkubectl create deployment nginx --image=nginxkubectl get pods -w
# Step 7: Expose the deploymentkubectl expose deployment nginx --port=80 --type=NodePort
# Step 8: Access the serviceminikube service nginx
# Step 9: Check dashboardminikube dashboard
# Step 10: Clean upminikube deleteSummary
Section titled “Summary”Key Takeaways
Section titled “Key Takeaways”- Choose the right tool - Minikube for dev, Kind for CI/CD, k3s for edge
- kubectl is essential - Install and configure it for all clusters
- Managed services - EKS/GKE/AKS for production
- Verify installation - Check nodes, pods, and component status
Quick Reference Commands
Section titled “Quick Reference Commands”# Minikubeminikube startminikube stopminikube delete
# Kindkind create clusterkind get clusterskind delete cluster
# k3scurl -sfL https://get.k3s.io | sh -kubectl get nodes
# kubectlkubectl cluster-infokubectl get nodeskubectl get pods -ANext Steps
Section titled “Next Steps”In the next chapter, we’ll explore Kubernetes ReplicaSets (Chapter 19), covering:
- ReplicaSet concepts
- Scaling applications
- Managing replicas