Computer Science

Getting Started with Kubernetes (K8s) – A Step-by-Step Guide

Kubernetes (K8s) is the leading container orchestration platform that automates the deployment, scaling, and management of containerized applications. If you’re new to Kubernetes, this guide will walk you through setting up a Kubernetes cluster, deploying applications, and managing workloads efficiently.


📌 1. Prerequisites

Before starting Kubernetes, ensure you have the following:

Basic Linux & Docker Knowledge (Containers, Images, Networking)
A Machine with at least 2 vCPUs & 4GB RAM (for local cluster)
Docker Installed (Required for Kubernetes container runtime)
kubectl (Kubernetes CLI) Installed
Minikube, Kind, or a Cloud Provider for Kubernetes Deployment


📌 2. Installing Kubernetes CLI (kubectl)

kubectl is the Kubernetes command-line tool used to manage clusters and workloads.

🔹 Install kubectl on Linux / macOS / Windows

# 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/

# macOS (Homebrew)
brew install kubectl

# Windows (Chocolatey)
choco install kubernetes-cli

Verify Installation

kubectl version --client

📌 3. Setting Up a Kubernetes Cluster

There are multiple ways to run Kubernetes:

EnvironmentToolBest For
Local MachineMinikube, KindLearning, Development
CloudAWS EKS, GCP GKE, Azure AKSProduction Deployment
On-PremiseK3s, kubeadmSelf-Managed Cluster

🔹 Option 1: Running Kubernetes Locally with Minikube

Minikube is the easiest way to create a local Kubernetes cluster.

Step 1: Install Minikube

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

# Windows (Powershell)
choco install minikube

Step 2: Start Minikube

minikube start --driver=docker

Step 3: Check Cluster Status

kubectl cluster-info
kubectl get nodes

Step 4: Enable Kubernetes Dashboard

minikube dashboard

This opens a GUI dashboard for monitoring cluster resources.


🔹 Option 2: Running Kubernetes on Cloud (AWS, GCP, Azure)

For production, use managed Kubernetes services like:

Cloud ProviderKubernetes ServiceCommand to Deploy
AWSAmazon EKSeksctl create cluster
Google CloudGoogle Kubernetes Engine (GKE)gcloud container clusters create
AzureAzure Kubernetes Service (AKS)az aks create

Example (AWS EKS):

eksctl create cluster --name my-cluster --region us-west-2

📌 4. Deploying Your First Application in Kubernetes

Now that your Kubernetes cluster is running, let’s deploy an application.

Step 1: Create a Deployment

A Deployment manages replica pods of your application.

# nginx-deployment.yaml
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:latest
        ports:
        - containerPort: 80

Apply the deployment:

kubectl apply -f nginx-deployment.yaml

Verify the deployment:

kubectl get deployments
kubectl get pods

Step 2: Expose the Application

To access the app from outside the cluster, create a Service.

# 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:

kubectl apply -f nginx-service.yaml

Get the service details:

kubectl get svc

If using Minikube, open the service:

minikube service nginx-service

📌 5. Scaling Applications in Kubernetes

Kubernetes makes it easy to scale applications.

Scale Pods Up or Down

kubectl scale deployment nginx-deployment --replicas=5

Enable Auto-Scaling

Automatically scale based on CPU usage:

kubectl autoscale deployment nginx-deployment --cpu-percent=50 --min=2 --max=10

📌 6. Managing Kubernetes Resources

CommandDescription
kubectl get nodesList all cluster nodes
kubectl get podsList all running pods
kubectl describe pod <pod-name>Detailed info about a pod
kubectl logs <pod-name>View logs of a running pod
kubectl delete pod <pod-name>Delete a specific pod
kubectl delete deployment nginx-deploymentRemove deployment

📌 7. Monitoring & Logging in Kubernetes

Enable Metrics Server for Monitoring

kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

View Real-Time Pod Metrics

kubectl top pod

Set Up Prometheus & Grafana for Advanced Monitoring

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm install prometheus prometheus-community/kube-prometheus-stack

📌 8. Kubernetes Networking & Ingress

Use an Ingress Controller

An Ingress Controller allows external access to services.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-ingress
spec:
  rules:
  - host: myapp.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx-service
            port:
              number: 80

Apply the Ingress:

kubectl apply -f nginx-ingress.yaml

📌 9. Cleaning Up

To remove resources:

kubectl delete -f nginx-deployment.yaml
kubectl delete -f nginx-service.yaml

To delete the Minikube cluster:

minikube delete

To remove AWS/GCP cluster:

eksctl delete cluster --name my-cluster

📌 10. Summary: Kubernetes Learning Path

Beginner:

  • Install Minikube or use Cloud Kubernetes (EKS/GKE/AKS)
  • Learn basic kubectl commands
  • Deploy simple applications with Pods, Deployments, Services

Intermediate:

  • Implement Scaling & Auto-scaling
  • Configure Storage (Persistent Volumes, PVCs)
  • Use ConfigMaps & Secrets for external configurations

Advanced:

  • Set up Ingress Controllers & Load Balancers
  • Implement Monitoring (Prometheus, Grafana)
  • Deploy real-world Microservices with Kubernetes

🚀 Kubernetes is the backbone of modern cloud-native applications! Mastering it will boost your DevOps & Cloud career. 🚀

Aquinas

Hello! I'm Aquinas, a lifelong learner who finds everything in the world fascinating. I can’t ignore my curiosity, and this blog is where I document my journey of learning, exploring, and understanding various topics. I don’t limit myself to a single field—I enjoy diving into science, philosophy, technology, the arts, and more. For me, learning isn’t just about gathering information; it’s about applying knowledge, analyzing it from different perspectives, and discovering new insights along the way. Through this blog, I hope to record my learning experiences, share ideas, and connect with others who have a similar passion for knowledge. Let’s embark on this journey of exploration together! 😊

Recent Posts

What Is EPS(Earnings Per Share)?

When analyzing a stock, one of the first financial indicators you’ll encounter is EPS, or Earnings Per Share. It’s one… Read More

8 months ago

What is Market Capitalization? Everything Investors Need to Know

When you look at a stock’s profile on a financial website, one of the first things you’ll see is its… Read More

8 months ago

The MIT License

In the world of open-source software, simplicity and flexibility are often just as important as legal protection. That’s why the… Read More

9 months ago

Mozilla Public License (MPL)

If you want your software to be open source, but still compatible with commercial use—and not as restrictive as the… Read More

9 months ago

The Apache License 2.0

When it comes to open-source software, developers and businesses alike need licenses that balance freedom, legal clarity, and long-term security.… Read More

9 months ago

BSD (Berkeley Software Distribution) License

If you’re working on open-source projects or choosing third-party libraries for your software, understanding software licenses is essential. Among the… Read More

9 months ago