Kubernetes Complete Guide for Beginners
Advertisement
Kubernetes Complete Guide for Beginners
Kubernetes orchestrates containerized applications at scale. This guide introduces core concepts and hands-on operations.
Introduction
Kubernetes (K8s) automates deployment, scaling, and management of containerized applications. Understanding its architecture and core concepts is fundamental to modern DevOps.
- Kubernetes Complete Guide for Beginners
- Kubernetes Architecture
- Control Plane (Master)
- Worker Nodes
- Installation and Setup
- Kind (Kubernetes in Docker)
- Minikube
- kubectl Installation
- Core Kubernetes Objects
- Pods
- Deployments
- Services
- Basic Operations
- Namespaces
- Labels and Selectors
- Resource Limits
- Manifest Management
- Using kubectl apply
- Getting Resources
- Troubleshooting
- Check Status
- Debugging
- Next Steps
- FAQ
Kubernetes Architecture
Control Plane (Master)
The control plane manages the cluster:
- API Server: Central interface for cluster management
- Scheduler: Places pods on nodes
- Controller Manager: Runs controllers maintaining desired state
- etcd: Distributed key-value store with cluster state
Worker Nodes
Nodes run containerized applications:
- kubelet: Agent ensuring containers run in pods
- kube-proxy: Network proxy maintaining network rules
- Container Runtime: Docker, containerd, or cri-o
Installation and Setup
Kind (Kubernetes in Docker)
# Install Kind
wget https://kind.sigs.k8s.io/dl/v0.20.0/kind-linux-amd64
chmod +x kind-linux-amd64
sudo mv kind-linux-amd64 /usr/local/bin/kind
# Create cluster
kind create cluster --name mycluster
# Check cluster
kubectl cluster-info
kubectl get nodes
Minikube
# Install Minikube
curl -Lo minikube https://github.com/kubernetes/minikube/releases/latest/download/minikube-linux-amd64
chmod +x minikube
sudo mv minikube /usr/local/bin/
# Start cluster
minikube start
# Access dashboard
minikube dashboard
kubectl Installation
# Install kubectl
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/
# Verify
kubectl version --client
Core Kubernetes Objects
Pods
Smallest deployable units—wrappers around containers:
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
Deploy and interact:
# Create pod
kubectl apply -f pod.yaml
# View pods
kubectl get pods
# View pod details
kubectl describe pod nginx-pod
# View logs
kubectl logs nginx-pod
# Execute command in pod
kubectl exec -it nginx-pod -- /bin/bash
# Delete pod
kubectl delete pod nginx-pod
Deployments
Manage replicas and updates of pods:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.21
ports:
- containerPort: 80
Deploy and manage:
# Create deployment
kubectl apply -f deployment.yaml
# View deployments
kubectl get deployments
# View replica sets
kubectl get replicasets
# Scale deployment
kubectl scale deployment nginx-deployment --replicas=5
# Update image
kubectl set image deployment/nginx-deployment nginx=nginx:1.22
# Check rollout status
kubectl rollout status deployment/nginx-deployment
# Rollback
kubectl rollout undo deployment/nginx-deployment
Services
Expose pods to network:
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: LoadBalancer
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
Service types:
- ClusterIP: Internal access only
- NodePort: Access via node IP and port
- LoadBalancer: External load balancer
- ExternalName: Reference external service
# Create service
kubectl apply -f service.yaml
# View services
kubectl get svc
# Port forward for testing
kubectl port-forward service/nginx-service 8080:80
Basic Operations
Namespaces
Isolate workloads within cluster:
# Create namespace
kubectl create namespace production
# Deploy to namespace
kubectl apply -f deployment.yaml --namespace=production
# View resources in namespace
kubectl get pods --namespace=production
# Set default namespace
kubectl config set-context --current --namespace=production
Labels and Selectors
Organize resources:
apiVersion: v1
kind: Pod
metadata:
name: myapp
labels:
app: myapp
environment: production
tier: frontend
Select by labels:
# Select by single label
kubectl get pods -l app=myapp
# Select by multiple labels
kubectl get pods -l app=myapp,environment=production
# Complex selectors
kubectl get pods -l "tier in (frontend, backend)"
Resource Limits
Ensure fair resource allocation:
apiVersion: apps/v1
kind: Deployment
metadata:
name: resource-limited
spec:
replicas: 1
template:
spec:
containers:
- name: app
image: myapp:1.0
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 512Mi
Manifest Management
Using kubectl apply
# Apply manifest file
kubectl apply -f deployment.yaml
# Apply all files in directory
kubectl apply -f ./manifests/
# View what will be applied (dry run)
kubectl apply -f deployment.yaml --dry-run=client -o yaml
Getting Resources
# Get resource in YAML
kubectl get deployment nginx-deployment -o yaml
# Get resource in JSON
kubectl get deployment nginx-deployment -o json
# Get custom columns
kubectl get pods -o custom-columns=NAME:.metadata.name,CPU:.spec.containers[0].resources.limits.cpu
Troubleshooting
Check Status
# Pod status
kubectl describe pod myapp-pod
# Deployment status
kubectl describe deployment myapp
# Events
kubectl get events
# View logs
kubectl logs myapp-pod
# Previous logs (crashed pod)
kubectl logs myapp-pod --previous
Debugging
# Execute command
kubectl exec -it myapp-pod -- /bin/bash
# Port forward
kubectl port-forward pod/myapp-pod 3000:3000
# Copy files
kubectl cp myapp-pod:/var/log/app.log ./app.log
Next Steps
This guide covers Kubernetes basics. Next topics:
- Workload resources: StatefulSets, DaemonSets, Jobs
- Configuration: ConfigMaps, Secrets
- Networking: Services, Ingress, Network Policies
- Storage: Persistent Volumes, PersistentVolumeClaims
- Advanced: Helm, Operators, Custom Resources
FAQ
Q: What's the difference between a Pod and a Deployment? A: A Pod is a single instance. A Deployment manages multiple Pod replicas, handles updates, and ensures the desired number runs.
Q: Can I run a single pod without a Deployment? A: Yes, but Deployments are recommended for production. They provide self-healing, scaling, and rolling updates.
Q: How do I access my application running in Kubernetes? A: Create a Service. Use ClusterIP for internal access, LoadBalancer or NodePort for external access.
Advertisement