Building and Deploying a Spring Boot Application on Amazon EKS with Jenkins CI/CD

Building and Deploying a Spring Boot Application on Amazon EKS with Jenkins CI/CD

ยท

20 min read

Table of contents

Step 1: Creating Ec2 Instance

  • We will begin by launching an Amazon EC2 instance with the instance type set to 'm3.medium'.

Step 2: Install Apache Maven

  • In this step, we will install Apache Maven on the EC2 instance that we launched in Step 1.

    You can install Maven by running the following commands on your EC2 instance:

      sudo hostname Jenkins
      sudo apt update
      sudo apt install maven -y
      mvn --version
    

Step 3: Install Java

  • In this step, we will install the Java Development Kit (JDK) on your EC2 instance.

    You can install OpenJDK (a popular choice) by running the following commands:

      sudo apt install openjdk-17-jre -y
    

    After the installation is complete, you can verify the Java version:

      java -version
    

Step 4: Install Jenkins

  • In this step, we will install Jenkins on your EC2 instance.

  • You can install Jenkins by following these commands:

      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
    

    Once Jenkins is installed, start the Jenkins service and enable it to start on boot:

      sudo systemctl enable jenkins
      sudo systemctl status jenkins
    

    Jenkins should now be up and running on your EC2 instance. You can access the Jenkins web interface by opening your web browser and navigating to http://your_ec2_instance_public_ip:8080. Follow the on-screen instructions to complete the initial setup.

Step 5: Install AWS Command Line Interface (CLI)

  • In this step, we will install the AWS Command Line Interface (CLI) on your EC2 instance. The AWS CLI allows you to interact with various AWS services from the command line.

      sudo apt install awscli
      curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
      sudo apt install unzip -y
      unzip awscliv2.zip
      sudo ./aws/install --update
    

    After the installation is complete, you can verify the AWS CLI version:

      aws --version
    

Step 6: Install EKSCTL

  • In this step, we will install eksctl, a command-line utility for creating and managing Amazon Elastic Kubernetes Service (EKS) clusters. EKS is a managed Kubernetes service offered by AWS.

      curl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp
      sudo mv /tmp/eksctl /usr/local/bin
    

    After the installation is complete, you can verify the eksctl version:

      eksctl version
    

Step 7: Install kubectl

  • In this step, we will install kubectl, the Kubernetes command-line tool, on your EC2 instance. kubectl is used to interact with Kubernetes clusters, including Amazon Elastic Kubernetes Service (EKS) clusters.

      sudo curl --silent --location -o /usr/local/bin/kubectl   https://s3.us-west-2.amazonaws.com/amazon-eks/1.22.6/2022-03-09/bin/linux/amd64/kubectl
      sudo chmod +x /usr/local/bin/kubectl
      kubectl version --short --client
    

Step 8: Install Docker

  • In this step, we will install Docker on your EC2 instance.To install Docker, follow these commands:

      sudo apt-get update
      sudo apt install docker.io -y
      sudo usermod -a -G docker jenkins
      sudo service jenkins restart
      sudo systemctl daemon-reload
      sudo service docker stop
    

    After the installation is complete, you can start the Docker service and enable it to start on boot:

      sudo systemctl start docker
      sudo systemctl enable docker
      sudo systemctl status docker
    

Step 9: Create a New Repository on GitHub

  • In this step, we will create a new Git repository on GitHub to host your project's source code.

    Take note of the repository's URL, which you can use to push your code to GitHub.

Step 10: Clone the Repository

  • In this step, we will clone the GitHub repository you created in the previous step to your local development environment.

    1. Open your terminal or command prompt on your local development environment.

    2. Navigate to the directory where you want to clone the repository. You can use the cd command to change to the desired directory. For example:

    3. To clone the repository, use the git clone command and provide the URL of your GitHub repository. Replace <repository_url> with the actual URL of your repository.

    git clone https://github.com/eddzaa/newspringbootapp.git

Step 11: Retrieve Manifests, Source Code, Target Files, and pom.xml from GitHub Repository

In this step, we will retrieve specific files from another GitHub repository, download them as a ZIP archive, copy the files into your previously cloned project folder, and then commit and push the changes to your repository.

  1. Open your web browser and go to the GitHub repository from which you want to retrieve files. In this case, it's github.com/malibalakrishna/springbootapp.

  2. Click on the green "Code" button and select "Download ZIP" to download the entire repository as a ZIP archive.

  3. Extract the contents of the downloaded ZIP archive to a temporary folder on your local machine.

  4. Identify the specific files you want to copy from the extracted folder. In your case, you mentioned "manifests," "src," "target," and "pom.xml" files.

  5. Copy the files from the temporary folder into the project folder.

  1. Stage the changes for commit using the git add command.Commit the changes with a meaningful commit message,Finally, push the changes to your GitHub repository

     git add .
     git commit -m "first commit"
     git push
    

Step 12: Create a Jenkinsfile and Push to Your Repository

In this step, we will create a Jenkinsfile that defines the pipeline for your project, and then we'll push it to your GitHub repository. The Jenkinsfile will specify how Jenkins should build and deploy your application.

  1. Create a new file named Jenkinsfile in the root directory of your project.

  2. Copy and paste the following pipeline script into your Jenkinsfile. This script defines a basic Jenkins pipeline that builds your project using Maven and checks out your source code from GitHub:

pipeline {
   tools {
        maven 'Maven3'
    }
    agent any
    environment {
        registry = "923770093922.dkr.ecr.us-east-1.amazonaws.com/myrepo"
    }

    stages {
        stage('Cloning Git') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '', url: 'https://github.com/eddzaa/newspringbootapp.git']]])     
            }
        }
      stage ('Build') {
          steps {
            sh 'mvn clean install'           
            }
      }
    }
}

Note:

1. Replace the URL github.com/eddzaa/newspringbootapp.git with the actual URL of your Git repository.

2. Replace '*/main' with '*/master' if your project uses the "master" branch as the default branch. If your project uses a different branch name as the default, replace 'master' accordingly.

  1. Save the Jenkinsfile in your project directory.

  2. Open your terminal or command prompt and navigate to your project directory (the one containing the Jenkinsfile).

  3. Use Git to add and commit the Jenkinsfile

  4. Push the changes to your GitHub repository::

Step 13: Open Port 8080 in AWS Security Group and Connect to Jenkins

  • In this step, we will connect to the Jenkins web interface, which runs on port 8080 by default. Additionally, if your Jenkins server is hosted on an AWS EC2 instance, you'll need to open port 8080 in the AWS Security Group to access Jenkins remotely.

Connecting to Jenkins:

  1. Open your web browser.

  2. In the address bar, enter the public IP address or DNS name of your Jenkins server followed by :8080. For example, if your EC2 instance's public IP address is 1.2.3.4, enter http://1.2.3.4:8080.

Step 14: Configure Maven Installation in Jenkins

In this step, we will configure Maven installation in Jenkins by specifying a tool named "Maven3" with the appropriate Maven home directory.

  1. Navigate to Manage Jenkins:

    • Click on "Manage Jenkins" on the Jenkins home page.
  2. Configure Global Tools:

    • In the "Manage Jenkins" page, click on "Global Tool Configuration."
  3. Configure Maven:

    • Scroll down to the "Maven" section.

    • Click the "Add Maven" button to add a new Maven installation.

  4. Specify Maven Details:

    • In the "Name" field, enter "Maven3" (or any name you prefer to identify this Maven installation).

    • In the "MAVEN_HOME" field, enter /usr/share/maven.

  5. Save Maven Configuration:

    • Scroll down and click the "Save" button to save the Maven configuration.

Now, Jenkins is configured to use Maven with the name "Maven3," and the MAVEN_HOME environment variable is set to /usr/share/maven.

You can use this Maven installation in your Jenkins pipelines by referring to it as "Maven3."

Step 15: Create a Jenkins Pipeline

In this step, we will create a Jenkins pipeline to automate the build process of your project using the Jenkinsfile you previously added to your repository. This pipeline will use the Maven installation you configured in Step 14.

  1. Create a New Pipeline:

    • Click on "New Item" in the Jenkins home page.
  2. Enter a Name:

    • Enter a name for your pipeline in the "Item name" field (e.g., "springboot").
  3. Select Pipeline Project:

    • Choose the "Pipeline" option and click "OK."

  1. Configure Pipeline:

    • In the pipeline configuration page, scroll down to the "Pipeline" section.

    • Definition:

      • In the "Definition" dropdown, select "Pipeline script from SCM" as the pipeline script source.
    • SCM (Source Code Management):

      • In the "SCM" dropdown, choose the version control system you're using for your project (e.g., Git).
    • Repository URL:

    • Branches to Build:

      • Specify the branch you want Jenkins to build (e.g., master).

Save Pipeline:

  • Scroll to the bottom and click the "Save" button to create the pipeline.

  • To start a build, go back to the Jenkins dashboard, find your pipeline project, and click on "Build Now." You can also set up webhooks or scheduled builds to automate the process further.

Step 16: Add a Unit Testing Stage to the Jenkins Pipeline

In this step, we will edit the Jenkinsfile to add a stage that performs unit tests on your code. After editing the file, we will push the updated Jenkinsfile to your Git repository and trigger a build in Jenkins.

  1. Add the following stage within the stages section to perform unit testing using Maven Surefire:
stage('Unit Test') {
      steps {
        echo '<--------------- Unit Testing started  --------------->'
        sh 'mvn surefire-report:report'
        echo '<------------- Unit Testing stopped  --------------->'
      }
    }

This stage will echo messages indicating when unit testing starts and stops and will run the mvn surefire-report:report command to execute unit tests using Maven Surefire.

  1. Save the Jenkinsfile:

    • After adding the stage, save the Jenkinsfile in Visual Studio Code.
  2. Commit and Push Changes to Git:

    • Open your terminal or command prompt.

    • Navigate to your project directory.

    • Use Git to add, commit, and push the changes to your Git repository:

  1. Trigger a Jenkins Build:
  • Go back to the Jenkins dashboard in your web browser.

  • Find your pipeline project and click on "Build Now" to trigger a new build.

Step 17: Install the SonarQube Scanner Extension in Jenkins

In this step, we will install the SonarQube Scanner extension in Jenkins, allowing you to integrate SonarQube code analysis into your CI/CD pipeline. SonarQube helps you analyze code quality, find bugs, and track code smells and security vulnerabilities.

  1. Navigate to Manage Jenkins:

    • Click on "Manage Jenkins" on the Jenkins home page.
  2. Manage Plugins:

    • In the "Manage Jenkins" page, click on "Manage Plugins."
  3. Available Tab:

    • Click on the "Available" tab to view available Jenkins plugins.
  4. Search for SonarQube Scanner:

    • In the "Filter" field, type "SonarQube Scanner."
  5. Select the Plugin:

    • Check the checkbox next to "SonarQube Scanner" to select it for installation.
  6. Install the Plugin:

    • Scroll down to the bottom of the page and click the "Download now and install after restart" button.

Step 18: Create a Token on SonarCloud.io

In this step, we will create an authentication token (also known as an access token) on SonarCloud.io. This token will be used to authenticate Jenkins with your SonarCloud.io account when performing code analysis.

  1. Access SonarCloud:

  2. Log In:

    • Log in to your SonarCloud account using your GitHub credentials.
  3. Generate Token:

    • Click your profile picture or username in the top right corner.
  4. Access Security:

    • Select "Security" from the dropdown menu.
  5. Generate Token:

    • Click the "Generate" button.
  6. Provide Token Details:

    • Name: Give your token a descriptive name (e.g., "Jenkins ").
  7. Generate Token:

    • Click the "Generate" button.
  8. Copy the Token:

    • Copy the generated token to a secure location

Step 19: Add the SonarCloud Token as a Secret Key in Jenkins

In this step, we will add the SonarCloud access token that you generated in Step 18 as a secret key in Jenkins. This token will be used by Jenkins to authenticate with SonarCloud for code analysis.

  1. Manage Credentials:

    • Click "Manage Jenkins" on the Jenkins home page.
  2. Add SonarCloud Token:

    • In the left-hand menu, click "Manage Credentials."

    • Click "Global credentials (unrestricted)."

    • Click the "Add Credentials" link.

  3. Select Credential Type:

    • In the "Kind" dropdown, select "Secret text."
  4. Enter Token Value:

    • In the "Secret" field, paste the SonarCloud access token.
  5. Optional ID and Description:

    • You can provide an optional ID and description to identify this credential.
  6. Save the Credential:

    • Click "Create" to save the credential.

Step 20: Add SonarQube Server in Jenkins

In this step, we will configure Jenkins to use your SonarQube server for code analysis. You'll need to provide the SonarQube server URL and the SonarCloud access token you generated in previous steps.

  1. Navigate to Manage Jenkins:

    • Click on "Manage Jenkins" on the Jenkins home page.
  2. Configure System:

    • In the "Manage Jenkins" page, click on "Configure System."
  3. Scroll Down to SonarQube Configuration:

    • Scroll down to find the "SonarQube servers" section.
  4. Add SonarQube Server:

    • Click the "Add SonarQube" button to add a new SonarQube server configuration.

  5. Configure SonarQube Server:

    • Fill in the following details:

      • Name: Enter a name to identify this SonarQube server configuration (e.g., "sonar-cloud").

      • Server URL: Enter the URL of your SonarQube server (e.g., https://sonarcloud.io for SonarCloud).

      • Server authentication token: Select the SonarCloud access token that you added in Step 19 from the dropdown menu.

  6. Save Configuration:

    • Click the "Save" button to save the SonarQube server configuration.

Step 21: Add SonarQube Scanner Installation in Jenkins

In this step, we will configure Jenkins to use the SonarQube Scanner for code analysis by adding it as a tool installation.

  1. Manage Global Tool Configuration:

    • Click "Manage Jenkins" on the Jenkins home page.
  2. Add SonarQube Scanner:

    • In the "Manage Jenkins" page, scroll down to the "SonarQube Scanner" section.
  3. Add SonarQube Scanner Installation:

    • Click "Add SonarQube Scanner."
  4. Configure SonarQube Scanner:

    • Fill in the following details:

      • Name: Enter a name for this SonarQube Scanner installation (e.g., "Sonar-scanner").

      • Install automatically: Check this option to allow Jenkins to automatically install the SonarQube Scanner if it's not already installed on the build agent.

  5. Save Configuration:

    • Click "Save" to save the SonarQube Scanner installation configuration.

Step 22: Add SonarQube Analysis Stage to Jenkinsfile

In this step, we will add a SonarQube analysis stage to your Jenkinsfile. This stage will perform code analysis using SonarQube with the specified organization, project, and access token.

Add SonarQube Analysis Stage:

  • Add the following stage to your Jenkinsfile, which includes the SonarQube analysis step with the necessary configuration changes:
 stage('Sonar Analysis') {
      environment {
        scannerHome = tool 'sonar-scanner'
      }
      steps {
        echo '<--------------- Sonar Analysis started  --------------->'
        //         withSonarQubeEnv('sonar-cloud') {
        //         sh "${scannerHome}/bin/sonar-scanner"

        // }
        withSonarQubeEnv('sonar-cloud') {
          sh 'mvn clean verify sonar:sonar -Dsonar.projectKey=newspringbootapp -Dsonar.organization=eddzaa -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=ecb90074d03b180890ed61d4d0e7096698c58371'
          echo '<--------------- Sonar Analysis stopped  --------------->'
        }
      }
    }
  • Replace newspringbootapp with your actual SonarQube project key.

  • Replace eddzaa with your SonarQube organization.

  • Replace ecb90074d03b180890ed61d4d0e7096698c58371 with your SonarQube access token.

  • Save the Jenkinsfile:

    • After adding the SonarQube analysis stage, save the Jenkinsfile.
  • Add Commit and Push Changes:

    • Add and commit your changes and push them to your Git repository

Trigger a Jenkins Build:

a. Go to the Jenkins dashboard in your web browser.

c. Click on "Build Now" to trigger a new build of your pipeline.

Jenkins will now start a new build, which includes the updated Jenkinsfile with the SonarQube analysis stage . The pipeline will perform code analysis using SonarQube as part of the build process.

Navigate to SonarCloud.io.

  • Review the analysis results to assess code quality, identify issues, and gain insights into your project's health.

Step 23: Add Quality Gate Stage to Jenkinsfile and Push Changes

In this step, we will add a Quality Gate stage to your Jenkinsfile. This stage will check the results of your SonarQube analysis against predefined quality gate conditions and ensure that the code meets the required quality standards. If the code doesn't meet the standards, the pipeline will fail.

  1. Edit Jenkinsfile:

    • Locate the Jenkinsfile in your workspace.
  2. Add Quality Gate Stage:

    • Add the following Quality Gate stage to your Jenkinsfile:
stage('Quality Gate') {
      steps {
        script {
          echo '<--------------- Quality Gate started  --------------->'
          timeout(time: 1, unit: 'MINUTES') {
            def qg = waitForQualityGate()
            if (qg.status != 'OK') {
              error 'Pipeline failed due to the Quality gate issue'
            }
          }
          echo '<--------------- Quality Gate stopped  --------------->'
        }
      }
    }
  1. Save the Jenkinsfile:

    • After adding the Quality Gate stage, save the Jenkinsfile.
  2. Add, Commit, and Push Changes:

Now, your Jenkins pipeline includes a "Quality Gate" stage that will check the SonarQube analysis results against predefined quality gate conditions. If the code doesn't meet the quality standards, the pipeline will fail.

Step 24: Create Webhooks in SonarCloud

In this step, we will create webhooks in SonarCloud to notify external services or systems (such as Jenkins) when code analysis is completed or when quality gate status changes. Webhooks allow for automated integration and can trigger actions in response to specific events.

  1. Go to Project Settings:

    • In the project dashboard, locate and click on "Administration."
  2. Webhooks Section:

    • Look for the "Webhooks" section in the project settings.
  3. Create a New Webhook:

    • Click the option to create a new webhook .
  4. Configure Webhook Settings:

    • Fill in the webhook details:

      • Name: Give your webhook a descriptive name.

      • URL: Enter the URL of the Jenkins webhook endpoint. This URL is where SonarCloud will send webhook payloads.

  5. Save Webhook:

    • Click the "Save" or "Create" button to create the webhook.

  6. Include new code definition sets in SonarCloud, you can configure it using the following steps:

    1. Administration Section:

      • Look for the "New Code" or similar section within project settings.
    2. Select Previous Version:

      • You should find an option called "previous version" of your project.
    3. Save Configuration:

      • After selecting the previous version, click the "Save" button to save the configuration.

Click on "Build Now" to trigger a new build of your pipeline.

  • After the build is completed, Jenkins will perform the SonarQube analysis, and the Quality Gate stage will evaluate the results.

  • Check the "Quality Gate" section or tab within your project. It should display the Quality Gate status, including whether it passed or failed.

Step 25: Build a Docker Image in Jenkins

In this step, we will add a stage to your Jenkinsfile that builds a Docker image for your application

  1. Add Docker Image Build Stage:

    Add the following stage to your Jenkinsfile to build a Docker image:

      stage('Build Docker Image') {
                 steps {
                     script {
                         // Build the Docker image
                         sh 'docker build -t myrepo .'
                     }
                 }
         }
    

    Save the Jenkinsfile:

    After adding the Docker image build stage, save the Jenkinsfile.

  2. Create a Dockerfile for your Spring Boot application. The Dockerfile defines how your application should be packaged into a Docker image.

    1. Create a New File:

      • Create a new file in the same directory as your Spring Boot project. You can name it Dockerfile (without any file extension).
    2. Add Dockerfile Contents:

      • Copy and paste the following Dockerfile contents into your new Dockerfile
    FROM lolhens/baseimage-openjre
    ADD target/springbootApp.jar springbootApp.jar
    EXPOSE 80
    ENTRYPOINT ["java", "-jar", "springbootApp.jar"]

This Dockerfile does the following:

  • Specifies a base image with OpenJRE (you can adjust the base image as needed).

  • Adds your Spring Boot JAR file (springbootApp.jar) to the image.

  • Exposes port 80 (you can modify this to match the port your Spring Boot application uses).

  • Sets the entry point to run the Spring Boot application.

Save, Add, commit, and push your changes to the Git repository:

Click on "Build Now" to trigger a new build of your pipeline.

Step 26: Create an Amazon Elastic Container Registry (ECR) Repository

In this step, we will create an Amazon ECR repository where you can store and manage your Docker images. ECR is a fully managed container registry service provided by Amazon Web Services (AWS).

In the AWS Management Console, use the "Services" menu at the top, or you can search for "ECR" in the search bar to locate Amazon Elastic Container Registry.

  1. Create a New Repository:

    • In the Amazon ECR dashboard, click the "Create repository" button.
  2. Configure the Repository:

    • In the "Create repository" wizard, configure the repository settings:

      • Repository name: Enter a name for your ECR repository, e.g., "myrepo."

      • Click the "Create" button to create the ECR repository.

Step 27: Involves creating an Amazon Elastic Kubernetes Service (EKS) cluster.

Before proceeding, ensure that your Jenkins server instance has the necessary administrative access in AWS Identity and Access Management (IAM) to create and manage EKS resources. Here's the step-by-step process:

  • Ensure that the Jenkins server instance has the necessary IAM permissions to create and manage EKS clusters. The instance should have IAM roles or permissions attached that grant administrative access to EKS resources.

SSH into Jenkins Server:

  • Log in to your Jenkins server as a user with administrative privileges

  •   sudo su - jenkins
    

    Create the EKS Cluster:

    • Use the eksctl command-line tool to create the EKS cluster with the following command:

    •   eksctl create cluster --name demo-eks --region us-east-1 --nodegroup-name my-nodes --node-type t3.small --managed --nodes 2
      

  • Once the cluster creation is successful, you will see a confirmation message in the terminal.

Step 28: Push Docker Image to Amazon Elastic Container Registry (ECR)

In this step, you'll add a stage to your Jenkins pipeline that pushes the Docker image you built earlier to Amazon Elastic Container Registry (ECR).

stage('Pushing to ECR') {
     steps{  
         script {
                sh 'aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 680549223804.dkr.ecr.us-east-1.amazonaws.com'
                sh 'docker tag myrepo:latest 680549223804.dkr.ecr.us-east-1.amazonaws.com/myrepo:latest'
                sh 'docker push 680549223804.dkr.ecr.us-east-1.amazonaws.com/myrepo:latest'
         }
        }
      }

Here's what each part of this stage does:

  1. Authentication: This step authenticates Docker with your ECR repository in the specified AWS region (us-east-1). It uses the aws ecr get-login-password command to obtain a temporary authentication token, which is then passed to docker login.

  2. Tagging: The Docker image you built earlier is tagged with the ECR repository URI. This step ensures that the image is correctly associated with your ECR repository.

  3. Pushing: Finally, the Docker image is pushed to the ECR repository.

Make sure to replace 680549223804.dkr.ecr.us-east-1.amazonaws.com with the correct ECR repository URI for your specific setup.

With this stage, your Jenkins pipeline will push the Docker image to ECR, making it available for use in your Amazon Elastic Kubernetes Service (EKS) cluster or other AWS services.

Step 29: Deploy to Amazon Elastic Kubernetes Service (EKS)

In this step, you'll add a stage to your Jenkins pipeline that deploys your application to Amazon Elastic Kubernetes Service (EKS) by applying Kubernetes manifest files.

stage('Deploy to EKS') {
            steps {
                script {
                    // Authenticate with the EKS cluster (ensure AWS credentials are configured)
                    sh 'aws eks --region us-east-1 update-kubeconfig --name demo-eks'

                    // Apply Kubernetes manifest files to deploy your application
                     // sh "kubectl delete -f eks-deploy-k8s.yaml"
                      sh "kubectl apply -f eks-deploy-k8s.yaml"
                }
            }
        }

Here's what each part of this stage does:

  1. Authentication: This step authenticates your Jenkins server with the Amazon EKS cluster named demo-eks in the us-east-1 region. It uses the aws eks update-kubeconfig command to configure the Kubernetes configuration file (kubeconfig) with the necessary credentials and cluster information.

  2. Deployment: Once authenticated with the EKS cluster, the stage applies Kubernetes manifest files to deploy your application. You should replace "eks-deploy-k8s.yaml" with the actual filename or path to your Kubernetes deployment manifest.

    With this stage, your Jenkins pipeline will deploy your application to the Amazon EKS cluster.

Step 30: Create a Kubernetes Deployment Manifest (eks-deploy-k8s.yaml)

In this step, you will create a Kubernetes deployment manifest file (eks-deploy-k8s.yaml) that defines the deployment of your Spring Boot application on Amazon Elastic Kubernetes Service (EKS).

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: springboot-app
  name: springboot-app
spec:
  replicas: 4
  selector:
    matchLabels:
      app: springboot-app
  template:
    metadata:
      labels:
        app: springboot-app
    spec:
      containers:
      - name: my-springboot-app
        image: 837083512344.dkr.ecr.us-east-1.amazonaws.com/myrepo:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 8085
          name: http
          protocol: TCP
# service type loadbalancer       
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: springboot-app
    k8s-app: springboot-app
  name: springboot-app
spec:
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 8085
  type: LoadBalancer
  selector:
    app: springboot-app

Make sure to replace the image reference (image) with the correct Amazon ECR repository URI and image tag that you want to deploy.

Save this YAML file as eks-deploy-k8s.yaml in your project directory. This file will be used to deploy your Spring Boot application to Amazon EKS in your Jenkins pipeline.

Save, Add, commit, and push your changes to the Git repository:

Click on "Build Now" to trigger a new build of your pipeline.

Access Your Spring Boot Application via Load Balancer DNS

  • The application should respond, and you should be able to interact with it via the Load Balancer's DNS URL.

We have successfully deployed your Spring Boot application on Amazon Elastic Kubernetes Service (EKS) and accessed it .This application is now running on a scalable and managed Kubernetes cluster in the AWS cloud.Happy Coding!!

Did you find this article valuable?

Support Edvin Dsouza by becoming a sponsor. Any amount is appreciated!

ย