#90daysofdevops #31:Kubernetes

Launching your First Kubernetes Cluster with Nginx running

Let's read about minikube and implement k8s in our local machine

  1. What is minikube?

Ans:- Minikube is a tool which 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.

  1. Features of minikube

Ans :-

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

(b) Cross-platform (Linux, macOS, Windows)

(c) Deploy as a VM, a container, or on bare-metal

(d) Multiple container runtimes (CRI-O, containerd, docker)

(e) Direct API endpoint for blazing fast image load and build

(f) Advanced features such as LoadBalancer, filesystem mounts, FeatureGates, and network policy

(g) Addons for easily installed Kubernetes applications

(h) Supports common CI environments

  •     sudo apt update
    
  • Install the required dependencies:

    minikube needs a virtual machine to run in our case I install docker

        sudo apt-get install docker.io
        # add docker in user group
        sudo usermod -aG docker $USER
        #Reboot system
        sudo reboot
    
  • Download Minikube:

      LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
    

  • Install Minikube

        sudo install minikube-linux-amd64 /usr/local/bin/minikube
    

  • Verify the installation by running the following command:

        minikube version # This command should display the Minikube version if the installation was successful.
    

  • Start Minikube by running the following command:

        minikube start
    
    • pod.yml

      ```plaintext apiVersion: v1 kind: Pod metadata: name: nginx spec: containers:

      • name: nginx image: nginx:1.14.2 ports:
        • containerPort: 80

After creating this file , run below command:

kubectl apply -f


        ### **What is Kubeadm?**

        * **kubeadm** is a tool provided by the Kubernetes project to simplify the process of setting up a production-grade Kubernetes cluster. It automates many of the manual steps involved in creating a cluster, such as initializing the control plane, joining worker nodes, and configuring networking.

        * Here are some key features and functionalities of Kubeadm:

            1. **Cluster Initialization:** kubeadm helps initialize the control plane node of a Kubernetes cluster. It handles the generation of necessary certificates, API server configuration, and secure communication between cluster components.

            2. **Worker Node Join:** Once the control plane is initialized, kubeadm provides a command to join worker nodes to the cluster. This command retrieves the necessary configuration and tokens required for a worker node to communicate with the control plane.

            3. **Networking Configuration:** kubeadm can integrate with various network plugins to configure networking within the cluster. It supports popular options like Calico, Flannel, Weave, and more. By default, kubeadm uses the kube-proxy component for basic cluster networking.

            4. **Version Compatibility:** kubeadm ensures that the version of Kubernetes being installed is compatible with the control plane. It checks for required dependencies and provides necessary warnings or recommendations during the setup process.

            5. **Cluster Upgrades:** kubeadm supports cluster upgrades by providing commands to safely upgrade the control plane and worker nodes to a new version of Kubernetes. It follows specific upgrade procedures and checks compatibility to ensure a smooth transition.

            6. **Configuration File Generation:** kubeadm generates a Kubernetes configuration file (`kubeconfig`) that allows you to interact with the cluster using `kubectl`. This file contains the necessary credentials and cluster information for authentication and authorization.

        * kubeadm is a powerful tool for creating and managing Kubernetes clusters. However, it focuses mainly on the control plane and worker nodes' setup, leaving other cluster components (such as networking and storage) to be configured separately. It is commonly used by system administrators, cluster operators, and anyone responsible for deploying and managing Kubernetes clusters in production environments.


        ### **Install kubeadm.**

        1. we need at least 2 virtual machines and launch 2 instances. One is t2.medium and the other is t2.micro or t2.medium

            ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1684245881130/e60106fa-2c80-46e3-865d-f22b0065e0a2.png?auto=compress,format&format=webp align="left")

        2. To install the Kubernetes cluster in both nodes we need CRI( CONTAINER RUNING INTERFACE ) for that we need to install docker in our nodes (Instances). so Container can run our machines.

            Note:- when we are installing docker that time ContainerD will be installed automatically.

            **COPY**

            **COPY**

            **COPY**

            **COPY**

            **COPY**

            **COPY**

            **COPY**

            ```plaintext
             # run these command in both  Master & Worker 
             sudo apt update -y && sudo apt install docker.io -y
             # in the both node we have to update and install docker
             systemctl status docker
             # command to check docker install or not
             sudo systemctl start docker
             # and then
             sudo systemctl enable docker
             # sudo systemctl enable docker command After running this command, Docker will be set to start automatically whenever the system boots up.
  1. Add Kubernetes Repository: To install kubeadm, you need to add the Kubernetes repository to your package manager. Run the following commands to add the Kubernetes repository:

    COPY

    COPY

    COPY

    COPY

    COPY

    COPY

    COPY

      # Both Master & Worker 
    
      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
    
  2. After adding the Kubernetes repository, you can install kubeadm by running the following command:

    COPY

    COPY

    COPY

    COPY

    COPY

    COPY

    COPY

      sudo apt update -y
      sudo apt install kubeadm=1.20.0-00 kubectl=1.20.0-00 kubelet=1.20.0-00 -y
    

  3. Initialize the Cluster: Once kubeadm is installed, you can use it to initialize the control plane of your Kubernetes cluster.

    COPY

    COPY

    COPY

    COPY

    COPY

    COPY

    COPY

      #Run this command in Master node
    
      sudo su #This command allows you to switch to the root user
      kubeadm init # initializes the Kubernetes control plane on the current node. It sets up the necessary configuration and generates certificates for secure communication between cluster components. After running this command, you will receive instructions and a token to join worker nodes to the cluster.
    
      mkdir -p $HOME/.kube
      sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
      sudo chown $(id -u):$(id -g) $HOME/.kube/config
      #Creates the .kube directory in the user's home directory if it doesn't exist already. 
      #Copies the configuration file for the Kubernetes cluster (usually located at /etc/kubernetes/admin.conf) to the .kube/config file in the user's home directory.
      #Sets the ownership of the copied configuration file to the current user.
    
      kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml
      #applies the Weave CNI (Container Network Interface) configuration by downloading the YAML file from the specified URL and deploying it to the Kubernetes cluster. Weave is a popular networking solution for Kubernetes clusters.
    
      kubeadm token create --print-join-command
      # generates a token and provides the necessary command to join worker nodes to the Kubernetes cluster. It prints the kubeadm join command with the required parameters, which you can run on worker nodes to join them to the cluster.
    

    kubeadm token create --print-join-command--->this command generates a token eith ip & port

    Enable mention port on Worker instance Security Groups Inbound rule

  4. Now we need to setup our Worker node

      sudo su
      kubeadm reset pre-flight #checks -----> Paste the Join command on worker node 
      append --v=5 #at end
    

  5. Now finally our both nodes are ready, to verify.

      kubectl get nodes
    

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.

Create your first pod on Kubernetes

To create your first pod on Kubernetes, you'll need to define a Pod manifest file in YAML format and apply it using kubectl. Here's an example of creating a simple NGINX web server pod:

  1. Create a file named nginx-pod.yaml and open it for editing.

  2. Add the following content to the nginx-pod.yaml file:

        apiVersion: v1
        kind: Pod
        metadata:
          name: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.14.2
            ports:
            - containerPort: 80
        # After creating this file , run below command:
        # kubectl apply -f <yaml file name>

This manifest describes a pod with a single container using the NGINX image. The container listens on port 80.

  1. Save and close the file.

  2. Apply the pod manifest using kubectl by running the following command:

      kubectl apply -f nginx-pod.yaml
      #This command creates the pod described in the YAML file. If the pod creation is successful, you will see a confirmation message.
    

  3. Verify that the pod is running by executing the following command:

      kubectl get pods #This command lists all the pods in the default namespace. You should see your nginx-pod listed with a status of "Running."
    

  4. Also, verify the running Docker container on the Worker node.

  • or:

    Create your first pod on Kubernetes through minikube.

    Install kubectl using the command

      sudo snap install kubectl --classic
    

    No alt text provided for this image

    • create pod.yaml file for nginx.

No alt text provided for this image

To create a pod using pod.yaml file use below command:

    kubectl apply -f <pod.yaml>

No alt text provided for this image

To check list of pods:

    kubectl get pods

You can verify that an active nginx container has been created.

No alt text provided for this image