DEV Community

Cover image for Deploy Spring Boot App to GCP
Bhargav M
Bhargav M

Posted on

Deploy Spring Boot App to GCP

This blog mainly concentrates how to deploy spring boot app to GCP

1)Write the docker file


2) Docker image building and testing locally
 docker image build  --tag="spring_boot_hello_world:0.0.1" .
 docker run -p 8080:8080 -t spring_boot_hello_world:0.0.1

3) For those of us don't know GCP runs everything through as projects, so in order to start doing anything on GCP we need to have project.

 gcloud projects list

If the projects are empty and you want to create one

 gcloud projects create example-spring-boot-project --name="first project"

creates a new project with name first project and example-spring-boot-project as project id.

gcloud config set project example-spring-boot-project 

check whether spring-boot-example-project is set as default project in the workspace

NOTE: Step 4 is needed only for docker images from docker hub registry, not needed for google registry

4) This is an extra step if you are pulling your image from docker hub registry. You need to create a secret first with the docker hub and then use that secret whenever we want to pull the image from docker hub during deployments.

kubectl create secret docker-registry --docker-server=DOCKER_REGISTRY_SERVER --docker-username=DOCKER_USER --docker-password=DOCKER_PASSWORD --docker-email=DOCKER_EMAIL

or
Create your own secret by following steps in the link

5) Now we have all the environment set up to do the real work
Deployment Controller: Deployment Controller is the abstraction of pods and replica controller. It always tries to maintain the same no of replicas as mentioned in the manifest file.


now we have the manifest file ready, lets go and execute
  kubectl apply -f spring-boot-deployment.yaml. 

In order to verify it created pods or not we can do

  kubectl get pods

it will display the total pods with their status as Running

6) Services: Now Deployment Controller deployed our app in to desirable number of pods running at internal ip which can not be accessed by outside cluster.

How to we make the our service available over the internet?
The answer is Services which selects running pods based on labels and exposes them to the internet. NodePort, Loadbalancer follow different mechanisms to expose it.

7) Node port Service
Node Port service is basically where a vm listens to a port and sends traffic to the target port where pod is listening


8) LoadBalancer Service
Loadbalancer is one way where you create a loadbalancer which has an external ip and listens to a port at that particular ip and which ever requests come throught that port it will forward to the imploicit port of the node port and from there it will be forwarded to the pod instances
kubectl apply -f spring-boot-loadbalancer-service.yaml
kubectl get svc
take the external ip of spring-boot-loadbalancer-service and hit our endpoint /time
bhargavryl@cloudshell:~ (example-spring-boot-project)$ curl http://35.226.27.116:80/time
Wed Aug 28 02:50:53 GMT 2019

credits:@dariusx

Top comments (0)