Kubernetes deployments webapp

Kubernetes deployments webapp

In this project, the web app is deployed in Kubernetes. Code is fetched by Jenkins from GitHub after that ansible will create a docker image and push it to the docker hub. The Ansible server runs a playbook for the deployment web app in Kubernetes.

Github url for the all files required in this project

Three Instances

To begin this project you need three servers one for Jenkins, a second for ansible, third for Kubernetes.

  • Setting up Server

    • Jenkins Server

      • Create a t2 micro instance to run the Jenkins server

      • run a script to install Jenkins

      •             sudo apt update -y
        
                    sudo apt install default-jre -y
        
                    java -version
        
                    sudo apt update -y
        
                    wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
        
                    sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
        
                    sudo apt update -y
        
                    sudo add-apt-repository universe -y
        
                    sudo apt-get install jenkins -y
        
                    sudo service jenkins start
        
                    cat /var/lib/jenkins/secrets/initialAdminPassword
        
  • Ansible Server

    • create a t2 micro instance to run the ansible server with the same as Jenkins.

    • run the script for installing ansible on the server

    •             sudo apt-add-repository ppa:ansible/ansible -y
      
                  sudo apt update -y 
      
                  sudo apt install ansible -y
      
    • install docker on the ansible server

    •             sudo apt update -y
      
                  sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
      
                  curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
      
                  sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable" -y
      
                  sudo apt update -y
      
                  apt-cache policy docker-ce -y
      
                  sudo apt install docker-ce -y
      
                  #sudo systemctl status docker
      
                  sudo chmod 777 /var/run/docker.sock
      
  • Kubernetes Server

    • In the Kubernetes server I will create a single-node cluster minikube you can set up a multi-node cluster setup as well.

    • Create t2.medium instance with 4gb ram

    • Install Docker for run minikube driver

    • install minikube

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

    •             curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
      
                  curl -LO https://dl.k8s.io/release/v1.26.0/bin/linux/amd64/kubectl
      
                  curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"
      
                  echo "$(cat kubectl.sha256)  kubectl" | sha256sum --check
      
                  sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
      
                  chmod +x kubectl
                  mkdir -p ~/.local/bin
                  ./kubectl ~/.local/bin/kubectl
      

Jenkins starting

  • Install plugins that are used in the project

  • Install ssh-agent

  • Create the first Pipeline Project

Setting Up Credentials for ansible and Kubernetes

  • To setup credentials click pipeline syntax

  • select ssh agent

  • set credentials for the ansible server and Kubernetes server.

  • provide a key that you already generate from the ec2 instance

  • Set credential for docker hub user click on pipeline Syntax.

  • click on add to give a docker password and save with the variable name

  • f

Setup password-less authentication

  • log in to the ansible server

  • Change file in Kubernetes server /etc/ssh/sshd_config

  • make this change

  • Change the password of the root user on the Kubernetes server "passwd root"

  • Restart sshd service on the Kubernetes server by this command "service sshd restart"

  • create ssh key by running the ssh-keygen command on Jenkins and ansible server

  • my Kubernetes private ip is here 172.31.0.86

  • run this command to copy ssh keys to the Kubernetes server.

  •         ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.31.18.252
    
  • create ssh keys and run both commands on Jenkins and Kubernetes to make sure all servers are in the same network.

Run Pipeline

  • Click on Build Now

  • The build job will successful after a second

Run command in Kubernetes server to expose node port

kubectl port-forward svc/harish343 8080:8080 --address 0.0.0.0 &
  • the web app has been exposed at 8080 port

Jenkins file : change ip address according to server. Two ip address has to be changed one for ansible and second kubernetes.

Also change local ip in asible.yml for kubernetes