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
- Install a hypervisor like VirtualBox or Docker.
- 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
- Install Minikube via Homebrew:bashCopy code
brew install minikube
For Windows
- 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
- Install a hypervisor like VirtualBox or Docker.
- Download Minikube:bashCopy code
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 sudo install minikube-linux-amd64 /usr/local/bin/minikube
For macOS
- Install Minikube via Homebrew:bashCopy code
brew install minikube
For Windows
- Install Minikube using Chocolatey:bashCopy code
choco 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
- Start Minikube:bashCopy code
minikube start - Check cluster information:bashCopy code
kubectl cluster-info - Explore available nodes:bashCopy code
kubectl get nodes - Access the Kubernetes Dashboard:bashCopy code
minikube dashboard
Practical Example: Deploying Your First Application
- Create a Deployment: Deploy an Nginx server:bashCopy code
kubectl create deployment nginx-app --image=nginx - Check the Deployment:bashCopy code
kubectl get deployments kubectl get pods - Expose the Deployment: Make the application accessible:bashCopy code
kubectl expose deployment nginx-app --type=NodePort --port=80 - Access the Application:bashCopy code
minikube service nginx-app
Conclusion
By completing this chapter, you’ve learned:
- What Kubernetes is and why it’s important.
- Key features that make Kubernetes powerful.
- The architecture and components of a Kubernetes cluster.
- How to set up a local Kubernetes environment with Minikube and kubectl.
- How to deploy and expose your first application.