DevOps Project

You are hired as a DevOps engineer for Analytics Pvt Ltd. This company is a product based organization which uses Docker for their containerization needs within the company. The final product received a lot of traction in the first few weeks of launch. Now with the increasing demand, the organization needs to have a platform for automating deployment, scaling, and operations of application containers across clusters of hosts, As a DevOps engineer, you need implement a DevOps life cycle, such that all the requirements are implemented without any change in the Docker containers in the testing environment.

Up until now, this organization used to follow a monolithic architecture with just 2 developers. The product is present on

Following are the specifications of life-cycle:

  1. Git workflow should be implemented. Since the company follows monolithic architecture of Development you need to take care of version control. The release should happen only on 25th of every month.

  2. Code build should be triggered once the commits are made in the master Branch.

  3. The code should be containerized with the help of the Docker file, The Dockerfile should be built every time if there is a push to Git-Hub. Create a custom Docker image using a Dockerfile.

  4. As per the requirement in the production server, you need to use the Kubernetes cluster and the containerized code from Docker hub should be deployed with 2 replicas. Create a NodePort service and configure the same for port 30008.

  5. Create a Jenkins pipeline script to accomplish the above task.

  6. For configuration management of the infrastructure, you need to deploy the configuration on the servers to install necessary software and configurations.

  7. Using Terraform accomplish the task of infrastructure creation in the AWS cloud provider.

Architectural Advice Software’s to be installed on the respective machines using configuration management.

  • Worker1: Jenkins, Java.

  • Worker2: Docker, Kubernetes.

  • Worker3: Java, Docker, Kubernetes

  • Worker4: Docker, Kubernetes.

Step1:creating the instance (as controller) and installing terraform in that.

follow the below commands:

sudo su
apt-get update -y
#Now we will install terraform create the terra_install bash file and run it
vi terra_install.sh
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
gpg --no-default-keyring \
--keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \
--fingerprint
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update -y
sudo apt-get install terraform -y

Step-2: Launching 3 more instance for the kube-master and 2 worker nodes.

creating terraform file main.tf

vi main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
    }
  }
}

provider "aws" {
  region  = "us-east-1"
  access_key = "" #put your IAM access_key
  secret_key = "" # put your IAM secret_key
}


resource "aws_instance" "example" {
  ami = "ami-007855ac798b5175e"
  count = 2
  instance_type = "t2.medium"
  key_name = "project02"
  tags = {
    Name = "kub-slave-${count.index}"
  }
}
resource "aws_instance" "main" {
  ami = "ami-007855ac798b5175e"
  count = 1
  instance_type = "t2.medium"
  key_name = "project02"
  tags = {
     Name = "kub1-master"
  }
}
terraform init 
terraform validate
terraform plan
terraform apply

Step-3: installing kubernetes in the master and worker-nodes

make the file as kube_install.sh

vi kube_install.sh
sudo apt update -y
sudo apt-get install openjdk-11-jdk -y
sudo apt install docker.io -y

sudo systemctl start docker
sudo systemctl enable docker

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

sudo apt update -y
sudo apt install kubeadm=1.20.0-00 kubectl=1.20.0-00 kubelet=1.20.0-00 -y
bash kube_install.sh
sudo su (in master node)
kubeadm init #(this will generate the join token command which needs to be pasted on the worker nodes)

mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config
# Your not going to get the node so network configuration pligin
(CNI)
kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml

kubectl get nodes

Step-4: Install Jenkins in controller.

create the jenkins_install.sh

vi jenkins_install.sh
sudo apt-get install openjdk-11-jdk -y
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
  /usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update

sudo apt-get install jenkins -y
bash jenkins_install.sh

Step-5:Login to jenkin and create the pipeline.

create node :kub-master

create the project02 job inside that paste the groovy script.

pipeline{
    agent none
    environment {
        DOCKERHUB_CREDENTIALS=credentials('b529aefe-28fa-45fd-bcea-57f0fa861cb1')
    }

    stages{
        stage('Hello'){
            agent{ 
                label 'Kub-master'
            }
            steps{
                echo 'Hello World'
            }
        }
        stage('git'){
            agent{ 
                label 'Kub-master'
            }

            steps{

                git 'https://github.com/santosh-nellagi/website.git'
            }
        }
        stage('docker') {
            agent { 
                label 'Kub-master'
            }

            steps {

                sh 'sudo docker build /home/ubuntu/jenkins/workspace/project02 -t santosnellagi740/demo1'
                sh 'sudo echo $DOCKERHUB_CREDENTIALS_PSW | sudo docker login -u $DOCKERHUB_CREDENTIALS_USR --password-stdin'
                sh 'sudo docker push santosnellagi740/demo1'

            }
        }
        stage('Kuberneets') {
            agent { 
                label 'Kub-master'
            }

            steps {

                sh 'sudo kubectl create -f deploy.yml'
                sh 'sudo kubectl create -f svc.yml'
            }
        }        

    }
}

output :