Post

Application Deployment in Apache Tomcat on GCE Using Ansible

Think about a person who needs a cloud instance temporarily to deploy a web application to do tests frequently and throughout the time he deploys the application, use it for a while and then deletes the instance to save the cost. Or someone needs to create a cluster, thus he needs to instantiate several cloud servers at once, install dependencies and deploy the application on each server. Doing these tasks by hand costs much effort and it is inefficient. The way to make such scenarios easier, efficient and effective is making a reusable structure which does these repetitive tasks when we invoke it. For that purpose, we use configuration management.

cm_what_happens

This tutorial is about a such scenario, to splash the easiness of using a reusable code base when deploying an application, using Ansible(a radical and impressive configuration management tool with its capabilities and ease of use compared to other tools), Google cloud platform(future potential cloud services with a good pricing model) and Apache Tomcat (one of the most popular web servers in the Java community). For this purpose, we are going to use the gce-tomcat-ansible-demo repository, a concatenation of Ansible playbooks(the reusable code base) implemented by me to reflect this task. Running this will create a Google compute engine in a given Google cloud platform project, install java, configure an Apache Tomcat server and will deploy a war file according to given metadata. (To understand playbooks knowing Ansible basics is more than enough. Refer Ansible documentation)

playbook_flow


Getting started

To run the playbook you need to have a Google cloud platform account, a Google cloud platform project and a service account to manipulate Google cloud project with appropriate roles and permission and an Ansible running machine.

There are several ways to install Ansible but here let’s install it using python pip package manager since the installation is not going to depend on which operating system you use. But first make sure Python version 2, Python development and Python pip(most probably python-dev and python-pip respectively, refer installing pip with package managers for more information) packages have been installed on the machine that you are going to install Ansible. To make sure, try running the command below.

pip list

pip_list

And then clone the repository gce-tomcat-ansible-demo, which contains playbooks to create a Google compute engine instance, install Java, configure a Apache Tomcat server and deploy a given war file on the configured application server.

git clone https://github.com/lpsandaruwan/gce-tomcat-ansible-demo.git

Then change the current working directory into the cloned repository.

cd gce-tomcat-ansible-demo

Now use the requirements.txt to install appropriate Python pip package versions which this playbook has been written and tested for.

pip install -r requirements.txt

This will install ansible and apache-libcloud(a fine interface to deal with popular cloud services) Python packages which we are going to use for manipulating Google cloud project.

Make sure that you have a working Google cloud platform project(if not refer creating and manage projects), and a service account assigned to it(do not use the default service account since it has full permission over the project, refer service accounts for more information) and then obtain the project ID, private JSON keyfile(you can obtain the JSON key file when creating a new service account, if not refer service account credentials) and the service account email from them. Now we have to configure Ansible running machine to access the GCP project. Here Let’s use Google cloud SDK to make things easier(refer install Google cloud SDK). After installing the SDK run the below command to initialize.

gcloud init

gcloud_init

And it will direct you to the web page in your browser. From there allow the access to the SDK. Now in the terminal select the appropriate project ID. After that you should be able to run playbooks on the appropriate project and manipulate it. If you run into a permission problem connecting the instance configure SSH authentication using cloud SDK tools by running the command below. (refer gcloud compute config-ssh)

gcloud compute config-ssh


Play it

Now configure the file gce-vars/authentication and update the obtained metadata from GCP project and service account in playbook.

project_id: “project-id-193706”
credentials_file: “/path/to/private/json/key/file”
service_account_email: “tomcat-ansible-demo@service-account-193706.iam.gserviceaccount.com”

After that change instance metadata in gce-vars/instance as you need. Here, we are going to add firewall rules to allow HTTP traffic on 8080 ports for Apache Tomcat server

name: tomcat-ansible-demo
type: f1-micro
image: debian-9
zone: europe-west1-b
allowed_ports_tcp: tcp:8080
allowed_ports_udp: udp:8080

Then set the ANSIBLE_HOSTS environment variable required by Ansible for SSH interactions. To do that simply put hostnames in hosts file and import it as below.

export ANSIBLE_HOSTS=hosts

Now you should be able to run this project. Simply run the main playbook.

ansible-playbook run.ym

Final output will be as below. And after a successful run you will have your application deployed in an Apache Tomcat server on a Google compute engine instance.

final web

Appendix

(If you wish to change Java version, Tomcat version etc. configure main.yml in defaults directory in roles. They contain configuration variables with lower priorities.)

gce-tomcat-ansible-demo
|------gce_vars				# variables related to Google cloud platform
|	|	authentication		# Google project and service account related metadata
|	|	instance		# GCE instance related metadata
|
|-------roles
|	|-------java				# role to install Java
|	|	|-------defaults
|	|	|	|	main.yml	# default variables for java role
|	|	|
|	|	|-------tasks
|	|	|	|	main.yml	# tasks to download and install Java
|	|
|	|-------tomcat
|	|	|-------defaults
|	|	|	|	main.yml	# default variables for tomcat role
|	|	|
|	|	|-------files
|	|	|	|	tomcat-users.xml	# set tomcat manager credentials
|	|	|
|	|	|-------tasks
|	|	|	|	main.yml	# tasks to download and configure tomcat
|	|
|	|-------tomcat-deploy
|	|	|-------defaults
|	|	|	|	main.yml	# default variables for tomcat-deploy role
|	|	|
|	|	|-------tasks
|	|	|	|	main.yml	# tasks to deploy the given war file
|
|	hosts					# ansible hosts
|	bootstrap-instance.yml			# playbook to initiate google cloud instance
|	deploy-war.yml				# playbook to deploy war file
|	install-java.yml			# playbook to install Java
|	install-tomcat.yml			# playbook to install Apache Tomcat
|	run.yml					# main playbook to run


Creative Commons License
gce-tomcat-ansible-demo post by Lahiru Pathirage is licensed under a Creative Commons Attribution 4.0 International License.
Based on a work at https://github.com/lpsandaruwan/gce-tomcat-ansible-demo.

This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.