Back to Blog

Kubernetes Basics: Complete Guide

⚙️ What is Kubernetes?

Kubernetes (K8s) is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications across clusters of hosts.

"Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services." - Kubernetes Documentation

🏗️ Core Concepts

Essential Components

  • Pods: Smallest deployable units containing one or more containers
  • Services: Network abstraction layer for accessing pods
  • Deployments: Manage pod replicas and rolling updates
  • ConfigMaps: Configuration data management
  • Secrets: Sensitive information storage

🏛️ Kubernetes Architecture

Control Plane Components

  • API Server: Central management entity and communication hub
  • etcd: Distributed key-value store for cluster data
  • Scheduler: Assigns pods to appropriate nodes
  • Controller Manager: Manages various controllers

💻 Essential kubectl Commands

Basic Operations

# Get cluster information
kubectl cluster-info

# List all nodes in cluster
kubectl get nodes

# List pods in current namespace
kubectl get pods

# List all services
kubectl get services

# Describe detailed pod information
kubectl describe pod 

# View pod logs
kubectl logs 

# Execute commands inside pod
kubectl exec -it  -- /bin/bash

🚀 Creating Your First Pod

Simple Pod YAML

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:latest
    ports:
    - containerPort: 80

📦 Deployments

Deployments provide declarative updates for pods and replica sets, ensuring desired state management:

Deployment YAML

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:latest
        ports:
        - containerPort: 80

🌐 Services

Services expose pods to network traffic and provide stable networking endpoints:

Service YAML

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  type: LoadBalancer

⚙️ Common kubectl Operations

Deployment Management

# Apply configuration from file
kubectl apply -f deployment.yaml

# Scale deployment replicas
kubectl scale deployment nginx-deployment --replicas=5

# Update container image
kubectl set image deployment/nginx-deployment nginx=nginx:1.21

# Rollback to previous version
kubectl rollout undo deployment/nginx-deployment

# Check deployment rollout status
kubectl rollout status deployment/nginx-deployment

🔧 ConfigMaps and Secrets

Configuration Management

ConfigMaps: Store non-sensitive configuration data

Secrets: Store sensitive information like passwords and API keys

ConfigMap Example

# Create ConfigMap from command line
kubectl create configmap app-config --from-literal=database_url=mongodb://localhost:27017

# Use ConfigMap in Pod
apiVersion: v1
kind: Pod
metadata:
  name: app-pod
spec:
  containers:
  - name: app
    image: myapp:latest
    env:
    - name: DATABASE_URL
      valueFrom:
        configMapKeyRef:
          name: app-config
          key: database_url

💡 Best Practices

  • Namespaces: Use namespaces for resource organization and isolation
  • Resource Management: Set resource limits and requests for containers
  • Health Checks: Implement liveness and readiness probes
  • Monitoring: Set up proper logging and monitoring solutions
  • Security: Use secrets for sensitive data and follow least privilege
  • Labels: Use consistent labeling for resource management

🎯 Conclusion

Kubernetes provides powerful container orchestration capabilities for modern applications. Start with basic concepts like pods and services, then gradually explore advanced features like deployments, ConfigMaps, and networking.

Next Steps: Practice with minikube or kind locally, then explore production-ready clusters with managed services like EKS, GKE, or AKS.