Architecture in Kubernetes - Course K8s ...

Architecture in Kubernetes - Course K8s 02

Feb 12, 2025

Introduction to Kubernetes (K8s) Architecture for Beginners

Kubernetes, often abbreviated as K8s, is an open-source container orchestration platform designed to automate the deployment, scaling, and management of containerized applications. Understanding its architecture is crucial to effectively utilizing its features. This guide provides a beginner-friendly introduction to Kubernetes architecture, including a practical example to clarify its components.


Kubernetes Architecture Overview

Kubernetes follows a master-worker architecture, where the master node controls and manages the worker nodes that run the application workloads.

1. Master Node (Control Plane)

The master node is responsible for managing the cluster and making global decisions about scheduling and lifecycle management. It consists of several core components:

  • API Server: The entry point for all Kubernetes commands. It exposes RESTful APIs that interact with other components.

  • Controller Manager: Manages controllers that regulate the state of the cluster (e.g., handling failed nodes, scaling applications).

  • Scheduler: Assigns workloads (Pods) to worker nodes based on resource availability and constraints.

  • etcd: A distributed key-value store that stores all cluster data, including configurations and current states.

2. Worker Nodes

Worker nodes execute the application workloads as instructed by the control plane. Each node consists of the following components:

  • Kubelet: The agent that communicates with the API Server and ensures that containers are running as expected.

  • Container Runtime: Runs the containerized applications. Kubernetes supports Docker, containerd, and CRI-O.

  • Kube Proxy: Manages network communication between Pods and external services.

  • Pods: The smallest deployable unit in Kubernetes, which runs one or more containers.

3. Additional Key Concepts

  • Namespaces: Logical partitions within a cluster for organizing resources.

  • Services: Enable communication between different Pods, exposing applications internally or externally.

  • Ingress: Manages external access to services via HTTP and HTTPS routing.

  • Persistent Volumes (PV): Provide storage for applications beyond a Pod’s lifecycle.


Workflow in Kubernetes

  1. Developer writes application code and creates a container image using Docker or another containerization tool.

  2. Container image is pushed to a container registry like Docker Hub, Amazon Elastic Container Registry (ECR), or Google Container Registry (GCR).

  3. Deployment configuration is created using YAML manifests, defining how Pods, Services, and other components should be deployed.

  4. Kubernetes Control Plane schedules workloads to Worker Nodes based on resource availability.

  5. Kubelet ensures containers are running, and Kube Proxy manages networking between Pods.

  6. Services expose applications internally or externally, allowing users to interact with them.

  7. Scaling and self-healing mechanisms automatically adjust the cluster, ensuring high availability.


Example: Deploying a Simple Nginx Web Server in Kubernetes

Let’s go through a step-by-step process to deploy an Nginx web server using Kubernetes.

Step 1: Install Kubernetes (Minikube for Local Testing)

If you haven't installed Kubernetes yet, Minikube is a great way to start:

# Install Minikube
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube

# Start Minikube
minikube start

Step 2: Create a Deployment

A deployment ensures that a specified number of replicas of an application are running. Create a file named nginx-deployment.yaml with the following content:

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

Apply the deployment using:

kubectl apply -f nginx-deployment.yaml

Step 3: Expose the Deployment as a Service

A Kubernetes Service allows external traffic to reach the Nginx application. Create a file named nginx-service.yaml:

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

Apply the service using:

kubectl apply -f nginx-service.yaml

Step 4: Verify the Deployment

To ensure everything is running correctly, check the status of your pods and services:

kubectl get pods
kubectl get services

To access your Nginx application, run:

minikube service nginx-service

This command will open the deployed Nginx service in your default web browser.


Use Cases of Kubernetes

  1. Microservices Architecture: Deploy and manage microservices efficiently with automatic scaling and load balancing.

  2. CI/CD Pipelines: Automate application deployments using Kubernetes with Jenkins, GitHub Actions, or GitLab CI/CD.

  3. Hybrid and Multi-Cloud Deployments: Run applications across on-premise and cloud environments seamlessly.

  4. Batch Processing & Machine Learning: Train ML models and execute large-scale batch jobs with Kubernetes.

  5. Serverless Applications: Utilize Kubernetes-based serverless frameworks like Knative to run functions on demand.


Conclusion

Kubernetes simplifies the deployment and management of containerized applications by automating scaling, load balancing, and failover mechanisms. By following this example, you’ve successfully deployed a simple Nginx web server in Kubernetes.

If you're just starting, experiment with Kubernetes using Minikube or a managed Kubernetes service like Google Kubernetes Engine (GKE), Amazon Elastic Kubernetes Service (EKS), or Azure Kubernetes Service (AKS). Happy coding and happy learning!

Vous aimez cette publication ?

Achetez un café à Steve Bang

Plus de Steve Bang