DEV Community

Harshil Patel
Harshil Patel

Posted on

Kubernetes on Windows: A Guide to Running Kubernetes Using Minikube and kubectl

There is a GitHub repository where I have added detailed notes on what Kubernetes is, along with notes on its components and architecture. You can check it out here:

GitHub logo hpatel292-seneca / Learning-Kubernetes

This repo contains basic notes about kubernetes like its Architecture, components and how to use kubernetes using kubectl

Learning-Kubernetes

What is Kubernetes?

  • Kubernetes is an open source Container Orchestration tool.

What is Container Orchestration??

Container Orchestration is a process of automatically managing and coordinating a large number containers. It manage task like

  • Starting and Stopping containers
  • Placing containers on right machine
  • Scaling containers up and down based on usage
  • Restart containers on failures

Kubernetes helps you manage containerized application in different environment like on-permise, Cloud, Hybrid setup. It ensures you application runs effectively, remain scalable, and are resilient to failures, without you having to manage containers manually.

Why Kubernetes was developed??

Kubernetes was developed by Google and later open-sourced to help manage the complex challenges of deploying and running large-scale containerized applications. Before Kubernetes, Google used its internal system called Borg, which inspired Kubernetes.

Need for Container Orchestration tool:

As Container adoption grew, so did the complexity for managing those containers. Here are few reasons why Kubernetes became…

Installing Minikube

To get started, you first need to install Minikube. Visit the official Minikube site: Minikube Start Documentation. Choose your operating system and follow the provided steps to install Minikube.

For Windows users, you can use PowerShell to install Minikube by running the following commands:

New-Item -Path 'c:\' -Name 'minikube' -ItemType Directory -Force
Invoke-WebRequest -OutFile 'c:\minikube\minikube.exe' -Uri 'https://github.com/kubernetes/minikube/releases/latest/download/minikube-windows-amd64.exe' -UseBasicParsing
Enter fullscreen mode Exit fullscreen mode

This will download and install Minikube to your system. Next, add Minikube to your system’s PATH environment variable by running the following command:

$oldPath = [Environment]::GetEnvironmentVariable('Path', [EnvironmentVariableTarget]::Machine)
if ($oldPath.Split(';') -notcontains 'C:\minikube') {
  [Environment]::SetEnvironmentVariable('Path', $('{0};C:\minikube' -f $oldPath), [EnvironmentVariableTarget]::Machine)
}
Enter fullscreen mode Exit fullscreen mode

This ensures that you can run Minikube commands from any terminal session.

Setting Up a Hypervisor

Minikube requires a hypervisor to run. Depending on your operating system, you can choose from several options. For a detailed list of supported drivers, refer to the Minikube Drivers Documentation.

If you already have Docker Desktop installed on your Windows system, you can use Docker as the Minikube driver. To start a Kubernetes cluster using the Docker driver, run the following command:

minikube start --driver=docker
Enter fullscreen mode Exit fullscreen mode

Minikube Start Example

This command will also install kubectl as a dependency. You can now use kubectl to interact with your Kubernetes cluster, specifically the API server.

Now as minikube is running let's check if it's actually running or not. To get all nodes in the cluster you can cun |

kubectl get nodes
Enter fullscreen mode Exit fullscreen mode

Image description

Basic Operations with kubectl

Now, as kubectl is installed and configured, now we can use it to interact with Kubernetes cluster.

1. Check the Status of Nodes

  • Command: kubectl get nodes
  • What It Does: Lists all nodes in the cluster with their status (e.g., Ready, NotReady).
  • Example Output:
  NAME       STATUS   ROLES    AGE   VERSION
  minikube   Ready    master   5m    v1.23.0
Enter fullscreen mode Exit fullscreen mode
  • Details: Shows information like node name, roles, and Kubernetes version. For Minikube, there is typically one master node.

2. List Pods

  • Command: kubectl get pods
  • What It Does: Displays all running pods in the default namespace.

3. List Services

  • Command: kubectl get services
  • What It Does: Lists all Kubernetes services in the default namespace.
  • Example Output:
  NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
  kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP   5m
Enter fullscreen mode Exit fullscreen mode
  • Details: Services provide networking between pods. The default service is Kubernetes' internal DNS service.

4. Create a Deployment

  • Command: kubectl create deployment <name> --image=<image-name>
  • What It Does: Creates a deployment, which manages the pods and ReplicaSets. In Kubernetes we cannot interact with pods directly, instead we can use deployment and deployment act as abstraction layer on pods.
  • Example:
  kubectl create deployment nginx-deployment --image=nginx
Enter fullscreen mode Exit fullscreen mode
  • Details:
    • Creates a deployment named nginx-deployment.
    • Downloads the nginx image from Docker Hub and creates a pod using it.
    • Verification: Run kubectl get deployments to confirm the deployment.

5. View Deployment

  • Command: kubectl get deployments
  • What It Does: Lists all deployments in the current namespace.
  • Example Output:
  NAME               READY   UP-TO-DATE   AVAILABLE   AGE
  nginx-deployment   1/1     1            1           5m
Enter fullscreen mode Exit fullscreen mode
  • Details: Displays the deployment's readiness and availability status.

6. View ReplicaSets

  • Command: kubectl get replicasets
  • What It Does: Lists all ReplicaSets associated with the deployments.
  • Example Output:
  NAME                      DESIRED   CURRENT   READY   AGE
  nginx-deployment-6fd8c8   1         1         1       5m
Enter fullscreen mode Exit fullscreen mode
  • Details: Shows the desired and actual number of pods for each ReplicaSet.

7. Edit a Deployment

  • Command: kubectl edit deployment <name>
  • What It Does: Opens the deployment's configuration file in your default text editor.
  • Example:
  kubectl edit deployment nginx-deployment
Enter fullscreen mode Exit fullscreen mode
  • Details:

    1. Opens the configuration file for the deployment.
    2. Edit fields like the container image. Example:
     spec:
       containers:
       - name: nginx
         image: nginx:1.16
    
  1. Save and close the editor.
  2. Kubernetes automatically terminates old pods and creates new ones with the updated configuration.
  3. Verification: Run kubectl get pods to see old pods terminating and new ones starting.

8. Check Logs

  • Command: kubectl logs <pod-name>
  • What It Does: Fetches logs from a specific pod.
  • Example:
  kubectl logs nginx-deployment-6fd8c8
Enter fullscreen mode Exit fullscreen mode
  • Details:
    • Logs provide output generated by the container application inside the pod.
    • If no logs are available, it means the application hasn't logged anything yet.

9. Describe a Pod

  • Command: kubectl describe pod <pod-name>
  • What It Does: Provides detailed information about a pod.
  • Example:
  kubectl describe pod nginx-deployment-6fd8c8
Enter fullscreen mode Exit fullscreen mode
  • Details:
    • Shows detailed events like image pulling, container creation, and issues (if any).
    • Useful for debugging pod startup problems.

10. Access Pod Shell

  • Command: kubectl exec -it <pod-name> -- <command>
  • What It Does: Opens an interactive terminal session in a container inside the pod.
  • Example:
  kubectl exec -it nginx-deployment-6fd8c8 -- /bin/bash
Enter fullscreen mode Exit fullscreen mode
  • Details:
    • You are now inside the container of the pod.
    • Use this to debug or test the container environment.
    • Exit the session by typing exit.

11. Delete a Deployment

  • Command: kubectl delete deployment <name>
  • What It Does: Deletes the deployment along with its associated pods and ReplicaSets.
  • Example:
  kubectl delete deployment nginx-deployment
Enter fullscreen mode Exit fullscreen mode
  • Details:
    • Automatically removes all pods and ReplicaSets under the deployment.
    • Verification: Run kubectl get pods and kubectl get replicasets to confirm deletion.

12. Apply Configuration File

  • Command: kubectl apply -f <file.yaml>
  • What It Does: Creates or updates resources defined in a configuration file.
  • Example:
  kubectl apply -f nginx-deployment.yaml
Enter fullscreen mode Exit fullscreen mode
  • Details:

    • The file can define multiple resources, like deployments, services, etc.
    • Example Configuration:
    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
            ports:
            - containerPort: 80
    
    • To update the deployment, modify the file (e.g., change replicas to 3) and reapply it:
    kubectl apply -f nginx-deployment.yaml
    

Top comments (0)