Setting up Minikube and Launching Your First Cluster with Nginx

Setting up Minikube and Launching Your First Cluster with Nginx

ยท

5 min read

๐ŸŒŸ Introduction

  • Minikube is a tool that allows you to run a local Kubernetes cluster on your computer. It is a lightweight and easy-to-use tool that is ideal for developers, beginners, and applications that require specialized deployment.

  • Minikube creates a single-node Kubernetes cluster on your computer using a virtual machine. This allows you to test and develop Kubernetes applications without having to set up a full-scale cluster.

  • Minikube is also useful for learning about Kubernetes. It provides a simple and interactive way to experiment with Kubernetes concepts and features.

  • Here are some of the benefits of using Minikube:

    • It is lightweight and easy to use.

    • It can be used on macOS, Linux, and Windows.

    • It allows you to test and develop Kubernetes applications without having to set up a full-scale cluster.

    • It is a great way to learn about Kubernetes.

๐Ÿ› ๏ธ Prerequisites

  • Make sure you have installed docker correctly and added the user to the docker group, and restart once after adding a user to the docker group.

  • If you are using an AWS instance use t2.medium having 2-CPU because Minikube requires 2 CPUs. As we have discussed in the previous blog, there are two servers Master and Worker both utilize 1 complete CPU so we use t2.medium.

๐Ÿš€ Installing Minikube

  • Let's get started with installing Minikube

  • Update the indexing of your system using sudo apt update

  • Let's start with installing Kubectl first, I hope you know what is kubectl refer this and for Kubectl installation

      curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
    
      # give execute permissions and move it to the local/bin
      chmod +x ./kubectl
      sudo mv ./kubectl /usr/local/bin/kubectl
    

  • Now, Install docker if you didn't, I mentioned it in the pre-requisites as well

      # updating and installing docker
      sudo apt-get update && sudo apt-get install docker.io -y
      # adding user to docker group
      sudo usermod -aG docker $USER
    
  • Now we'll install Conntrack, It is used by Kubernetes to keep track of the connections between pods and services. This is necessary because Kubernetes uses a stateful networking model, where each pod has its own IP address and port.

      sudo apt install conntrack -y
    

  • Here come's the Minikube installation, you can refer to this doc

      # this is there in the document,this installs minikube directly in local/bin
      curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
      sudo install minikube-linux-amd64 /usr/local/bin/minikube
      # this installs the minikube,gives execute permission and move it to local/bin
      curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 
      && chmod +x minikube && sudo mv minikube /usr/local/bin/
    

  • Now, we need a driver (VM or docker) to run the minikube, so we'll use docker as a driver to run the minikube

  • For this you have to give permission to docker, we have already given while installing docker but you can also use this:

      sudo usermod -aG docker $USER & newgrp docker
      # temporarily switches your shell session to the "docker" group, allowing you to use Docker without sudo for that session only. 
      # The effect is lost when you close the terminal or start a new terminal window.
      # Better to go with 
      sudo usermod -aG docker $USER
    
  • Let's start the minikube

    • Run minikube start --driver=docker

Let's understand the concept of pod

  • Pods are the smallest deployable units of computing that you can create and manage in Kubernetes.

  • A Pod (as in a pod of whales or pea pod) is a group of one or more containers, with shared storage and network resources, and a specification for how to run the containers. A Pod's contents are always co-located and co-scheduled, and run in a shared context. A Pod models an application-specific "logical host": it contains one or more application containers that are relatively tightly coupled.

  • You can refer to my previous blog for a more detailed explanation.

Let's Create our first pod on Kubernetes using Minikube

  • Create a file pod.yaml and paste the below code :

      apiVersion: v1
      kind: Pod
      metadata:
        name: nginx
      spec:
        containers:
        - name: nginx
          image: nginx:1.14.2
          ports:
          - containerPort: 80
    
    • You can refer to this for the code documentation

    • Let's understand and breakdown the above code :

      • apiVersion: v1: Specifies the Kubernetes API version being used. In this case, it's the core/v1 API version.

      • kind: Pod: Indicates that the YAML configuration is defining a Kubernetes Pod.

      • metadata: Contains metadata information about the Pod. In this case, the only metadata provided is the name of the Pod, which is set to "nginx".

      • spec: Describes the desired state of the Pod.

        • containers: Specifies an array of containers that should run within the Pod. In this case, there is a single container being defined.

          • name: nginx: Specifies the name of the container as "nginx".

          • image: nginx:1.14.2: Specifies the Docker image to be used for the container. The image "nginx:1.14.2" will be pulled from a container registry (like Docker Hub) and used to run the container. This is the official NGINX image version 1.14.2.

          • ports: Specifies an array of port mappings for the container. In this case, a single port mapping is defined:

            • containerPort: 80: Specifies that the container exposes port 80. This means that the container's application (NGINX in this case) is expected to be reachable via port 80 within the container.
    • Now to check if the pod is created or not, use kubectl get pods and

      kubectl get pods -o wide for a detailed view.

    • As you can see our pod is running on IP : 10.244.0.3

    • Let's check it, we have to inside the Kubernetes cluster, minikube makes this really easy, just type minikube ssh to get inside and then use curl to get the data from the IP

    • As you can see nginx is running inside a pod ...!!

    • Whooaa !!

I hope you can create your own pods now
We'll learn about deployment , auto healing and auto scaling in my upcoming blog

Stay Tuned !!

Happy Learning :D

ย