Chapter 1: What is Kubernetes?

What is Kubernetes?

Kubernetes, also known as K8s, is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. Originally developed by Google, Kubernetes is now maintained by the Cloud Native Computing Foundation (CNCF). It is designed to handle large-scale systems, enabling applications to run consistently across different environments, including on-premises servers, public clouds, and hybrid setups.

Why Kubernetes is Important

Before Kubernetes, deploying applications required manual effort:

  • Setting up individual servers.
  • Configuring networking and load balancers.
  • Ensuring failover and availability.

Kubernetes eliminates these complexities by offering automation, scalability, and resilience for modern applications.

Key Terms

  • Containers: Lightweight, portable units (like Docker) that package applications with their dependencies.
  • Cluster: A group of machines (nodes) managed by Kubernetes.
  • Node: A single machine (physical or virtual) in a Kubernetes cluster.
  • Pod: The smallest deployable unit in Kubernetes, often containing one or more containers.

Key Features of Kubernetes

1. Automated Scaling

Kubernetes adjusts the number of application instances (Pods) based on traffic or workload demand.
Example: If your application experiences a sudden surge in traffic, Kubernetes can scale up the number of instances automatically.

kubectl autoscale deployment my-app --cpu-percent=50 --min=1 --max=10

2. Self-Healing

If a container crashes, Kubernetes restarts it automatically. If a node fails, Kubernetes reschedules the workload to other nodes.

kubectl get pods
kubectl describe pod <pod-name> # Check pod details if one is failing.

3. Load Balancing

Distributes incoming traffic evenly across all instances of an application.

kubectl expose deployment my-app --type=LoadBalancer --port=80

4. Declarative Configuration

You define the desired state of your application using YAML or JSON files, and Kubernetes ensures the actual state matches your desired state.

Example YAML file:

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

Apply this file with:

kubectl apply -f deployment.yaml

5. Service Discovery

Automatically assigns DNS names to Pods and ensures communication between services without needing manual configuration.

kubectl get services

Kubernetes Architecture Overview

Kubernetes operates on a cluster architecture. A cluster consists of Master Node(s) and Worker Nodes.

1. Master Node (Control Plane)

The master node is the brain of the Kubernetes cluster, responsible for maintaining the cluster’s desired state.

Components:

  • API Server: Serves as the front-end for Kubernetes. All kubectl commands communicate with this component.
  • etcd: A distributed key-value store that holds the entire configuration and state of the cluster.
  • Scheduler: Assigns workloads (Pods) to nodes based on resource availability.
  • Controller Manager: Monitors the state of the cluster and enforces desired states.

2. Worker Nodes

Worker nodes run the application workloads. Each worker node has:

  • kubelet: Ensures containers are running properly.
  • kube-proxy: Manages networking and forwards traffic to the correct Pod.
  • Container Runtime: Runs the containers (e.g., Docker, containerd).

Installing Minikube and kubectl

To start learning Kubernetes, we’ll set up a local Kubernetes environment using Minikube. This is ideal for beginners as it simulates a single-node Kubernetes cluster on your machine.

Step 1: Install Minikube

For Linux

  1. Install a hypervisor like VirtualBox or Docker.
  2. Download Minikube
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube

For macOS

  1. Install Minikube via Homebrew:bashCopy code
brew install minikube

For Windows

  1. Install Minikube using Chocolatey:
choco install minikube

Step 2: Install kubectl

kubectl is the command-line tool to interact with your Kubernetes cluster.

For Linux

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/

For macOS

brew install kubectl

For Windows

choco install kubernetes-cli

Step 3: Verify Installations

Check Minikube:

minikube version

Check kubectl:

kubectl version --client

Starting Your First Kubernetes Cluster

Start Minikube:

minikube start

Check cluster information:

kubectl cluster-info

Explore available nodes:

kubectl get nodes

Access the Kubernetes Dashboard:

minikube dashboard

Practical Example: Deploying Your First Application

Create a Deployment: Deploy an Nginx server:

kubectl create deployment nginx-app --image=nginx

Check the Deployment:

kubectl get deployments
kubectl get pods

Expose the Deployment: Make the application accessible:

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

Access the Application:

minikube service nginx-app

Chapter 1: What is Kubernetes?


What is Kubernetes?

Kubernetes, also known as K8s, is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. Originally developed by Google, Kubernetes is now maintained by the Cloud Native Computing Foundation (CNCF). It is designed to handle large-scale systems, enabling applications to run consistently across different environments, including on-premises servers, public clouds, and hybrid setups.

Why Kubernetes is Important

Before Kubernetes, deploying applications required manual effort:

  • Setting up individual servers.
  • Configuring networking and load balancers.
  • Ensuring failover and availability.

Kubernetes eliminates these complexities by offering automation, scalability, and resilience for modern applications.

Key Terms

  • Containers: Lightweight, portable units (like Docker) that package applications with their dependencies.
  • Cluster: A group of machines (nodes) managed by Kubernetes.
  • Node: A single machine (physical or virtual) in a Kubernetes cluster.
  • Pod: The smallest deployable unit in Kubernetes, often containing one or more containers.

Key Features of Kubernetes

1. Automated Scaling

Kubernetes adjusts the number of application instances (Pods) based on traffic or workload demand.
Example: If your application experiences a sudden surge in traffic, Kubernetes can scale up the number of instances automatically.

bashCopy codekubectl autoscale deployment my-app --cpu-percent=50 --min=1 --max=10

2. Self-Healing

If a container crashes, Kubernetes restarts it automatically. If a node fails, Kubernetes reschedules the workload to other nodes.

bashCopy codekubectl get pods
kubectl describe pod <pod-name> # Check pod details if one is failing.

3. Load Balancing

Distributes incoming traffic evenly across all instances of an application.

bashCopy codekubectl expose deployment my-app --type=LoadBalancer --port=80

4. Declarative Configuration

You define the desired state of your application using YAML or JSON files, and Kubernetes ensures the actual state matches your desired state.

Example YAML file:

yamlCopy codeapiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app-container
        image: nginx
        ports:
        - containerPort: 80

Apply this file with:

bashCopy codekubectl apply -f deployment.yaml

5. Service Discovery

Automatically assigns DNS names to Pods and ensures communication between services without needing manual configuration.

bashCopy codekubectl get services

Kubernetes Architecture Overview

Kubernetes operates on a cluster architecture. A cluster consists of Master Node(s) and Worker Nodes.

1. Master Node (Control Plane)

The master node is the brain of the Kubernetes cluster, responsible for maintaining the cluster’s desired state.

Components:

  • API Server: Serves as the front-end for Kubernetes. All kubectl commands communicate with this component.
  • etcd: A distributed key-value store that holds the entire configuration and state of the cluster.
  • Scheduler: Assigns workloads (Pods) to nodes based on resource availability.
  • Controller Manager: Monitors the state of the cluster and enforces desired states.

2. Worker Nodes

Worker nodes run the application workloads. Each worker node has:

  • kubelet: Ensures containers are running properly.
  • kube-proxy: Manages networking and forwards traffic to the correct Pod.
  • Container Runtime: Runs the containers (e.g., Docker, containerd).

Kubernetes Cluster Architecture Diagram

Include a simple architecture diagram to visually explain the relationships between components.


Installing Minikube and kubectl

To start learning Kubernetes, we’ll set up a local Kubernetes environment using Minikube. This is ideal for beginners as it simulates a single-node Kubernetes cluster on your machine.

Step 1: Install Minikube

For Linux

  1. Install a hypervisor like VirtualBox or Docker.
  2. Download Minikube:bashCopy codecurl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 sudo install minikube-linux-amd64 /usr/local/bin/minikube

For macOS

  1. Install Minikube via Homebrew:bashCopy codebrew install minikube

For Windows

  1. Install Minikube using Chocolatey:bashCopy codechoco install minikube

Step 2: Install kubectl

kubectl is the command-line tool to interact with your Kubernetes cluster.

For Linux

bashCopy codecurl -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/

For macOS

bashCopy codebrew install kubectl

For Windows

bashCopy codechoco install kubernetes-cli

Step 3: Verify Installations

Check Minikube:

bashCopy codeminikube version

Check kubectl:

bashCopy codekubectl version --client

Starting Your First Kubernetes Cluster

  1. Start Minikube:bashCopy codeminikube start
  2. Check cluster information:bashCopy codekubectl cluster-info
  3. Explore available nodes:bashCopy codekubectl get nodes
  4. Access the Kubernetes Dashboard:bashCopy codeminikube dashboard

Practical Example: Deploying Your First Application

  1. Create a Deployment: Deploy an Nginx server:bashCopy codekubectl create deployment nginx-app --image=nginx
  2. Check the Deployment:bashCopy codekubectl get deployments kubectl get pods
  3. Expose the Deployment: Make the application accessible:bashCopy codekubectl expose deployment nginx-app --type=NodePort --port=80
  4. Access the Application:bashCopy codeminikube service nginx-app

Conclusion

By completing this chapter, you’ve learned:

  1. What Kubernetes is and why it’s important.
  2. Key features that make Kubernetes powerful.
  3. The architecture and components of a Kubernetes cluster.
  4. How to set up a local Kubernetes environment with Minikube and kubectl.
  5. How to deploy and expose your first application.

Leave a Reply