-
Notifications
You must be signed in to change notification settings - Fork 0
/
nationalparks-java.html
536 lines (461 loc) · 20.6 KB
/
nationalparks-java.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="apple-touch-icon" sizes="180x180" href="apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="favicon-16x16.png">
<link rel="manifest" href="site.webmanifest">
<link rel="mask-icon" href="safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="theme-color" content="#ffffff">
<script src="jquery/jquery.min.js"></script>
<script src="popper/popper.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<script src="requirejs/require.js"></script>
<script>var workshop_base_url = "/user/user3/workshop";</script>
<link rel="stylesheet" href="asciidoctor/css/asciidoctor.css">
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="fontawesome/css/all.min.css">
<link rel="stylesheet" href="css/workshop.css">
<link rel="stylesheet" href="css/workshop-asciidoc.css">
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-135921114-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag("js", new Date());
gtag("config", "UA-135921114-11");
</script>
</head>
</body>
<!-- Header -->
<header class="header">
<div class="container-fluid">
<div class="row">
<div class="col-sm-6">
<a href="" class="logo">Starter Labs (Java)</a>
</div>
</div>
</div>
</header>
<!-- Main -->
<div class="container-fluid">
<div class="row">
<div class="col-sm-3 d-sm-block d-none">
<!-- Table of Contents -->
<ul class="menu">
<li class="category">
<ul class="modules">
<h5 class="category-title">Workshop Modules</h5>
<li class="page"><a href="common-workshop-summary.html">Workshop Summary</a></li>
<li class="page"><a href="common-environment.html">Environment Overview</a></li>
<li class="page"><a href="common-using-homeroom.html">Using Homeroom</a></li>
<li class="page"><a href="common-parksmap-architecture.html">Architecture Overview of the ParksMap Application</a></li>
<li class="page"><a href="common-explore.html">Exploring the CLI and Web Console</a></li>
<li class="page"><a href="parksmap-container-image.html">Deploying Your First Container Image</a></li>
<li class="page"><a href="parksmap-scaling.html">Scaling and Self Healing</a></li>
<li class="page"><a href="parksmap-routes.html">Exposing Your Application to the Outside World</a></li>
<li class="page"><a href="parksmap-logging.html">Exploring OpenShift's Logging Capabilities</a></li>
<li class="page"><a href="parksmap-permissions.html">Role-Based Access Control</a></li>
<li class="page"><a href="parksmap-rsh.html">Remote Access to Your Application</a></li>
<li class="page active"><a href="nationalparks-java.html">Deploying Java Code</a></li>
<li class="page"><a href="nationalparks-java-databases.html">Adding a Database (MongoDB)</a></li>
<li class="page"><a href="nationalparks-java-application-health.html">Application Health</a></li>
<li class="page"><a href="nationalparks-java-pipeline.html">Automate Build and Deployment with Pipelines</a></li>
<li class="page"><a href="nationalparks-java-pipeline-codechanges-gogs.html">Automation for Your Application on Code Changes</a></li>
<li class="page"><a href="mlbparks-templates.html">Using Application Templates</a></li>
<li class="page"><a href="mlbparks-binary-build.html">Binary Builds for Day to Day Development</a></li>
<li class="page"><a href="common-further-resources.html">Further Resources</a></li>
<li class="page"><a href="common-workshop-links.html">Workshop Links</a></li>
</ul>
</li>
</ul>
</div>
<div class="col-sm-9">
<section class="page-content">
<!-- Top Navigation -->
<div class="btn-group btn-group-xs float-right">
<button type="button" onclick="location.href='parksmap-rsh';" class="btn btn-xs btn-transparent" aria-label="Prev">
<span class="fas fa-arrow-left" aria-hidden="true"></span>
</button>
<button type="button" onclick="location.href='';" class="btn btn-xs btn-transparent" aria-label="Home">
<span class="fas fa-home" aria-hidden="true"></span>
</button>
<button type="button" onclick="location.href='nationalparks-java-databases';" class="btn btn-xs btn-transparent" aria-label="Next">
<span class="fas fa-arrow-right" aria-hidden="true"></span>
</button>
</div>
<!-- Title -->
<h1 class="title">Deploying Java Code</h1>
<!-- Content -->
<div class="paragraph">
<p>In this section, we’re going to deploy a backend service, developed in Java that will expose 2 main REST endpoints to the visualizer
application (<code>parksmap</code> web component that was deployed in the previous labs).
The application will query for national parks information (including its
coordinates) that is stored in a MongoDB database. This application will also
provide an external access point, so that the API provided can be directly used
by the end user.</p>
</div>
<div class="imageblock text-center">
<div class="content">
<img src="images/roadshow-app-architecture-nationalparks-1.png" alt="Application architecture" width="800">
</div>
</div>
<div class="sect1">
<h2 id="_background_source_to_image_s2i">Background: Source-to-Image (S2I)</h2>
<div class="sectionbody">
<div class="paragraph">
<p>In a previous lab, we learned how to deploy a pre-existing image
image. Now we will expand on that by learning how OpenShift builds
container images using source code from an existing repository. This is accomplished using the Source-to-Image project.</p>
</div>
<div class="paragraph">
<p><a href="https://github.com/openshift/source-to-image">Source-to-Image (S2I)</a> is a
open source project sponsored by Red Hat that has the following goal:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code>Source-to-image (S2I) is a tool for building reproducible container images. S2I
produces ready-to-run images by injecting source code into a container image and
assembling a new container image which incorporates the builder image and built
source. The result is then ready to use with docker run. S2I supports
incremental builds which re-use previously downloaded dependencies, previously
built artifacts, etc.</code></pre>
</div>
</div>
<div class="paragraph">
<p>OpenShift is S2I-enabled and can use S2I as one of its build mechanisms (in
addition to building container images from Dockerfiles, and "custom" builds).</p>
</div>
<div class="paragraph">
<p>OpenShift runs the S2I process inside a special <strong>Pod</strong>, called a Build
Pod, and thus builds are subject to quotas, limits, resource scheduling, and
other aspects of OpenShift.</p>
</div>
<div class="paragraph">
<p>A full discussion of S2I is beyond the scope of this class, but you can find
more information about it either in the
<a href="https://docs.openshift.com/container-platform/latest/creating_images/s2i.html">OpenShift S2I documentation</a>
or on <a href="https://github.com/openshift/source-to-image">GitHub</a>. The only key concept you need to
remember about S2I is that it’s magic.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_exercise_creating_a_java_application">Exercise: Creating a Java application</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The backend service that we will be deploying as part of this exercise is
called <code>nationalparks</code>. This is a Java Spring Boot application that performs 2D
geo-spatial queries against a MongoDB database to locate and return map
coordinates of all National Parks in the world. That was just a fancy way of
saying that we are going to deploy a webservice that returns a JSON list of
places.</p>
</div>
<div class="sect2">
<h3 id="_add_to_project">Add to Project</h3>
<div class="paragraph">
<p>Because the <code>nationalparks</code> component is a backend to serve data that our
existing frontend (parksmap) will consume, we are going to build it inside the existing
project that we have been working with. To illustrate how you can interact with OpenShift via the CLI or the web console, we will deploy the nationalparks component using the web console.</p>
</div>
</div>
<div class="sect2">
<h3 id="_using_application_code_on_an_embedded_git_server">Using Application Code on an Embedded Git Server</h3>
<div class="paragraph">
<p>OpenShift can work with any accessible Git repository. This could be GitHub,
GitLab, or any other server that speaks Git. You can even register webhooks in
your Git server to initiate OpenShift builds triggered by any update to the
application code!</p>
</div>
<div class="paragraph">
<p>The repository that we are going to use is already cloned in the internal Gogs repository
and located at the following URL:</p>
</div>
<div class="paragraph">
<p>Your Gogs credentials are:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">username: user3
password: gogs</code></pre>
</div>
</div>
<div class="paragraph">
<p><a href="http://gogs-labs.apps.cluster-test-efcf.test-efcf.example.opentlc.com/user3/nationalparks.git">Gogs Repository</a></p>
</div>
<div class="paragraph">
<p>Later in the lab, we want you to make a code change and then rebuild your
application. This is a fairly simple Spring framework Java application.</p>
</div>
</div>
<div class="sect2">
<h3 id="_build_the_code_on_openshift">Build the Code on OpenShift</h3>
<div class="paragraph">
<p>Similar to how we used <strong>+Add</strong> before with an existing image, we
can do the same for specifying a source code repository. Since for this lab you
have your own git repository, let’s use it with a simple Java S2I image.</p>
</div>
<div class="paragraph">
<p>In the Developer Perspective, click <strong>+Add</strong> in the left navigation and then choose "From Git"</p>
</div>
<div class="imageblock">
<div class="content">
<img src="images/nationalparks-show-add-options.png" alt="Add to Project">
</div>
</div>
<div class="paragraph">
<p>The <strong>Import from Git</strong> workflow will guide you through the process of deploying your app based on a few selections.</p>
</div>
<div class="paragraph">
<p>Enter the following for Git Repo URL:</p>
</div>
<div class="listingblock copypaste">
<div class="content">
<pre class="highlight"><code>http://gogs-labs.apps.cluster-test-efcf.test-efcf.example.opentlc.com/user3/nationalparks.git</code></pre>
</div>
</div>
<div class="paragraph">
<p>In <strong>Git Type</strong> select <strong>Other</strong>.</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<div class="title">Note</div>
</td>
<td class="content">
if you copied the Gogs URL correctly (check spaces etc) and you still get a warning like 'Git repository is not reachable' please ignore it at this time, since the UI may fail to ping the repo sometimes.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>Select <strong>Java</strong> as your Builder Image, and be sure to select version <strong>11</strong>.</p>
</div>
<div class="imageblock">
<div class="content">
<img src="images/nationalparks-import-from-git-url-builder.png" alt="Import from Git">
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<div class="title">Note</div>
</td>
<td class="content">
All of these runtimes shown are made available via <strong>Templates</strong> and
<strong>ImageStreams</strong>, which will be discussed in a later lab.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>Scroll down to the <strong>General</strong> section. Select:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><strong>Application Name</strong> : workshop</p>
</li>
<li>
<p><strong>Name</strong> : nationalparks</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>In <strong>Resources</strong> section, select <strong>Deployment</strong>.</p>
</div>
<div class="paragraph">
<p>Expand the Labels section and add 3 labels:</p>
</div>
<div class="paragraph">
<p>The name of the Application group:</p>
</div>
<div class="listingblock copy">
<div class="content">
<pre class="highlight"><code>app=workshop</code></pre>
</div>
</div>
<div class="paragraph">
<p>Next the name of this deployment.</p>
</div>
<div class="listingblock copy">
<div class="content">
<pre class="highlight"><code>component=nationalparks</code></pre>
</div>
</div>
<div class="paragraph">
<p>And finally, the role this component plays in the overall application.</p>
</div>
<div class="listingblock copy">
<div class="content">
<pre class="highlight"><code>role=backend</code></pre>
</div>
</div>
<div class="imageblock">
<div class="content">
<img src="images/nationalparks-configure-service.png" alt="Runtimes">
</div>
</div>
<div class="paragraph">
<p>To see the build logs, in Topology view, click the <code>nationalparks</code> entry, then click on <strong>View Logs</strong> in the <strong>Builds</strong> section of the <strong>Resources</strong> tab.</p>
</div>
<div class="imageblock">
<div class="content">
<img src="images/nationalparks-java-new-java-build.png" alt="Nationalparks build">
</div>
</div>
<div class="paragraph">
<p>This is a Java-based application that uses Maven as the build and dependency system. For this reason, the initial build
will take a few minutes as Maven downloads all of the dependencies needed for
the application. You can see all of this happening in real time!</p>
</div>
<div class="paragraph">
<p>From the command line, you can also see the <strong>Builds</strong>:</p>
</div>
<div class="listingblock execute-1">
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">oc get builds</code></pre>
</div>
</div>
<div class="paragraph">
<p>You’ll see output like:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">NAME TYPE FROM STATUS STARTED DURATION
nationalparks-1 Source Git@b052ae6 Running About a minute ago 1m2s</code></pre>
</div>
</div>
<div class="paragraph">
<p>You can also view the build logs with the following command:</p>
</div>
<div class="listingblock execute-1">
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">oc logs -f builds/nationalparks-1</code></pre>
</div>
</div>
<div class="paragraph">
<p>After the build has completed and successfully:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The S2I process will push the resulting image to the internal OpenShift registry</p>
</li>
<li>
<p>The <strong>Deployment</strong> (D) will detect that the image has changed, and this
will cause a new deployment to happen.</p>
</li>
<li>
<p>A <strong>ReplicaSet</strong> (RS) will be spawned for this new deployment.</p>
</li>
<li>
<p>The RS will detect no <strong>Pods</strong> are running and will cause one to be deployed, as our default replica count is just 1.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>In the end, when issuing the <code>oc get pods</code> command, you will see that the build Pod
has finished (exited) and that an application <strong>Pod</strong> is in a ready and running state:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">NAME READY STATUS RESTARTS AGE
nationalparks-1-tkid3 1/1 Running 3 2m
nationalparks-1-build 0/1 Completed 0 3m
parksmap-57df75c46d-xltcs 1/1 Running 0 2h</code></pre>
</div>
</div>
<div class="paragraph">
<p>If you look again at the web console, you will notice that, when you create the
application this way, OpenShift also creates a <strong>Route</strong> for you. You can see the
URL in the web console, or via the command line:</p>
</div>
<div class="listingblock execute-1">
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">oc get routes</code></pre>
</div>
</div>
<div class="paragraph">
<p>Where you should see something like the following:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">NAME HOST/PORT PATH SERVICES PORT TERMINATION WILDCARD
nationalparks nationalparks-user3.apps.cluster-test-efcf.test-efcf.example.opentlc.com nationalparks 8080-tcp
parksmap parksmap-user3.apps.cluster-test-efcf.test-efcf.example.opentlc.com parksmap 8080-tcp edge none</code></pre>
</div>
</div>
<div class="paragraph">
<p>In the above example, the URL is:</p>
</div>
<div class="listingblock copypaste">
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">http://nationalparks-user3.apps.cluster-test-efcf.test-efcf.example.opentlc.com</code></pre>
</div>
</div>
<div class="paragraph">
<p>Since this is a backend application, it doesn’t actually have a web interface.
However, it can still be used with a browser. All backends that work with the parksmap
frontend are required to implement a <code>/ws/info/</code> endpoint. To test, visit this URL in your browser:</p>
</div>
<div class="paragraph">
<p><a href="http://nationalparks-user3.apps.cluster-test-efcf.test-efcf.example.opentlc.com/ws/info/">National Parks Info Page</a></p>
</div>
<div class="admonitionblock warning">
<table>
<tr>
<td class="icon">
<div class="title">Warning</div>
</td>
<td class="content">
The trailing slash is <strong>required</strong>. If the Pod is Running and the application is not available, please wait a few seconds and retry since we haven’t configured yet Health Checks for that.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>You will see a simple JSON string:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-json" data-lang="json">{"id":"nationalparks","displayName":"National Parks","center":{"latitude":"47.039304","longitude":"14.505178"},"zoom":4}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Earlier we said:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">This is a Java Spring Boot application that performs 2D geo-spatial queries
against a MongoDB database</code></pre>
</div>
</div>
<div class="paragraph">
<p>But we don’t have a database. Yet.</p>
</div>
</div>
</div>
</div>
<!-- Bottom Navigation -->
<div class="page-meta clearfix">
<input type="button" class="btn btn-lg btn-primary float-right" onclick="location.href='nationalparks-java-databases';" value="Continue" />
</div>
</section>
</div>
</div>
</div>
<!-- Footer -->
<footer class="footer">
</footer>
<!-- Javascript-->
<script src="js/workshop.js"></script>
<script src="js/workshop-asciidoc.js"></script>
</body>
</html>