Tag Archives: minikube

Chapter 2: Setting Up Your Kubernetes Cluster

In this chapter, we’ll cover the step-by-step process to set up Kubernetes using Minikube. You’ll learn how to install and configure Minikube, explore essential kubectl commands, and navigate the Kubernetes Dashboard. Each section includes detailed commands, live examples, and insights to simulate production-like environments.

1. Installing and Configuring Minikube

Minikube creates a single-node Kubernetes cluster on your local machine, making it perfect for learning and testing.

Step 1: Prerequisites

Ensure the following are installed:

  • Hypervisor: Examples: VirtualBox, Hyper-V, or Docker.
  • Command-Line Tools:
    • kubectl: For cluster interaction.
    • curl: For downloading utilities.

Verify your system requirements:

  • CPU: Minimum 2 cores.
  • Memory: At least 2GB.

Step 2: Install Minikube

For Linux

1. Download Minikube:

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

2. Verify Installation:

minikube version

For macOS

1. Install Minikube using Homebrew

brew install minikube

2. Verify Installation:

minikube version

For Windows

1. Install Minikube using Chocolatey:

choco install minikube

2. Verify Installation:

minikube version

Step 3: Start the Minikube Cluster

Start Minikube with default settings:

minikube start

Production-like Example: To simulate a high-availability environment:

minikube start --cpus=4 --memory=8192 --nodes=3

Output Example:

😄  minikube v1.30.1 on Linux
✨  Using the docker driver based on existing profile
🔥  Starting control plane node minikube in cluster minikube
🚀  Launching Kubernetes ...
🌟  Kubernetes 1.28.0 is now running on "minikube"

2. Exploring kubectl Commands

What is kubectl?

kubectl is the command-line tool to interact with Kubernetes clusters. It allows you to deploy applications, inspect cluster resources, and troubleshoot issues.

Common kubectl Commands

1. Verify the Cluster

Check the status of the cluster:

kubectl cluster-info

Example Output:

Kubernetes control plane is running at https://127.0.0.1:8443
CoreDNS is running at https://127.0.0.1:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

2. List Nodes

Display the nodes in the cluster:

kubectl get nodes

Example Output:

NAME       STATUS   ROLES                  AGE   VERSION
minikube   Ready    control-plane,master   1m    v1.28.0

3. Create a Namespace

Namespaces help isolate resources. Create a namespace for your application:

kubectl create namespace prod-env

4. Deploy a Sample Application

Deploy an Nginx container in the prod-env namespace:

kubectl create deployment nginx-app --image=nginx --namespace=prod-env

Verify the deployment:

kubectl get deployments -n prod-env
kubectl get pods -n prod-env

5. Expose the Deployment

Expose your application with a LoadBalancer Service:

kubectl expose deployment nginx-app --type=LoadBalancer --port=80 --namespace=prod-env

Check the service:

kubectl get services -n prod-env

6. Access Application Logs

Inspect application logs for troubleshooting:

kubectl logs <pod-name> -n prod-env

3. Understanding the Kubernetes Dashboard

The Kubernetes Dashboard provides a graphical interface for managing and monitoring your cluster.

Step 1: Enable the Dashboard

Enable the Kubernetes Dashboard in Minikube:

minikube dashboard

his command automatically opens the dashboard in your default web browser. Example URL: http://127.0.0.1:8001/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/

Step 2: Navigating the Dashboard

Main Sections:

  1. Overview: View all resources (Pods, Deployments, Services, etc.).
  2. Workloads: Monitor Deployments, ReplicaSets, and Pods.
  3. Services: Manage networking resources.
  4. Config and Storage: Handle ConfigMaps, Secrets, and Persistent Volumes.

Step 3: Access Dashboard Securely

To simulate a production-like secure environment:

1. Create a service account for dashboard access:

kubectl create serviceaccount dashboard-admin
kubectl create clusterrolebinding dashboard-admin-binding \
  --clusterrole=cluster-admin \
  --serviceaccount=default:dashboard-admin

2. Retrieve the access token:

kubectl get secrets
kubectl describe secret <secret-name>

3. Use the token to log in to the Dashboard.

Production-Like Example: Deploying an E-Commerce App

Step 1: Create a Namespace

kubectl create namespace ecommerce

Step 2: Deploy the App

Create a YAML file (ecommerce-app.yaml) for the deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ecommerce-backend
  namespace: ecommerce
spec:
  replicas: 3
  selector:
    matchLabels:
      app: ecommerce-backend
  template:
    metadata:
      labels:
        app: ecommerce-backend
    spec:
      containers:
      - name: backend
        image: node:14
        ports:
        - containerPort: 3000

Apply the YAML file:

kubectl apply -f ecommerce-app.yaml

Step 3: Expose the App

kubectl expose deployment ecommerce-backend --type=NodePort --port=3000 --namespace=ecommerce

Step 4: Monitor the App

Check Pods, Services, and Logs:

kubectl get pods -n ecommerce
kubectl get services -n ecommerce
kubectl logs <pod-name> -n ecommerce

Step 5: Access via Kubernetes Dashboard

Navigate to the ecommerce namespace in the Dashboard and monitor the app’s health.

Conclusion

By completing this chapter, you’ve:

  1. Installed and configured Minikube.
  2. Explored kubectl commands for cluster interaction.
  3. Navigated the Kubernetes Dashboard for better resource management.