Kubernetes (K8s) for Beginners - Course ...

Kubernetes (K8s) for Beginners - Course K8s 01

Feb 12, 2025

Introduction to Kubernetes (K8s) 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. Originally developed by Google, Kubernetes is now maintained by the Cloud Native Computing Foundation (CNCF). This guide provides a beginner-friendly introduction to Kubernetes along with a practical example to help you understand how it works.


Why Use Kubernetes?

Kubernetes offers several advantages that make it the go-to solution for container management:

  • Automated Scaling: Kubernetes scales applications up or down automatically based on demand.

  • Self-Healing: If a container crashes or fails, Kubernetes restarts it automatically.

  • Load Balancing: Kubernetes distributes incoming traffic to ensure application stability.

  • Efficient Resource Utilization: It optimizes computing resources across clusters.

  • Declarative Configuration: Define your application’s desired state using YAML or JSON.

  • Portability: Kubernetes runs across different environments, including cloud, on-premises, and hybrid infrastructures.


Core Concepts of Kubernetes

To effectively use Kubernetes, it's essential to understand its fundamental components:

  1. Pod: The smallest deployable unit in Kubernetes, containing one or more containers.

  2. Node: A worker machine (physical or virtual) that runs application workloads.

  3. Cluster: A group of nodes managed by Kubernetes.

  4. Deployment: A configuration that defines how many replicas of an application should run.

  5. Service: Provides networking and load balancing for applications, ensuring smooth communication.

  6. Namespace: A logical partition within a Kubernetes cluster to isolate resources.

  7. ConfigMap & Secret: Used to manage configuration data and sensitive information like API keys.


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.


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. As you continue exploring Kubernetes, you can dive into advanced topics such as ConfigMaps, Secrets, Persistent Volumes, and Helm charts.

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!

Enjoy this post?

Buy Steve Bang a coffee

More from Steve Bang