Tuesday, January 11, 2022

Deploy Springboot Microservices App into Amazon EKS Cluster using Jenkins Pipeline and Kubectl CLI Plug-in | Containerize Springboot App and Deploy into EKS Cluster using Jenkins Pipeline

We will learn how to automate springboot microservices builds using Jenkins pipeline and Deploy into AWS EKS - Kubernetes Cluster with help of Kubernetes CLI plug-in.

We will use Springboot Microservices based Java application. I have already created a repo with source code + Dockerfile. The repo also have Jenkinsfile for automating the following:

- Automating builds using Jenkins
- Automating Docker image creation
- Automating Docker image upload into AWS ECR
- Automating Deployments to Kubernetes Cluster


Please watch the above steps in YouTube channel:

1. Amazon EKS Cluster is setup and running. Click here to learn how to create Amazon EKS cluster.
2. Create ECR repo in AWS
4. Docker installed on Jenkins instance 
5. Docker, Docker pipeline and Kubernetes CLI plug-ins are installed in Jenkins

6. Install kubectl on your instance

Step # 1 - Create Maven3 variable under Global tool configuration in Jenkins

Make sure you create Maven3 variable under Global tool configuration.

Step #2 - Create Credentials for Kubernetes Cluster
Click on Add Credentials, use Kubernetes configuration from drop down.

execute the below command to get kubeconfig info, copy the entire content of the file:
sudo cat ~/.kube/config

Enter ID as K8S and choose enter directly and paste the above file content and save.


Step # 3 - Create a pipeline in Jenkins
Create a new pipeline job.

Step # 4 - Copy the pipeline code from below
Make sure you change red highlighted values below as per your settings:
Your docker user id should be updated.
your registry credentials ID from Jenkins from step # 1 should be copied

pipeline {
   tools {
        maven 'Maven3'
    agent any
    environment {
        registry = "211223789150.dkr.ecr.us-east-2.amazonaws.com/my-docker-repo"
    stages {
        stage('Cloning Git') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'd4df484b-80dd-41e1-b85d-e9e07599abfd', url: 'https://github.com/akannan1087/my-docker-spring-boot-app']]])     
      stage ('Build') {
          steps {
            sh 'mvn clean install'           
    // Building Docker images
    stage('Building image') {
        script {
          dockerImage = docker.build registry
    // Uploading Docker images into AWS ECR
    stage('Pushing to ECR') {
         script {
                sh 'aws ecr get-login-password --region us-east-2 | docker login --username AWS --password-stdin 211223789150.dkr.ecr.us-east-2.amazonaws.com'
                sh 'docker push 211223789150.dkr.ecr.us-east-2.amazonaws.com/my-docker-repo:latest'
      stage ('K8S Deploy') {
       steps {
         withKubeConfig([credentialsId: 'K8S']) {
            sh 'kubectl apply -f eks-deploy-from-ecr.yaml'

Step # 5 - Build the pipeline
Once you create the pipeline and changes values per your configuration, click on Build now:

Step # 6 - Verify deployments to K8S

kubectl get pods

kubectl get deployments

kubectl get services

Steps # 7 - Access SpringBoot App in K8S cluster
Once build is successful, go to browser and enter master or worker node public ip address along with port number mentioned above

You should see page like below:

No comments:

Post a Comment

Install Helm 3 Linux - Setup Helm 3 on Linux | Install Helm 3 on Ubuntu | Setup Helm 3 on Linux | Setup Helm 3 on Ubuntu

What is Helm and How to install Helm  version  3? Helm is a package manager for Kubernetes. Helm is the K8s equivalent of yum or apt. I t a...