Project 3: Deployment app in EKS using Jenkins

Project 3: Deployment app in EKS using Jenkins

taraps.hashnode.dev/project-1-django-applic..

taraps.hashnode.dev/project-1-django-applic..

https://taraps.hashnode.dev/project-2-docker-as-jenkins-agent?t=1699369683609#heading-installing-plugin-for-docker-to-run-as-docker-agent

Installing prerequisite for EKS and connecting to AWS

https://taraps.hashnode.dev/setting-up-eks-cluster-via-cliterminal

Creating Namespace in EKS

  1. After establishing the connection with aws, you can create a namespace in the cluster

     kubectl create ns app
    
  2. Now to check the namespaces and pods inside the namespace

     kubectl get ns   # It will show all the namespaces prest in EKS
     kubectl get pods -n <namespace name> # It will show all the pods inside the perticular namespace
    

Storing dockerhub credential in jenkins

  1. Go to Manage Jenkins

  2. Then select credentials > system (global) and store the credential

Installing required plugins

Go to Manage Jenkins from the Jenkin's Dashboard > Plugins > Available plugins and install Kubernetes CLI plugins.

Create a job

  1. Go to Jenkin's dashboard > Click on New Item

  2. Give the project name "node-todo", select Pipeline and then select ok

  3. In the general section select github project and enter the URL.

  4. In the pipeline section choose Pipeline scrip from SCM by providing the github URL where we have jenkinsfile and click on SAVE.

  5. For more detailed about SCM Polling and webhook click on the below link

    https://taraps.hashnode.dev/creating-webhook-and-scm-polling-in-jenkins

  6. Run the Build

  7. Below is the Log of the build

  8. In the EC2 instance where jenkins is running run the below commands to connect to EKS.

     sudo snap install kubectl --classic
     sudo snap install aws-cli --classic
     aws configure
     aws eks update-kubeconfig --name demo-eks --region us-east-1
    
  9. Write the Dockerfile to push the application to the dockerhub

     FROM node:12.2.0-alpine
     WORKDIR app
     COPY . .
     RUN npm install
     RUN npm run test
     EXPOSE 8000
     CMD ["node","app.js"]
    
  10. Write the Deployment.yaml to deploy the application into the EKS

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: todo
    spec:
      selector:
        matchLabels:
          app: todo
      template:
        metadata:
          labels:
            app: todo
        spec:
          containers:
          - name: myapp
            image: tara0674/node-app-test-new:latest
            ports:
            - containerPort: 8000
    
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: todo
    spec:
      selector:
        app: todo
      ports:
      - protocol: TCP
        port: 8000
        targetPort: 8000
      type: LoadBalancer
    
  11. Write the Jenkinsfile to create the pipeline

    pipeline {
        agent any
    
        environment {
            KUBE_NAMESPACE = 'app'
        }
        stages{
            stage("Clone Code"){
                steps{
                    git url: "https://github.com/prasadtara09/node-todo-cicd.git", branch: "master"
                }
            }
            stage("Build and Test"){
                steps{
                    sh "docker build . -t node-app-test-new"
                }
            }
            stage("Push to Docker Hub"){
                steps{
                      // fetch the dockerhub login credentials from jenkins
                    withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")])
                    {
                        sh "docker tag node-app-test-new ${env.dockerHubUser}/node-app-test-new:latest"
                        sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
                        sh "docker push ${env.dockerHubUser}/node-app-test-new:latest"
                    }
                }
            }
                stage ('EKS Deploy') {
                    steps{
                        // Deploy the application to EKS
                        sh "kubectl apply -f Deployment.yaml -n $KUBE_NAMESPACE"
                    }
    
            }
    
        }
    
    }
    

Output

  1. Repository created in DockerHub

  2. Loadbalacer is created in EKS

  3. Pods are running