Launching your First Kubernetes Cluster with Nginx running

Launching your First Kubernetes Cluster with Nginx running

What is minikube?

  • Minikube is a tool that quickly sets up a local Kubernetes cluster on macOS, Linux, and Windows. It can deploy as a VM, a container, or on bare metal.

  • Minikube is a pared-down version of Kubernetes that gives you all the benefits of Kubernetes with a lot less effort.

  • This makes it an interesting option for users who are new to containers, and also for projects in the world of edge computing and the Internet of Things.

Features of minikube

  1. Supports the latest Kubernetes release (+6 previous minor versions)

  2. Cross-platform (Linux, macOS, Windows)

  3. Deploy as a VM, a container, or on bare-metal

  4. Multiple container runtimes (CRI-O, containerd, docker)

  5. Direct API endpoint for blazing-fast image load and build

  6. Advanced features such as LoadBalancer, filesystem mounts, FeatureGates, and network policy

  7. Addons for easily installed Kubernetes applications

  8. Supports common CI environments.

Define 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.

Types of Installation of K8s

  1. Mini Kube (Docker Inside Docker DIND) → least use in Prod → Easiest

  2. Kubeadm :→ Baremetal (open-source tool) → Used in Prod → Intermediate

  3. Managed K8s Cluster

    AWS → EKS (Elastic Cloud Kubernetes)

    Azure → AKS (Azure Kubernetes Service)

    GCP → GKE (Google Kubernetes Engine)

  4. KIND (Kubernetes in Docker)

Task 1: Install minikube on your local

  1. Create a new VM instance having 2 CPUs, 4GB of free memory, 20 GB of free disk space.

    When creating a new EC2 instance select t2.medium.

  2. Install Docker in your system.

      sudo apt update -y
      sudo apt install docker.io -y
    
      sudo systemctl start docker
      sudo systemctl enable docker
      sudo systemctl status docker
    

    Add the user to the docker group

      sudo usermod -aG docker $USER && newgrp docker
    
  3. Install Minikube in system.

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

      sudo snap install kubectl --classic
    
  5. Start Minikube as per the image and check Minikube status

      minikube start --driver=docker
    

  6. Check if minikube cluster has been set up successfully or not by checking pods or namespace status.

      kubectl get pods
    
      kubectl get namespace
    

Task 2: Create your first pod on Kubernetes through minikube.

  1. To create a pod, we have to write a YAML file which is a.k.a Manifest file. So to create a pod for NGINX we have to pass the values & attributes in key-value format.

    In the manifest file, we are passing values:

    apiVersion → Kubernetes Version

    Kind → Type of deployment

    metadata → More Details about pod

    container → Details of containers in object

    containerPort → The port where the pod will deploy

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

  2. Run the kubectl command to create a pod.

      kubectl apply -f pod.yml
    

  3. Check the pod's status by kubectl get pods, you can see a NGINX pod is created successfully by it's status

      kubectl get pods
    

  4. Run the kubectl get pods -o wide command to get more detailed information about the pod-like IP, node, age of node, and status.

  5. To check if nginx is running locally or not, do we have to ssh the minikube go inside the minikube cluster. Then curl the IP address of the pod.

      #Get the IP
      kubectl get pods -o wide
    
      # SSH into minikube
      minikube ssh
    
      # Curl the IP address to access the NGINX
      curl http://<IP-Addr>
    

Task 3: Create NGINX pod on K8s through Kubeadm

  1. Create 2 VM instances for Master and Node.

  2. Install Docker on both Master & Node

      sudo apt update -y
      sudo apt install docker.io -y
    
      sudo systemctl start docker
      sudo systemctl enable docker
      sudo systemctl status docker
    
  3. Install Kebeadm on both master and node.

      sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
    
      echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
    
  4. Again update the system.

      sudo apt update -y
    
  5. Install Kubeadm,Kubectl and Kebelet in both Master and Node.

      sudo apt install kubeadm=1.20.0-00 kubectl=1.20.0-00 kubelet=1.20.0-00 -y
    
  6. Connect Master with Node:

    Initialized Kubeadm:

    Run the following command only on Master:

      sudo su
      kubeadm init
    

  7. Setup the kubeconfig for the current user

      mkdir -p $HOME/.kube
      sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
      sudo chown $(id -u):$(id -g) $HOME/.kube/config
    

  8. Finish the Master Setup using the following Command:

      kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml
    

  9. Now create a token to join the Master & Node connection

      kubeadm token create --print-join-command
    

  10. We will get nodes for master

  11. After that open port 6443 in master

  12. Then on the Worker Node reset the checks so it can't assign as Master.

    sudo su
    kubeadm reset pre-flight checks
    

  13. Paste the Join command on the worker node and append --v=5 at the end

  14. Verify by running the command in Master:

    kubectl gets nodes
    

    Kubeadm Installation Step

Create the Nginx Pod

  1. By default, the kubectl run command creates a deployment and a replica set along with the pod. If you only want to create a pod without creating a deployment or replica set, you can use the --restart=Never flag.

  2. But if you pass --restart=Always, if your pod is deleted or having an issue, then a new pod will be replaced immediately.

      kubectl run nginx --image=nginx --restart=Never
    

  3. Now we can see the docker container in the worker node

      docker ps
    

  4. To check if the pods are running or not

      kubectl get pods
    

  5. Get the details of the pod

      kubectl get pods -o wide
    

  6. To delete a pod in

      # kubectl delete pod  <pod-name>
      kubectl delete pod nginx
    

In this blog, we explored setting up a Kubernetes cluster using Minikube and creating a pod within it. If you have questions or want to share your experiences, please leave a comment below. Stay tuned for more blogs and connect with me on LinkedIn for further discussions.

I'm always open to feedback and suggestions for improving my content. You can find me on LinkedIn as Mudit Mathur. Feel free to reach out with your thoughts.

#Day31 #90daysofdevops

Did you find this article valuable?

Support Mudit Mathur by becoming a sponsor. Any amount is appreciated!