Introduction to Kubernetes

Understanding the fundamentals of container orchestration with Kubernetes

Beyond Single Containers

While Docker is excellent for running individual containers or simple multi-container applications on a single host, real-world applications often require more complex deployments across multiple servers. This is where container orchestration comes in, with Kubernetes being the leading solution.

The Orchestra Analogy

Think of container orchestration like conducting a symphony orchestra:

  • Individual musicians are like containers—each specializes in one instrument/task
  • Sections of instruments are like sets of identical containers (replicas) working together
  • The conductor is like Kubernetes—coordinating everything, responding to tempo changes, and maintaining harmony
  • The musical score is like your Kubernetes configuration files that define how everything should work together
  • The concert hall is like your cluster of servers where everything runs

Just as a conductor allows dozens of musicians to work together to create complex symphonies, Kubernetes enables hundreds or thousands of containers to work together to create robust, scalable applications.

Evolution of Deployment Models

flowchart TD subgraph "Traditional Deployment" A1[Physical Servers] --> B1[One App Per Server] B1 --> C1[Resource Waste] B1 --> D1[Difficult Scaling] B1 --> E1[Hardware Dependencies] end subgraph "Virtualized Deployment" A2[Physical Servers] --> B2[Hypervisor] B2 --> C2[Virtual Machine 1] B2 --> D2[Virtual Machine 2] B2 --> E2[Virtual Machine 3] C2 --> F2[App 1] D2 --> G2[App 2] E2 --> H2[App 3] end subgraph "Container Deployment" A3[Physical/Virtual Servers] --> B3[Container Runtime] B3 --> C3[Container 1] B3 --> D3[Container 2] B3 --> E3[Container 3] B3 --> F3[Container 4] B3 --> G3[Container 5] end subgraph "Kubernetes Orchestration" A4[Multiple Servers] --> B4[Kubernetes] B4 --> C4[Node 1] B4 --> D4[Node 2] B4 --> E4[Node 3] C4 --> F4[Pods] D4 --> G4[Pods] E4 --> H4[Pods] F4 --> I4[Containers] G4 --> J4[Containers] H4 --> K4[Containers] end

What is Kubernetes?

Kubernetes (often abbreviated as K8s) is an open-source platform designed to automate deploying, scaling, and operating application containers. It was originally developed by Google and is now maintained by the Cloud Native Computing Foundation (CNCF).

Core Kubernetes Capabilities Service Discovery Expose containers with their own IP addresses and DNS names Load balancing included Storage Automatically mount storage systems of your choice: local, cloud provider, or network Deployments Automated rollouts and rollbacks of application containers with zero downtime Bin Packing Automatically places containers based on resource requirements and constraints Self-healing Restarts failed containers Replaces and reschedules containers when nodes die Kills unhealthy containers Configuration Store and manage sensitive information Deploy and update configuration without rebuilding images

Why Use Kubernetes?

Kubernetes addresses several critical challenges in containerized applications:

When to Use Kubernetes

Kubernetes is particularly valuable when:

However, Kubernetes may be overkill for:

Kubernetes Architecture

flowchart TB subgraph "Control Plane Components" direction TB api[API Server] scheduler[Scheduler] etcd[etcd] cm[Controller Manager] ccm[Cloud Controller Manager] api --- etcd api --- scheduler api --- cm api --- ccm end subgraph "Worker Node Components" direction TB kubelet[Kubelet] proxy[Kube Proxy] cri[Container Runtime] kubelet --- cri kubelet --- proxy end api --- kubelet subgraph "Node 1" pod1[Pod 1] pod2[Pod 2] end subgraph "Node 2" pod3[Pod 3] pod4[Pod 4] pod5[Pod 5] end kubelet --- Node 1 kubelet --- Node 2

Control Plane Components

The Kubernetes control plane is the brain of the cluster, making global decisions and responding to cluster events:

Worker Node Components

Worker nodes are the machines where your applications run:

Basic Kubernetes Objects

Kubernetes uses various object types to represent the state of your system:

Higher-level Abstractions

Kubernetes also provides higher-level abstractions for managing applications:

Kubernetes vs. Docker

There's often confusion about how Kubernetes and Docker relate to each other. They're complementary technologies that serve different purposes:

Feature Docker Kubernetes
Primary Function Container runtime and tooling Container orchestration platform
Scale Single host (Docker) or limited multi-host (Swarm) Designed for large-scale, multi-host deployments
Deployment Units Containers Pods (groups of containers)
Service Discovery Basic DNS in Swarm Advanced, with internal DNS and load balancing
High Availability Limited in Swarm Built-in, with self-healing capabilities
Rolling Updates Basic support in Swarm Sophisticated, with rollback capabilities
Learning Curve Lower Steeper
Community & Ecosystem Large Very large, growing rapidly

Docker and Kubernetes Together

Docker and Kubernetes work together in a complementary way:

flowchart TD A[Developer Laptop] --> B[Dockerfile] B --> C[docker build] C --> D[Container Image] D --> E[Container Registry] E --> F[Kubernetes Cluster] F --> G[Deployment] G --> H[ReplicaSet] H --> I[Pod 1] H --> J[Pod 2] H --> K[Pod 3] I --> L[Container] J --> M[Container] K --> N[Container]

Docker Swarm vs. Kubernetes

Docker Swarm is Docker's native orchestration solution, which competes more directly with Kubernetes:

Key Kubernetes Concepts

Pods

Pods are the smallest deployable units in Kubernetes:

# Example Pod YAML
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.19
    ports:
    - containerPort: 80

Services

Services provide network access to a set of pods:

# Example Service YAML
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - port: 80
    targetPort: 80
  type: ClusterIP

Deployments

Deployments manage the lifecycle of pods:

# Example Deployment YAML
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.19
        ports:
        - containerPort: 80

Namespaces

Namespaces provide isolation and organization within a cluster:

# Example Namespace YAML
apiVersion: v1
kind: Namespace
metadata:
  name: development

ConfigMaps and Secrets

Store configuration and sensitive data:

# Example ConfigMap YAML
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  database_url: "postgres://postgres:5432/mydb"
  cache_ttl: "300"

# Example Secret YAML
apiVersion: v1
kind: Secret
metadata:
  name: app-secrets
type: Opaque
data:
  db_password: cGFzc3dvcmQxMjM=  # base64 encoded "password123"
  api_key: dGhpc2lzYXNlY3JldGtleQ==  # base64 encoded "thisisasecretkey"

Kubernetes Tools and Ecosystem

Command-line Tools

Local Development Tools

Managed Kubernetes Services

Kubernetes Distributions

Monitoring and Observability

Getting Started with Kubernetes

Setting Up a Local Kubernetes Environment

Minikube is one of the easiest ways to get started with Kubernetes locally:

# Install Minikube (macOS with Homebrew)
brew install minikube

# Start Minikube
minikube start

# Check status
minikube status

# Open Kubernetes dashboard
minikube dashboard

Install kubectl to interact with your cluster:

# Install kubectl (macOS with Homebrew)
brew install kubectl

# Check kubectl version
kubectl version

# View cluster information
kubectl cluster-info

# Get all resources in the cluster
kubectl get all

Basic kubectl Commands

# Get resources
kubectl get pods                      # List all pods
kubectl get services                  # List all services
kubectl get deployments               # List all deployments
kubectl get nodes                     # List all nodes

# Describe resources
kubectl describe pod nginx-pod        # Show details of a pod
kubectl describe service my-service   # Show details of a service

# Create resources
kubectl create -f my-resource.yaml    # Create resource from a file
kubectl apply -f my-resource.yaml     # Create or update resource

# Delete resources
kubectl delete pod nginx-pod         # Delete a pod
kubectl delete -f my-resource.yaml   # Delete resource from a file

# Interact with pods
kubectl logs nginx-pod               # View logs of a pod
kubectl exec -it nginx-pod -- /bin/bash  # Get a shell into a pod
kubectl port-forward nginx-pod 8080:80   # Forward local port to pod port

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

Your First Kubernetes Deployment

Create a file named nginx-deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.19
        ports:
        - containerPort: 80

Deploy it to your cluster:

kubectl apply -f nginx-deployment.yaml

Expose the deployment with a service:

kubectl expose deployment nginx-deployment --type=NodePort --port=80

Access the service in Minikube:

minikube service nginx-deployment

Real-world Kubernetes Use Cases

Microservices Architecture

Kubernetes excels at managing microservices applications:

Stateful Applications

Kubernetes can manage stateful applications using StatefulSets:

Batch Processing and Jobs

Kubernetes Jobs and CronJobs handle batch workloads:

Multi-tenant Environments

Kubernetes can isolate different teams or customers:

Challenges and Considerations

Kubernetes Complexity

Kubernetes has a significant learning curve:

Resource Requirements

Kubernetes has substantial resource needs:

Security Considerations

Securing Kubernetes requires attention to multiple areas:

When Kubernetes Might Not Be the Right Choice

Consider alternatives for:

Hands-on Exercises

Exercise 1: Setting Up Your First Kubernetes Cluster

Get started with a local Kubernetes environment:

  1. Install Minikube and kubectl on your machine
  2. Start a Minikube cluster and verify it's running
  3. Explore the Kubernetes dashboard
  4. Use basic kubectl commands to inspect your cluster
  5. Create a simple NGINX deployment using kubectl commands
  6. Expose the deployment with a service and access it

Exercise 2: Deploying a Web Application

Deploy a web application with a database:

  1. Create YAML files for a simple web application (e.g., a Node.js app)
  2. Create a deployment for the application
  3. Create a service to expose the application
  4. Deploy a database using a StatefulSet
  5. Configure the application to connect to the database
  6. Test the application and verify it works

Exercise 3: Scaling and Updating Applications

Practice scaling and updating a deployment:

  1. Deploy a simple application with multiple replicas
  2. Scale the deployment up and down
  3. Perform a rolling update to a new version
  4. Rollback to the previous version
  5. Configure resource limits and requests
  6. Monitor the deployment during scaling and updates

Summary and Next Steps

Kubernetes has become the industry standard for container orchestration due to its powerful capabilities for automating deployment, scaling, and operations of containerized applications. While it has a significant learning curve, the benefits for complex, scalable applications are substantial.

Key Takeaways

Learning Path

To continue your Kubernetes journey:

  1. Practice basics: Set up a local cluster and deploy simple applications
  2. Deepen understanding: Learn about more advanced concepts and object types
  3. Explore tools: Try Helm, Kustomize, and monitoring solutions
  4. Apply patterns: Implement microservices patterns in Kubernetes
  5. Consider certification: Certified Kubernetes Administrator (CKA) or Certified Kubernetes Application Developer (CKAD)

In the next lecture, we'll explore Docker Swarm, an alternative container orchestration solution that's built into Docker.

Additional Resources