Day 2: Setting Up Your Cluster and kubectl
What You'll Learn Today
- How to set up a local Kubernetes cluster
- Basic kubectl operations
- Checking cluster status and node information
- YAML manifest structure
Kubernetes Environment Options
To learn Kubernetes, you first need a cluster. Here are the main options for local development.
| Tool | Features | Best For |
|---|---|---|
| Docker Desktop | Built into Docker Desktop. One-click activation | Docker users |
| Minikube | Lightweight local cluster with add-ons | Learning & development |
| kind | Runs clusters inside Docker. Multi-node support | CI/CD & testing |
Using Docker Desktop (Recommended)
If you already have Docker installed, this is the easiest option.
- Open Docker Desktop
- Go to Settings β Kubernetes
- Check Enable Kubernetes
- Click Apply & Restart
flowchart LR
A["Open Docker\nDesktop Settings"] --> B["Select\nKubernetes Tab"]
B --> C["Enable\nKubernetes"]
C --> D["Apply & Restart"]
D --> E["Cluster Ready"]
style A fill:#3b82f6,color:#fff
style E fill:#22c55e,color:#fff
Using Minikube
# Install (macOS)
brew install minikube
# Install (Linux)
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
# Start cluster
minikube start
# Stop cluster
minikube stop
# Delete cluster
minikube delete
Using kind
# Install (macOS)
brew install kind
# Install (Linux)
curl -Lo ./kind https://kind.sigs.k8s.io/dl/latest/kind-linux-amd64
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind
# Create cluster
kind create cluster
# Delete cluster
kind delete cluster
kubectl Basics
kubectl is the CLI tool for interacting with Kubernetes clusters β the equivalent of the docker command.
Verify Installation
# Check version
kubectl version --output=yaml
About kubeconfig
kubectl uses the ~/.kube/config file (kubeconfig) to manage cluster connections.
# ~/.kube/config structure (simplified)
apiVersion: v1
kind: Config
clusters:
- cluster:
server: https://127.0.0.1:6443
name: docker-desktop
contexts:
- context:
cluster: docker-desktop
user: docker-desktop
name: docker-desktop
current-context: docker-desktop
users:
- name: docker-desktop
user:
client-certificate-data: ...
| Element | Description |
|---|---|
| clusters | Cluster connection information |
| users | Authentication credentials |
| contexts | Cluster + user combinations |
| current-context | Currently active context |
Context Operations
# Check current context
kubectl config current-context
# List available contexts
kubectl config get-contexts
# Switch context
kubectl config use-context docker-desktop
Checking Cluster Status
Node Information
# List nodes
kubectl get nodes
# Example output:
# NAME STATUS ROLES AGE VERSION
# docker-desktop Ready control-plane 10d v1.31.4
# Detailed node info
kubectl describe node docker-desktop
Cluster Info
# Cluster information
kubectl cluster-info
# Component status
kubectl get componentstatuses
Namespaces
Kubernetes logically separates resources using Namespaces.
# List namespaces
kubectl get namespaces
# Example output:
# NAME STATUS AGE
# default Active 10d
# kube-system Active 10d
# kube-public Active 10d
# kube-node-lease Active 10d
| Namespace | Purpose |
|---|---|
| default | Default when none is specified |
| kube-system | Kubernetes system components |
| kube-public | Publicly accessible resources |
| kube-node-lease | Node health management |
Essential kubectl Commands
kubectl commands follow this general pattern:
kubectl [verb] [resource-type] [name] [options]
Common Verbs
| Command | Description | Docker Equivalent |
|---|---|---|
get |
List resources | docker ps |
describe |
Show resource details | docker inspect |
create |
Create a resource | docker run |
apply |
Apply a manifest (declarative) | - |
delete |
Delete a resource | docker rm |
logs |
Show logs | docker logs |
exec |
Execute a command in a container | docker exec |
Output Formats
# Default (table)
kubectl get nodes
# Wide output
kubectl get nodes -o wide
# YAML format
kubectl get nodes -o yaml
# JSON format
kubectl get nodes -o json
# Custom columns
kubectl get nodes -o custom-columns=NAME:.metadata.name,STATUS:.status.conditions[-1].type
YAML Manifest Basics
Kubernetes resources are defined using YAML manifests. Every manifest has four required fields.
apiVersion: v1 # API version
kind: Pod # Resource type
metadata: # Metadata
name: my-pod
labels:
app: my-app
spec: # Resource specification (desired state)
containers:
- name: my-container
image: nginx:1.27
| Field | Description |
|---|---|
| apiVersion | Kubernetes API version |
| kind | Resource type (Pod, Service, Deployment, etc.) |
| metadata | Name, labels, annotations |
| spec | Desired state definition |
Resources and API Versions
| Resource | apiVersion |
|---|---|
| Pod | v1 |
| Service | v1 |
| ConfigMap | v1 |
| Deployment | apps/v1 |
| ReplicaSet | apps/v1 |
| Ingress | networking.k8s.io/v1 |
Creating Your First Pod
Let's create our first Pod using a YAML file.
# my-first-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: my-nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.27
ports:
- containerPort: 80
Creating and Verifying the Pod
# Create the Pod
kubectl apply -f my-first-pod.yaml
# Check Pod status
kubectl get pods
# NAME READY STATUS RESTARTS AGE
# my-nginx 1/1 Running 0 30s
# Pod details
kubectl describe pod my-nginx
# View logs
kubectl logs my-nginx
# Execute command inside Pod
kubectl exec -it my-nginx -- /bin/bash
# Port forwarding (access from local machine)
kubectl port-forward my-nginx 8080:80
# Access http://localhost:8080 in your browser
# Delete the Pod
kubectl delete pod my-nginx
# or
kubectl delete -f my-first-pod.yaml
flowchart LR
A["Create YAML"] --> B["kubectl apply"]
B --> C["Pod Created"]
C --> D["kubectl get pods\nto verify"]
D --> E["kubectl port-forward\nto access"]
style A fill:#8b5cf6,color:#fff
style C fill:#3b82f6,color:#fff
style E fill:#22c55e,color:#fff
Useful kubectl Tips
Setting Aliases
# Add to ~/.bashrc or ~/.zshrc
alias k='kubectl'
alias kgp='kubectl get pods'
alias kgs='kubectl get services'
alias kgn='kubectl get nodes'
Enabling Command Completion
# Bash
echo 'source <(kubectl completion bash)' >> ~/.bashrc
# Zsh
echo 'source <(kubectl completion zsh)' >> ~/.zshrc
Dry Run
# Verify without creating
kubectl apply -f my-first-pod.yaml --dry-run=client
# Generate YAML
kubectl run test-pod --image=nginx --dry-run=client -o yaml
Summary
| Concept | Description |
|---|---|
| Docker Desktop | Easiest local Kubernetes environment |
| kubectl | CLI tool for Kubernetes cluster operations |
| kubeconfig | File managing cluster connections and credentials |
| Namespace | Logical resource isolation mechanism |
| YAML Manifest | File defining the desired state of resources |
| kubectl apply | Declaratively create/update resources |
Key Takeaways
- Docker Desktop enables Kubernetes with a single click
- kubectl is the gateway to all operations β learn the basic commands
- YAML manifests have four required fields: apiVersion, kind, metadata, spec
Practice Exercises
Exercise 1: Basics
Use kubectl to list cluster nodes and inspect their detailed information.
Exercise 2: Pod Creation
Write a YAML manifest for a Pod with these requirements:
- Name:
hello-app - Image:
httpd:2.4 - Label:
app: hello - Port: 80
Challenge
Use kubectl run with --dry-run=client -o yaml to auto-generate a YAML manifest. Compare it with a hand-written YAML.
References
Next up: In Day 3, you'll learn about "Understanding Pods." We'll dive deep into Pods β the smallest deployable unit in Kubernetes β and explore multi-container Pod patterns.