Day31-Launching your First Kubernetes Cluster with Nginx running

Siri ChandanaSiri Chandana
8 min read

Awesome! You learned the architecture of one of the most important tools "Kubernetes" in your previous task.

What is minikube?

Minikube is a tool in Kubernetes that allows developers to run a single-node Kubernetes cluster on their local machine. It is designed to simplify the process of setting up and running Kubernetes applications for development and testing purposes. With Minikube, developers can quickly create, deploy, and manage containers within their local environment, providing a seamless transition between development and production environments.

Minikube offers various features such as virtual machines, container networking, cluster management, and resource scaling, which enable developers to mimic the behavior of a full-scale Kubernetes cluster while working on a smaller scale.

This allows for faster and more efficient development cycles, as well as the ability to experiment and troubleshoot applications in a controlled environment. Overall, minikube is a valuable tool for developers looking to optimize their Kubernetes workflows and accelerate the development process.

Features of Minikube

Minikube offers several features that make it a popular choice for running Kubernetes locally. Here are some key features of Minikube:

  1. Single-node Kubernetes Cluster: Minikube allows you to run a single-node Kubernetes cluster on your local machine, providing a lightweight and easy-to-use environment for development and testing purposes.

  2. Cross-Platform Support: Minikube is designed to work on multiple operating systems, including Windows, macOS, and Linux. This cross-platform support makes it accessible to a wide range of developers.

  3. Hypervisor Support: Minikube supports multiple hypervisors, such as VirtualBox, KVM, and Hyper-V, allowing you to choose the one that best fits your local machine's capabilities.

  4. Easy Setup and Configuration: Minikube provides a simple setup process. Once installed, you can start a local Kubernetes cluster with a single command, minimizing the configuration overhead.

  5. Add-on Management: Minikube includes a set of optional add-ons that can be enabled to enhance the cluster's functionality. Add-ons such as the Kubernetes Dashboard, metrics server, and storage provisioner can be easily enabled or disabled as needed.

  6. Integration with kubectl: Minikube seamlessly integrates with kubectl, the primary command-line tool for interacting with Kubernetes clusters. This allows you to use familiar kubectl commands to manage your Minikube cluster.

  7. Networking and DNS: Minikube provides a built-in networking solution that allows your local services to be accessible from within the cluster. It also sets up a DNS server to enable service discovery between pods.

  8. Persistent Volume Support: Minikube supports the creation and management of persistent volumes, allowing you to simulate persistent storage for your applications running in the local cluster.

  9. Automatic Updates: Minikube can automatically update itself to the latest stable version, ensuring that you have access to the latest features, bug fixes, and security patches.

  10. Community and Documentation: Minikube has an active community and extensive documentation, making it easy to find support, troubleshoot issues, and learn more about using Minikube effectively.

Minikube Installation Guide for Ubuntu

This guide provides step-by-step instructions for installing Minikube on Ubuntu. Minikube allows you to run a single-node Kubernetes cluster locally for development and testing purposes.

Pre-requisites

  • 2 CPUs or more

  • 2GB of free memory

  • 20GB of free disk space

  • Internet connection

  • Container or virtual machine manager, such as Docker, QEMU, Hyperkit, Hyper-V, KVM, Parallels, Podman, VirtualBox, or VMware Fusion/Workstation

I'm using an EC2 instance to install and run Minikube.

The recommended EC2 instance type for Minikube is a t2.medium instance. This instance type has 2 vCPUs and 8 GB of memory, which is sufficient for running a Minikube cluster and a few workloads.

Task-01:

Install Minikube on your local.

To implement Kubernetes using Minikube on your local machine, follow these general steps:

  1. Install the necessary dependencies:

    • Install a hypervisor such as VirtualBox, or Docker. Minikube uses a virtual machine (VM) to run the Kubernetes cluster.

          # updating and installing docker
          sudo apt-get update 
          sudo apt-get upgrade
          sudo apt-get install docker.io -y
          # adding user to docker group
          sudo usermod -aG docker $USER
      

    • Install kubectl, the Kubernetes command-line tool, to interact with the cluster .

      
          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
           #kubectl version
          kubectl version
      

Install Minikube:

  • Update the indexing of your system using sudo apt update

  • Download and install the Minikube binary appropriate for your operating system from the official Minikube GitHub repository.

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

  • Start Minikube:

    • Open a terminal or command prompt and run the minikube start command. This starts a virtual machine and provisions a single-node Kubernetes cluster.

    •         minikube start
      

  • Kubectl is installed in your system and now check the configuration.

  • Interact with the cluster:

    • You can use kubectl commands to interact with your local Kubernetes cluster.

    • For example, you can create and manage deployments, services, and pods using kubectl create, kubectl apply, kubectl get, and other commands.

Remember that Minikube runs a single-node cluster, so it may not perfectly replicate the behavior of a multi-node production cluster. However, it provides a convenient way to get started with Kubernetes and experiment with its features locally.

What is a pod?

A Pod in Kubernetes is the smallest and simplest unit in the Kubernetes object model that you create or deploy. A Pod represents a running process on your cluster and can contain one or more containers. .The containers can even be Docker containers. The Pods in Kubernetes provide environmental dependencies which include persistent storage volumes which means it is permanent and is available to all pods in the said cluster and even configuration data that is required to run the container within the pod.

what does a pod do?

The creation of a pod is due to a workload resource called controller, which means rollout, replicate, and health of the pods present in a cluster. If we consider that a node in a cluster fails then a controller detects that the pod on the mode is unresponsive and then replicates a pod or pods on other nodes to carry out the same function.

The three most used controllers are Jobs, Deployments, and Stateful Sets.

  • Jobs are used for batch-type jobs that are mostly ephemeral and will run a task to completion.

  • Deployments are used for applications that are stateless and persistent, for example, web services.

  • StatefulSets is used for applications that are both stateful and Persistent like a database.

Here are some key points about Pods:

  1. Single-instance of an Application: A Pod encapsulates an application container (or, in some cases, multiple containers), storage resources, a unique network IP, and options that govern how the container(s) should run.

  2. Shared Storage/Networking: Each Pod is assigned a unique IP address. Every container in a Pod shares the network namespace, including the IP address and network ports. Pods can also share storage volumes.

  3. Pods and Containers: A Pod can host multiple containers and these containers are always co-located and co-scheduled, and run in a shared context. The containers in a Pod have the advantage of being able to easily communicate with each other and manage shared resources, which gives them a higher level of coupling than if they were separate entities.

  4. Pod Lifecycle: Pods are designed to support multiple co-located containers that need to share resources and lifecycle elements such as termination and scaling.

Remember, If a Pod gets deleted or dies for some reason, Kubernetes will automatically create a new replica of that Pod to maintain the desired state of the application.

Task-02:

Create your first pod on Kubernetes through Minikube.

Create a Pod: Create a file named pod.yaml (or any name you prefer) and open it in a text editor. Add the following content to define a simple Pod:


  apiVersion: v1
  kind: Pod
  metadata:
    name: nginx
  spec:
    containers:
    - name: nginx
      image: nginx:1.14.2
      ports:
      - containerPort: 80

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: Specifies the name of the container as "nginx".

  • image: 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: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.

    1. Apply the Pod manifest: Save the pod.yaml file and apply it to the Minikube cluster using the command kubectl apply -f pod.yaml. This instructs Kubernetes to create the Pod based on the manifest.

        kubectl apply -f pod.yml
       kubectl get pods
      

    2. Verify the Pod: Run the command kubectl get pods to verify that the Pod has been created and is running. You should see the status of the Pod as "Running".

      The Pod is now running the Nginx container. You can access the Nginx service running inside the Pod through port forwarding or by exposing it as a service.

Congratulations! You have created your first Pod on Kubernetes using Minikube.

0
Subscribe to my newsletter

Read articles from Siri Chandana directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Siri Chandana
Siri Chandana