| 1 |
--- |
| 2 |
layout: default |
| 3 |
title: Fractal Application (1/3) |
| 4 |
srcroot: https://github.com/google/jsonnet/blob/master/case_studies |
| 5 |
service_jsonnet: https://github.com/google/jsonnet/blob/master/case_studies/fractal/service.jsonnet |
| 6 |
packer_jsonnet: https://github.com/google/jsonnet/blob/master/case_studies/fractal/lib/packer.libsonnet |
| 7 |
terraform_jsonnet: https://github.com/google/jsonnet/blob/master/case_studies/fractal/lib/terraform.libsonnet |
| 8 |
cassandra_jsonnet: https://github.com/google/jsonnet/blob/master/case_studies/fractal/lib/cassandra.libsonnet |
| 9 |
makefile: https://github.com/google/jsonnet/blob/master/case_studies/fractal/Makefile |
| 10 |
--- |
| 11 |
|
| 12 |
<div class="hgroup"> |
| 13 |
<div class="hgroup-inline"> |
| 14 |
<div class="panel"> |
| 15 |
<h1 id=top> |
| 16 |
<p class=jump_to_page_top> |
| 17 |
Pages 1, |
| 18 |
<a href="fractal.2.html">2</a>, |
| 19 |
<a href="fractal.3.html">3</a> |
| 20 |
</p> |
| 21 |
Fractal Application |
| 22 |
</h1> |
| 23 |
</div> |
| 24 |
<div style="clear: both"></div> |
| 25 |
</div> |
| 26 |
</div> |
| 27 |
|
| 28 |
<div class="hgroup"> |
| 29 |
<div class="hgroup-inline"> |
| 30 |
<div class="panel"> |
| 31 |
<h2 id=intro>Introduction</h2> |
| 32 |
</div> |
| 33 |
<div style="clear: both"></div> |
| 34 |
</div> |
| 35 |
</div> |
| 36 |
|
| 37 |
<div class="hgroup"> |
| 38 |
<div class="hgroup-inline"> |
| 39 |
<div class="panel"> |
| 40 |
<p> |
| 41 |
This case study illustrates that <a href="/index.html">Jsonnet</a> can be used to |
| 42 |
centralize, unify, and manage configuration for all parts of a cloud hosted multi-tier web |
| 43 |
application (a Mandelbrot viewer). Jsonnet centralizes configuration files for the various |
| 44 |
application software, database schemas and initial data sets, system configuration files, |
| 45 |
package manifests, software build configurations, image configurations (<a |
| 46 |
href="https://www.packer.io/">Packer</a>) and cloud resources / connectivity (<a |
| 47 |
href="https://www.terraform.io/">Terraform</a>). Although the running example is deployed on |
| 48 |
<a href="https://cloud.google.com">Google Cloud Platform</a> and uses specific application |
| 49 |
software, Jsonnet can be used to generate configuration for any application and (via Packer |
| 50 |
& Terraform) a wide range of cloud providers. |
| 51 |
</p> |
| 52 |
|
| 53 |
<p> |
| 54 |
Prerequisites: It is assumed that the reader has read the Jsonnet <a |
| 55 |
href="/docs/tutorial.html">tutorial</a>, and has a basic knowledge of Packer and Terraform. |
| 56 |
</p> |
| 57 |
</div> |
| 58 |
<div style="clear: both"></div> |
| 59 |
</div> |
| 60 |
</div> |
| 61 |
|
| 62 |
<div class="hgroup"> |
| 63 |
<div class="hgroup-inline"> |
| 64 |
<div class="panel"> |
| 65 |
<h2 id=app>Example Web Application</h2> |
| 66 |
</div> |
| 67 |
<div style="clear: both"></div> |
| 68 |
</div> |
| 69 |
</div> |
| 70 |
|
| 71 |
<div class="hgroup"> |
| 72 |
<div class="hgroup-inline"> |
| 73 |
<a href="/img/fractal_screenshot.png"><img src="/img/fractal_screenshot.png" class="wide"></a> |
| 74 |
<div style="clear: both"></div> |
| 75 |
</div> |
| 76 |
</div> |
| 77 |
|
| 78 |
<div class="hgroup"> |
| 79 |
<div class="hgroup-inline"> |
| 80 |
<div class="panel"> |
| 81 |
<p> |
| 82 |
The example application allows the user to zoom and pan a Mandelbrot fractal (dynamically |
| 83 |
rendered server side, in C++). The user is able to save the location of features they find, |
| 84 |
deep in the fractal, and a time-ordered list of these with thumbnails is displayed in the |
| 85 |
left hand pane. All required files are available in the Jsonnet repository if you want to |
| 86 |
deploy this yourself. |
| 87 |
</p> |
| 88 |
<p> |
| 89 |
Although admittedly a little contrived, this example is intended to represent the structure |
| 90 |
of a typical non-trivial real-world web application. It is an example of micro-service |
| 91 |
architecture (illustrated below). The services are: An Application Server, a backend |
| 92 |
database (Cassandra) and a backend service for generating the fractal PNG tiles (C++). Each |
| 93 |
service is scalable and fault-tolerant. |
| 94 |
</p> |
| 95 |
</div> |
| 96 |
<div style="clear: both"></div> |
| 97 |
</div> |
| 98 |
</div> |
| 99 |
|
| 100 |
<div class="inverse hgroup"> |
| 101 |
<div class="hgroup-inline"> |
| 102 |
<div class="panel wide"> |
| 103 |
<img src="/img/fractal_architecture.png"> |
| 104 |
</div> |
| 105 |
<div style="clear: both"></div> |
| 106 |
</div> |
| 107 |
</div> |
| 108 |
|
| 109 |
<div class="hgroup"> |
| 110 |
<div class="hgroup-inline"> |
| 111 |
<div class="panel"> |
| 112 |
<p> |
| 113 |
The Application Server hosts static content (CSS) and Jinja'd HTML. The <a href="{{ |
| 114 |
page.srcroot }}/fractal/appserv/templates/page.html">HTML</a> contains JavaScript that |
| 115 |
issues AJAX requests to 1) the Tile Generating Service and 2) the Application Server (to |
| 116 |
fetch / amend the discoveries list). The Application Server therefore does not communicate |
| 117 |
directly with the fractal Tile Generating Service, but it needs to know the host:port |
| 118 |
endpoint in order to embed it in the HTML so that the user's browser can do so. The user |
| 119 |
does not communicate directly with the Cassandra database. |
| 120 |
</p> |
| 121 |
<p> |
| 122 |
Both the Application server and the tile generation service use Nginx, uWSGI and flask to |
| 123 |
host their content. For the application Server, this means transforming HTTP requests into |
| 124 |
database accesses and/or serving content (code <a href="{{ page.srcroot |
| 125 |
}}/fractal/appserv/main.py">here</a>). For the tile generation service, this means invoking |
| 126 |
a compiled C++ <a href="{{ page.srcroot }}/fractal/tilegen/mandelbrot.cpp">executable</a> |
| 127 |
from the Flask <a href="{{ page.srcroot |
| 128 |
}}/fractal/tilegen/mandelbrot_service.py">handler</a> in order to construct a PNG for a |
| 129 |
given tile / thumbnail of the fractal. Both services consist of a group of <a |
| 130 |
href="https://cloud.google.com/compute/docs/instances">instances</a> behind a <a |
| 131 |
href="https://cloud.google.com/compute/docs/load-balancing/network/">layer 3 cloud load |
| 132 |
balancer</a> with a static IP and a simple <a |
| 133 |
href="https://cloud.google.com/compute/docs/load-balancing/health-checks">health check</a>. |
| 134 |
The Cassandra database is simply a set of instances, as the Cassandra client library (used |
| 135 |
by the Application Server) does client-side load balancing and transparent failover, thus |
| 136 |
does not need a cloud load balancer. |
| 137 |
</p> |
| 138 |
<p> |
| 139 |
The application is deployed by first using Packer to build an image for each of the 3 kinds |
| 140 |
of cloud instances (Application Server, Tile Generating Service, Cassandra). Then, all the |
| 141 |
cloud resources (instances, load balancers, etc.) are deployed using Terraform. The Packer |
| 142 |
build compiles, installs and configures all of the required software on each image. The |
| 143 |
Terraform configuration provides last minute configuration (host:port endpoints, passwords, |
| 144 |
etc.) to the instances via <a |
| 145 |
href="https://cloud.google.com/compute/docs/metadata">metadata</a>. |
| 146 |
</p> |
| 147 |
<p> |
| 148 |
The choice about what configuration to provide at image build time (embedded in Packer |
| 149 |
configurations) vs deployment time (embedded in Terraform configuration) is up to the user. |
| 150 |
The advantage of doing more at image build time is that instances can then be deployed more |
| 151 |
quickly (useful in an auto-scaling situation). But allowing some configuration at |
| 152 |
deployment time makes the images more flexible. Some configuration (e.g. host:port |
| 153 |
endpoints) is only known at deployment time so must be specified in the Terraform |
| 154 |
configuration. In our case, we try to do all time consuming steps (downloading, generating, |
| 155 |
compiling) in Packer, while leaving finer details until deployment. |
| 156 |
</p> |
| 157 |
</div> |
| 158 |
<div style="clear: both"></div> |
| 159 |
</div> |
| 160 |
</div> |
| 161 |
|
| 162 |
<div class="hgroup"> |
| 163 |
<div class="hgroup-inline"> |
| 164 |
<div class="panel"> |
| 165 |
<p class=jump_to_page> |
| 166 |
Pages 1, |
| 167 |
<a href="fractal.2.html">2</a>, |
| 168 |
<a href="fractal.3.html">3</a> |
| 169 |
</p> |
| 170 |
</div> |
| 171 |
<div style="clear: both"></div> |
| 172 |
</div> |
| 173 |
</div> |