Home/Deployment on Google kubernetes clusters with Maven

Deployment on Google kubernetes clusters with Maven

Published On: 19 March 2021.By .
  • DevOps

There are two ways to do the deployment

  1. By Installing gcloud sdk and kubectl at your local system.
  2. By using Google cloud shell

Here I am describing the steps for method 2 (By using Google cloud shell) because you don’t need any setup for your local system.

Step 1: Crete a file with name Dockerfile in your git repository root directory with the following content

FROM maven:3.6-jdk-11 as build
#copy pom
COPY pom.xml .
#resolve maven dependencies
RUN mvn clean install -Dmaven.test.skip -Dmaven.main.skip -Dspring-boot.repackage.skip && rm -r target/
#copy source
COPY src ./src
# build the app (no dependency download here)
RUN mvn clean install -Dmaven.test.skip
#RUN mvn -f app/pom.xml  clean install package

# latest oracle openjdk is the basis
FROM openjdk:11
# copy jar file into container image under app directory
#COPY app/target/kickstart-0.0.1-SNAPSHOT.jar app/app.jar
COPY --from=build ./target/kickstart-0.0.1-SNAPSHOT.jar app/app.jar
# expose server port accept connections
# start application
#CMD ["java","-Dspring.profiles.active=local", "-jar", "app/app.jar"]
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar","app/app.jar"]

Step 2: Open https://console.cloud.google.com and Open Cloud Shell by clicking the button  in the navigation bar in the upper-right corner of the console.

At the bottom below screen will be opened

Step 3: Run the cammand with your cluster name

gcloud container clusters get-credentials <Cluster-name>

Step 4: Crete the docker image with git repository

sudo docker build -t <docker-image-name> <url-to-clone-repo>#<branch_name>

A prompt for git password

Step 5: Push the docker image

docker push <docker-image-name>

Step 6 : create a deployment.yaml file  and place the following code or copy from previous deployment

apiVersion: apps/v1
kind: Deployment
  name: guardian
  replicas: 1
      app: guardian
        app: guardian
        - name: app
          image: <docker-image-name>
            - containerPort: 8080
          imagePullPolicy: Always
              path: /api/global/health  # for checking readiness
              port: 8080
            initialDelaySeconds: 5
            periodSeconds: 30
              path: /api/global/health # for checking liveness
              port: 8080
            initialDelaySeconds: 5
            periodSeconds: 30

and apply the deployment

kubectl apply -f <path to deployment.yaml file>

You can check deployment & pods status – scale by following cammands

kubectl get deployments
kubectl get pods
kubectl get service


Related content

We Love Conversations

Say Hello
Go to Top