Deploying Application On Kubernetes Cluster

Deploying Application On Kubernetes Cluster

Overview:-

In this blog, we will create a Docker file, build an image, push it to Docker Hub and at the end we will deploy it on Kubernetes Cluster.

Structure:-

  1. Creating the Application Dockerfile.

  2. Building the Image.

  3. Pushing the Image to DockerHub.

  4. Creating Master and Worker nodes and connecting them.

  5. Writing the Deployment file and Service file for deploying the Application on Kubernetes Cluster.

  6. Running the Application on our Kubernetes cluster.

Creating Dockerfile, building the image and Pushing the Image to DockerHub:-

  • At first, we will pick my Project GitHunter.

  • This is my Project GitHub Repository link:

    GitHunter

  • In the Repository, I have already created the Dockerfile.

  • Now we have the Dockerfile ready we will build the image using the command:

      docker build -t shivamkumar28dec/githunter .
    

    Replace shivamkumar28dec with your DockerHub username and githunter with the image name you want.

  • This command will build a Docker image.

  • We will push the Image to DockerHub. So to do this we will first login to our docker hub account by the command:

      docker login
    

    Enter your account username and password.

  • After Successfully login, we will push the image by the command:

      docker push shivamkumar28dec/githunter
    

    Again replace your username and image name.

  • This will push to our docker hub account and to run the Docker Container we will run the command:

      docker run -p 80:80 shivamkumar28dec/githunter
    

    Again replace your username and image name.

Creating Kubernetes Cluster and installing Docker:-

  • At first, we will need 2 nodes(Master and Worker nodes) and connect both with our system.

  • I will use AWS EC2 to create Master and Worker nodes and connect them with our system using SSH.

  • Sign into your AWS Console and Search for EC2.

  • Click on EC2 and Click on Launch Instance which is in the Orange box.

  • Name it I will name it Kubernetes-Master-Node and from the Application and OS Images(Amazon Machine Image) select Ubuntu.

  • Coming down from the Instance type select t2.medium as Kubernetes Cluster requires some more Inputs.

  • Now in the Key pair(login) choose any Key. I have already one I will select it.

  • Now in the Network settings choose all three SSH, HTTPs and HTTP traffic.

  • From the right side write 2 in the Number of instances as we need Master and Worker nodes and then click on Launch instance.

  • Now our 2 instances will be launched with the same name so we have to rename one instance as Kubernetes-Worker-Node.

  • We will Connect both the Servers with our system using SSH. So open two different terminals in your system.

  • I will show one server process the same for the other one.

  • Check any one Instance and click on Connect from the top right side.

  • After clicking on Connect we will see a box like this:

  • Now in the SSH client section copy the line in the Example.

  • Open your terminal and navigate to the path where your key pair is present. In most cases, it's in the Downloads.

  • Run the Command:

      cd Downloads/
    
  • After Navigating to Downloads paste your Example line with sudo at the beginning. It should look like this:

  • This will connect your Server with the System Using SSH.

  • Do the same for another server.

Set up Kubernetes cluster using Kubeadm:-

  • We have both the Master and Worker nodes connected to our system using SSH.

  • Run these commands together in both the nodes:

    ```bash sudo apt update sudo apt-get install -y apt-transport-https ca-certificates curl sudo apt install docker.io -y

    sudo systemctl enable --now docker

    curl -fsSL "packages.cloud.google.com/apt/doc/apt-key.gpg" | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/kubernetes-archive-keyring.gpg

echo 'deb packages.cloud.google.com/apt kubernetes-xenial main' | sudo tee /etc/apt/sources.list.d/kubernetes.list

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


    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693236003365/d3d3487e-d0c1-4d25-8509-07bac216f5e4.png align="center")


<mark>We have to run these commands in both the nodes Master and Worker.</mark>

### Initializing Kubernetes Master node(Master Server):-

* Now coming in the Master node we will set some things.

* At first, run the command:

    ```bash
    sudo kubeadm init

  • Then to set local KubeConfig run the commands:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

  • To install CNI run the command:

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

  • We will generate a token for our worker node to get connected with the master node for this run the command:

      sudo kubeadm token create --print-join-command
    

    This command will give us a token for connection.

  • Make sure to Expose PORT 6443 in the Security group in the Master node.

Connecting Worker Node with Master node:

  • Now we have to connect the Worker node with the master node.

  • Af first runs this command in the Worker node:

      sudo kubeadm reset pre-flight checks
    

  • Now the token that we got before pasted it with --v=5 at the end. This will connect both the nodes and our Kubernetes Cluster will be completed.

  • For verification in the Master node run the command:

      kubectl get nodes
    

    This will show us two nodes.

Deploying the Application on the Cluster:-

  • This is the last stage where we will deploy the project on the cluster.

  • Create a directory named project and navigate to it. Use these commands:

      mkdir project 
      cd project
    

  • Clone the Project with all files continuing the Dockerfile also by the command:

      git clone https://github.com/Bharadwajshivam28/GitHunter.git
    

  • Navigate to the GitHunter directory by the command:

      cd GitHunter
    

  • Create a file named Deployment.yaml by the Command:

      vim Deployment.yaml
    

  • Now Paste these codes in the file:

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

    Replace your username and image again here in the image line.

  • Save and exit the file.

  • After saving and exiting the file run this Command:

      kubectl apply -f Deployment.yaml
    

    This will make the Deployment.yaml file ready.

  • Now we need another file named Service.yaml so run the command:

      vim Service.yaml
    

  • Now paste these codes into this file:

      apiVersion: v1
      kind: Service
      metadata:
        name: githunter-service
      spec:
        selector:
          app: githunter
        ports:
          - protocol: TCP
            port: 80
            targetPort: 80
            nodePort: 31111 
        type: NodePort
    

  • Save and exit this file.

  • Run the Command:

      kubectl apply -f Service.yaml
    

  • We are done with all things now we will open our deployed project on the EC2 Worker node because applications run on Worker nodes.

Running the Application:

  • Open the PORT 31111 in your Worker Node before doing the steps below.

  • Navigate to your Worker Node EC2 and check it.

  • After checking it copy the Public IPv4 Address of the Worker EC2.

  • Paste it in the Browser tab and add 31111 at the end. It should look like this:

  • We will see that our Project is Running on the Kubernetes Cluster.

Thanks.