| 1 |
--- |
| 2 |
layout: default |
| 3 |
title: Fractal Application (3/3) |
| 4 |
service_jsonnet: https://github.com/google/jsonnet/blob/master/case_studies/fractal/service.jsonnet |
| 5 |
packer_jsonnet: https://github.com/google/jsonnet/blob/master/case_studies/fractal/lib/packer.libsonnet |
| 6 |
terraform_jsonnet: https://github.com/google/jsonnet/blob/master/case_studies/fractal/lib/terraform.libsonnet |
| 7 |
cassandra_jsonnet: https://github.com/google/jsonnet/blob/master/case_studies/fractal/lib/cassandra.libsonnet |
| 8 |
makefile: https://github.com/google/jsonnet/blob/master/case_studies/fractal/Makefile |
| 9 |
--- |
| 10 |
|
| 11 |
<div class="hgroup"> |
| 12 |
<div class="hgroup-inline"> |
| 13 |
<div class="panel"> |
| 14 |
<h1 id=top> |
| 15 |
<p class=jump_to_page_top> |
| 16 |
Pages <a href="fractal.1.html">1</a>, |
| 17 |
<a href="fractal.2.html">2</a>, |
| 18 |
3 |
| 19 |
</p> |
| 20 |
Fractal Application |
| 21 |
</h1> |
| 22 |
</div> |
| 23 |
<div style="clear: both"></div> |
| 24 |
</div> |
| 25 |
</div> |
| 26 |
|
| 27 |
<div class="hgroup"> |
| 28 |
<div class="hgroup-inline"> |
| 29 |
<div class="panel"> |
| 30 |
<h2 id=using>Using The Configuration</h2> |
| 31 |
</div> |
| 32 |
<div style="clear: both"></div> |
| 33 |
</div> |
| 34 |
</div> |
| 35 |
|
| 36 |
<div class="hgroup"> |
| 37 |
<div class="hgroup-inline"> |
| 38 |
<div class="panel"> |
| 39 |
<p> |
| 40 |
The rest of this article gives simple methodologies for deploying and managing the fractal |
| 41 |
application by editing the Jsonnet configuration and then applying these changes. In order |
| 42 |
to reproduce this case study there are a few pre-requisites: |
| 43 |
</p> |
| 44 |
<ul> |
| 45 |
<li> |
| 46 |
A Linux or OSX system with GNU Make. |
| 47 |
</li> |
| 48 |
<li> |
| 49 |
Packer, built from github, in your $PATH. |
| 50 |
</li> |
| 51 |
<li> |
| 52 |
Terraform, built from github, in your $PATH. |
| 53 |
</li> |
| 54 |
<li> |
| 55 |
Jsonnet, built from github (this is also where you find the configuration and source |
| 56 |
code). |
| 57 |
</li> |
| 58 |
<li> |
| 59 |
An account on Google Cloud Platform (hosting the application will incur charges). |
| 60 |
</li> |
| 61 |
</ul> |
| 62 |
<p> |
| 63 |
Once those are satisfied, follow these steps: |
| 64 |
</p> |
| 65 |
<ol> |
| 66 |
<li> |
| 67 |
In the Google Cloud Platform console, open your project, go to APIs and Auth / |
| 68 |
credentials, and create a new service account. This will automatic download a p12 key, |
| 69 |
which you can delete as we will not be using it. Instead, click the button to download a |
| 70 |
JSON key for the new service account, move it to the fractal directory, and call it |
| 71 |
<tt>service_account_key.json</tt>. |
| 72 |
</li> |
| 73 |
<li> |
| 74 |
Create a credentials.jsonnet file based on the template, fill in your GCP project name and |
| 75 |
make up some unique passwords. |
| 76 |
</li> |
| 77 |
</ol> |
| 78 |
</div> |
| 79 |
<div style="clear: both"></div> |
| 80 |
</div> |
| 81 |
</div> |
| 82 |
|
| 83 |
<div class="hgroup"> |
| 84 |
<div class="hgroup-inline"> |
| 85 |
<div class="panel"> |
| 86 |
<h3 id=using_deploy>Initial Deployment and Tear Down</h3> |
| 87 |
</div> |
| 88 |
<div style="clear: both"></div> |
| 89 |
</div> |
| 90 |
</div> |
| 91 |
|
| 92 |
<div class="hgroup"> |
| 93 |
<div class="hgroup-inline"> |
| 94 |
<div class="panel"> |
| 95 |
<p> |
| 96 |
To deploy the application, run make -j. This should start running 3 Packer builds in |
| 97 |
parallel. In a separate terminal, use tail -f *.log in order to watch their progress. When |
| 98 |
the images are built, Terraform will show you the proposed changes (a long list of resources |
| 99 |
to be created). Enter <tt>y</tt> to confirm the changes. In time, the application will be |
| 100 |
deployed. Now you only need the appserv ip address to connect to it. You can get this |
| 101 |
using "gcloud compute addresses list" or by navigating the Google Cloud Platform console to |
| 102 |
"networks". Opening that ip in a web browser should take you to the fractal application |
| 103 |
itself. |
| 104 |
</p> |
| 105 |
<p> |
| 106 |
The application can be brought down again by running <tt>terraform destroy</tt>. Terraform |
| 107 |
remembers the resources it created via the <tt>terraform.tfstate</tt> file. This will not |
| 108 |
destroy the Packer images; they can be deleted from the console or from gcloud. |
| 109 |
</p> |
| 110 |
<p> |
| 111 |
Managing a production web service usually means making continual changes to it instead of |
| 112 |
bringing the whole thing down and up again, as we will shortly discuss. However it is still |
| 113 |
useful to bring up a fresh application for testing / development purposes. A copy or |
| 114 |
variant of the production service can be brought up concurrently with the production service |
| 115 |
(e.g., in a different project). This can be useful for QA, automatic integration testing, |
| 116 |
or load testing. It is also useful for training new ops staff or rehearsing a complex |
| 117 |
production change in a safe environment. |
| 118 |
</p> |
| 119 |
</div> |
| 120 |
<div style="clear: both"></div> |
| 121 |
</div> |
| 122 |
</div> |
| 123 |
|
| 124 |
<div class="hgroup"> |
| 125 |
<div class="hgroup-inline"> |
| 126 |
<div class="panel"> |
| 127 |
<h3 id=using_cassandra>Add / Remove Cassandra Nodes</h3> |
| 128 |
</div> |
| 129 |
<div style="clear: both"></div> |
| 130 |
</div> |
| 131 |
</div> |
| 132 |
|
| 133 |
<div class="hgroup"> |
| 134 |
<div class="hgroup-inline"> |
| 135 |
<div class="panel"> |
| 136 |
<p> |
| 137 |
Managing the Cassandra cluster requires a combination of configuration alteration (to |
| 138 |
control the fundamental compute resources) and use of the Cassandra command line tool |
| 139 |
"nodetool" on the instances themselves. For example "nodetool status fractal" on any |
| 140 |
Cassandra instance will give information about the whole cluster. |
| 141 |
</p> |
| 142 |
<p> |
| 143 |
To add a new node (expand the cluster), simply edit <a href="{{ page.service_jsonnet |
| 144 |
}}"><code>service.jsonnet</code></a>, add another |
| 145 |
instance in the Terraform configuration and run make -j. Confirm the changes (the only |
| 146 |
change should be the new instance, e.g., db4). It will start up and soon become part of the |
| 147 |
cluster. |
| 148 |
</p> |
| 149 |
<p> |
| 150 |
To remove a node, first decommission it using nodetool -h HOSTNAME decommission. When that |
| 151 |
is complete, destroy the actual instance by updating <a href="{{ page.service_jsonnet |
| 152 |
}}"><code>service.jsonnet</code></a> to remove the |
| 153 |
resource and run make -j again. Confirm the removal of the instance. It is OK to remove |
| 154 |
the first node, but its replacement should use <tt>GcpTopUpMixin</tt> instead of |
| 155 |
<tt>GcpStarterMixin</tt>. You can recycle all of the nodes if you do it one at a time, |
| 156 |
which is actually necessary for emergency kernel upgrades. |
| 157 |
</p> |
| 158 |
<p> |
| 159 |
If a node is permanently and unexpectedly lost (e.g., a disk error), or you removed it |
| 160 |
without first decommissioning it, the cluster will remain in a state where it expects the |
| 161 |
dead node to return at some point (as if it were temporarily powered down or on the wrong |
| 162 |
side of a network split). This situation can be rectified with nodetool removenode UUID, |
| 163 |
run from any other node in the cluster. In this case it is probably also necessary to run |
| 164 |
nodetool repair on the other nodes to ensure data is properly distributed. |
| 165 |
</p> |
| 166 |
</div> |
| 167 |
<div style="clear: both"></div> |
| 168 |
</div> |
| 169 |
</div> |
| 170 |
|
| 171 |
|
| 172 |
<div class="hgroup"> |
| 173 |
<div class="hgroup-inline"> |
| 174 |
<div class="panel"> |
| 175 |
<h3 id=using_appserv>Canary A Change To The Application Server</h3> |
| 176 |
</div> |
| 177 |
<div style="clear: both"></div> |
| 178 |
</div> |
| 179 |
</div> |
| 180 |
|
| 181 |
<div class="hgroup"> |
| 182 |
<div class="hgroup-inline"> |
| 183 |
<div class="panel"> |
| 184 |
<p> |
| 185 |
To introduce new functionality to the application server it is useful to divert a small |
| 186 |
proportion of user traffic to the new code to ensure it is working properly. After this |
| 187 |
initial "canary" test has passed, the remaining traffic can then be confidently transferred |
| 188 |
to the new code. The same can be said for the tile generation service (e.g. to update the |
| 189 |
C++ code). |
| 190 |
</p> |
| 191 |
<p> |
| 192 |
The model used by this example is that the application server logic and static content are |
| 193 |
embedded in the application server image. Canarying consists of building a new image and |
| 194 |
then rolling it out gradually one instance at a time. Each step of this methodology |
| 195 |
consists of a small modification to <a href="{{ page.service_jsonnet |
| 196 |
}}"><code>service.jsonnet</code></a> and then running make -j. |
| 197 |
</p> |
| 198 |
<ol> |
| 199 |
<li> |
| 200 |
Edit the <tt>appserv.packer.json</tt> packer configuration in <a href="{{ |
| 201 |
page.service_jsonnet }}"><code>service.jsonnet</code></a> to update the date embedded in |
| 202 |
the <tt>name</tt> field to the current date, and also make any desired changes to the |
| 203 |
configuration of the image or any of the referenced Python / HTML / CSS files. |
| 204 |
</li> |
| 205 |
<li> |
| 206 |
Run make -j to build the new image. Note that the previous image is still available under |
| 207 |
the old name, which means it is possible to create new instances using either the old or |
| 208 |
new image. This feature is essential to allow ongoing maintenance of the cluster if the |
| 209 |
change is rolled back. |
| 210 |
</li> |
| 211 |
<li> |
| 212 |
Create a single instance with the new image by adding it to the |
| 213 |
<tt>google_compute_instance</tt> section of <a href="{{ page.service_jsonnet |
| 214 |
}}"><code>service.jsonnet</code></a> . The easiest way to do this is to copy-paste the |
| 215 |
existing definition, and modify the image name in the copy to reflect the new image. This |
| 216 |
allows easily rolling back by deleting the copied code, and you can also transition the |
| 217 |
rest of the nodes by deleting the original copy. Thus, the duplication is only temporary. |
| 218 |
At this point the configuration may look like this: |
| 219 |
|
| 220 |
<pre class="medium">google_compute_instance: { |
| 221 |
|
| 222 |
["appserv" + k]: resource.FractalInstance(k) { |
| 223 |
name: "appserv" + k, |
| 224 |
image: "appserv-v20141222-0300", |
| 225 |
... |
| 226 |
} |
| 227 |
for k in [1, 2, 3] |
| 228 |
|
| 229 |
} + { |
| 230 |
|
| 231 |
["appserv" + k]: resource.FractalInstance(k) { |
| 232 |
name: "appserv" + k, |
| 233 |
image: "appserv-v20150102-1200", |
| 234 |
... |
| 235 |
} |
| 236 |
for k in [4] |
| 237 |
|
| 238 |
} + ...</pre> |
| 239 |
Also modify the appserv target pool to add the new instance 4, thus ensuring it receives |
| 240 |
traffic. |
| 241 |
<pre class="medium">appserv: { |
| 242 |
name: "appserv", |
| 243 |
health_checks: ["${google_compute_http_health_check.fractal.name}"], |
| 244 |
instances: [ "%s/appserv%d" % [zone(k), k] for k in [1, 2, 3, 4] ], |
| 245 |
}, </pre> |
| 246 |
</li> |
| 247 |
<li> |
| 248 |
Run make -j to effect those changes, and monitor the situation to ensure that there is no |
| 249 |
spike in errors. It is also possible to run make -j between the above two steps if it is |
| 250 |
desired to interact with the new instance before directing user traffic at it. |
| 251 |
</li> |
| 252 |
<li> |
| 253 |
If there is a problem, pull 4 out of the target pool and re-run make -j. That will leave |
| 254 |
the instance up (useful for investigation) but it will no-longer receive user traffic. |
| 255 |
Otherwise, add more instances (5, 6, ...) and add them to the target pool. You can now |
| 256 |
start pulling the old instances out of the target pool ensuring that there is always |
| 257 |
sufficient capacity for your traffic load. As always, make -j punctuates the steps. |
| 258 |
</li> |
| 259 |
<li> |
| 260 |
Once the old instances are drained of user traffic, they can be destroyed. You can do |
| 261 |
this in batches or one at a time. Eventually, the configuration will look like this, at |
| 262 |
which point the first block no-longer contributes anything to the configuration and it can |
| 263 |
be deleted: |
| 264 |
<pre class="medium">google_compute_instance: { |
| 265 |
|
| 266 |
["appserv" + k]: resource.FractalInstance(k) { |
| 267 |
name: "appserv" + k, |
| 268 |
image: "appserv-v20141222-0300", |
| 269 |
... |
| 270 |
} |
| 271 |
for k in [] |
| 272 |
|
| 273 |
} + { |
| 274 |
|
| 275 |
["appserv" + k]: resource.FractalInstance(k) { |
| 276 |
name: "appserv" + k, |
| 277 |
image: "appserv-v20150102-1200", |
| 278 |
... |
| 279 |
} |
| 280 |
for k in [4, 5, 6] |
| 281 |
|
| 282 |
} + ... </pre> |
| 283 |
</li> |
| 284 |
</ol> |
| 285 |
</div> |
| 286 |
<div style="clear: both"></div> |
| 287 |
</div> |
| 288 |
</div> |
| 289 |
|
| 290 |
<div class="hgroup"> |
| 291 |
<div class="hgroup-inline"> |
| 292 |
<div class="panel"> |
| 293 |
<h2 id=conclusion>Conclusion</h2> |
| 294 |
</div> |
| 295 |
<div style="clear: both"></div> |
| 296 |
</div> |
| 297 |
</div> |
| 298 |
|
| 299 |
<div class="hgroup"> |
| 300 |
<div class="hgroup-inline"> |
| 301 |
<div class="panel"> |
| 302 |
<p> |
| 303 |
We have shown how Jsonnet can be used to centralize, unify, and manage configuration for a |
| 304 |
realistic cloud application. We have demonstrated how programming language abstraction |
| 305 |
techniques make the configuration very concise, with re-usable elements separated into |
| 306 |
template libraries. Complexity is controlled in spite of the variety of different |
| 307 |
configurations, formats, and tasks involved. |
| 308 |
</p> |
| 309 |
<p> |
| 310 |
Finally we demonstrated how with a little procedural glue to drive other processes (the |
| 311 |
humble UNIX make), we were able to build an operations methodology where many aspects of the |
| 312 |
service can be controlled centrally by editing a single Jsonnet file and issuing make -j |
| 313 |
update commands. |
| 314 |
</p> |
| 315 |
</div> |
| 316 |
<div style="clear: both"></div> |
| 317 |
</div> |
| 318 |
</div> |
| 319 |
|
| 320 |
<div class="hgroup"> |
| 321 |
<div class="hgroup-inline"> |
| 322 |
<div class="panel"> |
| 323 |
<p class=jump_to_page> |
| 324 |
Pages <a href="fractal.1.html">1</a>, |
| 325 |
<a href="fractal.2.html">2</a>, |
| 326 |
3 |
| 327 |
</p> |
| 328 |
</div> |
| 329 |
<div style="clear: both"></div> |
| 330 |
</div> |
| 331 |
</div> |