<?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 Rinor Maloku on Medium]]></title>
        <description><![CDATA[Stories by Rinor Maloku on Medium]]></description>
        <link>https://medium.com/@rinormaloku37?source=rss-cf86e8b1d3bf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*9C6jCdA-oXEO5J9R4x2A8A.png</url>
            <title>Stories by Rinor Maloku on Medium</title>
            <link>https://medium.com/@rinormaloku37?source=rss-cf86e8b1d3bf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 02:24:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rinormaloku37/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[Back to Microservices with Istio (Part 2) — Authentication & Authorization]]></title>
            <link>https://medium.com/google-cloud/back-to-microservices-with-istio-part-2-authentication-authorization-b079f77358ac?source=rss-cf86e8b1d3bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/b079f77358ac</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[istio]]></category>
            <category><![CDATA[authentication]]></category>
            <category><![CDATA[microservices]]></category>
            <dc:creator><![CDATA[Rinor Maloku]]></dc:creator>
            <pubDate>Tue, 26 Feb 2019 15:31:28 GMT</pubDate>
            <atom:updated>2022-06-07T13:45:00.298Z</atom:updated>
            <content:encoded><![CDATA[<h3>Back to Microservices with Istio (Part 2) — Authentication &amp; Authorization</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JuAIrafN3tEwZ6MPaZsI2Q.jpeg" /></figure><blockquote><strong>NOTE: </strong>This article is outdated. My newest article “<a href="https://www.freecodecamp.org/news/learn-istio-manage-microservices/">Learn Istio — How to manage, secure, and monitor your services</a>” works with the latest version of Istio.</blockquote><p><em>This is the second part of the article “</em><a href="https://medium.com/@rinormaloku37/back-to-microservices-with-istio-part-1-827c872daa53"><em>Back to Microservices with Istio</em></a><em>” (a prerequisite to follow along with the second part is completing the first one.)</em></p><p>In the first article, we set up a Kubernetes cluster in which we deployed <strong>Istio </strong>and the sample microservice application “Sentiment Analysis”, to showcase Istio’s features.</p><p>Using Istio we kept our services small and rendered the following layers obsolete: Retries, Timeouts, Circuit Breakers, Tracing, Monitoring (shown in figure 1) and additionally, we enabled advanced testing and deployment techniques like A/B Testing, Shadowing and Canary Deployments.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/379/1*rKvesxZlk_rlLAd5620Npw.png" /><figcaption>Figure 1. The Ceremony of a Microservice</figcaption></figure><p>In this article, we will tackle the final layers of Authentication &amp; Authorization and with Istio that’s a Joyride!</p><h3>Authentication &amp; Authorization in Istio</h3><p>I would have never believed that Authentication and Authorization would excite me! What on the technological spectrum could Istio possibly do to make these topics entertaining, and more importantly why should you be excited as well?</p><p><strong>The answer is simple:</strong> Istio offloads these responsibilities from our services to the Envoy proxies, and by the time when requests reach our services they are already authenticated and authorized, and we just write the code that provides business value.</p><p>Sounds good? Let&#39;s just jump into it!</p><h3>Authentication with Auth0</h3><p>As an Identity and Access Management server, we are going to use Auth0, which has a trial option, is intuitive to use, and I just love it! That said the same principles can be used for any <a href="https://openid.net/developers/certified/">OpenID Connect implementation</a> like KeyCloak, IdentityServer and many more.</p><p>To get started, navigate to <a href="https://manage.auth0.com/">Auth0 Portal</a> login with your preferred account, create a tenant navigate under Applications &gt; Default App and pick up the Domain, as seen in the image below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*ftKR9RCcLA32Us8HA6-47w.png" /><figcaption>Figure 2. Default App in Auth0 management portal</figcaption></figure><p>Update the file resource-manifests/istio/security/auth-policy.yaml to use your domain:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98bbc68c6058e8c9f6a5ffec2c797d84/href">https://medium.com/media/98bbc68c6058e8c9f6a5ffec2c797d84/href</a></iframe><p>With this resource, the pilot configures the envoys to authenticate requests before forwarding them to the services: <strong>sa-web-app</strong> and <strong>sa-feedback</strong>. At the same time, this is not applied to the envoys of the service <strong>sa-frontend</strong> enabling us to get the frontend unauthenticated. To apply the Policy, execute the command:</p><pre><strong>$ kubectl apply -f resource-manifests/istio/security/auth-policy.yaml<br></strong>policy.authentication.istio.io “auth-policy” created</pre><p>Go back to the page and make a request, you will see that it will end in 401 Unauthorized, now let’s forward users from the frontend to authenticate with Auth0.</p><h3>Authenticating Requests with Auth0</h3><p>To authenticate requests of an End User we need to create an API in Auth0 that represents the authenticated services namely: reviews, details, and ratings. To create an API, navigate to <strong>Auth0 Portal</strong> &gt; <strong>APIs</strong> &gt; <strong>Create API</strong>, as seen in the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*5HTL6Ji4yVn1mmzFrsoquA.png" /><figcaption>Figure 3. Creating a new API in Auth0</figcaption></figure><p>The important information here is the Identifier later used in the script as:</p><ul><li><strong>Audience:</strong> {YOUR_AUDIENCE}</li></ul><p>And the rest of the needed details are under <strong>Applications</strong> in the Auth0 Portal and then select the <strong>Test Application</strong> created automatically with the same name as the API.</p><p>Here note down:</p><ul><li><strong>Domain:</strong> {YOUR_DOMAIN}</li><li><strong>Client Id: </strong>{YOUR_CLIENT_ID}</li></ul><p>Scroll down in the Test Application to the <strong>Allowed Callback URLs </strong>text field, where we specify the URL where the call should be forwarded after the Authentication is completed, in our case it is:</p><p><a href="http://{external_ip}/callback">http://{EXTERNAL_IP}/callback</a></p><p>Add for the <strong>Allowed Logout URLs </strong>add the following URL:</p><p><a href="http://{external_ip}/logout">http://{EXTERNAL_IP}/logout</a></p><p>Let’s move over to the frontend.</p><h3>Updating the Frontend</h3><p>Checkout to the branch<strong> auth0</strong> of the [istio-mastery] repository. In this branch the frontend contains code changes to forward users to Auth0 for authentication and uses the JWT Token in requests to the other services as shown below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d320aaf4ad7a2e7326e0f7c4e56040a/href">https://medium.com/media/8d320aaf4ad7a2e7326e0f7c4e56040a/href</a></iframe><p>To update the frontend to use your tenant’s details navigate to the file sa-frontend/src/services/Auth.js and replace the following values, with the ones we noted down earlier:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd1a7a4f6ce84fe4665bd6dde09aaa10/href">https://medium.com/media/cd1a7a4f6ce84fe4665bd6dde09aaa10/href</a></iframe><p>The application is ready, specify your docker user id in the command below and then build and deploy the changes:</p><pre><strong>$ docker build -f sa-frontend/Dockerfile \<br> -t $DOCKER_USER_ID/sentiment-analysis-frontend:istio-auth0 \<br> sa-frontend</strong></pre><pre><strong>$ docker push $DOCKER_USER_ID/sentiment-analysis-frontend:istio-auth0</strong></pre><pre><strong>$ kubectl set image deployment/sa-frontend \<br> sa-frontend=$DOCKER_USER_ID/sentiment-analysis-frontend:istio-auth0</strong></pre><p>Give the app a try! You will be forwarded to Auth0 where you have to log in (or register) and are forwarded back to the page and can make Authenticated Requests. Meanwhile, if you try the earlier curl commands you will get a 401 Status Code indicating that the request was Unauthorized.</p><p>Let’s go one step further and authorize requests.</p><h3>Authorization with Auth0</h3><p>Authentication enables us to know who a user is, but we need the authorization to know what they can access. Istio provides the tools for this as well!</p><p>As an example, we’ll create two groups of users (shown in figure 24):</p><ul><li><strong>Users</strong>: with access to only the SA-WebApp and SA-Frontend service.</li><li><strong>Moderators</strong>: who can access all three services.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*ywhs7Ccrmomw-_saf_Ny5Q.png" /><figcaption>Figure 4. Authorization Concept</figcaption></figure><p>To create the user groups, we will use the Auth0 Authorization extension, and then using Istio we will provide them with different levels of access.</p><h3>Install and Configure Auth0 Authorization</h3><p>In the Auth0 portal navigate to Extensions and install the ‘Auth0 Authorization’ extension. After the installation, navigate to the Authorization Extension and configure by clicking your tenant in the top right corner and selecting the menu option “Configuration”. Enable Groups and click the button <strong>Publish rule</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*Ws7lIabKUyps3tiYri8NIg.png" /><figcaption>Figure 5. Activating Groups in Token Contents</figcaption></figure><h3>Create Groups</h3><p>In the Authorization Extension navigate to <strong>Groups</strong> and create the Moderators group. Meanwhile, we will treat all Authenticated Users as Regular Users and as such, there is no need to create an additional group.</p><p>Select the Moderators group and click Add Members, add your main account. Leave some users without any Group to verify that for them access is forbidden. (You can register new Users manually in Auth0 Portal &gt; Users &gt; Create User)</p><h3>Add Group Claim to the Access Token</h3><p>Users are added to the groups, but this information will not be reflected in the access token. To stay OpenID Connect conformant and at the same time to return the groups we need to <a href="https://auth0.com/docs/tokens/access-token#add-custom-claims">add custom namespaced claims</a> to the token. This can be done using Auth0 rules.</p><p>To create a rule in the Auth0 Portal navigate to Rules, click “Create Rule” and pick an <strong>empty rule</strong> from the templates.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*cYQlF2UIETjY85hV9Tixzw.png" /><figcaption>Figure 6. Creating a new Rule</figcaption></figure><p>Paste the code below and save the new rule named as “Add Group Claim”.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e00870e70c8ad4f9a711dd95873926cb/href">https://medium.com/media/e00870e70c8ad4f9a711dd95873926cb/href</a></iframe><p><strong>Note:</strong> This code picks the first group of the user as defined in the Authorization Extension and adds it to the access token as a custom namespaced claim.</p><p>Go back to the <strong>Rules page</strong> and verify that you got two roles in this order:</p><ul><li>auth0-authorization-extension</li><li>Add Group Claim</li></ul><p>The order matters, because the group field is retrieved asynchronously by the <strong>auth0-authorization-extension</strong> rule and then it is added as a namespaced claim by the second rule, resulting in the following access token:</p><pre>{<br> &quot;https://sa.io/group&quot;: &quot;Moderators&quot;,<br> &quot;iss&quot;: &quot;https://sentiment-analysis.eu.auth0.com/&quot;,<br> &quot;sub&quot;: &quot;google-oauth2|196405271625531691872&quot;<br> // [shortened for brevity]<br>}</pre><p>Now we must configure the Envoy proxies to verify user access by extracting the group from the claim https://sa.io/group in the returned access token. That is the topic of the next section, let’s move over there.</p><h3>Configuring Authorization in Istio</h3><p>For authorization to kick in we need to enable RBAC for Istio. To do so apply to the Mesh the following configuration:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2ec6a4d8ef6046429eed07636a71ae2b/href">https://medium.com/media/2ec6a4d8ef6046429eed07636a71ae2b/href</a></iframe><ol><li>Enables RBAC only for the services and or namespaces specified in the Inclusion field.</li><li>Include the list of services specified.</li></ol><p>Apply the configuration by executing the command below:</p><pre><strong>$ kubectl apply -f resource-manifests/istio/security/enable-rbac.yaml<br></strong>rbacconfig.rbac.istio.io/default created</pre><p>Now all services require Role-Based Access Control, in other words, access to all services is denied and will result in the response “RBAC: access denied”. Enabling access to authorized users will be the topic of the next sections.</p><h3>Configuring Regular User access</h3><p>All users should be able to access the <strong>SA-Frontend</strong> and <strong>SA-WebApp </strong>services, this is achieved with these Istio’s resources:</p><ul><li><strong>ServiceRole:</strong> specifies the permissions that a user has and</li><li><strong>ServiceRoleBinding:</strong> specifies to whom a ServiceRole applies.</li></ul><p>For regular users we will allow access to the specified services:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/01acb634dc776b4de29df10033adee66/href">https://medium.com/media/01acb634dc776b4de29df10033adee66/href</a></iframe><p>And using the <strong>regular-user-binding</strong> we will apply the ServiceRole to all visitors of our page:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c782756e173b41710972d1818b0b4bb7/href">https://medium.com/media/c782756e173b41710972d1818b0b4bb7/href</a></iframe><p>Oh! All users this means that unauthenticated users can SA WebApp? No, the policy will still check the validity of the JWT token. 😉</p><p>Apply the configurations:</p><pre><strong>$ kubectl apply -f resource-manifests/istio/security/user-role.yaml<br></strong>servicerole.rbac.istio.io/regular-user created<br>servicerolebinding.rbac.istio.io/regular-user-binding created</pre><h3>Configuring Moderator User access</h3><p>For our moderators we want to enable access to all the services:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/14754faca5e4f1d8dc019104fc71edff/href">https://medium.com/media/14754faca5e4f1d8dc019104fc71edff/href</a></iframe><p>But we want to bind it only to users whose Access Token has the claim https://sa.io/group equal to the value Moderators.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4d8fb9ef312e572270fb3b17d0a5256c/href">https://medium.com/media/4d8fb9ef312e572270fb3b17d0a5256c/href</a></iframe><p>To apply the configurations, execute:</p><pre><strong>$ kubectl apply -f resource-manifests/istio/security/mod-role.yaml<br></strong>servicerole.rbac.istio.io/mod-user created<br>servicerolebinding.rbac.istio.io/mod-user-binding created</pre><p>Due to caching in the envoys it could take a couple of minutes for the Authorization rules to go in effect, but after that, you will be able to verify that Users and Moderators have different levels of access.</p><h3>Part 2 — Summary</h3><p>Seriously have you ever seen any simpler, zero-effort scalable and secure authentication and authorization concept?</p><p>Using only three Istio resources (RbacConfig, ServiceRole, and ServiceRoleBinding) we have fine-grained control of authenticating and authorizing end-user access to our services.</p><p>Additionally, we offload these concerns from our services to the envoys where we:</p><ul><li>Reduce boilerplate code where security issues and bugs could creep in,</li><li>Reduce asinine cases where one endpoint was exposed by forgetting to annotate it.</li><li>You remove the ripple effect of updating all the services every time you add a new role or permission.</li><li>Keep adding new services simple, secure and fast.</li></ul><h3>Conclusion</h3><p><strong>Istio</strong> enables your team, once again to focus their resources on providing business value, without the ceremonial overhead of services, finally sending them back to being <strong>micro</strong>.</p><p>This article provided you with firm knowledge and hands-on practice to get started with Istio in real-world projects.</p><p>I take this opportunity to say thanks for joining me on this voyage, it for sure wasn’t easy and you are amazing for sticking with it. I would love to hear your thoughts in the comments below and feel free to reach out to me on <a href="https://twitter.com/rinormaloku">Twitter</a> or on my page <a href="https://rinormaloku.com/">rinormaloku.com</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b079f77358ac" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-cloud/back-to-microservices-with-istio-part-2-authentication-authorization-b079f77358ac">Back to Microservices with Istio (Part 2) — Authentication &amp; Authorization</a> was originally published in <a href="https://medium.com/google-cloud">Google Cloud - Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Back to Microservices with Istio (Part 1)]]></title>
            <link>https://medium.com/google-cloud/back-to-microservices-with-istio-p1-827c872daa53?source=rss-cf86e8b1d3bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/827c872daa53</guid>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[istio]]></category>
            <dc:creator><![CDATA[Rinor Maloku]]></dc:creator>
            <pubDate>Tue, 19 Feb 2019 20:04:40 GMT</pubDate>
            <atom:updated>2022-06-07T13:44:13.738Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PP6I7VYPqqzwfovHrr8DLw.jpeg" /></figure><blockquote><strong>NOTE: </strong>This article is outdated. My newest article “<a href="https://www.freecodecamp.org/news/learn-istio-manage-microservices/">Learn Istio — How to manage, secure, and monitor your services</a>” works with the latest version of Istio.</blockquote><p><strong>Istio</strong> is an Open Source project developed in partnership between teams from Google, IBM, and Lyft and it provides a solution to the complexities of microservice-based applications, to name a few:</p><ul><li><strong>Traffic management</strong>: Timeouts, retries, load balancing,</li><li><strong>Security: </strong>End-user Authentication and Authorization,</li><li><strong>Observability:</strong> Tracing, monitoring, and logging.</li></ul><p>All of these can be solved in the application layer, but your services don’t end up being so ‘Micro’ anymore, all the additional effort for implementing these is a strain in the company’s resources, resources that can be used to deliver business value. Let’s take an example:</p><blockquote>PM: How long will it take to add a feedback feature?</blockquote><blockquote>Dev: Two sprints.</blockquote><blockquote>PM: What…? That’s just a CRUD!</blockquote><blockquote>Dev: Creating the CRUD is the easy part but then we need to authenticate and authorize users and services. And because the network is not reliable we need to implement retries, and circuit breakers in the clients, and to make sure that we do not take the whole system down we need Timeouts and Bulkheads, additionally to detect issues we need monitoring, tracing […]</blockquote><blockquote>PM: Let’s just stick it in the Product Service then. Jeez!</blockquote><p>You get the idea, all the ceremony, and effort that must go in, for us to add one service is enormous. In this article, we’ll showcase how Istio removes all the above-mentioned cross-cutting concerns from our services.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/378/1*Z9G1wbqPf1MhpDwJUKz1ZQ.png" /><figcaption>Figure 1. The Ceremony of a Microservice</figcaption></figure><p><strong>Note:</strong> This article assumes that you have a working knowledge of Kubernetes. If it’s not the case I recommend you to read <a href="https://medium.freecodecamp.org/learn-kubernetes-in-under-3-hours-a-detailed-guide-to-orchestrating-containers-114ff420e882">my introduction to Kubernetes</a>, and then continue with this article.</p><h3>The Idea of Istio</h3><p>In a world without Istio one service makes direct requests to another and in cases of failure, the service needs to handle it by retrying, timeouting, opening the circuit breaker etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*NZLDRzdLh4WgP8E9t08QBQ.png" /><figcaption>Figure 2. Network traffic in Kubernetes</figcaption></figure><p>To resolve this, Istio provides an ingenious solution by being completely separated from the services and act only by intercepting all network communication. And doing so it can implement:</p><ul><li><strong>Fault Tolerance</strong> — Using response status codes it understands when a request failed and retries.</li><li><strong>Canary Rollouts</strong> — Forward only the specified percentage of requests to a new version of the service.</li><li><strong>Monitoring and Metrics</strong> — The time it took for a service to respond.</li><li><strong>Tracing and Observability</strong> — It adds special headers in every request and traces them in the cluster.</li><li><strong>Security</strong> — Extracts the JWT Token and Authenticates and Authorizes users.</li></ul><p>To name a few (for real just a few) and get you intrigued! Let’s get to the Technical details!</p><h3>Istio’s Architecture</h3><p>Istio intercepts all network traffic and applies a set of rules by injecting an intelligent proxy as a sidecar in every pod. The proxies that enable all the features comprise <strong>The Data Plane</strong> and those are dynamically configurable by <strong>The Control Plane.</strong></p><h3>The Data Plane</h3><p>The injected proxies enable Istio to easily achieve our requirements. For an example let’s check out the retrying and Circuit breaking functionalities.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*tHn9Gv_6UlfB0jQzlX2BSw.gif" /><figcaption>Figure 3. How envoys implement Retries &amp; CircuitBreaking</figcaption></figure><p>To summarize:</p><ol><li>Envoy sends the request to the first instance of service B and it fails.</li><li>The Envoy Sidecar retries. (1)</li><li>Returns a failed request to the calling proxy.</li><li>Which opens the Circuit Breaker and calls the next Service on subsequent requests. (2)</li></ol><p>This means that you don’t have to use another Retry library, you don’t have to develop your own implementation of Circuit Breaking and Service Discovery in Programming Language X, Y or Z. All of those and more are provided out of the box by Istio and <strong>NO </strong>code changes are required.</p><p>Great! Now you want to join the voyage with Istio, but you still have some doubts, some open questions. Is this a One-Size-Fits-All-Solution, which you’re suspicious about, as it always ends up being One-Size-Fits None solution!</p><p>You finally mutter the question: “Is this configurable?”</p><p>Welcome to the cruise my friend and let’s get introduced to the Control Plane.</p><h3>The Control Plane</h3><p>Is composed of three components: The <strong>Pilot</strong>, the <strong>Mixer,</strong> and the <strong>Citadel</strong> that in combination configure Envoys to route traffic, enforce policies and collect telemetry data. Visually presented in the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*8gH0GAnncEE6VUIbwnGUww.png" /><figcaption>Figure 4. Control Plane in relation to Data Plane</figcaption></figure><p>The envoys (i.e. the data plane) are configured using <a href="https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/">Kubernetes Custom Resource Definitions</a> defined by Istio and specialized for this purpose. Which means that for you it’s just another Kubernetes Resource with a familiar syntax. Which after being created will be picked up by <strong>the control plane</strong> that applies it to the envoys.</p><h3>Relation of Services to Istio</h3><p>We described the relation of Istio to our Services, but not the other way around, what’s the relation of our Services to Istio?</p><p>Frankly, our services have as much knowledge of Istio’s presence, as fish do of water, they will ask themselves “What the hell is water?”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PaPk2TSvDlGJdXPubvXQ1g.png" /><figcaption>Drawing by <a href="https://fityourself.club/@victoriadimitrakopoulos">Victoria Dimitrakopoulos</a></figcaption></figure><p>This means that you can pick a working cluster and after deploying the components of Istio, the services within will continue to work and in the same manner, you can remove the components and everything will be just fine. Understandably, you would lose the capabilities provided by Istio.</p><p>We had enough of theory, let’s put this knowledge into practice!</p><h3>Istio in Practice</h3><p>Istio requires a Kubernetes Cluster with at least 4 vCPU and 8 GB of RAM. To quickly set up a cluster and follow up with this article, I recommend using the Google Cloud Platform, which provides new users with a <a href="https://console.developers.google.com/billing/freetrial?hl=en">$300 free trial</a>.</p><p>After creating the cluster and configuring access with the Kubernetes command line tool we’re ready to install Istio using the Helm Package manager.</p><h3>Installing Helm</h3><p>Install the Helm client on your computer as explained in <a href="https://docs.helm.sh/using_helm/#installing-the-helm-client">the official docs</a>. Which we will use to generate the Istio installation templates in the next section.</p><h3>Installing Istio</h3><p>Download Istio’s resources from the <a href="https://github.com/istio/istio/releases/tag/1.0.5">latest release</a>, extract the contents into one directory that we will refer to as [istio-resources].</p><p>To easily identify the Istio resources create a namespace istio-system in your Kubernetes Cluster:</p><pre><strong>$ kubectl create namespace istio-system</strong></pre><p>Complete the installation by navigating to [istio-resources] directory and executing the command below:</p><pre><strong>$ helm template install/kubernetes/helm/istio \<br>  --set global.mtls.enabled=false \<br>  --set tracing.enabled=true \<br>  --set kiali.enabled=true \<br>  --set grafana.enabled=true \<br>  --namespace istio-system &gt; istio.yaml</strong></pre><p>The above command prints out the core components of Istio into the file istio.yaml. We customized the template using the following parameters:</p><ul><li><strong>global.mtls.enabled</strong> is set to false to keep the introduction focused.</li><li><strong>tracing.enabled </strong>enables tracing of requests using jaeger.</li><li><strong>kiali.enabled </strong>installs Kiali in our cluster for Visualizing Services and Traffic</li><li><strong>grafana.enabled </strong>installs Grafana to visualize the collected metrics.</li></ul><p>Apply the generated resources by executing the command:</p><pre><strong>$ kubectl apply -f istio.yaml</strong></pre><p>This marks the completion of the Istio installation in our cluster! Wait until all pods in the istio-system namespace are in Running or Completed state by executing the command below:</p><pre><strong>$ kubectl get pods -n istio-system</strong></pre><p>Now we’re ready to continue with the next section, where we will get the sample application up and running.</p><h3>Sentiment Analysis Application Architecture</h3><p>We will use the same microservice application used in the <a href="https://medium.freecodecamp.org/learn-kubernetes-in-under-3-hours-a-detailed-guide-to-orchestrating-containers-114ff420e882">Kubernetes Introduction article</a>, it’s complex enough to showcase Istio’s features in practice.</p><p>The App is composed of four microservice:</p><ul><li><strong>SA-Frontend service</strong>: Serves the frontend Reactjs application.</li><li><strong>SA-WebApp service</strong>: Handles requests for Sentiment Analysis.</li><li><strong>SA-Logic service</strong>: Performs sentiment analysis.</li><li><strong>SA-Feedback service</strong>: Receives feedbacks from the users about the accuracy of the analysis.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*AcfdupyEsbdgTtKV6obx7A.png" /><figcaption>Figure 6 Sentiment Analysis microservices</figcaption></figure><p>In figure 6, besides the services we see the Ingress Controller which in Kubernetes routes incoming requests to the appropriate services, Istio uses a similar concept called Ingress Gateway, which will be introduced in continuation of the article.</p><h3>Running the Application with Istio Proxies</h3><p>To follow up with this article clone the repository <a href="https://github.com/rinormaloku/istio-mastery">istio-mastery</a>, containing the application and the manifests for Kubernetes and Istio.</p><h4>Sidecar Injection</h4><p>The injection is done <strong>Automatically</strong> or <strong>Manually</strong>. To enable automatic sidecar injection, we need to label the namespace with istio-injection=enabled, by executing the command below:</p><pre><strong>$ kubectl label namespace default istio-injection=enabled<br></strong>namespace/default labeled</pre><p>From now every pod that gets deployed into the default namespace will get the sidecar injected. To verify this let’s deploy the sample application by navigating to the root folder of the [istio-mastery] repository and executing the following command:</p><pre><strong>$ kubectl apply -f resource-manifests/kube<br></strong>persistentvolumeclaim/sqlite-pvc created<br>deployment.extensions/sa-feedback created<br>service/sa-feedback created<br>deployment.extensions/sa-frontend created<br>service/sa-frontend created<br>deployment.extensions/sa-logic created<br>service/sa-logic created<br>deployment.extensions/sa-web-app created<br>service/sa-web-app created</pre><p>With the services deployed verify that the pods have two containers (the service and the sidecar) by executing the command <strong>kubectl get pods </strong>and ensuring that under the column ready, we see the value “<strong>2/2</strong>” indicating that both containers are running. As seen below:</p><pre><strong>$ kubectl get pods</strong><br>NAME                           READY     STATUS    RESTARTS   AGE<br>sa-feedback-55f5dc4d9c-c9wfv   2/2       Running   0          12m<br>sa-frontend-558f8986-hhkj9     2/2       Running   0          12m<br>sa-logic-568498cb4d-2sjwj      2/2       Running   0          12m<br>sa-logic-568498cb4d-p4f8c      2/2       Running   0          12m<br>sa-web-app-599cf47c7c-s7cvd    2/2       Running   0          12m</pre><p>Visually presented in figure 7.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/347/1*GeTGpCf5vAdPHzPznKYhdg.png" /><figcaption>Figure 7. Envoy proxy in one of the Pods</figcaption></figure><p>With the application up and running now we need to allow incoming traffic to reach our application.</p><h3>Ingress Gateway</h3><p>A best practice for allowing traffic into the cluster is through Istio’s <strong>Ingress Gateway</strong> which positions itself at the edge of the cluster and on incoming traffic enables Istio’s features like routing, load balancing, security, and monitoring.</p><p>During Istio’s installation, the Ingress Gateway component and a service that exposes it externally were installed into the cluster. To get the services External IP execute the command below:</p><pre><strong>$ kubectl get svc -n istio-system -l istio=ingressgateway</strong><br>NAME                   TYPE           CLUSTER-IP     EXTERNAL-IP<br>istio-ingressgateway   LoadBalancer   10.0.132.127   13.93.30.120</pre><p>In the continuation of this article we will access the application on this IP (referred to as the EXTERNAL-IP), for convenience, save it in a variable by executing the command below:</p><pre><strong>$ EXTERNAL_IP=$(kubectl get svc -n istio-system \<br>  -l app=istio-ingressgateway \<br>  -o jsonpath=&#39;{.items[0].status.loadBalancer.ingress[0].ip}&#39;)</strong></pre><p>If you reach this IP in your browser and you will get a Service Unavailable error, as <strong>by default Istio blocks any incoming traffic</strong> until we define a Gateway.</p><h3>The Gateway Resource</h3><p>A Gateway is a Kubernetes Custom Resource Definition defined upon Istio’s installation in our cluster that enables us to specify the Ports, Protocol and Hosts for which we want to allow incoming traffic.</p><p>In our scenario, we want to allow HTTP traffic on port 80, for all hosts. Achieved with the following definition:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/537dc8f0431ee39082e535b6f864351c/href">https://medium.com/media/537dc8f0431ee39082e535b6f864351c/href</a></iframe><p>All the configuration is self-explanatory besides the selector istio: ingressgateway. Using this selector, we can specify to which Ingress Gateway to apply the configuration, and in our case, it’s the default ingress gateway controller installed on Istio setup.</p><p>Apply the configuration by executing the command below:</p><pre><strong>$ kubectl apply -f resource-manifests/istio/http-gateway.yaml </strong>gateway.networking.istio.io/http-gateway created</pre><p>The gateway now allows access in port 80 but it has no concept where to route the requests. That is achieved using <strong>Virtual Services</strong>.</p><h3>The VirtualService resource</h3><p>The VirtualService instructs the Ingress Gateway how to route the requests that were allowed into the cluster.</p><p>For our application requests coming through the <strong>http-gateway </strong>must be routed to the <strong>sa-frontend, </strong><strong>sa-web-app</strong> and<strong> </strong><strong>sa-feedback </strong>services (show in figure 8).</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*anirEAIId7_cg_obeYW4Zw.png" /><figcaption>Figure 8. Routes to be configured with VirtualServices</figcaption></figure><p>Let’s break down the requests that should be routed to SA-Frontend:</p><ul><li><strong>Exact path </strong><strong>/</strong>should be routed to SA-Frontend to get the Index.html</li><li><strong>Prefix path </strong><strong>/static/*</strong> should be routed to SA-Frontend to get any static files needed by the frontend, like Cascading Style Sheets and JavaScript files.</li><li><strong>Paths matching the regex </strong><strong>&#39;^.*\.(ico|png|jpg)$&#39;</strong> should be routed to SA-Frontend as it is an image, that the page needs to show.</li></ul><p>This is achieved with the following configuration:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac524824a3255a99da284bafc06141cc/href">https://medium.com/media/ac524824a3255a99da284bafc06141cc/href</a></iframe><p>The important points here are:</p><ol><li>This VirtualService applies to requests coming through the <strong>http-gateway.</strong></li><li>Destination defines the service where the requests are routed to.</li></ol><p><strong>Note:</strong> The configuration above is in the file sa-virtualservice-external.yaml, it also contains the configuration to route to SA-WebApp and SA-Feedback but was shortened for brevity.</p><p>Apply the VirtualService by executing:</p><pre><strong>$ kubectl apply -f resource-manifests/istio/sa-virtualservice-external.yaml<br></strong>virtualservice.networking.istio.io/sa-external-services created</pre><p><strong>Note:</strong> When we apply Istio resources the Kubernetes API Server creates an event received by Istio’s Control Plane which then applies the new configuration to the envoy proxies of every pod. And the Ingress Gateway controller is another Envoy which is configured by the Control Plane, visually presented in figure 9.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*0XHfOTn7svQ8nvChkKmE5Q.png" /><figcaption>Figure 9. Configuring <strong>Istio-IngressGateway</strong> to route requests</figcaption></figure><p>The Sentiment Analysis app is now accessible on http://{EXTERNAL-IP}/. If you get a Not Found status, do not worry <em>sometimes it takes a bit for the configuration to go in effect and update the envoy caches</em>.</p><p>Before moving to the next section use the app to generate some traffic.</p><h3>Kiali — Observability</h3><p>To access Kiali’s Admin UI execute the command below:</p><pre>$ kubectl port-forward \<br>    $(kubectl get pod -n istio-system -l app=kiali \<br>    -o jsonpath=&#39;{.items[0].metadata.name}&#39;) \<br>    -n istio-system 20001</pre><p>And open <a href="http://localhost:20001/">http://localhost:20001/</a> login using “admin” (without quotes) for user and password. There is a ton of useful features, for example checking the configurations of Istio Components, visualizing services according to the information collected by intercepting network requests and answering, “who is calling who?”, “which version of a service has failures?” etc. Take some time to checkout Kiali before moving on to the next goodie, visualizing metrics with Grafana!</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*5s-LAV-rWNMWkWkkRfH8jg.png" /><figcaption>Figure 10. Kiali — Service Observability</figcaption></figure><h3>Grafana — Metrics Visualization</h3><p>The metrics collected by Istio are scraped into Prometheus and Visualized using Grafana. To access the Admin UI of Grafana execute the command below and open <a href="http://localhost:3000.">http://localhost:3000.</a></p><pre>$ kubectl -n istio-system port-forward \<br>    $(kubectl -n istio-system get pod -l app=grafana \<br>    -o jsonpath={.items[0].metadata.name}) 3000</pre><p>On the top left click the menu <strong>Home </strong>and<strong> </strong>select<strong> Istio Service Dashboard </strong>and on the top left corner select the service starting with<strong> sa-web-app, </strong>you will be presented with the collected metrics, as seen on the image below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*2dfPTHXZnFZ2attoax7GKQ.png" /></figure><p>Holly molly that’s an empty and totally non-exciting view, management would never approve of this. Let’s cause some load by executing the command below:</p><pre>$ while true; do \<br>    curl -i <a href="http://$external_ip/sentiment">http://$EXTERNAL_IP/sentiment</a> \<br>    -H “Content-type: application/json” \<br>    -d ‘{“sentence”: “I love yogobella”}’; \<br>    sleep .8; done</pre><p>Now we have prettier graphs 😊, and additionally, we have the amazing tools of Prometheus for monitoring and Grafana for visualizing the metrics that enable us to know the performance, health, improvement or degradation of our services throughout time!</p><p>Lastly, we will investigate Tracing requests throughout services.</p><h3>Jaeger — Tracing</h3><p>We need tracing because the more services we have the harder it gets to pinpoint the cause of failure. Let’s take the simple case in the image below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*8jBrTp5SF4ONfWUnJ2ux3Q.png" /><figcaption>Figure 12. A commonly random failed request</figcaption></figure><p>The request goes in, failure goes out, <em>what was the cause?</em> <em>The first service?</em> <em>Or the second?</em> Exceptions are in both, Let’s get down to the logs of each. How many times do you find yourself doing this? Our job feels more like Software Detectives than Developers.</p><p>This is a prevalent problem in Microservices and it’s solved using Distributed Tracing Systems where the services pass a unique header to each other and then this information is forwarded to the Distributed Tracing System where the request trace is put together. An example is shown in figure 13.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*9355tU9NGsRR04GwwVbl8w.png" /><figcaption>Figure 13. TraceId used to identify the span of a request</figcaption></figure><p>Istio uses Jaeger Tracer that implements the OpenTracing API, a vendor-neutral framework. To get access the Jaegers UI execute the command below:</p><pre>$ kubectl port-forward -n istio-system \<br>    $(kubectl get pod -n istio-system -l app=jaeger \<br>    -o jsonpath=&#39;{.items[0].metadata.name}&#39;) 16686</pre><p>Then open the UI in <a href="http://localhost:16686/">http://localhost:16686</a>, select the <strong>sa-web-app </strong>service, <em>if the service is not shown on the dropdown generate some activity on the page and hit refresh</em>. Afterward click the button <strong>Find Traces, </strong>which<strong> </strong>displays the most recent traces, select any and a detailed breakdown of all the traces will be shown<strong>, </strong>as seen in figure 14.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*mMKj2ld0zeTYVF1F4FmTBw.png" /><figcaption>Figure 14. Jaeger — a request trace</figcaption></figure><p>The trace shows:</p><ol><li>The request comes to the <strong>istio-ingressgateway</strong> (it’s the first contact with one of the services so for the request the Trace ID is generated) then the gateway forwards the request to the <strong>sa-web-app</strong> service.</li><li>In the <strong>sa-web-app</strong> service, the request is picked up by the Envoy sidecar and a span child is created (that’s why we see it in the traces) and forwarded to the <strong>sa-web-app</strong> container instance.</li><li>Here the method <strong>sentimentAnalysis</strong> handles the request. These traces are generated by the application, meaning that code changes were required).</li><li>From where a POST request is started to <strong>sa-logic. </strong>Trace ID needs to be propagated by <strong>sa-web-app</strong>.</li></ol><p>5. …</p><p><strong>Note:</strong> At the 4th point our application needs to pick up the headers generated by Istio and pass them down on the next requests, as shown in the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*YDMLGTWmx3l9QmUXT7TwZQ.png" /><figcaption>Figure 15. (A) Istio propagating headers, <strong>(B) Services propagating headers</strong></figcaption></figure><p>Istio does the main heavy lifting as it generates the headers on incoming requests, creates new spans on every sidecar, propagates them, but without our services propagating the headers as well, we will lose the full trace of the request.</p><p>The headers to propagate are:</p><pre>x-request-id<br>x-b3-traceid<br>x-b3-spanid<br>x-b3-parentspanid<br>x-b3-sampled<br>x-b3-flags<br>x-ot-span-context</pre><p>Despite it being a simple task, there are already <a href="https://github.com/opentracing-contrib">many libraries</a> that simplify the process, for example in the <strong>sa-web-app</strong> service, the <strong>RestTemplate</strong> client is instrumented to propagate the headers by simply adding the Jaeger and OpenTracing libraries in the <a href="https://github.com/rinormaloku/istio-mastery/blob/master/sa-webapp/pom.xml#L36-L47">dependencies</a>.</p><p><em>Note: The Sentiment Analysis app showcases implementations for Flask, Spring and ASP.NET Core.</em></p><p>Now after investigating what we get out of the box (or partially out of the box 😜) let’s get to the main topic here, fine-grained routing, managing network traffic, security and more!</p><h3>Traffic Management</h3><p>Using the Envoy’s Istio provides a host of new capabilities to your cluster enabling:</p><ul><li><strong>Dynamic request routing</strong>: Canary deployments, A/B testing,</li><li><strong>Load balancing: </strong>Simple<strong> </strong>and Consistent Hash balancing,</li><li><strong>Failure Recovery</strong>: Timeouts, retries, circuit breakers,</li><li><strong>Fault Injection</strong>: Delays, abort requests etc.</li></ul><p>In the sequence of this article, we’ll showcase these capabilities in our application and get introduced to new concepts along the way. The first concept we will delve into is DestinationRules and using those we’ll enable A/B Testing.</p><h3>A/B Testing — Destination Rules in Practice</h3><p>A/B Testing is used when we have two versions of an application (usually the versions differ visually) and we aren’t 100% sure which will increase user interaction, so we try both versions at the same time and collect metrics.</p><p>Execute the command below to deploy a second version of the frontend, needed for demonstrating A/B Testing:</p><pre><strong>$ kubectl apply -f resource-manifests/kube/ab-testing/sa-frontend-green-deployment.yaml<br></strong>deployment.extensions/sa-frontend-green created</pre><p>The deployment manifest for the green version differs in two points:</p><ol><li>The image is based on a different tag: <strong>istio-green</strong> and</li><li>Pods are labeled with <strong>version: green</strong>.</li></ol><p>And as both deployments have the label <strong>app: sa-frontend</strong> requests routed by the virtual service <strong>sa-external-services </strong>to the service <strong>sa-frontend</strong> will be forwarded to all of its instances and will be load balanced using the round robin algorithm, which causes the issue presented in figure 16.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*JD0GTy4FbRP9EHIFK80ZLA.png" /><figcaption>Figure 16. Requested files are not found</figcaption></figure><p>The files are not found because they are named differently in the different versions of the app. Let’s verify that:</p><pre><strong>$ curl --silent </strong><a href="http://$external_ip/"><strong>http://$EXTERNAL_IP/</strong></a><strong> | tr &#39;&quot;&#39; &#39;\n&#39; | grep main<br></strong>/static/css/main.c7071b22.css<br>/static/js/main.059f8e9c.js<br><strong>$ curl --silent </strong><a href="http://$external_ip/"><strong>http://$EXTERNAL_IP/</strong></a><strong> | tr &#39;&quot;&#39; &#39;\n&#39; | grep main<br></strong>/static/css/main.f87cd8c9.css<br>/static/js/main.f7659dbb.js</pre><p>This means that the index.html that requests one version of the static files might be load balanced to the pods delivering the other version, where understandably the other files do not exist.</p><p>This means that for our app to work we need to introduce the restriction that <strong>“the version of the app that served the index.html, must serve subsequent requests”</strong>.</p><p>We’ll achieve this using Consistent Hash Loadbalancing, which<strong> is the process that forwards requests from the same client to the same backend instance, </strong>using a predefined property, like for example an HTTP header<strong>. </strong>Made possible by<strong> </strong>DestionatioRules.</p><h3>DestinationRules</h3><p>After a request gets routed by the <strong>VirtualService</strong> to the correct service, then using <strong>DestinationRules</strong> we can specify policies that apply to the traffic intended for the instances of this Service, as seen in figure 17.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*jBqAaJnlk_XIKZ8unokfrg.png" /><figcaption>Figure 17. Traffic Management with Istio Resources</figcaption></figure><p><strong>Note:</strong> Figure 17, visualizes how Istio resources are affecting the network traffic, in an easily understandable way. But, to be precise the decision to which instance to forward the request to is made in the Ingress Gateway’s Envoy, configured by the CRDs.</p><p>Using Destination Rules we can configure load balancing to have consistent hashing and ensure that the same user is responded by the same instance of the service. Achieved with the following configuration:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f000ed39c1abf754a053933703a69eed/href">https://medium.com/media/f000ed39c1abf754a053933703a69eed/href</a></iframe><ol><li>Generate a consistent hash according to the contents of the “version” header.</li></ol><p>Apply the configuration by executing the command below and give it a try!</p><pre><strong>$ kubectl apply -f resource-manifests/istio/ab-testing/destinationrule-sa-frontend.yaml<br></strong>destinationrule.networking.istio.io/sa-frontend created</pre><p>Execute the command below and verify that you get the same files when specifying the version header:</p><pre><strong>$ curl --silent -H &quot;version: yogo&quot; </strong><a href="http://$external_ip/"><strong>http://$EXTERNAL_IP/</strong></a><strong> | tr &#39;&quot;&#39; &#39;\n&#39; | grep main</strong></pre><p><strong>Note: </strong>You can use this <a href="https://chrome.google.com/webstore/detail/modheader/idgpnmonknjnojddfkpgkljpfnnfcklj">chrome extension</a> to add different values to the version header, to test in your browser.</p><p>DestinationRules have more LoadBalancing capabilities, for all the details check out the <a href="https://preliminary.istio.io/docs/reference/config/istio.networking.v1alpha3.html#LoadBalancerSettings">official docs</a>.</p><p>Before moving on to explore VirtualService in more detail, remove the green version of the app and the destination rule by executing the commands below:</p><pre><strong>$ kubectl delete -f resource-manifests/kube/ab-testing/sa-frontend-green-deployment.yaml<br></strong>deployment.extensions “sa-frontend-green” deleted<br><strong>$ kubectl delete -f resource-manifests/istio/ab-testing/destinationrule-sa-frontend.yaml<br></strong>destinationrule.networking.istio.io “sa-frontend” deleted</pre><h3>Shadowing — Virtual Services in Practice</h3><p>Shadowing or Mirroring is used when we want to test a change in production but not affect end-users, so we mirror the requests into a second instance that has the change and evaluate it. <em>To phrase it simpler it’s when one of your colleagues picks the most critical issue and makes a Big ball of mud Pull Request, that nobody can really review.</em></p><p>To test out this feature lets create a second instance of SA-Logic (<em>which is buggy</em>) by executing the command below:</p><pre><strong>$ kubectl apply -f resource-manifests/kube/shadowing/sa-logic-service-buggy.yaml<br></strong>deployment.extensions/sa-logic-buggy created</pre><p>Execute the following command and verify that all instances are labeled with the respective versions and additionally with <strong>app=sa-logic</strong>:</p><pre><strong>$ kubectl get pods -l app=sa-logic --show-labels</strong><br>NAME                              READY   LABELS<br>sa-logic-568498cb4d-2sjwj         2/2     app=sa-logic,<strong>version=v1</strong><br>sa-logic-568498cb4d-p4f8c         2/2     app=sa-logic,<strong>version=v1</strong><br>sa-logic-buggy-76dff55847-2fl66   2/2     app=sa-logic,<strong>version=v2</strong><br>sa-logic-buggy-76dff55847-kx8zz   2/2     app=sa-logic,<strong>version=v2</strong></pre><p>As the service <strong>sa-logic</strong> targets pods labeled with <strong>app=sa-logic</strong>, any incoming requests will be load balanced between all instances, as shown in figure 18.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*K0L5SspnCi2uI1Wjhl8d8Q.png" /><figcaption>Figure 18. Round Robin load balancing</figcaption></figure><p>But we want requests to be routed to the instances with version v1 and mirrored to the instances with version v2, as shown in figure 19.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*8uf4iKo1yI3FtbcGBfR2bg.png" /><figcaption>Figure 19. Routing to v1 and Mirroring to v2</figcaption></figure><p>This is achieved using a VirtualService in combination with a DestinationRule, where the destination rule specifies the subsets and VirtualService routes to the specific subset.</p><h3>Specifying Subsets with Destination Rules</h3><p>We define the subsets with the following configuration:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a87d885b176332576008cabadd227a37/href">https://medium.com/media/a87d885b176332576008cabadd227a37/href</a></iframe><ol><li>Host defines that this rule applies only when routing has occurred towards <strong>sa-logic</strong> service</li><li>Subset name used when routing to instances of a subset.</li><li>Label defines the key-value pairs that need to match for an instance to be part of the subset.</li></ol><p>Apply the configuration executing the command below:</p><pre><strong>$ kubectl apply -f resource-manifests/istio/shadowing/sa-logic-subsets-destinationrule.yaml<br></strong>destinationrule.networking.istio.io/sa-logic created</pre><p>With the subsets defined we can move on and configure a VirtualService to apply to requests towards <strong>sa-logic</strong> where the requests are:</p><ol><li>Routed to the subset named v1 and,</li><li>Mirrored to the subset named v2.</li></ol><p>And this is achieved with the manifest below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c6543ff79f6bd6533699926f0418f91/href">https://medium.com/media/8c6543ff79f6bd6533699926f0418f91/href</a></iframe><p>As everything is self-explanatory let’s just see it in action:</p><pre><strong>$ kubectl apply -f resource-manifests/istio/shadowing/sa-logic-subsets-shadowing-vs.yaml<br></strong>virtualservice.networking.istio.io/sa-logic created</pre><p>Add some load by executing the following command:</p><pre><strong>$ while true; do curl -v </strong><a href="http://$external_ip/sentiment"><strong>http://$EXTERNAL_IP/sentiment</strong></a><strong> \<br>    -H “Content-type: application/json” \<br>    -d ‘{“sentence”: “I love yogobella”}’; \<br>    sleep .8; done</strong></pre><p>Check the results in Grafana, where we can see that the buggy version is failing about 60% of the requests, but none of the failures affected the end-users as they were responded by the currently active service.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*hgANZ4AinrNZZsZ6jjbVCA.png" /><figcaption>Figure 20. The success rate of sa-logic service versions</figcaption></figure><p>In this section, we saw for the first time a VirtualService that was applied to the envoys of our services, when the <strong>sa-web-app</strong> makes a request towards <strong>sa-logic </strong>this goes through the sidecar envoy, which via the VirtualService is configured to route to the subset v1 and mirror to the subset v2 of the <strong>sa-logic</strong> service.</p><p>I can see you thinking “Darn man Virtual Services are simple!”, in the next section, we’ll extend the sentence to “Simply Amazing!”.</p><h3>Canary Deployments</h3><p>Canary Deployment is the process of rolling out a new version of an application to a small set of users, as a step to verify the absence of issues, and then with a higher assurance of quality release to the wider audience.</p><p>We will continue with the same buggy subset of <strong>sa-logic</strong> to demonstrate canary deployments.</p><p>Let’s start boldly and send 20% of the users to the buggy version (this represents the canary deployment) and 80% to the healthy service by applying the VirtualService below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f73e8b5910255812b163b7ee521e42ea/href">https://medium.com/media/f73e8b5910255812b163b7ee521e42ea/href</a></iframe><ol><li>Weight specifies the percentage of requests to be forwarded to the destination or subset of the destination.</li></ol><p>Update the previous <strong>sa-logic</strong> virtual service configuration using the following commands:</p><pre><strong>$ kubectl apply -f resource-manifests/istio/canary/sa-logic-subsets-canary-vs.yaml<br></strong>virtualservice.networking.istio.io/sa-logic configured</pre><p>We immediately see that some of our requests are failing:</p><pre><strong>$ while true; do \<br>   curl -i </strong><a href="http://$external_ip/sentiment"><strong>http://$EXTERNAL_IP/sentiment</strong></a><strong> \<br>   -H “Content-type: application/json” \<br>   -d ‘{“sentence”: “I love yogobella”}’ \<br>   --silent -w “Time: %{time_total}s \t Status: %{http_code}\n” \<br>   -o /dev/null; sleep .1; done<br></strong>Time: 0.153075s Status: 200<br>Time: 0.137581s Status: 200<br>Time: 0.139345s Status: 200<br>Time: 30.291806s Status: 500</pre><p>VirtualServices enable Canary Deployments and with this method, we reduced potential damages to 20% of our user base. Beautiful! Now we can use Shadowing and Canary Deployments every time we are insecure about our code, in other words always. 😜</p><h3>Timeouts &amp; Retries</h3><p>It’s not always that the code is buggy. In the list of “<a href="https://en.wikipedia.org/wiki/Fallacies_of_distributed_computing#The_fallacies">The 8 fallacies of distributed computing</a>” the first fallacy is that “The network is reliable”. The network is NOT reliable, and that’s why we need Timeouts and Retries.</p><p>For demonstration purposes, we will continue to use the buggy version of <strong>sa-logic</strong>, where the random failures simulate the unreliability of the network.</p><p>The buggy service has a one-third chance of taking too long to respond, one-third chance of ending in an Internal Server Error and the rest complete successfully.</p><p>To alleviate these issues and improve the user experience we can:</p><ol><li>Timeout if the service takes longer than 8 seconds and</li><li>Retry on failed requests.</li></ol><p>This is achieved with the following resource definition:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7583bd312760664c64a37288029dcad8/href">https://medium.com/media/7583bd312760664c64a37288029dcad8/href</a></iframe><ol><li>The request has a timeout of 8 seconds,</li><li>It attempts 3 times,</li><li>An attempt is marked as failed if it takes longer than 3 seconds.</li></ol><p>This is an optimization, as the user won’t be waiting for longer than 8 seconds and we retry three times in case of failures, increasing the chance of resulting in a successful response.</p><p>Apply the updated configuration with the command below:</p><pre><strong>$ kubectl apply -f resource-manifests/istio/retries/sa-logic-retries-timeouts-vs.yaml<br></strong>virtualservice.networking.istio.io/sa-logic configured</pre><p>And check out the Grafana graphs for the improvement in success rate (shown in figure 21).</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*_b94m4BSkeBnBQg_nUUsrA.png" /><figcaption>Figure 21. Improvement after using Timeouts &amp; Retries</figcaption></figure><p>Before moving into the next section delete <strong>sa-logic-buggy</strong> and the VirtualService by executing the command below:</p><pre><strong>$ kubectl delete deployment sa-logic-buggy<br></strong>deployment.extensions “sa-logic-buggy” deleted<br><strong>$ kubectl delete virtualservice sa-logic<br></strong>virtualservice.networking.istio.io “sa-logic” deleted</pre><h3>Circuit Breaker and Bulkhead patterns</h3><p>Two important patterns in Microservice Architectures that enable self-healing of the services.</p><p>The <strong>Circuit Breaker </strong>is used to stop requests going to an instance of a service deemed as unhealthy and enable it to recover, and in the meantime client’s requests are forwarded to the healthy instances of that service (increasing success rate).</p><p>The <strong>Bulkhead pattern</strong> isolates failures from taking the whole system down, to take an example, Service B is in a corrupt state and another service (a client of Service B) makes requests to Service B this will result that the client will use up its own thread pool and won’t be able to serve other requests (even if those are not related to Service B).</p><p>I will skip implementations of these patterns because you can check out implementations in the <a href="https://istio.io/docs/tasks/traffic-management/circuit-breaking/">official docs</a> and I’m way too excited to showcase Authentication and Authorization, which will be the subject of the next article.</p><h3>Part I — Summary</h3><p>In this article, we deployed Istio in a Kubernetes cluster and using its Custom Resource Definitions like <strong>Gateways</strong>, <strong>VirtualServices</strong>, <strong>DestinationRules </strong>and it’s components it enabled the following features:</p><ul><li>Observability over our services by answering what services are running, how are they performing and how are they related, using <strong>Kiali</strong>.</li><li>Metric collection and visualization, with <strong>Prometheus </strong>and <strong>Grafana</strong>.</li><li>Request tracing with <strong>Jaeger</strong> (german for Hunter).</li><li>Full and fine-grained control over the network traffic, enabling <strong>Canary Deployments</strong>, <strong>A/B Testing, and Shadowing</strong>.</li><li>Easy implementation of <strong>Retries, Timeouts, and CircuitBreakers</strong>.</li></ul><p>And all were possible without code changes or any additional dependencies, keeping your services small, easy to operate and maintain.</p><p>For your development team removing these cross-cutting concerns and centralizing them into Istio’s Control plane, means that new services are easy to be introduced, they aren’t resource-heavy as developers can focus in solving business problems. And up to now, no developer complained about “having to solve interesting business problems!”.</p><p>I would love to hear your thoughts in the comments below and feel free to reach out to me on <a href="https://twitter.com/rinormaloku">Twitter</a> or on my page <a href="https://rinormaloku.com/">rinormaloku.com</a>.</p><p>Without further ado let’s move to the second part <a href="https://medium.com/google-cloud/back-to-microservices-with-istio-part-2-authentication-authorization-b079f77358ac">Back to Microservices with Istio (Part 2)</a>, and let’s tackle Authentication and Authorization!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=827c872daa53" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-cloud/back-to-microservices-with-istio-p1-827c872daa53">Back to Microservices with Istio (Part 1)</a> was originally published in <a href="https://medium.com/google-cloud">Google Cloud - Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learn Kubernetes in Under 3 Hours: A Detailed Guide to Orchestrating Containers]]></title>
            <link>https://medium.com/free-code-camp/learn-kubernetes-in-under-3-hours-a-detailed-guide-to-orchestrating-containers-114ff420e882?source=rss-cf86e8b1d3bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/114ff420e882</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Rinor Maloku]]></dc:creator>
            <pubDate>Sat, 14 Apr 2018 19:10:07 GMT</pubDate>
            <atom:updated>2019-10-03T14:19:30.986Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0ju9JOPACF90yXSi-s2gwQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/eUMEWE-7Ewg">Joseph Barrientos</a> on Unsplash</figcaption></figure><p>Why are banks paying me big bucks for something as simple as Kubernetes? When anybody — anybody can learn in under three hours?</p><p>If you doubt me, I challenge you to give it a try! By the end of this article, you will be able to run a Microservice based application on a Kubernetes Cluster. And I guarantee this because it’s how I introduce our clients to Kubernetes.</p><p><em>What does this guide do differently from the other resources, Rinor?</em></p><p>Quite a lot! Most guides start with the simple stuff: Kubernetes concepts and and kubectl commands. These guides assume the reader knows about application development, Microservices, and Docker containers.</p><p>In this Article we will go from :</p><ol><li>Running a Microservice based application on your computer.</li><li>Building container images for each service of the Microservice application.</li><li>Introduction to Kubernetes. Deploying a Microservice based application into a Kubernetes Managed Cluster.</li></ol><p>The gradual build up provides the depth required for a mere mortal being to grasp the <em>simplicity</em> of Kubernetes. Yes Kubernetes is simple when you know the context it is used in. Without further ado lets see what we will build.</p><h3>Application Demo</h3><p>The application has one functionality. It takes one sentence as input. Using Text Analysis, calculates the emotion of the sentence.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/513/1*f-BD5CUxm6a_XPKRGH54gw.gif" /><figcaption>Fig. 1. Sentiment Analysis Web App</figcaption></figure><p>From the technical perspective, the application consists of three microservices. Each has one specific functionality:</p><ul><li><strong>SA-Frontend:</strong> a Nginx web server that <strong>serves our ReactJS </strong>static<strong> </strong>files.</li><li><strong>SA-WebApp: </strong>a Java Web Application that <strong>handles requests </strong>from the frontend.</li><li><strong>SA-Logic: </strong>a python application that <strong>performs Sentiment Analysis</strong>.</li></ul><p>It’s important to know that Microservices don’t live in isolation, they enable “separation of concerns” but they still <strong>have to </strong>interact with each other.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*oH9lP-4GWT8eQHh_X5obsw.gif" /><figcaption>Fig. 2. Data flow in the Sentiment Analysis WebApp</figcaption></figure><p>This interaction is best illustrated by showing how the data flows between them:</p><ol><li>A client application requests the index.html (which in turn requests bundled scripts of ReactJS application)</li><li>The user interacting with the application triggers requests to the Spring WebApp.</li><li>Spring WebApp forwards the requests for sentiment analysis to the Python app.</li><li>Python Application calculates the sentiment and returns the result as a response.</li><li>The Spring WebApp returns the response to the React app. (Which then represents the information to the user.)</li></ol><p>The code for all of these applications can be found in <a href="https://github.com/rinormaloku/k8s-mastery">this repository.</a> I recommend cloning it immediately because we are going to build amazing things together.</p><h3>1. Running a Microservice based application on your computer</h3><p>We need to start up all three services. Let’s get started with the most attractive one, the front-end application.</p><h4>Setting up React for Local Development</h4><p>To start the React application you need to have NodeJS and NPM installed on your computer. After installing those navigate with your Terminal to the directory <strong>sa-frontend. </strong>Type the following command:</p><pre>npm install</pre><p>This downloads all the Javascript dependencies of the React application and places them in the folder <strong>node_modules. </strong>(Dependencies are defined in the package.json file). After all dependencies are resolved execute the next command:</p><pre>npm start</pre><p>That’s it! We started our react application and by default you can access it on <strong>localhost:3000. </strong>Feel free to modify the code and see the effects immediately on the browser. That is made possible using <strong>Hot Module Replacement. </strong>This makes front end development a breeze!</p><h4>Making Our React App Production Ready</h4><p>For production we need to build our application into static files and serve them using a web server.</p><p>To build the React application navigate in your terminal to the <strong>sa-frontend </strong>directory. Then execute the following command:</p><pre>npm run build</pre><p>This generates a folder named <strong>build</strong> in your project tree. This folder contains all the static files needed for our ReactJS application.</p><h4>Serving static files with Nginx</h4><p>Install and start the Nginx WebServer (<a href="https://www.nginx.com/resources/wiki/start/topics/tutorials/install/">how to</a>). Then move the contents of the sa-frontend/build folder to [<em>your_nginx_installation_dir</em>]/<strong>html.</strong></p><p>This way the generated index.html file will be accessible in [<em>your_nginx_installation_dir</em>]/html/index.html. T<strong>his is the default file that Nginx serves</strong>.</p><p>By default the Nginx WebServer listens on port 80. You can specify a different port by updating server.listen property in the file [<em>your_nginx_installation_dir</em>]/conf/nginx.conf.</p><p>Open your browser and hit the endpoint localhost:80, see the ReactJS application appear.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/833/1*ZQ32-7DHQJt-WDKI6nih0Q.png" /><figcaption>Fig. 3. React app served from Nginx</figcaption></figure><p>Typing in the field: “Type your sentence.” and pressing the button Send will fail with a 404 error (You can check it in your browser console). But why that? Let’s inspect the code.</p><h4>Inspecting the Code</h4><p>In the file <strong>App.js </strong>we can see that pressing the Send button triggers the analyzeSentence method. The code for this method is shown below. (Each line that is commented with #Number will be explained below the script):</p><pre>analyzeSentence() {<br>    fetch(&#39;http://<strong>localhost:8080</strong>/sentiment&#39;, {  // #1<br>        method: &#39;POST&#39;,<br>        headers: {<br>            &#39;Content-Type&#39;: &#39;application/json&#39;<br>        },<br>        body: JSON.stringify({<br>                       sentence: this.textField.getValue()})// #2<br>    })<br>        .then(response =&gt; response.json())<br>        .then(data =&gt; this.setState(data));  // #3<br>}</pre><p>#1: URL at which a POST call is made. (An application should be listening for calls at that URL).</p><p>#2: The Request body sent to that application as displayed below:</p><pre>{<br>    sentence: “I like yogobella!”<br>}</pre><p>#3: The response updates the component state. This triggers a re-render of the component. If we received the data, (i.e. the JSON object containing the typed sentence and the polarity) we would display the component polarityComponent because the condition would be fulfilled and we would define the component:</p><pre>const polarityComponent = this.state.polarity !== undefined ?<br>    &lt;Polarity sentence={this.state.sentence} <br>              polarity={this.state.polarity}/&gt; :<br>    null;</pre><p>Everything seems correct. But what are we missing? If you guessed that we didn’t set up anything to listen on localhost:8080, then you are correct! We must start our Spring Web application to listen on that port!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/695/1*puqSKYE9Yc4YBlagoEqMdw.png" /><figcaption>Fig. 4. Microservice Spring WebApp missing</figcaption></figure><h4>Setting up the Spring Web Application</h4><p>To start up the Spring application you need to have JDK8 and Maven installed. (their environment variables need to be set up as well). After installing those you can continue to the next part.</p><h4>Packaging the Application into a Jar</h4><p>Navigate in your Terminal to the directory <strong>sa-webapp </strong>and type the following command:</p><pre>mvn install</pre><p>This will generate a folder named <strong>target,</strong> in the directory <strong>sa-webapp. </strong>In the folder <strong>target</strong> we have our Java application packaged as a jar: ‘<strong>sentiment-analysis-web-0.0.1-SNAPSHOT.jar</strong>’</p><h4>Starting our Java Application</h4><p>Navigate to the target directory and start the application with the command:</p><pre>java -jar sentiment-analysis-web-0.0.1-SNAPSHOT.jar</pre><p>Darn.. We got an error. Our application fails on startup and our only lead is the exception in the stack trace:</p><pre>Error creating bean with name &#39;sentimentController&#39;: Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: <strong>Could not resolve placeholder &#39;sa.logic.api.url&#39;</strong> in value &quot;${sa.logic.api.url}&quot;</pre><p>The important information here is the placeholder sa.logic.api.url in the <strong>SentimentController</strong>. Let’s check that out!</p><h3>Inspecting the Code</h3><pre>@CrossOrigin(origins = &quot;*&quot;)<br>@RestController<br>public class SentimentController {</pre><pre>    @Value(&quot;${sa.logic.api.url}&quot;)    // #1<br>    private String saLogicApiUrl;</pre><pre>    @PostMapping(&quot;/sentiment&quot;)<br>    public SentimentDto sentimentAnalysis(<br>        @RequestBody SentenceDto sentenceDto) <br>    {<br>        RestTemplate restTemplate = new RestTemplate();</pre><pre>        return restTemplate.postForEntity(<br>                saLogicApiUrl + &quot;/analyse/sentiment&quot;,    // #2<br>                sentenceDto, SentimentDto.class)<br>                .getBody();<br>    }<br>}</pre><ol><li>The <strong>SentimentController</strong> has a field named saLogicApiUrl. The field get’s defined by the property sa.logic.api.url .</li><li>The String saLogicApiUrl is concatenated with the value “/analyse/sentiment”. Together they form the URL to make the request for Sentiment Analysis.</li></ol><p><strong>Defining the Property</strong></p><p>In Spring the default property source is <strong>application.properties.</strong> (Located in <em>sa-webapp/src/main/resources</em>). But that’s not the only means to define a property, it can be done with the earlier command as well:</p><pre>java -jar sentiment-analysis-web-0.0.1-SNAPSHOT.jar <br>     --sa.logic.api.url=WHAT.IS.THE.SA.LOGIC.API.URL</pre><p>The property should be initialized with the value that defines where our Python application is running, this way we will let our Spring Web Application know where to forward messages on run time.</p><p>To make things simpler let’s decide that we will run the python application on localhost:5000. Lets just not forget it!</p><p>Run the below command and we are ready to move to the last service the python application.</p><pre>java -jar sentiment-analysis-web-0.0.1-SNAPSHOT.jar <br>     --sa.logic.api.url=http://localhost:5000</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/695/1*jGL9dxdKW56qO2PQnsiP9g.png" /></figure><h4>Setting up the Python Application</h4><p>To start the Python application, we need to have Python3 and Pip installed. (Their environment variables need to be set up as well).</p><h4>Installing Dependencies</h4><p>Navigate in the Terminal to the directory <strong>sa-logic/sa </strong>(<a href="https://github.com/rinormaloku/k8s-mastery">repo</a>)<strong> </strong>and type the following command:</p><pre><strong>python -m pip install -r requirements.txt</strong><br><strong>python -m textblob.download_corpora</strong></pre><h4>Starting the app</h4><p>After using Pip to install the dependencies we are ready to start the application by executing the following command:</p><pre><strong>python sentiment_analysis.py</strong><br>* Running on <a href="http://localhost:5000/">http://0.0.0.0:5000/</a> (Press CTRL+C to quit)</pre><p>This means that our application is running and listening for HTTP Requests on port 5000 on localhost.</p><h4>Inspecting the Code</h4><p>Let’s investigate the code to understand what is happening in the <strong>SA Logic </strong>python application.</p><pre>from textblob import TextBlob<br>from flask import Flask, request, jsonify</pre><pre>app = Flask(__name__)                                   #1</pre><pre>@app.route(&quot;/analyse/sentiment&quot;, methods=[&#39;POST&#39;])      #2<br>def analyse_sentiment():<br>    sentence = request.get_json()[&#39;sentence&#39;]           #3<br>    polarity = TextBlob(sentence).sentences[0].polarity #4<br>    return jsonify(                                     #5<br>        sentence=sentence,<br>        polarity=polarity<br>    )</pre><pre>if __name__ == &#39;__main__&#39;:<br>    app.run(host=&#39;0.0.0.0&#39;, port=5000)                #6</pre><ol><li>Instantiate a Flask object.</li><li>Defines the path at which a POST request can be made.</li><li>Extract the “sentence” property from the request body.</li><li>Instantiate an anonymous TextBlob object and get the polarity from the first sentence. (We have only one).</li><li>Return the response with the body containing the sentence and the polarity to the caller.</li><li>Run the flask object <strong>app </strong>to listen for requests on 0.0.0.0:5000 (calls to localhost:5000 will reach this app aswell).</li></ol><p><strong>The services are set up to communicate with each other. Re-open the frontend in localhost:80 and give them a try before continuing on!</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/695/1*EGpzgc1wUT1hK2j3VGttpQ.png" /><figcaption>Fig. 6. Microservice architecture completed</figcaption></figure><p>In the next section, we will go over how to start the services in Docker Containers, as it is a prerequisite to being able to run them in a Kubernetes Cluster.</p><h3>2. Building container images for each service</h3><p>Kubernetes is a container orchestrator. Understandably we need containers to be able to orchestrate them. But what are containers? This is best answered from the Documentation at docker.</p><blockquote><em>A container image is a lightweight, stand-alone, executable package of a piece of software that includes everything needed to run it: code, runtime, system tools, system libraries, settings. Available for both Linux and Windows based apps, containerized software will always run the same, regardless of the environment.</em></blockquote><p>It means that containers can run on any computer — even in the production server — <strong>with no differences</strong>.</p><p>For illustration purposes let’s compare how our React Application would be served using a Virtual Machine vs. a Container.</p><h4>Serving React static files from a VM</h4><p>The cons of using a Virtual Machine:</p><ol><li>Resource inefficient, each VM has the overhead of a fully-fledged OS.</li><li>It is platform dependent. What worked on your computer might not work on the production server.</li><li>Heavyweight and slow scaling when compared to Containers.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/1*uR6NbK41hf-_TLyje5c-kQ.png" /><figcaption>Fig. 7. Nginx web server with static files on a VM</figcaption></figure><h4>Serving React static files from a Container</h4><p>The pros of using a Container.</p><ol><li>Resource efficient, use the Host OS with the help of Docker.</li><li>Platform independent. The container that you run on your computer will work anywhere.</li><li>Lightweight using image layers.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/1*le1QAML8kXdWM3w1rW3NBA.png" /><figcaption>Fig. 8. Nginx web server serving static files in a Container</figcaption></figure><p>Those are the most prominent features and benefits of using containers. For more information continue reading on the <a href="https://www.docker.com/what-container">Docker documentation</a>.</p><h4>Building the container image for the React App (Docker intro)</h4><p>The basic building block for a Docker container is the .dockerfile. The <strong>Dockerfile</strong> starts with a base container image and follows up with a sequence of instructions on how to build a new container image that meets the needs of your application.</p><p>Before we get started defining the Dockerfile, let’s remember the steps we took to serve the react static files using nginx:</p><ol><li>Build the static files (npm run build)</li><li>Startup the nginx server</li><li>Copy the contents of the <strong>build</strong> folder from your sa-frontend project to nginx<strong>/</strong>html.</li></ol><p>In the next section, you will notice parallels on how creating a Container is similar to what we did during local React setup.</p><h4>Defining the Dockerfile for SA-Frontend</h4><p>The instructions in the Dockerfile for the SA-Frontend is only a two-step task. That is because the Nginx Team provided us with <a href="https://hub.docker.com/_/nginx/">a base image</a> for Nginx, which we will use to build on top of. The two steps are:</p><ol><li>Start from the base <strong>Nginx Image</strong></li><li>Copy the <strong>sa-frontend/build</strong> directory to the containers nginx<strong>/</strong>html directory.</li></ol><p>Converted into a Dockerfile it looks like:</p><pre><strong>FROM </strong>nginx<br><strong>COPY </strong>build <strong>/</strong>usr<strong>/</strong>share<strong>/</strong>nginx<strong>/</strong>html</pre><p>Isn’t it amazing, it’s even humanly readable, let’s recapitulate:</p><p>Start from the nginx image. (Whatever the guys did over there). Copy the <strong>build </strong>directory to the <strong>nginx/html</strong> directory in the image. That’s it!</p><p>You may be wondering, how did I know where to copy the build files? i.e. /usr/share/nginx/html. Quite simple: It was documented in the nginx <a href="https://hub.docker.com/_/nginx/">image</a> in Docker Hub.</p><h4>Building and Pushing the container</h4><p>Before we can push our image, we need a Container Registry to host our images. Docker Hub is a free cloud container service that we will use for this demonstration. You have three tasks before continuing:</p><ol><li><a href="https://www.docker.com/community-edition">Install Docker CE</a></li><li>Register to the Docker Hub.</li><li>Login by executing the below command in your Terminal:</li></ol><pre>docker login -u=&quot;$DOCKER_USERNAME&quot; -p=&quot;$DOCKER_PASSWORD&quot;</pre><p>After completing the above tasks navigate to the directory <strong>sa-frontend.</strong> Then execute the below command (replace $DOCKER_USER_ID with your docker hub username. For e.g. rinormaloku/sentiment-analysis-frontend)</p><pre>docker build -f Dockerfile -t $DOCKER_USER_ID/sentiment-analysis-frontend .</pre><p>We can drop -f Dockerfile because we are already in the directory containing the Dockerfile.</p><p>To push the image, use the docker push command:</p><pre>docker push $DOCKER_USER_ID/sentiment-analysis-frontend</pre><p>Verify in your docker hub repository that the image was pushed successfully.</p><h4>Running the container</h4><p>Now the image in $DOCKER_USER_ID/sentiment-analysis-frontend can be pulled and run by anyone:</p><pre>docker pull $DOCKER_USER_ID/sentiment-analysis-frontend<br>docker run -d -p 80:80 $DOCKER_USER_ID/sentiment-analysis-frontend</pre><p>Our Docker container is running!</p><p>Before we continue lets elaborate the 80:80 which I find confusing:</p><ul><li>The first 80 is the port of the host (i.e. my computer)</li><li>The second 80 stands for the container port to which the calls should be forwarded.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/484/1*oLBvjr6KeBJ3EwnFR5pPJA.png" /><figcaption>Fig. 9. Port Mapping of Host to Container</figcaption></figure><p>It maps from &lt;hostPort&gt; to &lt;containerPort&gt;. Meaning that calls to host port 80 should be mapped to the port 80 of the container, as shown in figure 9.</p><p>Because the port was run in the host (your computer) in port 80 it should be accessible on the localhost:80. If you do not have native docker support, you can open the application in &lt;docker-machine ip&gt;:80. To find your docker-machine ip execute docker-machine ip</p><p>Give it a try! You should be able to access the react application in that endpoint.</p><h4>The Dockerignore</h4><p>We saw earlier that building the image for SA-Frontend was slow, pardon me, <strong>Extremely slow</strong>. That was the case because of the <strong>build context </strong>that had to be sent to the Docker Deamon. In more detail, the <strong>build context</strong> directory is defined by the last argument in the docker build command (the trailing dot), which specifies the build context. And in our case, it included the following folders:</p><pre>sa-frontend:<br>|   .dockerignore<br>|   Dockerfile<br>|   package.json<br>|   README.md<br><strong>+---build</strong><br>+---node_modules<br>+---public<br>\---src</pre><p>But the only data we need is in the <strong>build</strong> folder. Uploading anything else will be a waste of time. We can improve our build time by dropping the other directories. That’s where .dockerignore comes into play. For you it will be familiar because it’s like .gitignore, i.e. add all directories that you want to ignore in the .dockerignore file, as shown below:</p><pre>node_modules<br>src<br>public</pre><p>The .dockerignore file should be in the same folder as the Dockerfile. Now building the image takes only seconds.</p><p>Let’s continue with the Java Application.</p><h4>Building the container image for the Java Application</h4><p>Guess what! You learned almost everything about creating container images! That’s why this part is extremely short.</p><p>Open the Dockerfile in <strong>sa-webapp</strong>, and you will find only two new keywords:</p><pre><strong>ENV</strong> SA_LOGIC_API_URL <a href="http://localhost:5000/">http://localhost:5000</a><br>…<br><strong>EXPOSE</strong> 8080</pre><p>The keyword <strong>ENV</strong> declares an Environment Variable inside the docker container. This will enable us to provide the URL for the Sentiment Analysis API when starting the Container.</p><p>Additionally, the keyword <strong>EXPOSE</strong> exposes a port that we want to access later on. <strong>But hey!!!</strong> We didn’t do that in the Dockerfile in SA-Frontend, good catch! This is for documentation purposes only, in other words it will serve as information to the person reading the Dockerfile.</p><p>You should be familiar with building and pushing the container image. If any difficulties arise read the README.md file in <strong>sa-webapp</strong> directory.</p><h4>Building the container image for the Python Application</h4><p>In the Dockerfile in <strong>sa-logic</strong> there are no new keywords. Now you can call yourself a Docker-Master 😉.</p><p>For building and pushing the container image read the the README.md in <strong>sa-logic </strong>directory.</p><h4>Testing the Container-ized Application</h4><p>Can you trust something that you didn’t test? Neither can I. Let’s give these containers a test.</p><ol><li>Run the <strong>sa-logic</strong> container and configure to listen on port 5050:</li></ol><pre>docker run -d -p 5050:5000 $DOCKER_USER_ID/sentiment-analysis-logic</pre><p>2. Run <strong>sa-webapp container </strong>and configure to listen on port 8080, and additionally we need to change the port in which the python app listens by overriding the environment variable SA_LOGIC_API_URL.</p><pre>$ docker run -d -p 8080:8080 -e SA_LOGIC_API_URL=&#39;http://&lt;container_ip or docker machine ip&gt;:5000&#39; $DOCKER_USER_ID/sentiment-analysis-web-app</pre><p>Checkout the <a href="https://github.com/rinormaloku/k8s-mastery/blob/master/sa-webapp/README.md">README</a> on how to get the container ip or docker machine ip.</p><p>3. Run <strong>sa-frontend</strong> container:</p><pre>docker run -d -p 80:80 $DOCKER_USER_ID/sentiment-analysis-frontend</pre><p>We are done. Open your browser on <strong>localhost:80.</strong></p><p><strong>Attention:</strong> If you changed the port for the sa-webapp, or if you are using docker-machine ip, you need to update App.js file in <strong>sa-frontend</strong> in the method analyzeSentence to fetch from the new IP or Port. Afterwards you need to build, and use the updated image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*tvbz4g0zb0qw0qBjj_xfAg.png" /><figcaption>Fig. 10. Microservices running in Containers</figcaption></figure><h4>Brain Teaser — Why Kubernetes?</h4><p>In this section, we learned about the Dockerfile, how to use it to build an image, and the commands to push it to the Docker registry. Additionally, we investigated how to reduce the number of files sent to the build context by ignoring useless files. And in the end, we got the application running from containers. So why Kubernetes? We will investigate deeper into that in the next article, but I want to leave you a brainteaser.</p><ul><li>Our Sentiment Analysis web app became a world hit and we suddenly have a million requests per minute to analyze sentiments and we experience huge load on <strong>sa-webapp</strong> and <strong>sa-logic</strong>. How can we scale the containers?</li></ul><h3>Introduction to Kubernetes</h3><p>I promise and I am not exaggerating that by the end of the article you will ask yourself “Why don’t we call it Supernetes?”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*1L6WPtVM2paIttHkG7x8CA.png" /><figcaption>Fig. 11. Supernetes</figcaption></figure><p>If you followed this article from the beginning we covered so much ground and so much knowledge. You might worry that this will be the hardest part, but, it is the simplest. The only reason why learning Kubernetes is daunting is because of the “everything else” and we covered that one so well.</p><h3>What is Kubernetes</h3><p>After we started our Microservices from containers we had one question, let’s elaborate it further in a Q&amp;A format:<br><strong>Q:</strong> How do we scale containers?<br><strong>A:</strong> We spin up another one.<br><strong>Q:</strong> How do we share the load between them? What if the Server is already used to the maximum and we need another server for our container? How do we calculate the best hardware utilization?<br><strong>A:</strong> Ahm… Ermm… (Let me google).<br><strong>Q:</strong> Rolling out updates without breaking anything? And if we do, how can we go back to the working version.</p><p>Kubernetes solves all these questions (and more!). My attempt to reduce Kubernetes in one sentence would be: “Kubernetes is a Container Orchestrator, that abstracts the underlying infrastructure. (Where the containers are run)”.</p><p>We have a faint idea about Container Orchestration. We will see it in practice in the continuation of this article, but it’s the first time that we are reading about “abstracts the underlying infrastructure”. So let’s take a close-up shot, at this one.</p><h4>Abstracting the underlying infrastructure</h4><p>Kubernetes abstracts the underlying infrastructure by providing us with a simple API to which we can send requests. Those requests prompt Kubernetes to meet them at its best of capabilities. For example, it is as simple as requesting “Kubernetes spin up 4 containers of the image x”. Then Kubernetes will find under-utilized nodes in which it will spin up the new containers (see Fig. 12.).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1rEU8wnAL2FnVi-tCd6iOA.png" /><figcaption>Fig. 12. Request to the API Server</figcaption></figure><p>What does this mean for the developer? That he doesn’t have to care about the number of nodes, where containers are started and how they communicate. He doesn’t deal with hardware optimization or worry about nodes going down (and they will go down <em>Murphy’s Law</em>), because new nodes can be added to the Kubernetes cluster. In the meantime Kubernetes will spin up the containers in the other nodes that are still running. It does this at the best possible capabilities.</p><p>In figure 12 we can see a couple of new things:</p><ul><li><strong>API Server</strong>: Our only way to interact with the Cluster. Be it starting or stopping another container (err *pods) or checking current state, logs, etc.</li><li><strong>Kubelet</strong>: monitors the containers (err *pods) inside a node and communicates with the master node.</li><li><strong>*Pods</strong>: Initially just think of pods as containers.</li></ul><p>And we will stop here, as diving deeper will just loosen our focus and we can always do that later, there are useful resources to learn from, like the official documentation (the hard way) or reading the amazing book <a href="https://www.amazon.com/Kubernetes-Action-Marko-Luksa/dp/1617293725">Kubernetes in Action</a>, by <a href="https://twitter.com/markoluksa">Marko Lukša</a>.</p><h4>Standardizing the Cloud Service Providers</h4><p>Another strong point that Kubernetes drives home, is that it standardizes the Cloud Service Providers (CSPs). This is a bold statement, but let’s elaborate with an example:</p><p>– An expert in Azure, Google Cloud Platform or some other CSP ends up working on a project in an entirely new CSP, and he has no experience working with it. This can have many consequences, to name a few: he can miss the deadline; the company might need to hire more resources, and so on.</p><p>In contrast, with Kubernetes this isn’t a problem at all. Because you would be executing the same commands to the API Server no matter what CSP. You on a declarative manner request from the API Server <strong>what you want. </strong>Kubernetes abstracts away and implements the <strong>how </strong>for the CSP in question.</p><p>Give it a second to sink in — this is extremely powerful feature. For the company it means that they are not tied up to a CSP. They calculate their expenses on another CSP, and they move on. They still will have the expertise, they still will have the resources, and they can do that for <em>cheaper!</em></p><p>All that said, in the next section we will put Kubernetes in Practice.</p><h3>Kubernetes in Practice — Pods</h3><p>We set up the Microservices to run in containers and it was a cumbersome process, but it worked. We also mentioned that this solution is not scalable or resilient and that Kubernetes resolves these issues. In continuation of this article, we will migrate our services toward the end result as shown in figure 13, where the Containers are orchestrated by Kubernetes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/724/1*omxbZosFjTQZHC9jUelUjw.png" /><figcaption>Fig. 13. Microservices running in a Kubernetes Managed Cluster</figcaption></figure><p>In this article, we will use Minikube for debugging locally, though everything that will be presented works as well in Azure and in Google Cloud Platform.</p><h3>Installing and Starting Minikube</h3><p>Follow official documentation for installing <a href="https://kubernetes.io/docs/tasks/tools/install-minikube/">Minikube</a>. During Minikube installation, you will also install <strong>Kubectl. </strong>This is a client to make requests to the Kubernetes API Server.</p><p>To start Minikube execute the command minikube start and after it is completed, execute kubectl get nodes and you should get the following output</p><pre><strong>kubectl get nodes</strong><br>NAME       STATUS    ROLES     AGE       VERSION<br>minikube   Ready     &lt;none&gt;    11m       v1.9.0</pre><p>Minikube provides us with a Kubernetes Cluster that has only one node, but remember we do not care how many Nodes there are, Kubernetes abstracts that away, and for us to learn Kubernetes that is of no importance. In the next section, we will start with our first Kubernetes resource [DRUM ROLLS] <strong>the Pod</strong>.</p><h4>Pods</h4><p>I love containers, and by now you love containers too. So why did Kubernetes decide to give us Pods as the smallest deployable compute unit? What does a pod do? Pods can be composed of one or even a group of containers that share the same execution environment.</p><p>But do we really need to run two containers in one pod? Erm.. Usually, you run only one container and that’s what we will do in our examples. But for cases when for e.g. two containers need to share volumes, or they communicate with each other using inter-process communication or are otherwise tightly coupled, then that’s made possible using <strong>Pods</strong>. Another feature that Pods make possible is that we are not tied to Docker containers, if desired we can use other technologies for e.g. <a href="https://coreos.com/rkt/">Rkt</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/968/1*8vbHfzW79J2BzpK6X8Tp2g.png" /><figcaption><strong>Fig. 14. Pod properties</strong></figcaption></figure><p>To summarize, the main properties of Pods are (also shown in figure 14):</p><ol><li>Each pod has a unique IP address in the Kubernetes cluster</li><li>Pod can have multiple containers. The containers share the same port space, as such they can communicate via localhost (understandably they cannot use the same port), and communicating with containers of the other pods has to be done in conjunction with the pod ip.</li><li>Containers in a pod share the same volume*, same ip, port space, IPC namespace.</li></ol><p>*Containers have their own isolated filesystems, though they are able to share data using the Kubernetes resource <strong>Volumes.</strong></p><p>This is more than enough information for us to continue, but to satisfy your curiosity check out the <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod/">official documentation</a>.</p><h3>Pod definition</h3><p>Below we have the manifest file for our first pod <strong>sa-frontend</strong>, and then below we explain all the points.</p><pre>apiVersion: v1<br>kind: Pod                                            # 1<br>metadata:<br>  name: sa-frontend                                  # 2<br>spec:                                                # 3<br>  containers:<br>    - image: rinormaloku/sentiment-analysis-frontend # 4<br>      name: sa-frontend                              # 5<br>      ports:<br>        - containerPort: 80                          # 6</pre><ol><li><strong>Kind: </strong>specifies the kind of the Kubernetes Resource that we want to create. In our case, a <strong>Pod</strong>.</li><li><strong>Name:</strong> defines the name for the resource. We named it <strong>sa-frontend</strong>.</li><li><strong>Spec </strong>is the object that defines the desired state for the resource. The most important property of a Pods Spec is the Array of containers.</li><li><strong>Image </strong>is the container image we want to start in this pod.</li><li><strong>Name</strong> is the unique name for a container in a pod.</li><li><strong>Container Port:</strong>is the port at which the container is listening. This is just an indicator for the reader (dropping the port doesn’t restrict access).</li></ol><h3>Creating the SA Frontend pod</h3><p>You can find the file for the above pod definition in resource-manifests/<strong>sa-frontend-pod.yaml.</strong> You either navigate in your terminal to that folder or you would have to provide the full location in the command line. Then execute the command:</p><pre><strong>kubectl create -f sa-frontend-pod.yaml</strong><br>pod &quot;sa-frontend&quot; created</pre><p>To check if the Pod is running execute the following command:</p><pre><strong>kubectl get pods</strong><br>NAME                          READY     STATUS    RESTARTS   AGE<br>sa-frontend                   1/1       Running   0          7s</pre><p>If it is still in <strong>ContainerCreating</strong> you can execute the above command with the argument --watch to update the information when the Pod is in Running State.</p><h4>Accessing the application externally</h4><p>To access the application externally we create a Kubernetes resource of type <strong>Service</strong>, that will be our next article, which is the proper implementation, but for quick debugging we have another option, and that is port-forwarding:</p><pre><strong>kubectl port-forward sa-frontend 88:80</strong><br>Forwarding from 127.0.0.1:88 -&gt; 80</pre><p>Open your browser in <strong>127.0.0.1:88</strong> and you will get to the react application.</p><h4>The wrong way to scale up</h4><p>We said that one of the Kubernetes main features was scalability, to prove this let’s get another pod running. To do so create another pod resource, with the following definition:</p><pre>apiVersion: v1<br>kind: Pod                                            <br>metadata:<br>  name: sa-frontend2      # The only change<br>spec:                                                <br>  containers:<br>    - image: rinormaloku/sentiment-analysis-frontend <br>      name: sa-frontend                              <br>      ports:<br>        - containerPort: 80</pre><p>Create the new pod by executing the following command:</p><pre><strong>kubectl create -f sa-frontend-pod2.yaml</strong><br>pod &quot;sa-frontend2&quot; created</pre><p>Verify that the second pod is running by executing:</p><pre><strong>kubectl get pods</strong><br>NAME                          READY     STATUS    RESTARTS   AGE<br>sa-frontend                   1/1       Running   0          7s<br>sa-frontend2                  1/1       Running   0          7s</pre><p>Now we have two pods running!</p><p><strong>Attention:</strong> this is not the final solution, and it has many flaws. We will improve this in the section for another Kubernetes resource <strong>Deployments</strong>.</p><h4>Pod Summary</h4><p>The Nginx web server with the static files is running inside two different pods. Now we have two questions:</p><ul><li>How do we expose it externally to make it accessible via URL, and</li><li>How do we load balance between them?</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/599/1*kXggvKtMTy02abomLAuHaQ.png" /><figcaption>Fig. 15. Load balancing between pods</figcaption></figure><p>Kubernetes provides us the <strong>Services</strong> resource. Let’s jump right into it, in the next section.</p><h3>Kubernetes in Practice — Services</h3><p>The Kubernetes <strong>Service</strong> resource acts as the entry point to a set of pods that provide the same functional service. This resource does the heavy lifting, of discovering services and load balancing between them as shown in figure 16.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/877/1*B7jpH6tG868KSljB5jFnvg.png" /><figcaption>Fig. 16. Kubernetes Service maintaining IP addresses</figcaption></figure><p>In our Kubernetes cluster, we will have pods with different functional services. (The frontend, the Spring WebApp and the Flask Python application). So the question arises how does a service know which pods to target? I.e. how does it generate the list of the endpoints for the pods?</p><p>This is done using <strong>Labels</strong>, and it is a two-step process:</p><ol><li>Applying a label to all the pods that we want our Service to target and</li><li>Applying a “selector” to our service so that defines which labeled pods to target.</li></ol><p>This is much simpler visually:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nFNYyevWnLxgpfGSq5X_pg.png" /><figcaption>Fig. 17. Pods with labels and their manifests</figcaption></figure><p>We can see that pods are labeled with “app: sa-frontend” and the service is targeting pods with that label.</p><h4>Labels</h4><p>Labels provide a simple method for organizing your Kubernetes Resources. They represent a key-value pair and can be applied to every resource. Modify the manifests for the pods to match the example shown earlier in figure 17.</p><p>Save the files after completing the changes, and apply them with the following command:</p><pre><strong>kubectl apply -f sa-frontend-pod.yaml</strong><br>Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply<br>pod &quot;sa-frontend&quot; configured<br><strong>kubectl apply -f sa-frontend-pod2.yaml</strong> <br>Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply<br>pod &quot;sa-frontend2&quot; configured</pre><p>We got a warning (apply instead of create, roger that). In the second line, we see that the pods “sa-frontend” and “sa-frontend2” are configured. We can verify that the pods were labeled by filtering the pods that we want to display:</p><pre><strong>kubectl get pod -l app=sa-frontend</strong><br>NAME           READY     STATUS    RESTARTS   AGE<br>sa-frontend    1/1       Running   0          2h<br>sa-frontend2   1/1       Running   0          2h</pre><p>Another way to verify that our pods are labeled is by appending the flag --show-labels to the above command. This will display all the labels for each pod.<br>Great! Our pods are labeled and we are ready to target them with our Service. Lets get started defining the Service of type LoadBalancer shown in Fig. 18.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*dLmXo_8w0hfBZUXZY968Wg.gif" /><figcaption>Fig. 18. Load balancing with the LoadBalancer Service</figcaption></figure><h3>Service definition</h3><p>The YAML definition of the Loadbalancer Service is shown below:</p><pre>apiVersion: v1<br>kind: Service              # 1<br>metadata:<br>  name: sa-frontend-lb<br>spec:<br>  type: LoadBalancer       # 2<br>  ports:<br>  - port: 80               # 3<br>    protocol: TCP          # 4<br>    targetPort: 80         # 5<br>  selector:                # 6<br>    app: sa-frontend       # 7</pre><ol><li><strong>Kind: </strong>A service.</li><li><strong>Type: </strong>Specification type, we choose LoadBalancer because we want to balance the load between the pods.</li><li><strong>Port: </strong>Specifies the port in which the service gets requests.</li><li><strong>Protocol:</strong> Defines the communication.</li><li><strong>TargetPort:</strong> The port at which incomming requests are forwarded.</li><li><strong>Selector:</strong> Object that contains properties for selecting pods.</li><li><strong>app:</strong> sa-frontend Defines which pods to target, only pods that are labeled with “app: sa-frontend”</li></ol><p>To create the service execute the following command:</p><pre><strong>kubectl create -f service-sa-frontend-lb.yaml</strong><br>service &quot;sa-frontend-lb&quot; created</pre><p>You can check out the state of the service by executing the following command:</p><pre><strong>kubectl get svc</strong><br>NAME             TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE<br>sa-frontend-lb   LoadBalancer   10.101.244.40   &lt;pending&gt;     80:30708/TCP   7m</pre><p>The <strong>External-IP </strong>is in pending state (and don’t wait, as it’s not going to change). This is only because we are using <strong>Minikube</strong>. If we would have executed this in a cloud provider like Azure or GCP, we would get a Public IP, which makes our services worldwide accessible.</p><p>Despite that, Minikube doesn’t let us hanging it provides a useful command for local debugging, execute the following command:</p><pre><strong>minikube service sa-frontend-lb</strong><br>Opening kubernetes service default/sa-frontend-lb in default browser...</pre><p>This opens your browser pointing to the services IP. After the Service receives the request, it will forward the call to one of the pods (which one doesn’t matter). This abstraction enables us to see and act with the numerous pods as one unit, using the Service as an entry point.</p><h4>Service Summary</h4><p>In this section, we covered labeling resources, using those as selectors in Services, and we defined and created a LoadBalancer service. This fulfills our requirements to scale the application (just add new labeled pods) and to Load balance between the pods, using the service as an entry point.</p><h3>Kubernetes in Practice — Deployments</h3><p>Kubernetes Deployments help us with one constant in the life of every application, and that is <strong>change</strong>. Moreover, the only applications that do not change are the ones that are already dead, and while not, new requirements will come in, more code will be shipped, it will be packaged, and deployed. And on each step of this process, mistakes can be made.</p><p>The Deployment resource automates the process of moving from one version of the application to the next, with zero downtime and in case of failures, it enables us to quickly roll back to the previous version.</p><h4>Deployments in Practice</h4><p>Currently, we have <strong>two pods</strong> and <strong>a service</strong> exposing them and load balancing between them (see Fig. 19.). We mentioned that deploying the pods separately is far from perfect. It requires separately managing each (create, update, delete and monitoring their health). Quick updates and fast rollbacks are out of the question! This is not acceptable and the <strong>Deployments </strong>Kubernetes resource solves each of these issues.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*nuWYlwnTiTAFuv87e0sIvg.png" /><figcaption>Fig. 19. Current state</figcaption></figure><p>Before we continue let’s state what we want to achieve, as it will provide us with the overview that enables us to understand the manifest definition for the deployment resource. What we want is:</p><ol><li>Two pods of the image rinormaloku/sentiment-analysis-frontend</li><li>Zero Downtime deployments,</li><li>Pods labeled with <strong>app:</strong> sa-frontend so that the services get discovered by the Service <strong>sa-frontend-lb.</strong></li></ol><p>In the next section, we will translate the requirements into a Deployment definition.</p><h3>Deployment definition</h3><p>The YAML resource definition that achieves all the above-mentioned points:</p><pre>apiVersion: extensions/v1beta1<br>kind: Deployment                                          # 1<br>metadata:<br>  name: sa-frontend<br>spec:<br>  selector:                                               # 2<br>    matchLabels:<br>      app: sa-frontend  <br>  replicas: 2                                             # 3<br>  minReadySeconds: 15<br>  strategy:<br>    type: RollingUpdate                                   # 4<br>    rollingUpdate: <br>      maxUnavailable: 1                                   # 5<br>      maxSurge: 1                                         # 6<br>  template:                                               # 7<br>    metadata:<br>      labels:<br>        app: sa-frontend                                  # 8<br>    spec:<br>      containers:<br>        - image: rinormaloku/sentiment-analysis-frontend<br>          imagePullPolicy: Always                         # 9<br>          name: sa-frontend<br>          ports:<br>            - containerPort: 80</pre><ol><li><strong>Kind: </strong>A deployment.</li><li><strong>Selector: </strong>Pods matching the selector will be taken under the management of this deployment.</li><li><strong>Replicas </strong>is a property of the deployments Spec object that defines how many pods we want to run. So only 2.</li><li><strong>Type </strong>specifies the strategy used in this deployment when moving from the current version to the next. The strategy <strong>RollingUpdate</strong> ensures Zero Downtime deployments.</li><li><strong>MaxUnavailable </strong>is a property of the RollingUpdate object that specifies the maximum unavailable pods allowed (compared to the desired state) when doing a rolling update. For our deployment which has 2 replicas this means that after terminating one Pod, we would still have one pod running, this way keeping our application accessible.</li><li><strong>MaxSurge </strong>is another property of the RollingUpdate object that defines the maximum amount of pods added to a deployment (compared to the desired state). For our deployment, this means that when moving to a new version we can add one pod, which adds up to 3 pods at the same time.</li><li><strong>Template: </strong>specifies the pod template that the Deployment will use to create new pods. Most likely the resemblance with Pods struck you immediately.</li><li><strong>app:</strong> sa-frontend the label to use for the pods created by this template.</li><li><strong>ImagePullPolicy</strong> when set to <strong>Always</strong>, it<strong> </strong>will pull the container images on each redeployment.</li></ol><p>Honestly, that wall of text got even me confused, let’s just get started with the example:</p><pre><strong>kubectl apply -f sa-frontend-deployment.yaml</strong><br>deployment &quot;sa-frontend&quot; created</pre><p>As always let’s verify that everything went as planned:</p><pre><strong>kubectl get pods</strong><br>NAME                           READY     STATUS    RESTARTS   AGE<br>sa-frontend                    1/1       Running   0          2d<br>sa-frontend-5d5987746c-ml6m4   1/1       Running   0          1m<br>sa-frontend-5d5987746c-mzsgg   1/1       Running   0          1m<br>sa-frontend2                   1/1       Running   0          2d</pre><p>We got 4 running pods, two pods created by the Deployment and the other two are the ones we created manually. Delete the ones we created manually using the command kubectl delete pod &lt;pod-name&gt;.</p><p><strong>Exercise:</strong> Delete one of the pods of the deployment as well and see what happens. Think for the reason before reading the explanation below.</p><p><strong>Explanation: </strong>Deleting one pod made the Deployment notice that the current state (1 pod running) is different from the desired state (2 pods running) so it started another pod.</p><p>So what was so good about Deployments, besides keeping the desired state? Let’s get started with the benefits.</p><h4>Benefit #1: Rolling a Zero-Downtime deployment</h4><p>Our Product manager came to us with a new requirement, our clients want to have a green button in the frontend. The developers shipped their code and provided us with the only thing we need, the container image rinormaloku/sentiment-analysis-frontend:green. Now it’s our turn, we the DevOps have to roll a Zero-Downtime deployment, Will the hard work pay off? Let’s see that!</p><p>Edit the file sa-frontend-deployment.yaml by changing the container image to refer to the new image: rinormaloku/sentiment-analysis-frontend:green. Save the changes as sa-frontend-deployment-green.yaml and execute the following command:</p><pre><strong>kubectl apply -f sa-frontend-deployment-green.yaml --record</strong><br>deployment &quot;sa-frontend&quot; configured</pre><p>We can check the status of the rollout using the following command:</p><pre><strong>kubectl rollout status deployment sa-frontend</strong><br>Waiting for rollout to finish: 1 old replicas are pending termination...<br>Waiting for rollout to finish: 1 old replicas are pending termination...<br>Waiting for rollout to finish: 1 old replicas are pending termination...<br>Waiting for rollout to finish: 1 old replicas are pending termination...<br>Waiting for rollout to finish: 1 old replicas are pending termination...<br>Waiting for rollout to finish: 1 of 2 updated replicas are available...<br>deployment &quot;sa-frontend&quot; successfully rolled out</pre><p>According to the output the deployment was rolled out. It was done in such a fashion that the replicas were replaced one by one. Meaning that our application was always on. Before we move on let’s verify that the update is live.</p><h4>Verifying the deployment</h4><p>Let’s see the update live on our browsers. Execute the same command that we used before minikube service sa-frontend-lb, which opens up the browser. We can see that the button was updated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/506/1*VQXCuwiVyq9QwLusbGpXsA.png" /><figcaption>Fig. 20. The Green button</figcaption></figure><h4>Behind the scenes of “The RollingUpdate”</h4><p>After we applied the new deployment, Kubernetes compares the new state with the old one. In our case, the new state requests two pods with the image rinormaloku/sentiment-analysis-frontend:green. This is different from the current state running so it kicks in the <strong>RollingUpdate</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/524/1*5xvoBUs4OAu50XthbaN1Dg.png" /><figcaption>Fig. 21. RollingUpdate replacing pods</figcaption></figure><p>The RollingUpdate acts according to the rules we specified, those being “<strong>maxUnavailable: </strong>1″ and “<strong>maxSurge:</strong> 1″. This means that the deployment can terminate only one pod, and can start only one new pod. This process is repeated until all pods are replaced (see Fig. 21).</p><p>Let’s continue with the benefit number 2.</p><p><strong>Disclaimer:</strong><em> For entertainment purposes, the next part is written as a novella.</em></p><h4>Benefit #2: Rolling back to a previous state</h4><p>The Product Manager runs into your office and he is having a <strong>crisis!</strong></p><p>“The application has a critical bug, in PRODUCTION!! Revert back to the previous version immediately” — yells the product manager.</p><p>He sees the coolness in you, not twitching one eye. You turn to your beloved terminal and type:</p><pre><strong>kubectl rollout history deployment sa-frontend</strong><br>deployments &quot;sa-frontend&quot;<br>REVISION  CHANGE-CAUSE<br>1         &lt;none&gt;         <br>2         kubectl.exe apply --filename=sa-frontend-deployment-green.yaml --record=true</pre><p>You take a short look at the previous deployments. “The last version is buggy, meanwhile the previous version worked perfectly?” — you ask the Product Manager.</p><p>“Yes, are you even listening to me!” — screams the product manager.</p><p>You ignore him, you know what you have to do, you start typing:</p><pre><strong>kubectl rollout undo deployment sa-frontend --to-revision=1</strong><br>deployment &quot;sa-frontend&quot; rolled back</pre><p>You refresh the page and the change is undone!</p><p>The product managers jaw drops open.</p><p>You saved the day!</p><p><em>The end!</em></p><p>Yeah… it was a boring novella. Before Kubernetes existed it was so much better, we had more drama, higher intensity, and that for a longer period of time. Ohh good old times!</p><p>Most of the commands are self-explanatory, besides one detail that you had to work out yourself. Why the first revision has a <strong>CHANGE-CAUSE </strong>of &lt;none&gt; meanwhile the second revision has a <strong>CHANGE-CAUSE </strong>of “<em>kubectl.exe apply –filename=sa-frontend-deployment-green.yaml</em><strong><em> </em></strong><em>–record=true</em>“.</p><p>If you concluded that it’s because of the --record flag that we used when we applied the new image then you are totally correct!</p><p>In the next section, we will use the concepts learned thus far to complete the whole architecture.</p><h3>Kubernetes and everything else in Practice</h3><p>We learned all the resources that we need to complete the architecture that’s why this part is going to be quick. In figure 22 we have greyed out everything that we still have to do. Lets start from the bottom: <strong>Deploying the sa-logic deployment</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/724/1*xT32tIX5IAhNtK7LMfNjEg.png" /><figcaption>Fig. 22. Current Application State</figcaption></figure><h4>Deployment SA-Logic</h4><p>Navigate in your terminal to the folder resource-manifests and execute the following command:</p><pre><strong>kubectl apply -f sa-logic-deployment.yaml --record</strong><br>deployment &quot;sa-logic&quot; created</pre><p>The deployment SA-Logic created three pods. (Running the container of our python application). It labeled them with <strong>app:</strong> sa-logic. This labeling enables us to target them using a selector from the SA-Logic service. Please take time to open the file sa-logic-deployment.yaml and check out the contents.</p><p>It’s the same concepts used all over again, let’s jump right into the next resource: <strong>the service SA-Logic</strong>.</p><h4>Service SA Logic</h4><p>Lets elaborate why we need this Service. Our Java application (running in the pods of SA — WebApp deployment) depends on the sentiment analysis done by the Python Application. But now, in contrast to when we were running everything locally, we don’t have one single python application listening to one port, we have 2 pods and if needed we could have more.</p><p>That’s why we need a <strong>Service</strong> that “acts as the entry point to a set of pods that provide the same functional service”. This means that we can use the Service SA-Logic as the entry point to all the SA-Logic pods.</p><p>Let’s do that:</p><pre><strong>kubectl apply -f service-sa-logic.yaml</strong><br>service &quot;sa-logic&quot; created</pre><p><strong>Updated Application State: </strong>We have 2 pods (containing the Python Application) running and we have the SA-Logic service acting as an entry point that we will use in the SA-WebApp pods.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/724/1*TYp53OHX7D0Ull5INe6QIA.png" /><figcaption>Fig. 23. Updated Application State</figcaption></figure><p>Now we need to deploy the SA-WebApp pods, using a deployment resource.</p><h4>Deployment SA-WebApp</h4><p>We are getting the hang out of deployments, though this one has one more feature. If you open the file sa-web-app-deployment.yaml you will find this part new:</p><pre>- image: rinormaloku/sentiment-analysis-web-app<br>  imagePullPolicy: Always<br>  name: sa-web-app<br><strong>  env:<br>    - name: SA_LOGIC_API_URL<br>      value: &quot;</strong><a href="http://sa-logic/"><strong>http://sa-logic</strong></a><strong>&quot;</strong><br>  ports:<br>    - containerPort: 8080</pre><p>The first thing that interests us is what does the <strong>env </strong>property do? And we surmise that it is declaring the environment variable SA_LOGIC_API_URL with the value “<a href="http://sa-logic/">http://sa-logic</a>” inside our pods. But why are we initializing it to <a href="http://sa-logic/"><strong>http://sa-logic</strong></a>, what is <strong>sa-logic</strong>?</p><p>Lets get introduced to <strong>kube-dns</strong>.</p><h4>KUBE-DNS</h4><p>Kubernetes has a special pod the <strong>kube-dns</strong>. And by default, all Pods use it as the DNS Server. One important property of <strong>kube-dns </strong>is that it creates a DNS record for each created service.</p><p>This means that when we created the service <strong>sa-logic </strong>it got an IP address. Its name was added as a record (in conjunction with the IP) in kube-dns. This enables all the pods to translate the <strong>sa-logic </strong>to the SA-Logic services IP address.</p><p>Good, now we can continue with:</p><h4>Deployment SA WebApp (continued)</h4><p>Execute the command:</p><pre><strong>kubectl apply -f sa-web-app-deployment.yaml --record</strong><br>deployment &quot;sa-web-app&quot; created</pre><p>Done. We are left to expose the SA-WebApp pods externally using a LoadBalancer Service. This enables our react application to make http requests to the service which acts as an entry point to the SA-WebApp pods.</p><h4>Service SA-WebApp</h4><p>Open the file service-sa-web-app-lb.yaml, as you can see everything is familiar to you.<br>So without further adoexecute the command:</p><pre><strong>kubectl apply -f service-sa-web-app-lb</strong>.<strong>yaml</strong><br>service &quot;sa-web-app-lb&quot; created</pre><p>The architecture is complete. But we have one single dissonance. When we deployed the SA-Frontend pods our container image was pointing to our SA-WebApp in <a href="http://localhost:8080/sentiment">http://localhost:8080/sentiment</a>. But now we need to update it to point to the IP Address of the SA-WebApp Loadbalancer. (Which acts as an entry point to the SA-WebApp pods).</p><p>Fixing this dissonance provides us with the opportunity to succinctly encompass once more everything from code to deployment. (It’s even more effective if you do this alone instead of following the guide below). Let’s get started:</p><ol><li>Get the SA-WebApp Loadbalancer IP by executing the following command:</li></ol><pre>minikube service list<br>|-------------|----------------------|-----------------------------|<br>|  NAMESPACE  |         NAME         |             URL             |<br>|-------------|----------------------|-----------------------------|<br>| default     | kubernetes           | No node port                |<br>| default     | sa-frontend-lb       | <a href="http://192.168.99.100:30708/">http://192.168.99.100:30708</a> |<br>| default     | sa-logic             | No node port                |<br>| default     | sa-web-app-lb        | <a href="http://192.168.99.100:31691/"><strong>http://192.168.99.100:31691</strong></a> |<br>| kube-system | kube-dns             | No node port                |<br>| kube-system | kubernetes-dashboard | <a href="http://192.168.99.100:30000/">http://192.168.99.100:30000</a> |<br>|-------------|----------------------|-----------------------------|</pre><p>2. Use the SA-WebApp LoadBalancer IP in the file sa-frontend/src/App.js, as shown below:</p><pre>analyzeSentence() {<br>        fetch(&#39;http://<strong>192.168.99.100:31691</strong>/sentiment&#39;, { /* shortened for brevity */})<br>            .then(response =&gt; response.json())<br>            .then(data =&gt; this.setState(data));<br>    }</pre><p>3. Build the static files npm run build (you need to navigate to the folder <strong>sa-frontend</strong>)</p><p>4. Build the container image:</p><pre>docker build -f Dockerfile -t $DOCKER_USER_ID/sentiment-analysis-frontend:minikube .</pre><p>5. Push the image to Docker hub.</p><pre>docker push $DOCKER_USER_ID/sentiment-analysis-frontend:minikube</pre><p>6. Edit the sa-frontend-deployment.yaml to use the new image and</p><p>7. Execute the command kubectl apply -f sa-frontend-deployment.yaml</p><p>Refresh the browser or if you closed the window execute minikube service sa-frontend-lb. Give it a try by typing a sentence!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/724/0*QTdwsEuvcUFTgHmg.png" /></figure><h3>Article summary</h3><p>Kubernetes is beneficial for the team, for the project, simplifies deployments, scalability, resilience, it enables us to consume any underlying infrastructure and you know what? From now on, let’s call it Supernetes!</p><p>What we covered in this series:</p><ul><li>Building / Packaging / Running ReactJS, Java and Python Applications</li><li>Docker Containers; how to define and build them using Dockerfiles,</li><li>Container Registries; we used the Docker Hub as a repository for our containers.</li><li>We covered the most important parts of Kubernetes.</li><li>Pods</li><li>Services</li><li>Deployments</li><li>New concepts like Zero-Downtime deployments</li><li>Creating scalable apps</li><li>And in the process, we migrated the whole microservice application to a Kubernetes Cluster.</li></ul><p>I am Rinor Maloku and I want to thank you for joining me on this voyage. Since you read this far I know that you loved this article and would be interested in more. I write articles that go into this depth of detail for new technologies every 3 months. You can always expect an example application, hands-on practice, and a guide that provides you with the right tools and knowledge to tackle any real-world project.</p><p>To stay in touch and not miss any of my articles subscribe to my <a href="https://tinyletter.com/rinormaloku">newsletter</a>, follow me on <a href="https://twitter.com/rinormaloku">Twitter</a>, and check out my page <a href="https://rinormaloku.com/">rinormaloku.com</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=114ff420e882" width="1" height="1" alt=""><hr><p><a href="https://medium.com/free-code-camp/learn-kubernetes-in-under-3-hours-a-detailed-guide-to-orchestrating-containers-114ff420e882">Learn Kubernetes in Under 3 Hours: A Detailed Guide to Orchestrating Containers</a> was originally published in <a href="https://medium.com/free-code-camp">We’ve moved to freeCodeCamp.org/news</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[#My2017Milestones]]></title>
            <link>https://medium.com/@rinormaloku37/my2017milestones-71797e27a41b?source=rss-cf86e8b1d3bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/71797e27a41b</guid>
            <category><![CDATA[success]]></category>
            <category><![CDATA[happiness]]></category>
            <category><![CDATA[milestones]]></category>
            <category><![CDATA[productivity]]></category>
            <dc:creator><![CDATA[Rinor Maloku]]></dc:creator>
            <pubDate>Tue, 26 Dec 2017 20:39:09 GMT</pubDate>
            <atom:updated>2017-12-26T20:47:49.638Z</atom:updated>
            <content:encoded><![CDATA[<p>It’s the last week of the year 2017! [Dramatic pause to let it sink in]</p><p>We dust out our “New Year’s Resolutions” list (if we made one, or we mentally go over them if we didn’t). How much did we cross out?</p><ul><li>Get in good shape? Nada!</li><li>Learn another language? Nyet!</li><li>Travel to another country? 😢</li></ul><p>Remorse hits us in the gut.</p><p>And then we ask ourselves: Was it a failure? Did our 2017 really suck? Were you too optimistic with your new year’s resolutions?</p><p>Fuck that. I am sure your 2017 was better than you imagine.</p><p>You are agile, you are not an Operating System that gets an update and waits <strong>one year</strong>! You achieve milestones you didn’t and couldn’t plan on. You are amazing.</p><p>Instead of starting a new year with remorse. Lets’ start it with a reflection on how good your last year was! Hence <strong>#My2017Milestones</strong></p><p>Here are my Milestones:</p><ol><li>Increased my paycheck for 800%.</li><li>Learned Cloud Computing, Microservice Architectures, Kubernetes, Docker, C#, React.</li><li>Contributed to Microsoft Bot Emulator.</li><li>Helped two people advance in their careers.</li><li>Completed my Bachelor’s Degree (Now I have the freedom to dictate my education)</li><li>Developed a game. (Despite it being shitty)</li><li>Started my first personal project.</li></ol><p>I am extremely interested in your milestones, in what you achieved. They will fuel me into the next year!</p><p>And if someone really hasn’t achieved much. I think it is good for them to read and get motivated by everyone else!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=71797e27a41b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>