Docker & Kubernetes Complete Guide Part 7: Kubernetes Installation and Cluster Configuration
From Local Development to Cloud-Managed Services
Introduction: Starting Kubernetes Environment Setup
The first step to learning and utilizing Kubernetes is setting up an appropriate environment. Various installation options exist depending on your purpose: development, testing, or production deployment. In this Part 7, we'll cover everything needed for Kubernetes environment setup, from local development environments to cloud-managed services and building clusters yourself.
1. Local Development Environment
Local development environments are ideal for learning Kubernetes or developing and testing applications. You can run a Kubernetes cluster on your computer without incurring cloud costs.
1.1 Minikube
Minikube is the most popular tool for running a single-node Kubernetes cluster locally. It supports various virtualization drivers and allows testing most Kubernetes features locally.
Key Features:
- Various driver support (Docker, VirtualBox, Hyper-V, KVM, etc.)
- Extension features through Addons (Ingress, Dashboard, Metrics Server, etc.)
- Multi-node cluster simulation support
- Various Kubernetes version selection
1.2 Kind (Kubernetes in Docker)
Kind uses Docker containers as nodes to run local Kubernetes clusters. It's particularly useful in CI/CD environments and makes multi-node cluster configuration easy.
Key Features:
- Only requires Docker to run
- Fast cluster creation and deletion
- Easy multi-node cluster configuration
- Suitable for CI/CD pipelines
1.3 Docker Desktop
Docker Desktop is an all-in-one solution for using Docker and Kubernetes together on Windows and macOS. Simply enable Kubernetes in settings to use it immediately.
Key Features:
- Easy installation and setup
- Integrated Docker and Kubernetes environment
- Easy management through GUI
- Automatic update support
2. Cloud Managed Services
In production environments, it's common to use cloud providers' managed Kubernetes services. These services significantly reduce operational burden as the cloud provider handles control plane management.
2.1 Amazon EKS (Elastic Kubernetes Service)
A managed Kubernetes service provided by AWS. It's closely integrated with various AWS services.
Key Features:
- Authentication/authorization integrated with AWS IAM
- Native integration with AWS services like ELB, EBS, VPC
- Serverless container execution through Fargate
- Leverages AWS global infrastructure
2.2 Google GKE (Google Kubernetes Engine)
A managed service provided by Google, the creator of Kubernetes. It's one of the most mature managed Kubernetes services.
Key Features:
- Fully managed operations with Autopilot mode
- Automatic upgrades and recovery
- Close integration with GCP services
- Multi-cloud support through Anthos
2.3 Azure AKS (Azure Kubernetes Service)
A managed Kubernetes service provided by Microsoft Azure. It perfectly integrates with the Azure ecosystem.
Key Features:
- Azure Active Directory integration
- Azure DevOps integration
- Monitoring through Azure Monitor
- Windows container support
3. Building Clusters with kubeadm
kubeadm is the official cluster bootstrapping tool provided by Kubernetes. It's used to build production-grade clusters on bare-metal servers or VMs.
3.1 Prerequisites
- Linux servers (Ubuntu, CentOS, etc.)
- Minimum 2GB RAM, 2 CPUs
- Network connectivity (communication possible between nodes)
- Swap disabled
- Container runtime (containerd, CRI-O, etc.)
3.2 Installation Steps
1. Install Container Runtime (containerd example):
# Install required packages
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl
# Install containerd
sudo apt-get install -y containerd
# Configure containerd
sudo mkdir -p /etc/containerd
containerd config default | sudo tee /etc/containerd/config.toml
sudo systemctl restart containerd
2. Install kubeadm, kubelet, kubectl:
# Add Kubernetes apt repository
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.29/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
# Install packages
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
3. Initialize Cluster (on master node):
# Initialize cluster
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
# Configure kubectl
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
4. Install CNI Network Plugin (Flannel example):
kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
5. Join Worker Nodes:
# Use the join command generated on the master node
sudo kubeadm join <master-ip>:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash>
4. Minikube Installation and Usage
Let's look at Minikube in detail as it's the most accessible option.
4.1 Installation
Windows (PowerShell):
# Using Chocolatey
choco install minikube
# Or direct download
New-Item -Path 'c:\' -Name 'minikube' -ItemType Directory -Force
Invoke-WebRequest -OutFile 'c:\minikube\minikube.exe' -Uri 'https://github.com/kubernetes/minikube/releases/latest/download/minikube-windows-amd64.exe'
$oldPath = [Environment]::GetEnvironmentVariable('Path', [EnvironmentVariableTarget]::Machine)
[Environment]::SetEnvironmentVariable('Path', "$oldPath;C:\minikube", [EnvironmentVariableTarget]::Machine)
macOS:
# Using Homebrew
brew install minikube
# Or direct download
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-amd64
sudo install minikube-darwin-amd64 /usr/local/bin/minikube
Linux:
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
4.2 Basic Usage
# Start cluster
minikube start
# Start with specific driver
minikube start --driver=docker
# Start with specific Kubernetes version
minikube start --kubernetes-version=v1.29.0
# Specify resources
minikube start --cpus=4 --memory=8192
# Check cluster status
minikube status
# Stop cluster
minikube stop
# Delete cluster
minikube delete
# Open Kubernetes dashboard
minikube dashboard
# Manage Addons
minikube addons list
minikube addons enable ingress
minikube addons enable metrics-server
5. kubectl Installation and Setup
kubectl is the command-line tool for managing Kubernetes clusters. It's used for almost all Kubernetes operations.
5.1 Installation
Windows:
# Using Chocolatey
choco install kubernetes-cli
# Or direct download
curl.exe -LO "https://dl.k8s.io/release/v1.29.0/bin/windows/amd64/kubectl.exe"
macOS:
# Using Homebrew
brew install kubectl
# Or direct download
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/amd64/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin/
Linux:
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin/
5.2 Auto-completion Setup
# Bash
echo 'source <(kubectl completion bash)' >> ~/.bashrc
source ~/.bashrc
# Zsh
echo 'source <(kubectl completion zsh)' >> ~/.zshrc
source ~/.zshrc
# Alias setup
echo 'alias k=kubectl' >> ~/.bashrc
echo 'complete -F __start_kubectl k' >> ~/.bashrc
6. Understanding kubeconfig File
The kubeconfig file contains the settings for kubectl to connect to clusters. The default location is ~/.kube/config.
6.1 kubeconfig Structure
apiVersion: v1
kind: Config
current-context: my-cluster
# Cluster information
clusters:
- name: my-cluster
cluster:
server: https://192.168.1.100:6443
certificate-authority-data: LS0tLS1...
# User authentication information
users:
- name: admin
user:
client-certificate-data: LS0tLS1...
client-key-data: LS0tLS1...
# Contexts (cluster + user + namespace combination)
contexts:
- name: my-cluster
context:
cluster: my-cluster
user: admin
namespace: default
6.2 Context Management
# Check current context
kubectl config current-context
# List available contexts
kubectl config get-contexts
# Switch context
kubectl config use-context my-other-cluster
# Create new context
kubectl config set-context my-context --cluster=my-cluster --user=admin --namespace=my-namespace
# Merge multiple kubeconfig files
export KUBECONFIG=~/.kube/config:~/.kube/config-cluster2
kubectl config view --merge --flatten > ~/.kube/merged-config
7. Checking Cluster Status
Let's explore various ways to verify that your cluster is working properly.
7.1 Basic Status Check Commands
# Cluster information
kubectl cluster-info
# Node list and status
kubectl get nodes
kubectl get nodes -o wide
# Detailed node information
kubectl describe node <node-name>
# Component status check
kubectl get componentstatuses
# Check pods in all namespaces
kubectl get pods --all-namespaces
# Check system pods
kubectl get pods -n kube-system
7.2 Checking Resource Usage
# Node resource usage (requires metrics-server)
kubectl top nodes
# Pod resource usage
kubectl top pods
# Pod resource usage in specific namespace
kubectl top pods -n my-namespace
8. Namespace Management
Namespaces are a way to logically separate resources within a cluster. You can isolate resources by team, environment (dev, staging, prod), or project.
8.1 Default Namespaces
default: Default namespace used when no namespace is specifiedkube-system: Namespace where Kubernetes system components runkube-public: For public resources readable by all userskube-node-lease: Namespace for node heartbeats
8.2 Namespace Management Commands
# List namespaces
kubectl get namespaces
kubectl get ns
# Create namespace
kubectl create namespace my-namespace
# Create with YAML
kubectl apply -f - <<EOF
apiVersion: v1
kind: Namespace
metadata:
name: my-namespace
labels:
environment: development
EOF
# Delete namespace (deletes all resources inside)
kubectl delete namespace my-namespace
# Work in specific namespace
kubectl get pods -n my-namespace
kubectl create deployment nginx --image=nginx -n my-namespace
# Change default namespace
kubectl config set-context --current --namespace=my-namespace
8.3 Namespace Resource Quotas
# resource-quota.yaml
apiVersion: v1
kind: ResourceQuota
metadata:
name: my-quota
namespace: my-namespace
spec:
hard:
requests.cpu: "4"
requests.memory: 8Gi
limits.cpu: "8"
limits.memory: 16Gi
pods: "20"
services: "10"
# Apply resource quota
kubectl apply -f resource-quota.yaml
# Check resource quota
kubectl describe resourcequota my-quota -n my-namespace
Conclusion
In this Part 7, we explored various methods for setting up Kubernetes environments. For local development, you can quickly get started using Minikube or Kind, while for production environments, you can build stable clusters using cloud-managed services or kubeadm.
By learning kubectl and kubeconfig usage, and understanding cluster status checking and namespace management, you can establish the foundation for Kubernetes operations. In Part 8, we'll take a detailed look at Kubernetes core resources: Pod, Deployment, and Service.