Getting Started with Kubernetes: A Beginner's Guide
Getting Started with Kubernetes: A Beginner's Guide
Kubernetes has become the de facto standard for container orchestration. If you're new to Kubernetes, this guide will help you understand the core concepts and deploy your first application.
What is Kubernetes?
Kubernetes (K8s) is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. Think of it as a conductor for your containers, ensuring they run smoothly across multiple machines.
Why Use Kubernetes?
- Automated Deployment: Deploy containers automatically across your cluster
- Scaling: Scale applications up or down based on demand
- Self-Healing: Automatically restart failed containers
- Load Balancing: Distribute traffic across multiple containers
- Rolling Updates: Update applications with zero downtime
Core Concepts
Pods
A Pod is the smallest deployable unit in Kubernetes. It contains one or more containers that share storage and network resources.
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
Deployments
Deployments manage the desired state of your application. They handle rolling updates and rollbacks.
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: nginx:latest
ports:
- containerPort: 80
Services
Services expose your Pods to network traffic, providing a stable endpoint even as Pods are created and destroyed.
apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
Setting Up Your First Cluster
Using Minikube (Local Development)
# Install Minikube
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
# Verify installation
kubectl cluster-info
kubectl get nodes
Using Amazon EKS (Production)
# Install eksctl
curl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp
sudo mv /tmp/eksctl /usr/local/bin
# Create cluster
eksctl create cluster \
--name my-cluster \
--region us-east-1 \
--nodegroup-name standard-workers \
--node-type t3.medium \
--nodes 3
Deploying Your First Application
Step 1: Create a Deployment
Save this as deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-app
spec:
replicas: 2
selector:
matchLabels:
app: hello
template:
metadata:
labels:
app: hello
spec:
containers:
- name: hello-app
image: gcr.io/google-samples/hello-app:1.0
ports:
- containerPort: 8080
Apply the deployment:
kubectl apply -f deployment.yaml
Step 2: Expose Your Application
kubectl expose deployment hello-app \
--type=LoadBalancer \
--port=80 \
--target-port=8080
Step 3: Verify Deployment
# Check pods
kubectl get pods
# Check service
kubectl get service hello-app
# View logs
kubectl logs <pod-name>
Essential kubectl Commands
# Get resources
kubectl get pods
kubectl get deployments
kubectl get services
# Describe resource
kubectl describe pod <pod-name>
# Execute command in pod
kubectl exec -it <pod-name> -- /bin/bash
# View logs
kubectl logs <pod-name>
kubectl logs -f <pod-name> # Follow logs
# Scale deployment
kubectl scale deployment hello-app --replicas=5
# Delete resources
kubectl delete pod <pod-name>
kubectl delete deployment hello-app
Best Practices for Beginners
- Resource Limits: Always set resource requests and limits
- Health Checks: Implement liveness and readiness probes
- Labels: Use labels for organizing and selecting resources
- Namespaces: Use namespaces to separate environments
- ConfigMaps: Store configuration separately from code
- Secrets: Never hardcode sensitive information
Common Pitfalls to Avoid
- Running containers as root
- Not setting resource limits
- Storing state in containers
- Ignoring security best practices
- Not monitoring your cluster
Next Steps
After mastering the basics:
- Learn about StatefulSets for stateful applications
- Explore Helm for package management
- Implement CI/CD with Kubernetes
- Study network policies and security
- Learn about observability with Prometheus and Grafana
Need Kubernetes Training?
I offer hands-on Kubernetes workshops for individuals and teams. Whether you're just starting or looking to level up your skills, I can help. Contact me to discuss customized training.
