How to Deploy application on GKE using Jenkins

Hacker hands using laptop computer to code
Reading Time: 3 minutes

Prerequisite

  • Java install
  • Jenkins server
  • Service Account with the GKE permissions
  • Any application

Introduction

In this blog, we will write a declarative Jenkins pipeline to deploy applications on GKE and configure the multibranch pipeline. I am using the spring boot pet clinic for demonstration. This project consists of a spring-boot application and a docker file.

Declarative Pipeline

pipeline{
    agent any
    environment {
        PROJECT_ID = 'your project id'
        CLUSTER_NAME = 'your cluster name'
        LOCATION = 'zone'
        CREDENTIALS_ID = 'your project name '
    }

    stages{
        stage('checkout git'){
            steps{
                 git branch: 'main',
               url:'https://github.com/shivampateriya1/spring-petclinic.git'
            }
           
        }

        stage('Build App'){
            steps{
                sh './mvnw clean package'
            }
        post{
            success{
                junit 'target/surefire-reports/*.xml'
                archiveArtifacts "target/*.jar"
            } 
        }    
        }
       stage('Docker build'){
        steps{
            script{
                sh 'docker build -t shivampateriyaknoldus/petclinic:0.1 . '
            }
        }
       }
       stage('Docker login and push'){
        steps{
            withCredentials([string(credentialsId: 'dockersecret', variable: 'TOKEN')]) {
            sh 'docker login -u shivampateriyaknoldus -p $TOKEN'

            sh 'docker push shivampateriyaknoldus/petclinic:0.1'
        }

       }
       }


       stage('Deploy to kubernetes'){
        steps{
            
            step([$class: 'KubernetesEngineBuilder', projectId: env.PROJECT_ID, clusterName: env.CLUSTER_NAME, location: env.LOCATION, manifestPattern: 'petclinic.yaml', credentialsId: env.CREDENTIALS_ID, verifyDeployments: true])
		   echo "Deployment Finished ..."
        }
       }

    }
}

Stages

  1. Checkout git
    • This stage will clone the repo for further use.
  2. Build App
    • Build maven project
  3. Docker build
    • Build docker image
  4. Docker login
    • Login into docker and push the docker image for deployment
  5. Deploy to Kubernetes
    • Deploy application on GKE cluster
Jenkins
Create a Jenkins item and configure multibranch and add a source code repo link. Then build the job.

For practice purposes, you can use https://github.com/spring-projects/spring-petclinic.git

jobs
This job has failed because we need to download docker in an agent and also need to add docker cred as secret into Jenkins.

Download Google Kubernetes Engine

gke plugin
Search for GKE on the available plugins and install it.
save cred
Add project name and service account JSON key with GKE permissions and download kubectl on the agent.

Jenkins Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace:  petclinic
  name: petclinic
spec:
  selector:
    matchLabels:
      app: petclinic
  template:
    metadata:
      labels:
        app: petclinic
    spec:
      containers:
      - name: petclinic
        image: shivampateriyaknoldus/petclinic:0.1
        imagePullPolicy: Always
        ports:
        - containerPort: 8080

Expose using Loadbalancer

apiVersion: v1
kind: Service
  
metadata:
  namespace: petclinic
  name: petclinic
spec:
  type: LoadBalancer
  selector:
    app: petclinic
  ports:
  - port: 8081
    targetPort: 8080

Build Pipeline

successful build job
This job is successfully built and we can see our application is deployed on GKE. Let’s check the pods and service.
check pod
Let’s try to access our applications.
Accessing application

Conclusion

In this blog, we are able to build a multibranch pipeline and build it using maven, docker and push it on docker hub and deploy it on the GKE cluster using Jenkins.

For CI/CD blog https://blog.knoldus.com/jenkins-continuous-integration-ci-continuous-delivery-and-deployment-cd/

Written by 

Shivam Pateriya is a DevOps Engineer at Knoldus. He likes to learn about emerging technologies. His keen interest in Python, Cloud, and Automation.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading