Launching your First Kubernetes Cluster with Nginx running

Day 31 : #90DaysOfDevOps Challange

If you're looking to experiment with Kubernetes, you might be wondering how to set up a Kubernetes cluster on your local machine. Luckily, there's a tool called Minikube that allows you to quickly and easily set up a local Kubernetes cluster for development and testing purposes.

In this article, we will be covering two tasks related to Kubernetes and Minikube. The first task involves installing Minikube on your local machine, while the second task involves creating your first pod on Kubernetes through Minikube.

What is Minikube?

Minikube is a tool that allows you to run a single-node Kubernetes cluster on your local machine. It's designed for developers who want to experiment with Kubernetes without needing access to a full-scale production environment. Minikube is easy to set up and use, and it's a great way to get started with Kubernetes.

How Does Minikube Work?

Minikube uses a virtual machine to run the Kubernetes cluster on your local machine. It sets up a lightweight virtual machine using a tool like VirtualBox, and then installs Kubernetes inside that virtual machine. This allows you to run a Kubernetes cluster on your local machine without interfering with your host operating system.

Advantages of Using Minikube

Using Minikube has several advantages:

  • Ease of use: Minikube is easy to set up and use. You don't need a lot of experience with Kubernetes to get started.

  • Cost-effective: Minikube is free and can run on a lower-end machine, which means you can experiment with Kubernetes without needing to invest in expensive hardware.

  • Offline development: With Minikube, you can develop and test Kubernetes applications offline, which is especially useful if you're working on a plane or in a location without internet access.

  • Learning tool: Minikube is a great learning tool for developers who want to experiment with Kubernetes before moving to a production environment.

Pods:

In Kubernetes, a pod is the smallest and simplest unit in the deployment model. A pod is a logical host for one or more containers that are deployed together on the same node and share the same network namespace. Each pod has a unique IP address within the cluster and can communicate with other pods via a shared network.

A pod can contain one or more containers, which share the same network and file system. These containers can be tightly coupled and work together to provide a specific application service. For example, a pod can contain an application container and a sidecar container that performs logging or monitoring tasks.

Kubernetes manages pods as a unit, scheduling them to run on nodes in the cluster and ensuring that the desired number of replicas are always available. Pods are ephemeral and can be created, deleted, or replaced at any time by Kubernetes. Because of this, any data or state that needs to be persisted should be stored outside the pod, such as in a persistent volume.

Task 1: Install minikube on your local

here are the steps to install Minikube on an AWS EC2 instance running Ubuntu, using Docker as the driver:

  1. Launch an EC2 instance:

    • Go to the AWS Console and log in to your account.

    • Click on the "Launch Instance" button to start the instance creation wizard.

    • Choose an Ubuntu Server AMI and select the t2.medium instance type with 4GB RAM and 2 CPU.

    • Follow the wizard steps to configure the instance details, storage, security group, and key pair.

    • Review your configuration and launch the instance.

  2. Connect to your EC2 instance:

    • Once the instance is launched, you need to connect to it through SSH.

    • Open your terminal or command prompt and navigate to the directory where your key pair is saved, or else connect directly.

    • Use the following command to connect to your instance:

        ssh -i <key-pair>.pem ubuntu@<public-IP-address>
      
  3. Install Docker:

    • Before installing Minikube, you need to install Docker on your Ubuntu instance.

    • Run the following commands to update your Ubuntu instance and install Docker:

        sudo apt-get update
        sudo apt-get install docker.io
      
    • Start Docker using the following command:

        sudo systemctl start docker
      
    • Verify that Docker is running by using the following command:

        sudo systemctl status docker
      

  4. Install Minikube:

    • To install Minikube, use the following command:

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

  5. Start Minikube:

    • To start Minikube, use the following command:

        minikube start --driver=docker
      

    • This command will start a single-node Kubernetes cluster on your Ubuntu instance using Docker as the driver.

  6. Verify that Minikube is running:

    • Use the following command to verify that Minikube is running:

        kubectl version
      
    • This command will show the version of Kubernetes that Minikube is running.

πŸ₯³πŸ₯³πŸ₯³πŸ₯³πŸ₯³πŸ₯³πŸ₯³πŸ₯³πŸ₯³πŸŽ‰πŸŽ‰πŸŽ‰βœ¨βœ¨βœ¨βœ¨βœ¨βœ¨βœ¨βœ¨πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰Congratulations! You have successfully installed Minikube on your Ubuntu instance running on AWS EC2 and started a Kubernetes cluster using Docker as the driver. You can now start deploying your pods and services on the Kubernetes cluster.

Task 2. Create Niginx pod on Kubernetes through Minikube

  1. Create a deployment YAML file for Nginx using your preferred text editor. For example, create a file called nginx-deployment.yaml and paste the following content:

     apiVersion: apps/v1
     kind: Deployment
     metadata:
       name: nginx-deployment
       labels:
         app: nginx
     spec:
       replicas: 3
       selector:
         matchLabels:
           app: nginx
       template:
         metadata:
           labels:
             app: nginx
         spec:
           containers:
           - name: nginx
             image: nginx
             ports:
             - containerPort: 80
    

    This YAML file defines a deployment called nginx-deployment with three replicas, a selector, and a template that specifies an Nginx container with a single port exposed.

  2. Apply the deployment YAML file to the Kubernetes cluster using the following command:

     kubectl apply -f nginx-deployment.yaml
    

    This command creates a deployment object in the Kubernetes cluster that manages the creation and scaling of the Nginx pods.

  3. Verify that the deployment is running using the following command:

     kubectl get deployment
    

    This command shows the status of all the deployments running in the Kubernetes cluster, including the nginx-deployment.

  4. Verify that the pods are running using the following command:

     kubectl get pods
    

    This command shows the status of all the pods running in the Kubernetes cluster, including the Nginx pods created by the nginx-deployment.

  5. Verify that the Nginx service is running using the following command:

     kubectl get svc
    

    This command shows the status of all the services running in the Kubernetes cluster, including the Nginx service created by the nginx-deployment.

πŸ₯³πŸ₯³πŸ₯³πŸ₯³πŸ₯³πŸ₯³πŸ₯³πŸ₯³πŸ₯³πŸŽ‰πŸŽ‰πŸŽ‰βœ¨βœ¨βœ¨βœ¨βœ¨βœ¨βœ¨βœ¨πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰

Congratulations! You have successfully created an Nginx pod on Kubernetes through Minikube. You can now access the Nginx service from a web browser or a command-line tool using the IP address or hostname of your Ubuntu instance and the port number exposed by the Nginx container (usually port 80).

Did you find this article valuable?

Support Dhananjay Kulkarni by becoming a sponsor. Any amount is appreciated!

Β