<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by César Tron-Lozai on Medium]]></title>
        <description><![CDATA[Stories by César Tron-Lozai on Medium]]></description>
        <link>https://medium.com/@cesar.tronlozai?source=rss-d7cbf6ac998a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*G8N29qjlftoh-1Xw</url>
            <title>Stories by César Tron-Lozai on Medium</title>
            <link>https://medium.com/@cesar.tronlozai?source=rss-d7cbf6ac998a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 14:34:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cesar.tronlozai/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Blazing fast Fibonacci with Kotlin and Arrow library]]></title>
            <link>https://itnext.io/blazing-fast-fibonacci-with-kotlin-and-arrow-library-33c1d7eca0bb?source=rss-d7cbf6ac998a------2</link>
            <guid isPermaLink="false">https://medium.com/p/33c1d7eca0bb</guid>
            <category><![CDATA[fibonacci]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[kotlin-extension]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[matrix]]></category>
            <dc:creator><![CDATA[César Tron-Lozai]]></dc:creator>
            <pubDate>Fri, 15 May 2020 12:56:26 GMT</pubDate>
            <atom:updated>2020-05-17T16:57:29.016Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/507/1*dxX9TcMVVg61x6jKaOOFHg.png" /></figure><h3>TLDR;</h3><p>In this article, I show you how to compute Fibonacci numbers at blazing speed by leveraging both the <a href="https://arrow-kt.io/">Arrow</a> functional library for Kotlin and the ‘exponentiation by squaring algorithm’.</p><p>This approach is heavily inspired by this <a href="http://www.haskellforall.com/2020/04/blazing-fast-fibonacci-numbers-using.html">Haskell For All article</a>. You can find the code <a href="https://github.com/cesartl/arrow-fibonacci">here</a>.</p><h3><strong>Fibonacci Sequence</strong></h3><p>If you are a programer, chances are you have come across the Fibonacci sequence more than once but here is a quick reminder anyway.</p><p>The sequence is defined as follows:</p><pre>F(0) = 0<br>F(1) = 1<br>F(n) = F(n-1) + F(n-2), n &gt; 1</pre><h4>Double recursive implementation</h4><p>The simplest way to implement this sequence is with a doubly recursive function. For example, in Kotlin:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bfe9c573bf00600bee2bda884363ba01/href">https://medium.com/media/bfe9c573bf00600bee2bda884363ba01/href</a></iframe><p>This is a simple approach but extremely inefficient; the time complexity is O(2^n).</p><p>This can be improved to O(n) by memoizing the two previous values. Let’s see if we can do better.</p><h4>Closed-Form</h4><p>It turns out there is a mathematical formula that can compute any Fibonacci number in O(1)time and memory complexity:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7P7G5YrYl_4bkiMPfPRwgw.png" /></figure><blockquote>(You can see here why there is a link between Fibonacci numbers and the golden ratio 𝝋: if you divide <em>f(n+1)/f(n)</em> it converges to 𝝋.)</blockquote><p>Even though this is really fast in theory, it does not work in practice because of the floating precision arithmetic.</p><h4>Matrix form</h4><blockquote>Don’t be put off by the fancy looking maths, it will get much simpler when we get to the code.</blockquote><p>You can also express recursive sequences like Fibonacci using matrices.</p><p>For example for:</p><pre>f(n) = a.f(n-1) + b.f(n-2)</pre><p>You can write:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CY6davgRqNcR3OfkzDbCxQ.png" /></figure><p>The conditions:</p><pre>f(0) = ⍺<br>f(1) = β</pre><p>become:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/197/1*B4BtRn1Xp-HfzlbaIf_vPg.png" /></figure><p>So eventually you get:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zs3-JscxH308-v5Kh3CFDw.png" /></figure><p>or:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/436/1*wMOGxoRyR4n4rxLjamrQYw.png" /></figure><blockquote><em>Note that this works for any linear recursive sequence where f(n) depends on f(n-1), f(n-2),…,f(n-m). Then you get a </em>mxm<em> matrice.</em></blockquote><p>So our Fibonacci expression becomes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/437/1*N7-oNhAF4lLyt7Cb2KUQgw.png" /></figure><p>Don’t worry if you don’t fully understand these matrix operations, it’s actually quite straightforward to turn into code.</p><h3>Exponentiation by Squaring</h3><p>The beautiful part of the formula above is that all the work is done by the matrix multiplication. If you have a binary operation of the form <em>x⊗ y</em> and you want to compute <em>x⊗ x⊗…⊗ x, n </em>times, you can use <a href="https://en.wikipedia.org/wiki/Exponentiation_by_squaring">Exponentiation by Squaring</a>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/966/1*5xfnt28HI5k4EdTr3RxR7g.png" /></figure><p>Intuitively, you can define the operation pow(x,n) recursively:</p><pre>pow(x,n) = <br>   if(n == 0)      empty()<br>   else if(n even) pow(x<em>⊗</em>x, n/2)<br>   else            x<em>⊗</em>pow(x<em>⊗</em>x, (n-1)/2)</pre><p>We also need a neutral element here called empty() . For natural numbers this is just 1 .</p><p>This algorithm keeps dividing n by <em>2 </em>so the time complexity is O(log₂(n)).</p><h3>Enters Arrow</h3><p>Exponentiation by squaring doesn’t just work for integer multiplication, it works for any type that has a binary operation and neutral value for that operation (1 in the case of natural numbers). So in the Java world we could have an interface like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/75fab58378b9499ec362e0af8c6692e4/href">https://medium.com/media/75fab58378b9499ec362e0af8c6692e4/href</a></iframe><p>In the functional world, there is something called <a href="https://en.wikipedia.org/wiki/Type_class">Type Classes</a>. There is a set of standard type classes and <a href="https://arrow-kt.io/docs/0.10/typeclasses/intro/">they are shipped with Arrow</a>, a functional library for Kotlin.</p><h4>Semi-group</h4><p>The type class that defines a binary operation for a type is called <a href="https://arrow-kt.io/docs/0.10/arrow/typeclasses/semigroup/">Semigroup</a>. In Kotlin, it would look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f0cc66549eb7cafd8b411d5dd9709cb4/href">https://medium.com/media/f0cc66549eb7cafd8b411d5dd9709cb4/href</a></iframe><p>The interesting bit about the Kotlin implementation, is that it uses the <a href="https://kotlinlang.org/docs/reference/extensions.html">extension functions in Kotlin</a>. In the Java world if we had a Foo class, it would be extending a Semigroup interface like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0566cac229473c363541acfa5100b1ae/href">https://medium.com/media/0566cac229473c363541acfa5100b1ae/href</a></iframe><p>In Kotlin, we wouldn’t make Foo extend an interface, we would create a new singleton class called FooSemigroup, which provides an extension function for Foo :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ddd6508d15ab6088bdc920efe4468e1f/href">https://medium.com/media/ddd6508d15ab6088bdc920efe4468e1f/href</a></iframe><p>So, if we had two foo instances:</p><pre>val foo1 = Foo()<br>val foo2 = Foo()</pre><pre>val foo3 = foo1.combine(foo2)    //this does not compile</pre><pre>val foo3 = FooSemigroup().run {<br>   foo1.combine(foo2)            //this compiles<br>}</pre><p>You see that we have to run the code within the context of FooSemigroup for the Foo class to have the combine method.</p><p>Next, we need the ability for the type class to return an ‘empty’ value that is the identity for the combine method. Luckily there is already a type class for that.</p><h4>Monoid</h4><p>Monoid extends Semigroup and adds an empty element:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e164588f0eba82105d58e6c873c43ea3/href">https://medium.com/media/e164588f0eba82105d58e6c873c43ea3/href</a></iframe><h4>Exponentiation by squaring in Kotlin</h4><p>Now we can implement Exponentiation by Squaring for any Monoid because all we need is empty and combine . Here I use a non recursive implementation because it is a bit easier to understand than the ‘safe’ (i.e. tail-recursive) recursive implementations:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/404cf452b2a582dde505d3914deb22b2/href">https://medium.com/media/404cf452b2a582dde505d3914deb22b2/href</a></iframe><h3>Fast Fibonacci</h3><p>We are getting close to implementing our fast Fibonacci algorithm. But first we need to implement a Matrix22 class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e0f2fd468fb163486fb29ebff91c5a63/href">https://medium.com/media/e0f2fd468fb163486fb29ebff91c5a63/href</a></iframe><p>This is just a class holding 4 fields, one for each element of a 2 by 2 matrix:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/159/1*NJzVb4utK0DdGdkG0bxVSg.png" /></figure><p>Then we need to implement a Monoid for Matrix . For this we need to know how to multiply two 2x2 matrices. If you are not familiar with this type of mathematics don’t worry, this is just a formula you can find in different places online (like <a href="https://www.mathsisfun.com/algebra/matrix-multiplying.html">here</a>).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38c92777549a74db01b6d5a11809ef04/href">https://medium.com/media/38c92777549a74db01b6d5a11809ef04/href</a></iframe><p>The last bit at the end is just a Kotlin trick, so we can access that monoid class easily.</p><p>Then we can finally implement Fibonacci using the formula I mentioned earlier:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/437/1*N7-oNhAF4lLyt7Cb2KUQgw.png" /></figure><p>To get F(n) we just need to multiply the Fibonacci matrix n times using exponentiation by squaring. Let’s say</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/542/1*Xw9q_EcHgNcrz7MFf_MptQ.png" /></figure><p>Then we have:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PGHawdoxAAkwZ7HsLEYZEw.png" /></figure><p>Simply put,F(n) = b where b is the x01 element of the result of the exponentiation by squaring:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c4c95895d0156ca3811515bc4d3a0db/href">https://medium.com/media/5c4c95895d0156ca3811515bc4d3a0db/href</a></iframe><h4>Results</h4><p>I’ve done a quick benchmark: first column is n , second is the time it took to compute F(n) in ms, and last is the number of bits it takes to store the result:</p><pre>n,       time, bits<br>100000,  53,   69424<br>200000,  39,   138848<br>400000,  118,  277696<br>800000,  263,  555393<br>1600000, 516,  1110786</pre><p>You can see that this algorithm computes extremely large numbers of n and it certainly takes less time to compute the Fibonacci number than it would to print it in the console!</p><h3>Complexity</h3><p>Now we can have a look at the time complexity.</p><h4>“Linear” implementation</h4><p>At the beginning of the article, I mentioned that one can write a linear implementation for Fibonacci by simply remembering the last 2 values. This can be done like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b5bcb0fb37b6a035b8f585bc9aea4629/href">https://medium.com/media/b5bcb0fb37b6a035b8f585bc9aea4629/href</a></iframe><p>I said this was ‘linear’ because the loop has n steps, however, we need to look at how much work is done for each step. Here the work is done by additions and subtractions which, for BigInteger , have linear complexity. Therefore, the final complexity for the ‘linear’ implementation is O(n²) . This is consistent with a quick benchmark I did for this implementation: when I double n the total time is multiplied by 3.9</p><h4>“Logarithmic” implementation</h4><p>Exponentiation by squaring will execute log₂(n) steps, so perhaps we could call this the <em>logarithmic</em> implementation. However, like with the previous example, we need to see how much work is actually done in each step.</p><p>Each step involves a series of multiplications and additions. Additions have linear time complexity, however multiplications, depending on how it is implemented, can be as high asO(n²). I found this <a href="https://www.javaspecialists.eu/archive/Issue236.html">article</a> from Heinz Kabutz claiming that before Java 8 the multiplication was done in O(n²) but now multiplication is implemented by two faster algorithms in O(n^1.585) and O(n^1.465) .</p><p>Let’s say for sake of argument that multiplication is done in O(n^k) for some 1.4 &lt; k &lt; 1.6 . We can assume that at every step of the exponentiation by squaring algorithm the size of the numbers doubles. This means that at the i-th step the amount of work required is (2^i)^k . Formally, the total amount of work for all log(n) steps is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/328/1*1ksQSmnbSelkCxgND-KqIw.png" /></figure><p>In the sum above, we only care about the most dominant term (the last one), so we have:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*At6rsfCud1AloKSxMvHLLw.png" /></figure><p>I’ve done the same quick benchmark where I double n at each round and I measured that on average, the total time is multiplied by <em>2.79</em> (x2 would be O(n) and x4 would be O(n²) ). This corresponds to k=1.48, which is consistent with Heinz’s article.</p><p>Below is a quick benchmark I did comparing the “linear” (actually O(n²)) and the “logarithmic” (actually ~O(n^1.48)):</p><pre>n,linear(ms),logarithmic(ms)<br>40000,40,1<br>80000,146,2<br>160000,565,7<br>320000,2325,20<br>640000,8811,69</pre><h3>Exponentiation by squaring — Other applications</h3><p>You can reuse exponentiation by squaring any time you need to call a binary function multiple times.</p><p>I was curious about how BigInteger implements its pow method but I wasn’t surprised to see it used exponentiation by squaring. Don’t believe me check the source ;)</p><p>The first time I had to use exponentiation by squaring, was for <a href="https://adventofcode.com/2019/day/22">Advent of Code 2019 Day 22</a>. If you don’t know AOC, I strongly encourage you to do as many puzzles as you can, it taught me a lot of interesting algorithms and other programming tricks. Just a word of warning about 2019 Day 22, this was for me the most challenging puzzle of all 250 puzzles available at time of writing. This was because I didn’t know <a href="https://en.wikipedia.org/wiki/Modular_arithmetic">modular arithmetic</a> well but now I know the basics so I strongly recommend you give it a go :)</p><p>If you have come across interesting use cases for exponentiation by squaring please let me know in the comments :)</p><p>I hope you enjoyed this article. You can find the code <a href="https://github.com/cesartl/arrow-fibonacci">here</a>. If you enjoyed this consider following me on Twitter</p><p><a href="https://twitter.com/@cesarTronLozai">Cesar Tron-Lozai</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=33c1d7eca0bb" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/blazing-fast-fibonacci-with-kotlin-and-arrow-library-33c1d7eca0bb">Blazing fast Fibonacci with Kotlin and Arrow library</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How my Raspberry Pi listens to the stars]]></title>
            <link>https://itnext.io/how-my-raspberry-pi-listens-to-the-stars-538aa5f77e87?source=rss-d7cbf6ac998a------2</link>
            <guid isPermaLink="false">https://medium.com/p/538aa5f77e87</guid>
            <category><![CDATA[seti]]></category>
            <category><![CDATA[raspberry-pi]]></category>
            <category><![CDATA[mad-science]]></category>
            <category><![CDATA[slow-journalism]]></category>
            <dc:creator><![CDATA[César Tron-Lozai]]></dc:creator>
            <pubDate>Sun, 17 Mar 2019 16:08:46 GMT</pubDate>
            <atom:updated>2020-05-25T10:26:41.297Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1021/1*cnqL4VSvjmd0xPUWHr22_A.jpeg" /></figure><blockquote>TL;DR: I share how you can lend spare computing resources — such as a Raspberry Pi — to the global scientific community; using the opportunity to dabble along the way.</blockquote><h3>Single Board Computer</h3><p>Single Board Computers (<strong>SBC</strong>) such as <a href="https://www.raspberrypi.org/">Raspberry Pi</a> have become really popular in the last 10 years. They are relatively <strong>cheap</strong>, small, <strong>power efficient</strong> and provide a fully functioning Linux or Android computer. They are often used for education purposes, research, tinkering or used to build smart houses.</p><p>I recently bought 2 SBCs, a <a href="https://www.raspberrypi.org/products/raspberry-pi-3-model-b-plus/">Raspberry Pi 3 Model B+</a> and an <a href="https://www.asus.com/uk/Single-Board-Computer/Tinker-Board-S/overview/">Asus Tinker Board S</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/462/1*ECTuMUZoVf_AZx7VCKsGfQ.jpeg" /><figcaption>Raspberry Pi 3B+</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/302/1*im71X9dFRgvBclBHzwUpfw.jpeg" /><figcaption>Asus Tinker Board S</figcaption></figure><p>I bought them for a project which was quickly abandoned 🤷‍♂️.</p><p>Fortunately, I shortly came upon an article on the Search for Extra-Terrestrial Intelligence (SETI) in an edition of <a href="https://www.slow-journalism.com/">Delayed Gratification</a>.</p><h3>SETI</h3><p>The concept is relatively simple, large radio-telescopes are pointed at the sky in the hope of <strong>detecting radio signals that would have been sent by extra-terrestrial intelligence</strong>. It is pretty certain there is no other intelligent life capable of producing radio signals anywhere near our sun. Consequently, it is assumed that, if they exist, <strong>those signals would be pretty weak</strong>. As a result, it takes a lot of processing power to meticulously comb through this data for any trace of a weak radio pulse.</p><h4>SETI@HOME</h4><p>The <a href="https://setiathome.berkeley.edu/">SETI@HOME</a> project was born out of the dire realisation that <strong>no university or institute in the world would be able to afford</strong> <strong>the supercomputer</strong> required to process all this data.</p><blockquote>What if it were possible to use some of the thousands of idle computers from the public?</blockquote><p>I recently read about modern data centres used by the likes of Google, Amazon and Microsoft [1]. The sort of workload they run is typically <strong>data heavy</strong>, meaning the bottleneck is often around I/O (network, disk, RAM) rather than pure CPU power. This means both hardware and software engineers must work together to <strong>minimise the amount of data transfered between machines</strong> or make sure such traffic is as local as possible.</p><blockquote>This sort of workload would be impractical for a grid of small computers distributed on the internet.</blockquote><p>Luckily, the processing involved in SETI@HOME is <strong>not data-heavy and is relatively easy to parallelise</strong>. A radio signal is split into small chunks of data (&lt; 0.5 MB) which can be sent to a computer to perform analysis (which typically takes hours depending on how powerful the computer is). Once done the results are sent back and aggregated by the SETI@HOME team. You can find out the details of how it works <a href="http://seticlassic.ssl.berkeley.edu/about_seti/about_seti_at_home_1.html">here</a>.</p><blockquote>Sold? How do I participate?</blockquote><h3>BOINC</h3><p>The <a href="https://boinc.berkeley.edu/">Berkeley Open Infrastructure for Network Computing</a> (BOINC) is a program that<strong> allows volunteers to lend their computing power for science</strong> (multiple projects, not just SETI).</p><p>It’s quite straightforward to install on a Raspberry Pi or the Asus Tinker Board. They both run a variation of the Debian Linux distribution and BOINC can be installed using the standard package manager:</p><pre>sudo apt-get update<br>sudo apt-get upgrade<br><em>sudo apt-get install boinc</em></pre><p>Once BOINC is installed, you can either join a specific project like SETI@HOME, or you can use an <strong>account manager</strong>. An account manager is a website which allows you to search for projects which are looking for volunteers. I’m keen to participate in several science projects, so I registered with <a href="https://boincstats.com/en">BAM</a>, one such provider.</p><p>To use an account manager, you need to click on View -&gt; Advanced View then Tools -&gt; Use account manager</p><p>BOINC will synchronise with your account manager which will send you tasks to run.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6ocawCYxecX2Ga9jgC0s0g.png" /><figcaption>Multiple projects running on BOINC</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hDHEwLaHVvjSPqCOdE_2Vg.png" /><figcaption>Running tasks</figcaption></figure><p>If you don’t want to dedicate the whole computer to this task, BOINC gives you a lot of options to limit the amount of work such as:</p><ul><li>Limit the total amount of CPU time or CPU cores that BOINC can use.</li><li>Turn off BOINC if other tasks require CPU.</li><li>Schedule which day and time of the week BOINC can work.</li></ul><h3>Power and cost</h3><p>You may wonder how much it costs to run an SBC at full capacity, 24/7. <strong>The short answer is – it’s cheap.</strong></p><p>Let’s digress into the concepts of <strong>Power</strong> and <strong>Energy</strong>, skip if you are already familiar with them.</p><h4>Power and Energy</h4><p>Energy is a universal and abstract concept which describes the amount of “<em>work</em>” a system (mechanical, chemical, electrical, etc…) can do. It is expressed in Joule, kcal (yes the ones in your food) or W.h (more on that below).</p><p><strong>Power is the amount of energy a system can produce/consume</strong> (depending on the perspective) <strong>per unit of time</strong>. I find it useful to compare to more familiar and similar concepts:</p><ul><li><strong>Speed</strong> is the amount of distance per unit of time, i.e. distance/time. If you multiply the average speed by the total time you get the <strong>total distance travelled.</strong></li><li><strong>Electric current </strong>(measured in Ampere A ) is the amount of electric charge per unit of time, i.e. charge/time. If you multiply the average current by the total time you get the <strong>total charge</strong> (which is why battery capacity is often measured in A.h )</li><li><strong>Power</strong> is the amount of energy per unit of time, i.e. energy/time. If you multiply the average power by the total time you get the <strong>total energy</strong>. Which is why your electricity provider will typically charge you per kW.h , which is a convenient way of measuring the energy you consume.</li></ul><h4>Electric Power</h4><p>There are many formulas to compute power and energy, depending on the type of source. For an electric source (D.C current to be precise), the formula for power is P = U.I where U is the<strong> electric potential</strong> or tension expressed in <strong>Volt</strong> V , and I the <strong>electric current</strong> expressed in <strong>Ampere</strong> A.</p><p>In order to measure how much Power is consumed by my SBCs, I purchased a simple USB device like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OXXsG3vldwMahPFxwddSJw.jpeg" /><figcaption>USB tester, less than £10</figcaption></figure><p>You plug the device into the power source, then you plug the SBC into the device. It will monitor the voltage and current, can compute the instantaneous power and also the<strong> total amount of energy</strong> (in W.h) that has flown through the device.</p><blockquote>This can be quite useful if you want to check the battery capacity of a phone or other devices.</blockquote><h4>Cables are not born equal</h4><p>One thing which surprised me was to find out that not all USB cables are equal. I did some testing by charging a battery pack with different cables. Some of my cables (USB to micro-USB) were able to deliver 1.2A (which is how much the pack can produce I think) while others could only deliver 0.9A or even 0.7A. That’s a big difference and if<strong> you have a tester at home it might be worth separating the bad apples.</strong></p><h4>Total power consumed</h4><p>Because it fluctuates too much, I could not use the instant power from the device in order to measure the power consumed by the SBCs. Instead, I would let the device run for 1h and <strong>read the total amount of energy</strong> consumed in W.h. Then I only needed to divide by 1h to get the power. So I read 3.46 W.h after 1h of testing, it means the board consumed 3.46 W on average.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GX4wbb5UtDrRIHfJeRQYJg.jpeg" /><figcaption>Example of a reading. Right side reads from top to bottom: Instant power, accumulated energy, accumulation time</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3uaF49kEwtK7o81YAeG5IQ.jpeg" /><figcaption>I set up a webcam so I could read measurement away from home</figcaption></figure><p><em>(I felt I wasnt’ getting precise enough measurement with 1h so I switched to letting it run for 12h, but the principle is the same — divide by 12 instead of 1)</em></p><p>At full capacity (i.e. BOINC uses 100% CPU at all time) my Raspberry Pi consumed 4.8 W.</p><p>The Tinker Board S consumed 4.1 W in similar conditions.</p><p>My electricity provider charges around 0.18 £/kW.h meaning running the SBCs at 100% CPU 24/24h will cost me:</p><ul><li>£7.70/year for the Pi</li><li>£6.50/year for the Tinker board</li></ul><p>My provider generates electricity entirely from renewable sources so the environmental impact is pretty low too.</p><p>Based on the two facts above I don’t feel guilty for letting these two run all year round!</p><h4>Pi vs Tinker</h4><p>Using the benchmark provider by BOINC I could measure that:</p><ul><li>Pi: 4.5 Gops/s for floating points calculation and 70 Gops/s for integer calculation</li><li>Tinker board: 5.3 Gops/s for floating point calculation and 107 Gops/s for integer calculation</li></ul><p>Gops/s<em> means 1 billion operations per second, i.e the amount of calculation the board can do in 1 second</em></p><p>Here the Tinker Board is more cost-effective at £3.9p/Pops vs £5.4p/Pops for the Pi with floating point calculations, and £0.19p/Pops vs £0.35p/Pops for integer calculations.</p><p><em>Integer calculation means the computer only uses whole numbers, whereas floating calculation uses decimal numbers. The latter is harder hence why the boards can do less per seconds. </em><em>1Pops = 1 Peta operations = 1 quadrillion operations</em></p><h3>Energy proportionality</h3><p>I recently read a paper [1] describing the different aspects of building and running large data centres and came upon the very interesting concept of <a href="https://en.wikipedia.org/wiki/Energy_proportional_computing">energy proportionality</a>.</p><p>The idea stems from the economics of running those large data centres. A useful measure is the cost of a unit of computation. In this case, the cost is how <strong>much money it takes to run</strong> the whole facility; the cost of building the data centre is counted separately.</p><p>An ideal energy proportional server would <strong>consume </strong><strong>0W when idle</strong>, a certain maximum power Pmax at full capacity, and a <strong>linear progression anywhere in the middle.</strong></p><blockquote>Said differently, it means the server would be equally energy efficient when it runs at 100%, 80% or any other load.</blockquote><p>Obviously, this is never the case. Even when idle, a server will consume resources (spinning the disk, idle CPU, local fan if exists). When a component is not energy proportional, <strong>each unit of computation will get more expensive as the load decreases from the maximum.</strong></p><p>In simpler terms, a non-energy proportional component is <strong>not</strong> “pay as you go”. Imagine a sports centre that charges £10/month then £2/entry. The more often you go, the cheaper the actual cost per entry becomes.</p><p>Most servers are typically<strong> optimised to be the most efficient at full load</strong>, which is a problem for modern data centres. In such places, servers spend most of their time in the 10%-50% utilisation range [1] which is far from the range optimised by manufacturers. This low utilisation is required to be able to absorb peak traffic, which is not uncommon on the internet.</p><p>Research has shown [2] that improving energy proportionality of servers in modern data centres could reduce the energy consumption by half. This is a new area of research which hasn’t been fully explored yet.</p><h4>Experiment</h4><p>Since this is a relatively recent field of study, I thought it’d be interesting to see <strong>how energy proportional my SBCs are</strong>. I did a simple (but somewhat long) experiment where I used BOINC to cap the CPU usage at a certain % and measured the power (averaged over 12h). I did this experiment for each SBC by an increment of 20% utilisation.</p><p>I’m running the SBCs in my cellar which, at the time of writing, maintains a constant temperature of 15C throughout the day. Therefore, I don’t need to use a fan but might need to if it gets warmer.</p><h4>Disclaimer</h4><p>The results below represent a single type of workload and do not necessarily reflect how the boards would compare on different workloads.</p><h4>Results</h4><p>The results for both boards are plotted below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*gK9PwiB4gREz8v4mIev4aA.png" /><figcaption>Power consumption with load</figcaption></figure><p>I was surprised to see that the Tinker Board uses less energy while running faster calculations. It has to be noted that the Tinker Board cost approximately 2.5 more than the Pi, making the comparison somewhat unfair.</p><p>Another interesting graph can be obtained by <strong>normalising all the powers</strong> (i.e. dividing by the maximum power). After doing so, the Y-axis represents values from 0 to 1, a ratio of the current power to the maximum power. A perfectly energy proportional component would follow the line y=x, ie. 1 at full capacity, and 0 when idle, and going at a straight line between the two.</p><p>We can get a feeling for how non-proportional the SBCs are by looking at how far they are from the ideal line:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*y1Z8A6LkNtG4i4s6YhjQJQ.png" /><figcaption>Normalised power consumption</figcaption></figure><p>I wasn’t able to draw any conclusive facts from this graph. The Tinker Board seems slightly more efficient at rest (0% load) but the overall trend seems similar. Again this experiment is very specific to the workload I was running and may differ completely on other configurations.</p><h3>Conclusion</h3><p>I hope you found this article interesting, learnt something and hopefully got curious about lending a hand to global scientific projects.</p><p>I also want to close on what inspired this project for me. <a href="https://www.slow-journalism.com/">Delayed Gratification</a> is one of the leaders in the <strong>slow journalism</strong> movement. Instead of rushing to breaking news, journalists purposely wait a few months after the event and go back to investigate for a fuller analysis. An issue costs £10 (less with a subscription) and covers 3 months of the year (4 issues are released per year), has 120 pages without <strong>a single advert</strong> and is full of <strong>cool infographics</strong>. I highly recommend it if you enjoying reading in-depth news articles.</p><blockquote>This article is not sponsored by any of the products I mention.</blockquote><h4>Resources</h4><ul><li><a href="https://www.raspberrypi.org/magpi/share-boinc/">How to install BOINC on a Raspberry Pi</a></li><li><a href="http://seticlassic.ssl.berkeley.edu/about_seti/about_seti_at_home_1.html">How SETI@HOME works</a></li></ul><h4>References</h4><p>[1]: Barroso, Luiz André and Clidaras, Jimmy and Hölzle, Urs. The Datacenter as a Computer: An Introduction to the Design of Warehouse-Scale Machines, Second edition. Available at <a href="https://www.morganclaypool.com/doi/abs/10.2200/S00516ED2V01Y201306CAC024">https://www.morganclaypool.com/doi/abs/10.2200/S00516ED2V01Y201306CAC024</a></p><p>[2]: X. Fan, W. Weber, and L. A. Barroso. Power provisioning for a warehouse-sized computer. In Proceedings of the 34th Annual International Symposium on Computer Architecture, San Diego, CA, June 9–13, 2007. ISCA ’07. DOI: /10.1145/1250662.1250665 78, 84, 85, 113</p><p>If you like that sort of content, you can follow me on Twitter</p><p><a href="https://twitter.com/cesarTronLozai">Cesar Tron-Lozai</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=538aa5f77e87" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/how-my-raspberry-pi-listens-to-the-stars-538aa5f77e87">How my Raspberry Pi listens to the stars</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Embracing failures and cutting infrastructure costs: Spot instances in Kubernetes]]></title>
            <link>https://itnext.io/embracing-failures-and-cutting-infrastructure-costs-spot-instances-in-kubernetes-6976781beacc?source=rss-d7cbf6ac998a------2</link>
            <guid isPermaLink="false">https://medium.com/p/6976781beacc</guid>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[César Tron-Lozai]]></dc:creator>
            <pubDate>Fri, 21 Dec 2018 11:26:49 GMT</pubDate>
            <atom:updated>2018-12-21T13:00:49.110Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gW8C1v4ROBRDgJRQD1Vf2A.jpeg" /></figure><p>The last decades have seen a global shift from on-premise data centres to the provisioning of Virtual Machines (VMs) from mainstream cloud providers such as <em>Amazon Web Services</em>, <em>Azure</em>, <em>Google Cloud Platform</em>.</p><blockquote>Running and managing your own physical machines is hard and costly; chances are you’ll never be as successful and efficient as any of the top cloud providers.</blockquote><p>And what’s not to love when you can leverage a mature platform and features such as:</p><ul><li><strong>Vertical scalability</strong> — You can get instances of different sizes</li><li><strong>Horizontal scalability</strong> — You can get (almost) as many instances as you want</li><li><strong>Flexible pricing</strong> — You only pay for what you use</li><li><strong>Logistics cost</strong> — You don’t have to physically maintain any server (heat control, electricity, backup, storage cost, fire prevention etc…)</li><li><strong>Availability</strong> — Provision VM in separate data centres</li><li><strong>Reliability</strong> — If you pay for an instance you’ll keep it until you are done. Should it go down you’ll immediately (+- 5min) get a replacement</li></ul><p>In this article, we will explore the different pricing models of a typical cloud provider. We will focus on one strategy and see how it could <strong>cut your bill by up to 80%</strong> if you are willing to trade in reliability. Finally, we will see how Kubernetes makes that lack of reliability irrelevant and allows you to run a cheap yet highly available cluster.</p><h3>Pay-as-you-go: flexibility comes at a price</h3><p>The typical pricing model for cloud providers is based on a pay-as-you-go scheme. Compute resources come in different sizes (i.e. memory, CPU, disk etc..) and an hourly cost. <strong>You get billed for the amount of time the instance is running</strong>.</p><p>This flexibility of pricing is excellent and fair, but you have to be careful with what you consume. If you leave instances running while you don’t need them anymore, you’ll be throwing money out of the window.</p><blockquote>However let’s say you can foresee utilisation of a VM for a whole year. Shouldn’t you be able to get a bulk discount on your bill?</blockquote><h3>Get a bulk discount with Reserved Instances</h3><p>With reserved instances, you need to <strong>commit</strong> to compute resources for at least a whole year, and if you really love commitment, up to five years. You may decide to pay an amount of the bill upfront.</p><p>The discount that you get will depend on how long you are willing to commit and how much you can pay upfront. For example on Amazon Web Services, the <em>m4.large</em> instance type can be discounted as follow:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7RGE0PwL6j5SysiDewgphA.png" /><figcaption>Bulk discount</figcaption></figure><p>As you can see, the discount offered from reserved instances <strong>typically range from 30% to 40%</strong>.</p><blockquote>With reserved instances, you are basically <strong>trading flexibility for cash</strong>.</blockquote><p>Though 30% to 40% might sound like reasonable savings, it might not always be worth it. Are you able to forecast your compute resource utilisation for the next 1 to 5 years? If you are building a cutting-edge startup in the cloud can you accurately predict what your traffic will be like in a few years?</p><blockquote>If it sounds like a gamble, it is. Perhaps commitment and upfront payment is not the only way you could save on your cloud bill.</blockquote><h3>Spot Instances: when cheap is better than reliable</h3><p>AWS call them <a href="https://aws.amazon.com/ec2/spot/">Spot Instances</a>, Azure <a href="https://docs.microsoft.com/en-us/azure/virtual-machine-scale-sets/virtual-machine-scale-sets-use-low-priority">Low-priority VM</a> and Google <a href="https://cloud.google.com/compute/docs/instances/preemptible">Preemptible VM</a>. We will call them “spot instances” as it seems to be the most common terminology. Though their inner workings differ a little, they stem from the same rationale.</p><p>A typical cloud provider buys loads of powerful servers organised in large data centres. To maximise the utilisation of hardware, they divide those computers into smaller virtual machines.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*qH1LXbKbQ6woc23Fzjji7A.gif" /><figcaption>Cloud providers typically split bare metal server into smaller virtual machines</figcaption></figure><p><strong>Because they promise horizontal scalability to everyone, they need to keep a lot of unutilised hardware in case someone suddenly needs additional compute units. That, however, leaves a lot of resources unused.</strong></p><p>The idea behind spot instances is to allow users to tap into those extra resources at a much lower cost with the caveat that you might lose the instance at any moment. If you are running a spot instance and the cloud provider suddenly need that resource to accommodate demand from on-demand or reserved customers, you will immediately lose your instance.</p><p>Whereas <strong>flexibility</strong> was used as the bargaining chip with reserved instances, here it is <strong>reliability</strong> that has been given away. The saving benefits are much more significant though. You can typically expect to shave your bill by <strong>70% to 80%</strong>.</p><p>From that follows the big question:</p><blockquote><strong>Should you wager the stability of your infrastructure on account of 70% to 80% discount on your bill? What would be the impact on your customers if you are likely to lose a node at any moment?</strong></blockquote><h3>Embracing failure</h3><p>Observations from systems at scale have proven that your application <strong>will eventually go down</strong>. Hard-drives, networks, JVMs, etc., they <strong>all</strong>fail if you give them enough time and requests.</p><blockquote>Your primary weapon against failure is <strong>replication</strong> and <strong>redundancy</strong>.</blockquote><p>If you run several copies of each component, it might be resilient to a certain number of failures. The amount of failure you can recover from will depend on how much redundancy you are willing to put in place.</p><p>Don’t forget that redundancy means more compute resources. And more compute resources leads to a higher bill.</p><p>Another point to consider is the dynamic aspect of spot instances. Being based on idle resources, the size of instances available to you will <strong>depend on what is currently unpopular</strong>. In other words, <em>beggars can’t be choosers</em>.</p><p>Perhaps this week you can pick up cheap 2GB memory instances, which is great if it is the amount of memory which your application requires. What should you do next week if those instances become unavailable, and you can only buy instances with memory starting from 4GB of memory? Of course, you could use those instances, but you’d be paying twice the price, and the extra memory would be wasted.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/744/1*MA2u0B157ZeFnWM95A8XgA.gif" /></figure><p>Spot instances are an excellent deal, but the downsides might not be acceptable. How can you cope with random nodes disappearing without notice? How should your infrastructure handle nodes of ever-changing sizes?</p><blockquote>What you need is a tool that continually monitors nodes and automatically manages redundancy.</blockquote><p>This tool should scale and spread the components of applications on your infrastructure; when a node is lost or created, the infrastructure is rebalanced.</p><p>It seems that one wouldn’t be able to manage a consequent cloud infrastructure without such a tool. Chances are someone already built it. You are in luck, Google faced those issues years ago and have since open-sourced their solution to the problem: <strong>Kubernetes</strong>.</p><h3>Abstracting the data centre into a single computer</h3><p>In a traditional infrastructure — say the early 2000s — you had a <strong>fixed </strong>number of servers and a <strong>predictable</strong> amount of resources.</p><p>Cloud infrastructure — especially with spot instances — have completely changed the game. Kubernetes was developed to oversee the increasing complexity of managing ever-changing compute resources.</p><p>Kubernetes provides a layer of abstraction on all your compute resources — regardless of how many, irrespective of their sizes. You only have to interact with a <strong>single</strong> entity: <strong>the cluster</strong>.</p><p>Your cluster could be formed of 10 small virtual machines or 2 big bare metal servers, the end result is the same: <em>a single point of interaction that manages and scales workload on your nodes</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*I27gVyyO7FryFRFNQoWA-Q.gif" /><figcaption>Kubernetes lets you forget about individual nodes; you can just think of the “cluster”</figcaption></figure><p>When you install Kubernetes on your infrastructure you select one computer as the <strong>master</strong> node, the rest of your fleet join the cluster as <strong>worker</strong> nodes. As you add or remove nodes to the cluster, Kubernetes keeps track of the available memory and CPU on each node.</p><p>When your cluster is ready, you send a deployment request to the master node.</p><blockquote>Upon receiving the request, Kubernetes surveys the worker nodes for available memory and CPU and finds the best candidates to run your application.</blockquote><p>As a user, you don’t have to worry about where your application is running; it’s in the cluster. If a node running your application dies, its workload will immediately be moved to other nodes.</p><p>Interestingly, Kubernetes doesn’t care what the size of a worker node is, as long as it offers memory and CPU.</p><p>When a worker node with 4GB of memory and 2 CPUs registers to the cluster, the master node keeps track of the total available and spare capacities. It continually monitors the current workload on each node and can decide if a given node has enough available resources to run an application.</p><p>This is one of Kubernetes true beauty. <strong>You can forget how many individual nodes joined the cluster and how big they are: you only see a single unified entity.</strong> But if you’re interested to know how big is your cluster you can sum the memory and CPU of all nodes and this will tell you how much total capacity your cluster has.</p><p>If you have one 4GB/1vCPU and one 8GB/2vCPUs instances, you only have a cluster with 12GB and 3 vCPUs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*oDWxxE0wmWGVc1SmYzoeeQ.gif" /><figcaption>Kubernetes keeps track of the available memory and CPU</figcaption></figure><p>The other noteworthy feature in Kubernetes is that nodes are monitored for uptime.</p><blockquote>If a node is lost, Kubernetes will remove its memory and CPU from the cluster and migrate all applications to other worker nodes.</blockquote><h3>Self-healing infrastructure with Kubernetes</h3><p>The Master node runs a series of synchronisation loops which follow a simple principle: as a user, you specify the desired state, e.g. “I want 3 instances of my application”.</p><p>The master node regularly checks the current state, compares it with the desired state and make any required adjustment. For example, if a master node notices that only 2 instances of an application are running, but you requested three, it immediately starts another one.</p><blockquote>Most of Kubernetes components are designed this way: a control loop that constantly regulates the current state around the desired state.</blockquote><p>Imagine you have 3 nodes and 3 replicas of an application, one running on each node. When a node running on a spot instance is reclaimed by the cloud provider, the application on that node is lost. Kubernetes realises that you only have 2 replicas running instead of 3 and immediately starts another copy in one of the two remaining nodes (if space is available of course).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*EqG5u4Bx_KeQWIUBGr6YmQ.gif" /><figcaption>Kubernetes automatically recovers from node failure</figcaption></figure><h3>Spot instances and Kubernetes: a match made in heaven</h3><p>In the last decade, our industry has massively adopted microservices architecture. Some would argue it is a fad, others that it is just SOA rebranded. I would say that the most important revolution which came with microservices architecture, wasn’t that we decided to write smaller applications, but was the <strong>shift from preventing failures to embracing failures</strong>.</p><p>The most significant insight from the likes of <em>Netflix</em>, <em>Google</em> and <em>Amazon</em>, is that <strong>at scale, things go wrong</strong>. Even on the best and most expensive hardware, the probability of failure is strictly larger than zero.</p><p><em>So how do you design around it?</em></p><blockquote><strong>You test the failover with chaos engineering.</strong></blockquote><p><strong>Chaos engineering</strong> recommends that you actively create failures in your infrastructure to ensure you are resilient indeed.</p><blockquote>If you need to remember a single thing it is that <strong>availability and reliability can only be achieved if you test them actively</strong>.</blockquote><p>If you want to make sure your application is highly available, you need to kill nodes regularly.</p><p><em>And what better way to kill node at random times if not spot instances?</em></p><p>So not only you’re saving 80% of your cloud bill because you’re leveraging spare resources, but you’re also continuously testing your infrastructure for resilience.</p><p><strong>The precious reliability you obtained from reserved instances isn’t that much important anymore</strong>. In fact, you don’t actually care if your cloud provider reclaims your nodes unexpectedly; if you have modern and resilient architecture, you just won’t even notice.</p><p><strong>Reduced bill and chaos engineering. That’s a win-win.</strong></p><h3>Spot instances hot tips</h3><p>If you wish to try spot instances in your Kubernetes clusters, a few things could help you optimise your infrastructure.</p><h3>Be smart with the instance type</h3><p>Pick unpopular instance types. For instance, <em>m4</em> instances on Amazon Web Services are cheap because the <em>m5</em> instance family has recently been released. This made the <em>m4</em> instances go out of fashion meaning lower demand and better price for you!</p><h3>Maximum bid price (AWS only)</h3><p>While Azure’s Low Priority VM and Google Preemptible VMs have a fixed price, Spot instance prices are determined with a bidding process. As a user, you specify the maximum price per hour you are willing to pay. If AWS have enough spare VMs to serve everyone, everyone gets their instance at a low price. However, the price will get higher as demand increases.</p><p>Choosing the maximum bid price allows you to decide how much <strong>reliability you are willing to trade in</strong>. A <strong>low bid</strong> will ensure your <strong>bill stays low</strong> but will increase the <strong>likelihood of losing a node</strong>. A <strong>high bid</strong> (for example equal to the on-demand price) will <strong>reduce node failures</strong> but means you might pay the same as on-demand price, i.e. with no discount.</p><p>You may choose to align the bid price to the reserved instance price for 3 years (typically ~35% cheaper than on-demand). That way you are sure to never pay more than a reserved instance without having to actually reserve it.</p><p>That also depends on what you are doing. If you want to do non-critical work at the lowest price you may have a low bidding price, hoping to get cheap resource when you can but nothing otherwise.</p><h3>Monitor everything</h3><p>Appropriate monitoring will tell you if your system recovered properly after a node failure. If your application did not cope with losing a spot instance in your cluster, you need to know pretty quickly.</p><p>At the beginning of your journey with spot instances, you could set up email notifications when your cloud provider reclaims one of your nodes. As your system gets more robust and you get more confident you probably won’t need those anymore.</p><p>On AWS you could try the <a href="https://github.com/mumoshu/kube-spot-termination-notice-handler">Spot Termination Notice Handler</a> which can be notified of spot instance termination before it happens, leaving you time to reschedule your app onto other nodes gracefully.</p><p>On AWS you should also monitor how much you pay. If the bid price ends up being close to the on-demand price, you should probably find another instance type. Remember that <strong>Kubernetes helps you with that</strong>. Having 3 instances with 8GB of memory is almost identical to having 6 instances with 4GB. Be smart!</p><h3>Prepare alternative means</h3><p>Have an alternative means of providing instances. For example, if you are provisioning your cluster in Amazon Web Services, you can prepare additional node pools on the pay-as-you-go model and set the desired quantity to 0. If there are no available spot instances available you can always let Kubernetes switch to those other node pools.</p><h3>Summary</h3><p>Kubernetes was designed to <strong>abstract the size of nodes</strong> and to seamlessly <strong>move components between nodes</strong>. This makes it the perfect candidate to work with <strong>spot instances</strong>. A cluster built on top of spot instances will <strong>scarcely be less reliable</strong> than a cluster built on reserved virtual machines. When shopping for nodes for your Kubernetes cluster, reliability should not be your primary concern. You should focus on <strong>cheap memory and CPU</strong>! This echoes one of the fundamental principles at Google:</p><blockquote><strong>You don’t need reliable hardware with good enough software!</strong></blockquote><p>But remember that <strong>running your applications on Kubernetes doesn’t make them horizontally scalable</strong>. It is your <strong>duty</strong> to ensure that multiple copies of the application can run at the same time and can gracefully be shut down without dropping connections. More importantly, it is <strong>critical that you actively test your availability</strong>.</p><p>Choosing spot instances will force you to practice some degree of <strong>chaos engineering while slashing your bill</strong>. If you are not on that Bandwagon, you should try it now!</p><p>If you enjoyed this article, you might find the following articles interesting:</p><ul><li><a href="https://learnk8s.io/blog/installing-docker-and-kubernetes-on-windows">Getting started with Docker and Kubernetes on Windows 10</a> where you’ll get your hands dirty and install Docker and Kubernetes in your Windows environment.</li><li><a href="http://learnk8s.io/blog/scaling-spring-boot-microservices/">Scaling Microservices with Message Queues, Spring Boot and Kubernetes</a>. Learn how to use the Horizontal Pod Autoscaler to dynamically resize your fleet of applications.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6976781beacc" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/embracing-failures-and-cutting-infrastructure-costs-spot-instances-in-kubernetes-6976781beacc">Embracing failures and cutting infrastructure costs: Spot instances in Kubernetes</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Keep your Kubernetes cluster balanced: the secret to High Availability]]></title>
            <link>https://itnext.io/keep-you-kubernetes-cluster-balanced-the-secret-to-high-availability-17edf60d9cb7?source=rss-d7cbf6ac998a------2</link>
            <guid isPermaLink="false">https://medium.com/p/17edf60d9cb7</guid>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[cloud-services]]></category>
            <dc:creator><![CDATA[César Tron-Lozai]]></dc:creator>
            <pubDate>Sun, 17 Jun 2018 12:16:43 GMT</pubDate>
            <atom:updated>2018-10-07T09:05:37.270Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AdnZBbghNRa_9Ju8dUiMnQ.png" /><figcaption>Whatch out for those photoshop skills</figcaption></figure><h3>Kubernetes Scheduler</h3><p>As you probably know, a Kubernetes cluster is made of master and worker nodes. The <strong>scheduler</strong> is a component in a master node, which is responsible for deciding which worker node should run a given pod.</p><blockquote>Scheduling is a complex task and like any optimisation problem you will always find a scenario in which the result may seem sub-optimal to the human eye.</blockquote><p>Nonetheless, the default scheduler does a pretty decent job. It follows common sense strategies such as avoiding to schedule multiple replicas of a pod on the same node or avoiding overloading a node while others remain under allocated.</p><p>I am not familiar with the code behind the default scheduler but my observations have taught me that balancing node workload takes priority over preventing duplicates running on the same node.</p><blockquote>This means that you may find yourself in a situation where all replicas of a pod are on the same node.</blockquote><p>Therefore, if the node goes down — and you should assume that it will — availability will be impacted.</p><p>Interestingly, Kubernetes usually works by strictly enforcing some sort of state. For example, if you say you want three pods A , Kubernetes will imediately spawn new pod(s) should that number go below three. <strong>This is not the case for scheduling.</strong> Kubernetes will try to to spread your pods intelligently when it creates them but will not proactively enforce a good spread afterwards.</p><blockquote>Scheduling happens at pod creation only.</blockquote><h3>High availability</h3><p>Running multiple replicas of a pod will not guarantee high availability but it cannot be achieved without it. For obvious reasons, you should never have all the replicas of a pod on the same node.</p><p>High Availability is not an absolute concept; you can never be 100% highly available. How can your application be available if all the nodes go down simultaneously?</p><p>So you generally need to choose a level of availability you are comfortable with. For example, if you are running three nodes in three separate availability zones, you may choose to be resilient to a single node failure. Losing two nodes might bring your application down but the odds of loosing two data centres in separate availability zones are low.</p><blockquote>The bottom line is that there is no universal approach; only you can know what works for your business and the level of risk you deem acceptable.</blockquote><h3>When balance becomes an issue</h3><p>Let’s have a look at a scenario where scheduling can jeopardise availability.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*8t5Y9IZ5FUWOyJwxxDYtdw.jpeg" /><figcaption>Ideal scheduling</figcaption></figure><p>Let’s say we start in an ideal state of scheduling. We have four services A , B , C , and D , each scaled to run three replicas.</p><p>If Node 3 were to die, the master node(s) will notice that we only have two pods of each service. The master node(s) will immediately reschedule one of each on the remaining healthy nodes. This could lead to:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*aQrtKrgHXA0FJXi7aVWiRQ.jpeg" /><figcaption>Node 3 is dead, the remaining nodes share the load</figcaption></figure><p>After some time (typically a few minutes), your cloud provider will notice that a node is down and will spin another one. When that node is ready we end up with a cluster like that:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/831/1*bqV1Zs5pli_JUd-Wv0GyTw.jpeg" /><figcaption>An unbalanced cluster</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/1*EocdAdivYr5TQE5UcZ-0IA.jpeg" /><figcaption>Nothing to add here</figcaption></figure><p>The cluster is unbalanced but availability is not lost, yet… If we lose any of the nodes we still have at least one replica of each pod running.</p><p>However, let’s see what happens when we start soliciting the scheduler by releasing a new version of service A . This is typically done with a <strong>rolling deployment</strong>.</p><p>Kubernetes will start a new pod for service A , wait for the pod to be ready, and kill one of the old pods. It continues doing so until all the old pods have been replaced.</p><p>So, the scheduler is asked to place a new A v2 pod — the only place it can do so is Node 3. So far so good. However, where should it put the other two? As mentioned above the scheduler tries to avoid having duplicates on the same node but in that case Node 3 is massively under-utilised compared to the other nodes. So there is a <strong>high chance</strong> that all the new A v2 pods will be scheduled on Node 3, which would lead to this situation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*9N2h2H2iqlxqsY0bBVoTtg.jpeg" /></figure><p>And now we have a problem; all the A pods are on a single node. If Node 3 were to fail again we loose availability for service A .</p><p>The whole process is described by this magnificent GIF:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*_7xi3zeFWAveLXEXhdmxow.gif" /></figure><h3>De-scheduler</h3><p>One way to bring back order is by doing a bit of <strong>chaos engineering</strong>. The scheduler can only fix a balance problem when pods are created. So let’s get killing to force creation of new pods!</p><p>Enters the <a href="https://github.com/kubernetes-incubator/descheduler">descheduler</a>. This project runs as a Kubernetes <a href="https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/">Job</a> that aims at killing pods when it thinks the cluster is unbalanced. You can run it once or as a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/">Cron Job</a> to run it periodically.</p><p>The installation is pretty straightforward and well explained on the <a href="https://github.com/kubernetes-incubator/descheduler">GitHub page</a>.</p><p>You can use a variety of strategies to delete pods, which are defined in a <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/">Config Map</a>. Let’s review two of them.</p><pre>apiVersion: &quot;descheduler/v1alpha1&quot;<br>kind: &quot;DeschedulerPolicy&quot;<br>strategies:<br>  &quot;RemoveDuplicates&quot;:<br>     enabled: false</pre><p>Using the RemoveDuplicatesstrategy, the Descheduler will find identical pods on a node. It will then kill some of them, hoping the scheduler will schedule the pods on another node.</p><p>Note that it wouldn’t work with the scenario outlined above. This is because we still haven’t resolved the workload balance problem.</p><p>However, the LowNodeUtilization strategy can help with that:</p><pre>apiVersion: &quot;descheduler/v1alpha1&quot;<br>kind: &quot;DeschedulerPolicy&quot;<br>strategies:<br>  &quot;LowNodeUtilization&quot;:<br>     enabled: true<br>     params:<br>       nodeResourceUtilizationThresholds:<br>         thresholds:<br>           &quot;cpu&quot; : 20<br>           &quot;memory&quot;: 20<br>           &quot;pods&quot;: 20<br>         targetThresholds:<br>           &quot;cpu&quot; : 50<br>           &quot;memory&quot;: 50<br>           &quot;pods&quot;: 50</pre><p>You must define what it means for a node to be under and over utilised. In this example, if a node is below 20% CPU utilisation <strong>and</strong> below 20% memory utilisation <strong>and</strong> has less than 20 pods it is considered <strong>under-utilised</strong>. If a node is above 50% CPU utilisation <strong>or</strong> above 50% memory utilisation <strong>or</strong> has more than 50 pods it will be considered <strong>over-utilised</strong>.</p><p>For this strategy to work, the descheduler will need to find at least one under-utilised node and one over-utilised node.</p><p>As usual, there are no universal utilisation parameters that will work well everywhere. You need to tweak them and find out what works best for you.</p><p>If you are really confident in your availability you could run this job every hour. Otherwise you could run it at night when impact on traffic is low. In the latter case, it might be useful to run it a few times in a row. For example, you could run it everyday at 3am, 3:15am and 3:30am.</p><p>In conclusion, remember that High Availability is not something you will ever achieve without actively testing for it. If you don’t kill your nodes and test for disaster scenario your system <strong>will experience downtime</strong>.</p><p>Keeping your cluster balanced will not guarantee High Availability but it will certainly help you towards that goal.</p><p>If it becomes necessary, you can also try advanced scheduling techniques. You can find information about this <a href="https://kubernetes.io/blog/2017/03/advanced-scheduling-in-kubernetes/">here</a> and <a href="https://thenewstack.io/implementing-advanced-scheduling-techniques-with-kubernetes/">here</a>. It is also worth having a look at <a href="https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#affinity-and-anti-affinity">pod affinity and anti-affinity</a>.</p><p><a href="https://twitter.com/cesarTronLozai">https://twitter.com/cesarTronLoza</a>i</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=17edf60d9cb7" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/keep-you-kubernetes-cluster-balanced-the-secret-to-high-availability-17edf60d9cb7">Keep your Kubernetes cluster balanced: the secret to High Availability</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Save on your AWS bill with Kubernetes Ingress]]></title>
            <link>https://itnext.io/save-on-your-aws-bill-with-kubernetes-ingress-148214a79dcb?source=rss-d7cbf6ac998a------2</link>
            <guid isPermaLink="false">https://medium.com/p/148214a79dcb</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[César Tron-Lozai]]></dc:creator>
            <pubDate>Tue, 12 Jun 2018 07:34:55 GMT</pubDate>
            <atom:updated>2018-09-20T11:33:11.523Z</atom:updated>
            <content:encoded><![CDATA[<h3>The wonders of Kubernetes</h3><p>One of the first concept you learn when you get started with Kubernetes is the <a href="https://kubernetes.io/docs/concepts/services-networking/service/">Service</a>.</p><p>Internal Services allow for pod discovery and load balancing. If you need to make your pod available on the Internet, <strong>I thought</strong>, you should use a service with type LoadBalancer. This will have different effect depending on the cloud provider; on AWS, for example, it will create an ELB for <strong>each</strong> service externally exposed.</p><blockquote>ELBs are amazing but they are not cheap. They cost about $20 a month (plus extra per GB of data processed).</blockquote><p>If you’ve got a several services this will lead to a <strong>hefty</strong> <strong>bill</strong>.</p><h3>Here comes the Ingress</h3><p>It took me some time to get familiar with Kubernetes <a href="https://kubernetes.io/docs/concepts/services-networking/ingress/">Ingress</a>.</p><blockquote>Kubernetes is good at abstracting common problems, and giving you different concrete implementation for it.</blockquote><p>This is exactly what Ingresses do in the context of making your endpoints external. You specify in a common format how certain services should be routed and you leave the job of implementing this routing to an <strong>Ingress Controller</strong>.</p><p>For example:</p><pre>apiVersion: extensions/v1beta1<br>kind: Ingress<br>metadata:<br>  name: test<br>  annotations:<br>    nginx.ingress.kubernetes.io/rewrite-target: /<br>spec:<br>  rules:<br>  - host: foo.bar.com<br>    http:<br>      paths:<br>      - path: /foo<br>        backend:<br>          serviceName: s1<br>          servicePort: 80<br>      - path: /bar<br>        backend:<br>          serviceName: s2<br>          servicePort: 80</pre><p>This Ingress resource will expect to receive request made to host foo.bar.com and route the traffic to service s1 or s2 depending on the request path. You can also do routing based on the host (example below)</p><p>To be clear the Ingress does not replace internal services. You’d still want to have an internal service for each of your app/micro-service — this means your pods are always accessible at the same address, regardless of how they die or get created. Then the Ingress will route external traffic to internal services</p><pre>Request =&gt; Ingress =&gt; (Internal) Service =&gt; Pod</pre><p>By default Kubernetes comes with no Ingress Controller installed. It is your job to pick one (or more if you have more complex need).</p><h3>My requirements</h3><p>We use Kubernetes namespaces to separate our different environments. For example in a single Kubernetes cluster we might have a test , demo , and staging namespaces. I don’t want to have a separate ELB for each. This is too expensive.</p><p>I want to point the DNS for test, demo and staging to a single endpoint:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*PEkqkJNrf4uV_5vsSRF3PQ.jpeg" /></figure><h3>ALB Ingress controller</h3><p>I first came across the <a href="https://github.com/kubernetes-sigs/alb-ingress-controller">ALB Ingress Controller</a> and it sounded very promissing. Instead of creating an Elastic Load Balancer, it can create the newish <a href="https://docs.aws.amazon.com/elasticloadbalancing/latest/application/introduction.html">Application Load Balancer</a>.</p><p>So this Ingress Controller can transform Ingress Resource such as:</p><pre>apiVersion: extensions/v1beta1<br>kind: Ingress<br>metadata:<br>  name: test<br>  annotations:<br>    nginx.ingress.kubernetes.io/rewrite-target: /<br>spec:<br>  rules:<br>  - host: foo.com<br>    http:<br>      paths:<br>      - path: /<br>        backend:<br>          serviceName: foo<br>          servicePort: 80<br>  - host: bar.com<br>    http:<br>      paths:<br>      - path: /<br>        backend:<br>          serviceName: bar<br>          servicePort: 80</pre><p>Into a single ALB, with two target groups: one for the foo service when the the host is foo.com and one for the bar service when the host is bar.com</p><p>However there is a major issue for me because, at the time of writing:</p><blockquote>The ALB ingress controller does not support routing across multiple namespaces</blockquote><p>If you create one Ingress resource in test and one in staging , the ALB Ingress Controller will create two ALBs, which defeats the whole point. I’ve raised this <a href="https://github.com/kubernetes-sigs/alb-ingress-controller/issues/329">issue on Github </a>but it doesn’t seem to be moving yet.</p><h3>Solution 1: NGINX Ingress controller</h3><p>Some time ago I came across this <a href="https://github.com/kubernetes/kubernetes/issues/17088">Github issue about Cross-namespace Ingress</a> but I unfortunately I didn’t read carefully enough. After giving it a second read I realised the solution was really simple with the <a href="https://github.com/kubernetes/ingress-nginx">NGINX Ingress controller</a>.</p><p>The architecture is pretty simple. You have a single ELB pointing to NGINX which distribute the traffic internally:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*c_iuAj3MRV6pREabpfpjmg.jpeg" /><figcaption>NGINX Ingress Controller</figcaption></figure><p>And the good news is that this is really easy to setup too!</p><ol><li>Install the NGINX Ingress Controller as explained <a href="https://kubernetes.github.io/ingress-nginx/deploy/">here</a>.</li><li>In each of your namespace define an Ingress Resource</li></ol><p>For test:</p><pre>apiVersion: extensions/v1beta1<br>kind: Ingress<br>metadata:<br>  name: api-ingresse-test<br>  namespace: <strong>test</strong><br>  annotations:<br>    kubernetes.io/ingress.class: &quot;nginx&quot;<br>spec:<br>  rules:<br>  - host: test.blop.org<br>    http:<br>      paths:<br>      - backend:<br>          serviceName: myApp<br>          servicePort: 80<br>        path: /</pre><p>For staging:</p><pre>apiVersion: extensions/v1beta1<br>kind: Ingress<br>metadata:<br>  name: api-ingresse-staging<br>  namespace: <strong>staging</strong><br>  annotations:<br>    kubernetes.io/ingress.class: &quot;nginx&quot;<br>spec:<br>  rules:<br>  - host: staging.blop.org<br>    http:<br>      paths:<br>      - backend:<br>          serviceName: myApp<br>          servicePort: 80<br>        path: /</pre><p>For demo:</p><pre>apiVersion: extensions/v1beta1<br>kind: Ingress<br>metadata:<br>  name: api-ingresse-demo<br>  namespace: <strong>demo</strong><br>  annotations:<br>    kubernetes.io/ingress.class: &quot;nginx&quot;<br>spec:<br>  rules:<br>  - host: demo.blop.org<br>    http:<br>      paths:<br>      - backend:<br>          serviceName: myApp<br>          servicePort: 80<br>        path: /</pre><p>And voila! There is nothing more to do. The NGINX Ingress controller will process those resources and automatically create a single ELB. Then you simply point your test , demo , and staging DNS to the ELB and job done!</p><p>If you want to you can tune NGINX by writing configMaps but you don’t have to. It works well out of the box.</p><p>For better availability you can increase the number of replicas for the nginx-ingress-controller :</p><pre>kubectl -n ingress-nginx scale deploy nginx-ingress-controller --replicas=3</pre><h3>SSL termination</h3><p>You can easily terminate SSL traffic too. You can either choose to terminate SSL at the <a href="https://kubernetes.github.io/ingress-nginx/deploy/#aws">ELB level</a>, or with <a href="https://github.com/kubernetes/ingress-nginx/blob/master/docs/user-guide/tls.md">NGINX</a>.</p><p>Personally I prefer terminating SSL at the ELB as it is very easy to setup (a single annotation when installing the NGINX Ingress controller). But others might like the additional control offered by NGINX.</p><h3>Solution 2: Ambassador</h3><p>The second solution I came across doesn’t actually use Ingress.</p><blockquote><a href="https://www.getambassador.io/">Ambassador</a> is a Kubernetes-native microservices API gateway built on top of the <a href="https://www.envoyproxy.io/">Envoy Proxy</a>.</blockquote><p>It offers a similar architecture to NGINX Ingress controller:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*d2JVMoT33Z1ycuF9kZFkjQ.jpeg" /><figcaption>Ambassador pods route the traffic in your cluster</figcaption></figure><p>First you need to install Ambassador, which is very <a href="https://www.getambassador.io/user-guide/getting-started">easy to do</a>.</p><pre>kubectl apply -f <a href="https://getambassador.io/yaml/ambassador/ambassador-rbac.yaml">https://getambassador.io/yaml/ambassador/ambassador-rbac.yaml</a></pre><p>This will install 3 Ambassador pods which will route the traffic according to your pods. This redundancy provides high availability and helps with scaling the load.</p><p>Then you need to create a LoadBalancer type service to point to those pods. For AWS you do it like <a href="https://www.getambassador.io/reference/ambassador-with-aws">that</a>:</p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: ambassador-main<br>  namespace: ambassador<br>  annotations:<br>    service.beta.kubernetes.io/aws-load-balancer-ssl-cert: &quot;your_aws_cert_for_https&quot;<br>    service.beta.kubernetes.io/aws-load-balancer-ssl-ports: &quot;*&quot;<br>    service.beta.kubernetes.io/aws-load-balancer-backend-protocol: &quot;tcp&quot;<br>    service.beta.kubernetes.io/aws-load-balancer-cross-zone-load-balancing-enabled: &quot;true&quot;<br>    service.beta.kubernetes.io/aws-load-balancer-proxy-protocol: &quot;*&quot;<br>    getambassador.io/config: |<br>      ---<br>      apiVersion: ambassador/v0<br>      kind:  Module<br>      name:  ambassador<br>      config:<br>        use_proxy_proto: lower<br>        use_remote_address: true<br>spec:<br>  type: LoadBalancer<br>  ports:<br>  - name: ambassador<br>    port: 443<br>    targetPort: 80<br>  selector:<br>    service: ambassador</pre><p>This will create a single ELB . Like above, you can then point all your DNS names to this endpoint and let Ambassador do the routing.</p><p>With Ambassador you <strong>don’t need Ingress resources</strong>. You simply add annotations to your services. So in our case this would look like that:</p><p>If we had a service for our bar application in test , staging and demo we would simply add the getambassador.io/config annotation</p><p>For test:</p><pre>kind: Service<br>apiVersion: v1<br>metadata:<br>  name: test-bar<br>  namespace: test<br>  annotations:<br>    getambassador.io/config: |<br>      ---<br>      apiVersion: ambassador/v0<br>      kind:  Mapping<br>      name:  test-bar<br>      prefix: /<br>      service: test-bar:8080<br>      host: test.blop.org<br>spec:<br>  type: NodePort<br>  selector:<br>    app: bar<br>  ports:<br>    - protocol: TCP<br>      port: 8080<br>      targetPort: 8080</pre><p>For staging:</p><pre>kind: Service<br>apiVersion: v1<br>metadata:<br>  name: staging-bar<br>  namespace: test<br>  annotations:<br>    getambassador.io/config: |<br>      ---<br>      apiVersion: ambassador/v0<br>      kind:  Mapping<br>      name:  staging-bar<br>      prefix: /<br>      service: staging-bar:8080<br>      host: staging.blop.org<br>spec:<br>  type: NodePort<br>  selector:<br>    app: bar<br>  ports:<br>    - protocol: TCP<br>      port: 8080<br>      targetPort: 8080</pre><p>And for demo:</p><pre>kind: Service<br>apiVersion: v1<br>metadata:<br>  name: demo-bar<br>  namespace: demo<br>  annotations:<br>    getambassador.io/config: |<br>      ---<br>      apiVersion: ambassador/v0<br>      kind:  Mapping<br>      name:  demo-bar<br>      prefix: /<br>      service: demo-bar:8080<br>      host: test.blop.org<br>spec:<br>  type: NodePort<br>  selector:<br>    app: bar<br>  ports:<br>    - protocol: TCP<br>      port: 8080<br>      targetPort: 8080</pre><p>Ambassador also provides additional features like:</p><ul><li>gRPC support</li><li>Authentication</li><li>Rate limiting</li><li><a href="https://istio.io/">Istio</a> (Service mesh) integration</li></ul><p>If you want to learn more about Ambassador check it out <a href="https://www.getambassador.io/about/why-ambassador">here</a>.</p><p>If you, like me, have been using multiple ELB for too long, I hope this simple solution can make your AWS bill a bit lighter!</p><p>It goes without saying that this architecture works with GCE and Azure too.</p><p>PS: Other Ingress controller worth mentioning:</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=148214a79dcb" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/save-on-your-aws-bill-with-kubernetes-ingress-148214a79dcb">Save on your AWS bill with Kubernetes Ingress</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>