Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Set Up CI/CD for a Distributed Crossword Puzzle App on Kubernetes (Part 4)

#1
Set Up CI/CD for a Distributed Crossword Puzzle App on Kubernetes (Part 4)

<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/10/set-up-ci-cd-for-a-distributed-crossword-puzzle-app-on-kubernetes-part-4.png" width="256" height="256" title="" alt="" /></div><div><p><span><span>Part 3 had us running our Kr8sswordz Puzzle app, spinning up multiple instances for a load test, and watching Kubernetes gracefully balance numerous requests across the cluster. </span></span></p>
<p><span><span>Though we set up Jenkins for use with our Hello-Kenzan app in Part 2, we have yet to set up CI/CD hooks for the Kr8sswordz Puzzle app. Part 4 will walk through this set up. We will use a Jenkins 2.0 Pipeline script for the Kr8sswordz Puzzle app, this time with an important difference: triggering builds based on an update to the forked Git repo. The walkthrough will simulate updating our application with a feature change by pushing the code to Git, which will trigger the Jenkins build process to kick off. In a real world lifecycle, this automation enables developers to simply push code to a specific branch to then have their app build, push, and deploy to a specific environment.</span></span></p>
<p><em>Read the previous articles in the series:</em></p>
<div> </p>
<div>
<table>
<tbody>
<tr>
<td><span><img alt="6yke0wMHy6bUkeiH9ny2ih2c70XRtVHjvJ-6zScz" height="20" src="http://www.sickgaming.net/blog/wp-content/uploads/2018/10/set-up-ci-cd-for-a-distributed-crossword-puzzle-app-on-kubernetes-part-4.png" width="20" /></span></td>
<td>
<p><span><span>This tutorial only runs locally in Minikube and will not work on the cloud. You’ll need a computer running an up-to-date version of Linux or macOS. Optimally, it should have 16 GB of RAM. Minimally, it should have 8 GB of RAM. For best performance, reboot your computer and keep the number of running apps to a minimum. </span></span></p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<h2><span><span>Creating a Kr8sswordz Pipeline in Jenkins </span></span></h2>
<p><span><span>Before you begin, you’ll want to make sure you’ve run through the steps in </span><span>Parts 1,</span><span> </span><span>2</span><span>, and </span><span>3</span><span> so that you have all the components we previously built in Kubernetes (to do so quickly, you can run the </span><a href="https://docs.google.com/document/d/16KGjRwQ6FKYTM_6b8DAbjO1wbFi02yY-2fQjqtBBiSg/edit#heading=h.wzk28gyisgzi"><span>automated scripts detailed below</span></a><span>). For this tutorial we are assuming that Minikube is still up and running with all the pods from Part 3. </span></span></p>
<p><span><span>We are ready to create a new pipeline specifically for the puzzle service. This will allow us to quickly re-deploy the service as a part of CI/CD. </span></span></p>
<p><span><span>1. Enter the following terminal command to open the Jenkins UI in a web browser. Log in to Jenkins using the username and password you previously set up. </span></span></p>
<pre>
<span><span>minikube service jenkins</span></span></pre>
<p><span><span>2. We’ll want to create a new pipeline for the </span><span>puzzle </span><span>service that we previously deployed. On the left in Jenkins, click </span><span>New Item</span><span>.</span></span></p>
<p><span><span><img alt="LpJOpWJv-sFIXvfkZjvKhw_nZpmKtFpCT7wAmuQ5" height="191" src="http://www.sickgaming.net/blog/wp-content/uploads/2018/10/set-up-ci-cd-for-a-distributed-crossword-puzzle-app-on-kubernetes-part-4-1.png" width="243" /></span></span></p>
<div>
<div>
<table>
<tbody>
<tr>
<td><span><img alt="-ae-tsBuoj7wShvgYZQiF9eibNPb4G0kOBvWzO6P" height="20" src="http://www.sickgaming.net/blog/wp-content/uploads/2018/10/set-up-ci-cd-for-a-distributed-crossword-puzzle-app-on-kubernetes-part-4.png" width="20" /></span></td>
<td>
<p><span><span>For simplicity we’re only going to create a pipeline for the puzzle service, but we’ve  provided Jenkinsfiles for all the rest of the services, in order to allow the application to be fully CI/CD capable. </span></span></p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p><span><span>3. Enter the item name as </span><strong><span>Puzzle-Service</span></strong><span>, click </span><strong><span>Pipeline</span></strong><span>, and click<strong> </strong></span><strong><span>OK</span></strong><span>.</span></span></p>
<p><span><span><img alt="q4S5cCP3PL1uVKn4tWUNSYQ4a1WJ2VQaMJJYz1kQ" height="383" src="http://www.sickgaming.net/blog/wp-content/uploads/2018/10/set-up-ci-cd-for-a-distributed-crossword-puzzle-app-on-kubernetes-part-4-2.png" width="428" /></span></span></p>
<p><span><span>4. Under the </span><strong><span>Build Triggers</span></strong><span> section, select </span><span><strong>Poll SCM</strong>. </span><span>For the </span><strong><span>Schedule</span></strong><span><strong>,</strong> enter the string </span><span><strong>H/5 * * * *</strong> </span><span>which will poll the Git repo every 5 minutes for changes. </span></span><br class="kix-line-break" /><span><span><img alt="UvhE3i-J2RwjoZnVFR3622HsnMnL_aJ9coBwOF-O" height="186" src="http://www.sickgaming.net/blog/wp-content/uploads/2018/10/set-up-ci-cd-for-a-distributed-crossword-puzzle-app-on-kubernetes-part-4-3.png" width="551" /></span></span></p>
<p>5. In the <strong>Pipeline </strong>section, change the following.</p>
<p>  a. <strong>Definition:</strong> Pipeline script from SCM</p>
<p>  b. <strong>SCM:</strong><strong> </strong>Git</p>
<p>  c. <strong>Repository URL:</strong> Enter the URL for your forked Git repository</p>
<p>  d. <strong>Script Path:</strong> applications/puzzle/Jenkinsfile</p>
<p><span><span><img alt="QCeR2WtIvsJ0Mj5X5oEnb65WbDNY_RwafhaiVBj2" height="457" src="http://www.sickgaming.net/blog/wp-content/uploads/2018/10/set-up-ci-cd-for-a-distributed-crossword-puzzle-app-on-kubernetes-part-4-4.png" width="624" /></span></span></p>
<div>
<div>
<table>
<tbody>
<tr>
<td><span><img alt="-ae-tsBuoj7wShvgYZQiF9eibNPb4G0kOBvWzO6P" height="20" src="http://www.sickgaming.net/blog/wp-content/uploads/2018/10/set-up-ci-cd-for-a-distributed-crossword-puzzle-app-on-kubernetes-part-4.png" width="20" /></span></td>
<td>
<p><span><span>Remember in Part 3 we had to manually replace the $BUILD_TAG env var with the git commit ID? The Kubernetes Continuous Deploy plugin we’re using in Jenkins will automatically find variables in K8s manifest files ($VARIABLE or ${VARIABLE})  and replace them with environment variables pre-configured for the pipeline in the Jenkinsfile. Variable substitution is a functionality Kubernetes lacks in v1.11.0, however the Kubernetes CD plugin as a third party tool provides us with it.</span></span></p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p><span><span>6. When you are finished, click </span><strong><span>Save</span></strong><span>. On the left, click </span><strong><span>Build Now</span></strong><span> to run the new pipeline. This will rebuild the image from the registry, and redeploy the puzzle pod. You should see it successfully run through the build, push, and deploy steps in a few minutes.</span></span></p>
<p><span><span>Our Puzzle-Service pipeline is now setup to poll the Git repo for changes every 5 minutes and kick off a build if changes are detected.</span></span></p>
<p><h2>Pushing a Feature Update Through the Pipeline</h2>
</p>
<p><span><span>Now let’s make a single change that will trigger our pipeline and rebuild the puzzle-service. </span></span></p>
<p><span><span>On our current Kr8sswordz Puzzle app, hits against the </span><strong><span>puzzle</span></strong><span><strong> </strong>services show up as white in the UI when pressing </span><strong><span>Reload</span></strong><span><strong> </strong>or performing a </span><strong><span>Load Test</span></strong><span><strong>:</strong> </span></span><br class="kix-line-break" /><span><span><img alt="GmeeT13FUZXYN7OByHpxqhhnPed40_3HGa78i5nN" height="182" src="http://www.sickgaming.net/blog/wp-content/uploads/2018/10/set-up-ci-cd-for-a-distributed-crossword-puzzle-app-on-kubernetes-part-4-5.png" width="488" /></span></span></p>
<p><span><span>However, you may have seen that the same white hit does </span><strong><span>not</span></strong><span> light up when clicking the </span><strong><span>Submit</span></strong><span><strong> </strong>button. We are going to remedy this with an update to the code.  </span></span></p>
<p><span><span>7. In a terminal, open up the Kr8sswordz Puzzle app. (If you don’t see the puzzle, you might need to refresh your browser.)</span></span></p>
<pre>
<span><span>minikube service kr8sswordz</span></span></pre>
<p><span><span>8. Spin up several instances of the </span><strong><span>puzzle</span></strong><span> service by moving the slider to the right and clicking </span><strong><span>Scale</span></strong><span><strong>.</strong> For reference, click </span><strong><span>Submit</span></strong><span> and notice that the white hit does not register on the puzzle services.</span></span></p>
<div>
<div>
<table>
<tbody>
<tr>
<td><span><img alt="d-kHXuxVhttDXD2kzYzDAp1L3K2woqa2pO94Inls" height="20" src="http://www.sickgaming.net/blog/wp-content/uploads/2018/10/set-up-ci-cd-for-a-distributed-crossword-puzzle-app-on-kubernetes-part-4.png" width="20" /></span></td>
<td>
<p><span><span>If you did not allocate 8 GB of memory to Minikube, we suggest not exceeding 6 scaled instances using the slider.</span></span></p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p><span><span>9. Edit </span><strong><span>applications/puzzle/common/models/crossword.js</span></strong><span> in your favorite text editor, or edit it in nano using the commands below. </span></span></p>
<pre>
<span><span>cd ~/kubernetes-ci-cd</span></span>
<span><span>nano applications/puzzle/common/models/crossword.js</span></span></pre>
<p><span><span>You’ll see the following commented section on lines 42-43: </span></span></p>
<pre>
<span><span>// Part 4: Uncomment the next line to enable puzzle pod</span></span>
<span><span> highlighting when clicking the Submit button</span></span>
<span><span>//fireHit();</span></span></pre>
<p><span><span>Uncomment line 43 by deleting the forward slashes, then save the file. (In nano, Press </span><strong><span>Ctrl+X</span></strong><span><strong> </strong>to close the file, type </span><strong><span>Y</span></strong><span> to confirm the filename, and press </span><strong><span>Enter</span></strong><span> to write the changes to the file.) </span></span></p>
<p><span><span>10. Commit and push the change to your forked Git repo (you may need to enter your GitHub credentials):</span></span></p>
<pre>
<span><span>git commit -am "Enabled hit highlighting on Submit"</span></span>
<span><span>git push</span></span></pre>
<p><span><span>11. In Jenkins, open up the Puzzle-Service pipeline and wait until it triggers a build. It should trigger every 5 minutes. (If it doesn’t trigger right away, give it some time.)</span></span></p>
<p><span><span>12. After it triggers, observe how the puzzle services disappear in the Kr8sswordz Puzzle app, and how new ones take their place. </span></span></p>
<p><span><span>13. Try clicking </span><span>Submit</span><span> to test that hits now register as white.</span></span></p>
<p><span><span>If you see one of the puzzle instances light up, it means you’ve successfully set up a CI/CD pipeline that automatically builds, pushes, and deploys code changes to a pod in Kubernetes. It’s okay—go ahead and bask in the glory for a minute.</span></span></p>
<div><span><span><img alt="PUbF_s1wwsHhk_vdpiAb2kS_pTtQcdzciITDODih" height="295" src="https://lh5.googleusercontent.com/PUbF_s1wwsHhk_vdpiAb2kS_pTtQcdzciITDODihEHhCDfxYbr48ek4c3EOHvmIurAKz4P0ZZqHufviD5GXgiR945zBdfX4vTX5V2stnMEr6xUrKF4cRuTtuJYAH7esq3DWXQOYB" width="568" /></span></span></div>
<p><span><span>You’ve completed Part 4 and finished </span><a href="http://kenzan.io/"><span>Kenzan’s</span></a><span> blog series on CI/CD with Kubernetes!</span></span></p>
<p><span><span>From a development perspective, it’s worth mentioning a few things that might be done differently in a real-world scenario with our pipeline: </span></span></p>
<ul>
<li>
<p><span><span>You would likely have separate repositories for each of the services that compose the Kr8sswordz Puzzle to enforce separation for microservice develop/build/deploy. Here we’ve combined all services in one repo for ease of use with the tutorial. </span></span></p>
</li>
<li>
<p><span><span>You would also set up individual pipelines for the<strong> </strong></span><strong><span>monitor-scale</span></strong><span><strong> </strong>and </span><span><strong>kr8sswordz</strong> </span><span>services. Jenkins files for these services are actually included in the repository, though for the purpose of the tutorial we’ve kept things simple with a single pipeline to demonstrate CI/CD. </span></span></p>
</li>
<li>
<p><span><span>You would likely set up separate pipelines for each deployment environment, such as Dev, QA, Stage, and Prod environments. For triggering builds for these environments, you could use different Git branches that represent the environments you push code to. (For example, dev branch &gt; deploy to Dev, master branch &gt; deploy to QA, etc.) </span></span></p>
</li>
<li>
<p><span><span>Though easy to set up, the SCM Polling operation is somewhat resource intensive as it requires Jenkins to scan the entire repo for changes. An alternative is to use the </span><a href="https://help.github.com/enterprise/2.9/admin/guides/developer-workflow/continuous-integration-using-jenkins/"><span>Jenkins Github plugin on your Jenkins server</span></a><span>. </span></span></p>
</li>
</ul>
<div>
<table>
<tbody>
<tr>
<td>
<h3><span><span>Automated Scripts</span></span></h3>
<p><span><span>If you need to walk through the steps we did again (or do so quickly), we’ve provided npm scripts that will automate running the same commands in a terminal.  </span></span></p>
<ol>
<li>
<p><span><span>To use the automated scripts, you’ll need to install NodeJS and npm. </span></span></p>
</li>
</ol>
<p><span><span>On </span><strong><span>Linux</span></strong><span>, follow the </span><a href="https://nodejs.org/en/download/package-manager/"><span>NodeJS installation steps</span></a><span> for your distribution. To quickly install NodeJS and npm on Ubuntu 16.04 or higher, use the following terminal commands. </span></span></p>
<pre>
<span><span>a. curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -</span></span></pre>
<pre>
<span><span>b. sudo apt-get install -y nodejs</span></span></pre>
<p><span><span>On </span><strong><span>macOS</span></strong><span>, </span><a href="https://nodejs.org/en/"><span>download the NodeJS installer</span></a><span>, and then double-click the .pkg file to install NodeJS and npm.</span></span></p>
<p><span><span>2. Change directories to the cloned repository and install the interactive tutorial script:</span></span></p>
<pre>
a. <span><span>cd ~/kubernetes-ci-cd</span></span> <span><span>b. npm install</span></span></pre>
<p><span><span>3. Start the script</span></span></p>
<pre>
<span><span>npm run part1 (or part2, part3, part4 of the blog series) </span></span></pre>
<p><span><span><span><span>4. Press </span><strong><span>Enter</span></strong><span><strong> </strong>to proceed running each command. </span></span></span></span></p>
</td>
</tr>
</tbody>
</table>
</div>
<div>
<p><h2>Going Deeper</h2>
</p>
</div>
<p><span><span>Building the Kr8sswordz Puzzle app has shown us some pretty cool continuous integration and container management patterns: </span></span></p>
<ul>
<li>
<p><span><span>How infrastructure such as Jenkins or image repositories can run as pods in Kubernetes.</span></span></p>
</li>
<li>
<p><span><span>How Kubernetes handles scaling, load balancing, and automatic healing of pods. </span></span></p>
</li>
<li>
<p><span><span>How Jenkin’s 2.0 Pipeline scripts can be used to automatically run on a Git commit to build the container image, push it to repository, and deploy it as a pod in Kubernetes. </span></span></p>
</li>
</ul>
<p><span><span>If you are interested in going deeper into the CI/CD Pipeline process with deployment tools like Spinnaker, see Kenzan’s paper </span><a href="http://techblog.kenzan.com/image-everything-continuous-delivery-spinnaker-kubernertes/"><span>Image is Everything: Continuous Delivery with Kubernetes and Spinnaker</span></a><span>.</span></span><span><span> </span></span></p>
<p><em><a href="http://kenzan.com/?ref=linux">Kenzan</a> is a software engineering and full service consulting firm that provides customized, end-to-end solutions that drive change through digital transformation. Combining leadership with technical expertise, Kenzan works with partners and clients to craft solutions that leverage cutting-edge technology, from ideation to development and delivery. Specializing in application and platform development, architecture consulting, and digital transformation, Kenzan empowers companies to put technology first.</em></p>
<p><em>This article was revised and updated by David Zuluaga, a front end developer at Kenzan. He was born and raised in Colombia, where he studied his BE in Systems Engineering. After moving to the United States, he studied received his master’s degree in computer science at Maharishi University of Management. David has been working at Kenzan for four years, dynamically moving throughout a wide range of areas of technology, from front-end and back-end development to platform and cloud computing. David’s also helped design and deliver training sessions on Microservices for multiple client teams.</em></p>
<p><em>Curious to learn more about Kubernetes? <a href="https://www.edx.org/course/introduction-kubernetes-linuxfoundationx-lfs158x">Enroll in Introduction to Kubernetes</a>, a FREE training course from The Linux Foundation, hosted on edX.org.</em></p>
</div>
Reply



Forum Jump:


Users browsing this thread:
3 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016