1

I need to deploy my spring-boot application on compute engine in Google cloud platform. I have already created an instance and through SSH Apache and Maven have been installed. Further, war file has been uploaded into the bucket. Anybody can provide me with the remaining commands to deploy the war file on tomcat instance or any other cloud platforms with linux?

Thanks

JRichardsz
  • 14,356
  • 6
  • 59
  • 94
chk.buddi
  • 554
  • 1
  • 8
  • 29
  • Is this just a proof of concept or Do you need a basic devops/automation flow? – JRichardsz Dec 27 '18 at 20:13
  • @JRichardsz Actually, I need a basic develops. I searched everywhere but I cannot find any resources to deploy war file on tomcat which installed in compute engine instance.I have already copied my war file into instance. Can you please let me know which commands that need for deployment? – chk.buddi Dec 28 '18 at 00:32
  • Don't worry, I will help you. In the meantime, some questions. Are you in the ability to acquire instances? Basic environments for enterprise consists on 4 instances : dev, testing, production and the last one for continuous integration. Or do you need something more light like a startup or demo? Are you using git? – JRichardsz Dec 28 '18 at 01:12
  • @JRichardsz Thank you for your help. Actually, this is a demo website. Yes, I have used git.This is the git url. – chk.buddi Dec 28 '18 at 01:38
  • https://github.com/ChkBuk/myexamples/tree/master/SyneBiz – chk.buddi Dec 28 '18 at 01:39
  • Now I can see it better. I am preparing my answer. If is not clear try to contact me with my profile – JRichardsz Dec 28 '18 at 14:56
  • @JRichardsz Thank you you very much for your kind assistance. – chk.buddi Dec 28 '18 at 15:13

1 Answers1

2

Deploy in compute engine instance of google not substantially different from AWS, Azure or another linux host provider.

You just need an ssh connection to the remote machine and install the required software to compile, build, zip, deploy, etc

I will list some approaches from basic(manually) to advanced(automated):

#1 Bash scripting

  • unzip and configure git
  • unzip and configure java
  • unzip and configure maven
  • unzip and configure tomcat (this is not required if spring-boot is used)
  • configure the linux host to open 8080 port
  • create a script called /devops/pipeline.sh in your remote cloud linux instance, with the following steps

For war deployment :

# get the source code
cd /tmp/folder/3dac58b7
git clone http://github.com/myrepo.git .
# create war
mvn clean package
# move war to deploy tomcat folder
cp target/my_app.war /my/tomcat/webapps
# stop tomcat
bash /my/tomcat/shutdown.sh
# start tomcat
bash /my/tomcat/startup.sh

Or spring-boot startup

# get the source code
cd /tmp/folder/3dac58b7
git clone http://github.com/myrepo.git .
# create jar
mvn clean package
# kill or stop the application
killall java
# start the application
java $JAVA_OPTS -jar $jar_file_name

  • After push to git, just connect to you instance using ssh and execute

bash /devops/pipeline.sh

Improvements: Parametrize repository name, branch name, mvn profile, database credentials, create a tmp/uuid folder in every execution, delete the tmp/uuid after deploy,optimize start and stop of application using pid, etc

#2 Docker

  • Install docker in your remote cloud linux instance

  • Create a Dockerfile with all the steps for war or springboot (approach #1) and store it close to your source code (I mean in your git repository)

  • Perform a git push of your changes

  • Connect to your remote cloud linux instance using ssh:

  • Build your docker image: docker build ...

  • Delete previous container and run a new version:

    docker rm my_app -f docker run -d --name my_app -p 8080:8080 my-container-name


In the previous approaches, build operations are performed in the remote server. To do that, several tools are needed in that server. In the following approaches, build is performed in an intermediate server and just deploy is executed in the remote server. This is a a little better


#3 Local Build (an extra instance is required)

In this approach, the build is performed in the developer machine and its uploaded to some kind of repository. I advice you docker instead of just war or jar compilation.

In order to build and upload the docker image, one of these docker registries are required:

  • Docker simple registry
  • Amazon Elastic Container Registry (ECR)
  • Docker hub.
  • Harbor.
  • JFrog Container Registry.
  • Nexus Container Registry.
  • Portus
  • Azure Container Registry.

Choose one and install it in a new server. Configure your developer machine and your remote server to point to your new docker registry.

Final steps are:

  • Perform a docker build in your developer machine after. This will create a new docker image of your java application (tomcat+war or springboot jar)
  • Upload your local image to your new docker registry with something like:
    docker push example.com/test-image
  • Connect to your remote cloud linux instance using ssh and just download the docker image
docker pull example.com/test-image
  • In the remote server, just start your new downloaded image with docker run...

#4 Use a continuous integration server (an extra instance is required)

Same to the #3 but not in the developer machine. All the steps are performed in another server called: Continuous integration server.

#4.1 Use a continuous integration server (an extra instance is required)

  • Install Jenkins or another Continuous integration server in the new instance
  • Configure plugins and other required things in jenkins in order to enable webhook url : https://jrichardsz.github.io/devops/configure-webhooks-in-github-bitbucket-gitlab
  • Create a job in jenkins to call the script of approach #1 or execute docker commands of approach #2. If you can, Approach #3 would be perfect.
  • Configure your SCM (github, bitbucket, gitlab, etc) to point to the webhook url published by Jenkins.

When you are ready to deploy, just push the code to your scm, jenkins will be notified and will execute the previous created job. As you can see, there is no human required to deploy de application in the server(With the exception of developer push)

Note: At this point, you could migrate the scripts of approaches #1 and #2 to :

These are more advanced and scalable approaches to mapping all the commands and configurations required from the beginning to the deployment.

#5 Advanced (Sysadmin team or extra people and knowledge are required )

More instances and technologies will be required.

  • Kubernetes
  • Ansible
  • High availability / Load balancer
  • Backups
  • Configurations Management
  • And more automations

This will be necessary when more and more web applications, microservices are required in your company/enterprise.

#6 Saas

  • All the previous approaches could be simplified using WORLD CLASS platforms like:

  • Jelastic

  • Heroku

  • Openshift, etc

JRichardsz
  • 14,356
  • 6
  • 59
  • 94
  • Many thanks. I will try these ways and let you know the status. Thank you once again. – chk.buddi Dec 28 '18 at 17:11
  • @chk.buddi if this helped to solve your problem, please use the checkmark on the left to mark this as solved :D. Also don't forget to contact me if your have troubleshootings in one of the approaches or steps – JRichardsz Dec 28 '18 at 17:17
  • Sure, once I solve the issue I will mark as the solution. Thank you – chk.buddi Dec 28 '18 at 17:21
  • @JRichardz I tried the #1 method. Now, I'm getting a different issue. My app runs fine when I run as 'Spring boot App' on eclipse but when I run on Tomcat, index page is not displaying.For this app, client app is react based and I have built the client project and copied into the static folder of the main project.Any suggestion? – chk.buddi Dec 29 '18 at 18:03
  • If your app is an spring-boot, you don't need tomcat. Tomcat is mandatory in your requirement? Related to your architecture, I recommend to decouple web client app to the api rest. You can host your app in a separate instance with apache, nginx, nodejs, etc – JRichardsz Dec 29 '18 at 19:50
  • Yes, you are correct.I tried using new compute engine instance. I installed required softwares and ran the jar file as ‘java -jar my jar name.jar’. App is working perfectly inside the instance (localhost:8080).But when I tried to access the website using external ip of the instance, home page is not loading. Any idea on this? – chk.buddi Dec 29 '18 at 20:46
  • Maybe your firewall or port is not open. Try this command inside the instance: curl http://localhost:8080 If this command return the html of your home page, your app is correct and you must need to open the 8080 port in your intance https://stackoverflow.com/questions/21065922/how-to-open-a-specific-port-such-as-9090-in-google-compute-engine – JRichardsz Dec 30 '18 at 14:20
  • I tried opening firewall but I couldn’t.Then I chose AWS and it works fine. Thank you for the continuous support given. – chk.buddi Jan 01 '19 at 11:35
  • I'm glad it worked! If you choose the next approaches, just contact me. – JRichardsz Jan 01 '19 at 14:40
  • Ok,sure. Thank you – chk.buddi Jan 01 '19 at 14:41
  • Just two more thing : #1 What do you think about to change the question title, to something like : How to deploy java application in a cloud instance from the scratch to and advanced architecture? Also add tags like aws, azure, DigitalOcean,linux, docker. This will help to another developers to understand how cloud deployments works. #2 Review the issues on your github repository. – JRichardsz Jan 01 '19 at 14:56
  • #1-ok, That’s good idea.I will change the title.#2-ok I will review them – chk.buddi Jan 01 '19 at 15:00