Kubernetes, often abbreviated as K8s, is an open-source platform designed to automate deploying, scaling, and operating application containers. Originally developed by Google, Kubernetes is now maintained by the Cloud Native Computing Foundation (CNCF). It has become the de facto standard for container orchestration.
Key Concepts
- Cluster
A Kubernetes cluster is a set of nodes (machines) that run containerized applications. A cluster consists of at least one master node and multiple worker nodes.
- Node
A node is a single machine in the Kubernetes cluster, which can be either a physical machine or a virtual machine. There are two types of nodes:
- Master Node: Manages the Kubernetes cluster and coordinates all activities.
- Worker Node: Runs the containerized applications.
- Pod
A pod is the smallest and simplest Kubernetes object. It represents a single instance of a running process in your cluster. A pod can contain one or more containers.
- Service
A service in Kubernetes is an abstraction that defines a logical set of pods and a policy by which to access them. Services enable communication between different parts of an application.
- Deployment
A deployment provides declarative updates to applications. It describes the desired state of an application and the Kubernetes system ensures that the current state matches the desired state.
- Namespace
Namespaces are a way to divide cluster resources between multiple users. They provide a scope for names, allowing for resource isolation.
Kubernetes Architecture
Master Node Components
- API Server: Exposes the Kubernetes API. It is the front end of the Kubernetes control plane.
- etcd: A consistent and highly-available key-value store used as Kubernetes' backing store for all cluster data.
- Controller Manager: Runs controller processes that handle routine tasks in the cluster.
- Scheduler: Assigns workloads to specific nodes based on resource availability.
Worker Node Components
- Kubelet: An agent that runs on each node in the cluster. It ensures that containers are running in a pod.
- Kube-proxy: Maintains network rules on nodes. These rules allow network communication to your pods from network sessions inside or outside of your cluster.
- Container Runtime: The software that is responsible for running containers. Docker is a popular container runtime, but Kubernetes also supports others like containerd and CRI-O.
Practical Example: Deploying a Simple Application
Step 1: Create a Deployment
Create a file named nginx-deployment.yaml
with the following content:
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:1.14.2 ports: - containerPort: 80
Step 2: Apply the Deployment
Use the kubectl
command to apply the deployment:
Step 3: Verify the Deployment
Check the status of the deployment:
You should see an output similar to:
Step 4: Expose the Deployment as a Service
Create a service to expose the deployment:
Step 5: Verify the Service
Check the status of the service:
You should see an output similar to:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx-service LoadBalancer 10.0.0.1 <pending> 80:30685/TCP 1m
Exercises
Exercise 1: Create a Deployment
Create a deployment for a simple web application using the httpd
image. Ensure that the deployment has 2 replicas.
Solution:
Create a file named httpd-deployment.yaml
with the following content:
apiVersion: apps/v1 kind: Deployment metadata: name: httpd-deployment spec: replicas: 2 selector: matchLabels: app: httpd template: metadata: labels: app: httpd spec: containers: - name: httpd image: httpd:2.4 ports: - containerPort: 80
Apply the deployment:
Exercise 2: Expose the Deployment
Expose the httpd
deployment as a service of type NodePort
.
Solution:
Expose the deployment:
Summary
In this section, we introduced Kubernetes and its key concepts, including clusters, nodes, pods, services, deployments, and namespaces. We also covered the architecture of Kubernetes, detailing the components of both master and worker nodes. Finally, we provided a practical example of deploying a simple application and exposing it as a service, along with exercises to reinforce the learned concepts. In the next section, we will delve deeper into deploying Docker containers in Kubernetes.
Docker: From Beginner to Advanced
Module 1: Introduction to Docker
- What is Docker?
- Installing Docker
- Docker Architecture
- Basic Docker Commands
- Understanding Docker Images
- Creating Your First Docker Container
Module 2: Working with Docker Images
- Docker Hub and Repositories
- Building Docker Images
- Dockerfile Basics
- Managing Docker Images
- Tagging and Pushing Images
Module 3: Docker Containers
- Running Containers
- Container Lifecycle
- Managing Containers
- Networking in Docker
- Data Persistence with Volumes
Module 4: Docker Compose
- Introduction to Docker Compose
- Defining Services in Docker Compose
- Docker Compose Commands
- Multi-Container Applications
- Environment Variables in Docker Compose
Module 5: Advanced Docker Concepts
- Docker Networking Deep Dive
- Docker Storage Options
- Docker Security Best Practices
- Optimizing Docker Images
- Docker Logging and Monitoring
Module 6: Docker in Production
- CI/CD with Docker
- Orchestrating Containers with Docker Swarm
- Introduction to Kubernetes
- Deploying Docker Containers in Kubernetes
- Scaling and Load Balancing