<?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[Masked Weaver - Medium]]></title>
        <description><![CDATA[Our mission is to provide training and awareness about DevOps, its ecosystem and, the importance of designing and building effective products. - Medium]]></description>
        <link>https://medium.com/rackbrains?source=rss----231a6385b39d---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Masked Weaver - Medium</title>
            <link>https://medium.com/rackbrains?source=rss----231a6385b39d---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 00:33:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/rackbrains" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Avoid Handover Nightmares]]></title>
            <link>https://medium.com/rackbrains/https-medium-com-thomas-nyambati-how-to-avoid-handover-nightmares-aea38d9a3793?source=rss----231a6385b39d---4</link>
            <guid isPermaLink="false">https://medium.com/p/aea38d9a3793</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[general]]></category>
            <dc:creator><![CDATA[Thomas Nyambati]]></dc:creator>
            <pubDate>Mon, 22 Jul 2019 11:37:49 GMT</pubDate>
            <atom:updated>2019-07-22T11:38:04.231Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*nJsDFJHKSJOQHKziphia4w.jpeg" /></figure><p>A handover is a process of migrating resources and responsibility of a project from one team to another. It is one of the critical stages of any software development cycle. The way this process is carried out affects the project outcomes and therefore should be well planned and executed.</p><p>Being software engineers, we spend most of our time working with different teams and projects. This means that we have to go through an onboarding process to get us up to speed. At times this process can be a bit overwhelming and frustrating, especially if key elements are not tackled to pave the way for a smooth, clean migration.</p><p>Such elements include:</p><ul><li>Documentation of the project and infrastructure.</li><li>Accounts and services used in the project.</li><li>Product policies and guidelines, etc.</li></ul><p>Because engineers spend most of our time concentrating on building new features and enhancing products, we often completely ignore the possibility of handover at a later time. This eventually leads to handover nightmares that no one would like to go through.</p><p>I’ve been thinking about handovers a lot lately, as I recently managed a handover in my role as a DevOps engineer. My goal was to facilitate the handover process to ensure a smooth transition with minimal downtime. Along the way, we faced a few challenges that I felt could have been avoided if handover had been considered from the early stages of the project.</p><p>Today, I am going to share with some of the things I learned during this particular handover process, and how they should be handled during the initial stages of development to avoid transition inconveniences.</p><h3>Keep things simple.</h3><p>Software architecture can grow complex as the product scales. It is sometimes impossible to avoid this, but as engineers, our goal should always be to simplify the project as much as possible.</p><blockquote>“Making the simple complicated is commonplace; making the complicated simple, awesomely simple, that’s creativity.” — Charles Mingus</blockquote><p>Do not over-engineer your product. If your project is growing, consider breaking it up into smaller components that you can easily manage. You can also consider leveraging preexisting services or API to build your product rather than reinventing the wheel.</p><p>Here’s a good example: If you are working with video calls, you can implement your own service, which comes with hosting and cost implications. Or, you can consider using <a href="https://www.twilio.com/">Twilio</a> or Google Hangouts, saving tons of code, time, money and frustrations — and at the same time benefiting from a well-tested, quality service. The latter option makes much more sense.</p><h3>Document everything.</h3><p>Documentation is very important in software development. A well-documented project is easier to understand and maintain.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/639/1*w7oWwbDVSpzKzHb4XnbSew.png" /><figcaption>Good documentation always makes a developer happy credits (Allie Brosh)</figcaption></figure><p>It is very easy for someone to get lost in a large codebase if it’s not well documented. Do not assume that everybody knows what you were thinking. I myself sometimes get lost in my own codebase. Documenting it or using self-explained variables and function names makes it easier for me to debug and navigate the codebase with ease.</p><blockquote>“Lack of documentation is becoming a problem for acceptance”. — Wietse Venema</blockquote><p>Working with an undocumented codebase is like someone throwing you into an ocean with a raft with no compass and expecting you to get onshore in a set amount of time. To avoid this situation, make sure that every inch of your codebase is documented and straight to the point.</p><p>If you are using any additional services, it is also wise to include this in your documentation. This will allow the team taking over the project to be aware of any dependencies and cater to them when making any changes.</p><h3>Adopt workflow and best practices.</h3><p>In almost all languages and frameworks, there are recommended workflows and best practices. These workflows are designed to provide a common approach to using certain technologies for efficiency reasons, but mostly for maintainability.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/430/1*9NXGJo9PsuG0vEZQcLDIFA.jpeg" /></figure><p>Adopting community-recommended workflows makes it easier for other developers to navigate through your codebase and get up to speed as compared with using your own approach. I am not saying that you should never be creative and come up with your own workflows: At times a project might demand improvisation. But even if this is the case, it’s essential to ensure that your workflow is consistent and well documented.</p><p>The key thing here is having a set of rules that should be followed from development to deployment. This not only guarantees quality but also makes it easy to work in teams.</p><p>Another reason to use best practices is to ensure that quality code is being shipped to production and can be easily changed without causing a lot of downtime.</p><h3>Employ separation of concerns.</h3><p>In computer science, separation of concerns (SOC) is a design principle for separating a computer program into distinct sections, such that each section addresses a separate concern.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*3cxOa-P39DENr26TF7WtFQ.png" /></figure><p>When building applications, this is one of the things you incorporate into your workflow. Using this approach will enable you to distribute tasks across teams with minimal conflict, making it easy to manage complex apps.</p><p>A good example of separation of concerns is in microservices, where you break down monolithic apps into small, independent units that can work together as a fully functional app. If you are using Python, Django apps are another way of applying this principle.</p><p>Separation of concerns can also be used in building infrastructure for hosting applications. In this context, it is advised to separate the production account from the development account. This provides an easy way of managing both environments but also eradicates the possibility of polluting your production environment.</p><h3>Avoid using personal accounts or credentials.</h3><p>Continuous integration is one thing we cannot go without these days, especially if you are a DevOps Engineer: Tinkering with various tools for monitoring, logging, deployments, testing, and so forth is like a religion to us. However, the use of these tools can lead to having multiple accounts, especially when using a service that cannot be self-hosted.</p><p>To avoid this situation, most of the time we tend to use one account across multiple projects. In some instances, this is a good idea since you will have a consolidated dashboard for easy management; however, when it comes to handing over these projects, it doesn’t work so well.</p><p>Some services, like <a href="https://heroku.com/">Heroku</a>, provide account transfers. For others, this is not the case. Therefore, you will have to create other accounts and change configuration across all related services, which can be painful and frustrating.</p><p>To avoid this kind of situation, I recommend having isolated accounts for each project you are working on. Responsibility for managing individual accounts should be handed over to team leads or technical officers, and credentials stored in vaults (e.g., LastPass) which will make handover as simple as sharing the folder with credentials.</p><h3><strong>Automate as much as you can.</strong></h3><p>Modern-day software makes use of different services, and this complex infrastructure can be hard to maintain manually. A good example here is AWS. AWS has a lot of services that can be used to build software. Provisioning all the services can be done manually. However, in the event of failure or corruption of the infrastructure, it can be hard to replicate the same environment, thus leading to downtime.</p><p><em>AWS has come up with awesome tools to make provisioning services through the use of configuration approach e.g </em><a href="https://aws.amazon.com/cloudformation/"><em>CloudFormation</em></a><em>. The community, on the other hand, has also built tools that are built on this service making it easier to version infrastructure and rollback easily. With infrastructure as code, maintaining the production, dev, or staging environments can be done by just running scripts and commands.</em></p><p>Here are some of the tools that you can use to get you started:</p><ul><li><a href="https://www.ansible.com/">Ansible</a></li><li><a href="http://www.sparkleformation.io/">SparkleFormation</a></li><li><a href="http://terraform.io/">Terraform</a></li><li><a href="https://www.chef.io/chef/">Chef</a></li><li><a href="https://puppet.com/">Puppet</a></li></ul><p>But I will have to warn you: Once you take the automation path, there is no turning back.</p><p>And last but not least:</p><h3><strong>Write good code</strong>.</h3><p>I sometimes tell my colleagues that code is like poetry and should be written as if you are wooing the computer. It should be expressive, well indented, and easy to read.</p><blockquote>“Write code as if the next person reading it is a psychopath and he knows where you live.” — Unknown</blockquote><p>This is just an overview of some of the things you can do to make handovers easy for you and your team. The key point here is to ensure that you are considerate on how you design your applications to pave the way for seamless handovers.</p><p>(If you like this article, comment, like it, or share it with the world!)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aea38d9a3793" width="1" height="1" alt=""><hr><p><a href="https://medium.com/rackbrains/https-medium-com-thomas-nyambati-how-to-avoid-handover-nightmares-aea38d9a3793">How to Avoid Handover Nightmares</a> was originally published in <a href="https://medium.com/rackbrains">Masked Weaver</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kubernetes 101 part 5: Scaling the Application and rolling updates]]></title>
            <link>https://medium.com/rackbrains/kubernetes-101-part-5-scaling-the-application-and-rolling-updates-8d78f1b89852?source=rss----231a6385b39d---4</link>
            <guid isPermaLink="false">https://medium.com/p/8d78f1b89852</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[containerization]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Thomas Nyambati]]></dc:creator>
            <pubDate>Mon, 10 Dec 2018 10:01:00 GMT</pubDate>
            <atom:updated>2019-03-18T12:01:35.376Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-ZNAm7ibdwZ4j-SsxqIwOw.png" /></figure><p><em>In the previous article, we created a deployment and exposed it to the public via a service. The deployment created only one pod when traffic increases we will need to scale the application to keep up with the user demand.</em></p><p><a href="https://medium.com/rackbrains/kubernetes-101-part-4-exposing-application-via-services-4c42bd3d6e01">Kubernetes 101 part 4: Exposing application via Services</a></p><p><em>The codebase for this article can be found on our </em><a href="https://github.com/rackbrainz/kubernetes-101"><em>GitHub repositor</em></a><em>y</em></p><p><a href="https://github.com/rackbrainz/kubernetes-101">rackbrainz/kubernetes-101</a></p><p>Scaling is done by increasing or decreasing the number of replicas in a deployment which is also referred to as <strong><em>Scaling out and Scaling in </em></strong>respectively. Scaling out a deployment will ensure pods are created and scheduled to nodes with available resources. Scaling in will reduce the number of Pods to the desired state.</p><p>Running multiple instances of an application will require a way to distribute the traffic to all of them. Services have an integrated load-balancer that will distribute network traffic to all pods of an exposed deployment. Services will monitor continuously the running Pods using endpoints, to ensure traffic is sent to only available pods. Multiple instances also make it easy to do rolling updates without downtime.</p><p><em>Before we proceed its important to ensure that your cluster is running and its up-to-date with changes we have made in previous articles. Your cluster should have a running deployment and service. If not run the commands below to get started.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mqfdYwusA6tS3Dx7KuVBMw.png" /></figure><h3>Scaling Deployments.</h3><p>When we list our deployment you will notice we only have one pod running. To scale our replicas to 4 we will use <strong><em>kubectl scale</em></strong> command, followed by type, name and desired number of instances.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7nSLkoFIbeELvmqAQU_xrg.png" /></figure><p>Let&#39;s scale the deployment to 4 replicas</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1hGw-ZWADFV_R_DQy6NgUg.png" /></figure><p>When the command is done executing, list deployments and the number should have changed from previous 1 to 4 instances.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zq7ldcq3CgI2vpHC7oyiww.png" /></figure><p>The same applies to pods, Now we have 4 pods with different IP addresses.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XMvR5bVzwhvqvHs22t8MZQ.png" /></figure><p>All the changes are registered in the deployment events log which can be checked via describe deployments command.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c2U1jjaT-VJ7Z9fH1h3x0w.png" /></figure><p>On the output, you can see we have 4 replicas.</p><h3>Load Balancing</h3><p>Kubernetes services have integrated load balancing capabilities when the replicas are increased the IP address of each pod will be registered as endpoints of the service. If you describe it, you will notice that the number of endpoints has increased from 1 to 4. This means that our service is distributing load across all the pods.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OOmfUOlgqZDgJlYtf9pETg.png" /></figure><h3>Scaling Down Deployments</h3><p>In a scenario that traffic for our application reduces, we might need to reduce the number of instances running in our cluster. This can be done using the same scale commands but reducing replicas.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ph4wVYbQH39UhvE9WI2i3g.png" /></figure><p>And now if you list the deployment, it should only have two replicas running.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LPt2SCD88oFWCeexscHm1w.png" /></figure><p>Now you can see that the replicas have been decreased to 2 and same goes to the pods.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B8clYG75SNcLYhQOaW0S3A.png" /></figure><p>Cool right! However, when working with production deployment, you will need to scale deployments automatically based on performance metrics.</p><h3>Horizontal Pod Autoscaler — HPA</h3><p>The Horizontal Pod Autoscaler automatically scales the number of pods in a replication controller, deployment or replica set based on observed CPU utilization or with custom metrics.</p><p>HPA is implemented as a control loop with a period, during each period, the controller manager queries resource utilization against the metrics specified on each HPA definition. It then accesses the corresponding scalable controllers via scale sub-resource. This scale enables the HPA to set the number of replicas and examine their current state.</p><p>Below is a sample of HPA definition.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mHHTMzN5LyeBr4Rcv9TXUA.png" /></figure><p>In the above definition, we have specified the minimum replicas to be deployed and the maximum replicas to scale up to if the CPU utilization per pod exceeds 75. The definition also has the target deployment that will be managed.</p><p>Let&#39;s apply this definition and see how this will affect our deployments.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EaKsW084GkPEhUaZ6--65g.png" /></figure><p><em>It takes a while for HPA to complete resizing the deployments, 10 seconds should be enough though.</em></p><p>If we list our deployments they should be 3 as compared to 2 previously, the auto-scaler scaled the deployments to a minimum of 3 replicas as specified on the definition.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fYR-EMLY-lmAGqYHuwvxUA.png" /></figure><h3>Updating an application</h3><p>Once an application has been deployed to production, the users expect it to be live at all times. Developers on the other side can deploy new version several times a day depending on you each team velocity. If this process is not carefully handled users might experience downtime each time a new version is deployed.</p><p>In kubernetes handles, this problem by using rolling updates. Rolling updates allow Deployment update to take place with zero downtime by incrementally updating Pods instances with new ones.</p><p>In the previous section, we scaled our application to run multiple instances. This is important for performing updates without affecting application availability. By default, a cluster can have a maximum number of pods unavailable during update and the maximum number of new pods created is one. Both options can be configured as either numbers or percentage.</p><p>Kubernetes updates are versioned, this is crucial when an update has to be reverted to the previous stable version.</p><p>Rolling updates allow the following actions</p><ul><li>Promote an application from one environment to another</li><li>Rollback to previous versions</li><li>Continuous integration and delivery of application with zero downtime.</li></ul><h3>Updating Application Version</h3><p>To update the application version first we need to update our image. Change the <strong><em>index.js</em></strong> file to look as below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*99hwUZgZmO1iXITFkkROkw.png" /></figure><p>Build the image and tag it v2.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3qNZ5zfNZFJr6xi7JtmTaA.png" /></figure><p>Now that we have our image built and available in minikube, we can update the image of the application to <strong><em>v2</em></strong> using<strong> <em>set image</em></strong> command, followed by the deployment name and the new image version.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WiAFlrHEQlZYtN-P0rjEvg.png" /></figure><p>The command notified the deployment to use a different image for the app and initiated a rolling update. We can verify if the pods a running by using get pods command.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FHCKFgKJ6hSSpcARuc62Jg.png" /></figure><p>As you can see our pods are still running. The only thing left is to confirm if the updated image is being used.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_T94Vm0um9s4rzCDViYZ3w.png" /></figure><p>Bam! We get “Hello World! I am Version 2” as a response. This confirms that our application was updated successfully. You can also check the status of an update via rollout command.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZOeyAJHQqtq7zA6pICoRdg.png" /></figure><h3>Rollback an Update</h3><p>Application rollbacks are done using rollout undo command. This will revert your application to the previous stable version. For our case the v1 image., which displayed only “Hello World”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZSKNFdnBrUGd7djnYdlplg.png" /></figure><p>This command will revert our application to version one. We can confirm by listing the details of the pods, or use the curl command to see what output we will get from the application. We expect our application to display <strong><em>‘Hello World</em></strong>’.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MX5kRF-kT4zoLVKQuoHQ4A.png" /></figure><p>Hurray! We successfully rolled out our application back to the previous version.</p><h3>Conclusion</h3><p>Scaling application to handle increasing traffic is important. It makes your application dynamic and highly available. It&#39;s important to ensure that your deployed application is able to meet the traffic demand. Horizontal pod auto-scaler is recommended to make your deployments to scale automatically based on custom or CPU utilization metrics.</p><p>This article marks the end of our 5 part series on kubernetes. It is our hope that you now have a basic understanding of how to create and deploy and manage the application on a kubernetes cluster. We have only provided basic knowledge to get you started, we strongly recommend constant practice and research to improve on your current skills.</p><p><em>Has this article been useful to you? please share extensively, we also welcome feedback on content you would like us to cover or how to improve our delivery. Engage us on </em><a href="https://www.facebook.com/rackbrainz"><em>facebook</em></a><em>,</em><a href="https://twitter.com/@rackbrainz"><em> twitter,</em></a><em> or send us an email on info.rackbrains@gmail.com.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8d78f1b89852" width="1" height="1" alt=""><hr><p><a href="https://medium.com/rackbrains/kubernetes-101-part-5-scaling-the-application-and-rolling-updates-8d78f1b89852">Kubernetes 101 part 5: Scaling the Application and rolling updates</a> was originally published in <a href="https://medium.com/rackbrains">Masked Weaver</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kubernetes 101 part 4: Exposing application via Services]]></title>
            <link>https://medium.com/rackbrains/kubernetes-101-part-4-exposing-application-via-services-4c42bd3d6e01?source=rss----231a6385b39d---4</link>
            <guid isPermaLink="false">https://medium.com/p/4c42bd3d6e01</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[containerization]]></category>
            <dc:creator><![CDATA[Thomas Nyambati]]></dc:creator>
            <pubDate>Sat, 08 Dec 2018 10:01:01 GMT</pubDate>
            <atom:updated>2019-01-04T11:35:44.689Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-ZNAm7ibdwZ4j-SsxqIwOw.png" /></figure><p><em>In the previous article, we explored deployments and pods. We were able to troubleshoot the application using different </em><em>kubectl commands. The objective of this article is to learn about kubernetes services, understand how labels and labelSelector objects relate to it and how services can be used to expose the application outside the cluster.</em></p><p><a href="https://medium.com/rackbrains/kubernetes-101-part-3-deployments-and-pods-217808afddfe">Kubernetes 101 part 3: Deployments and pods</a></p><p><em>All the code changes can be found on our </em><a href="https://github.com/rackbrainz/kubernetes-101"><em>GitHub repository</em></a></p><p><a href="https://github.com/rackbrainz/kubernetes-101">rackbrainz/kubernetes-101</a></p><h3>Kubernetes Services</h3><p>Kubernetes pods are mortal, this means they can be created and killed. When a Node dies the pods running in the Node also die. A replicationController will then dynamically drive the cluster back to the desired by creating new pods to keep your application running.</p><p>Imagine a system with a backend API with three replicas. The frontend application should not care about the replicas or even if the pod is killed and recreated. Since each Pod in the cluster has a unique IP address, even though they are in the same Node, there is a need for a way to reconcile this changes among pods so that your application will continue to function.</p><p>Kubernetes service is an abstraction which defines a logical set of pods and the policy on how they can be accessed. The set of pods targeted by a service is determined by a label selector. We will talk about labels later in the article. This approach enables loose coupling between dependent pods. A service just like all kubernetes is defined using YAML— preferred or JSON.</p><p>The pods targeted by the Service are determined by LabelSelector. Although each pod has a unique address, those IPs are not exposed outside the cluster without a service. Therefore services allow the application to receive traffic from the outside the cluster.</p><h3>Publishing Services — Service Types</h3><p>When exposing a service to an external IP address, kubernetes allows you to specify what kind of service you want through ServiceType property. If none is specified, ClusterIP is assumed by default. These types define how the services should behave.</p><ul><li>ClusterIP: This type exposes the service on the cluster internal IP. This means that the service is only reachable from within the cluster.</li><li>NodePort: This type exposes the service on each Node’s static IP address.</li><li>LoadBalancer: This service type exposes a service using the cloud provider load balancer.</li><li>ExternalName: Maps the service to the contents of the externalName field such as example.com by returning a CNAME record with its value.</li></ul><p>We will cover services in details in the later article, for the scope of this article, we will be using NodePort service type.</p><h3>Creating a Kubernetes Service</h3><p>As we have learned above, services allow us to access our application outside the cluster. At the moment we only have one service in our cluster. This service is created by default by minikube when the cluster is created. To create a new service and expose it to external traffic, we will be using expose command with NodePortas parameter.</p><p><em>Note: minikube does not support load balancer yet.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DRUcNpd_hIfiYpt8I9W0cA.png" /></figure><p>Let’s list our services to confirm if our service has been created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HR6T1LNp040q1mCFQ76OXQ.png" /></figure><p>As you can see we now have two services, the default kubernetes services, and the newly created service which has been assigned a unique ClusterIP, internal port, and external IP. To find out what port was opened externally we can run describe service command.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JZtLI-0xZKL6ovdypryccQ.png" /></figure><p>This command gives us detailed information about the service which includes the NodePort. let&#39;s use environment variable NODE_PORT to hold the value of the external port.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j7ql662WuDapz6zFl1OP1A.png" /></figure><p>We can now use the node port to access the application via minikube ip and the exposed port.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6nXGbFzWvtZNmfaUfHtbTQ.png" /></figure><p>Via all the above command we have been able to access our application without using port forwarding or proxy.</p><h3>Labels and Selectors</h3><p>Labels are key/value pairs attached to kubernetes objects, such as pods, deployments, services etc. They are used to specify identifying attributes of objects that are meaningful and relevant to users. They can be used to organize and select a set of objects. Services use labels to identify pods that it should route traffic to. When a deployment is created, it automatically labels pods. you can see the labels attached to a deployment by running a describe command.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YhGUenreYQyC4P79UoR8HQ.png" /></figure><p>As you can see the pod was given a label <strong><em>app=kubernetes-101</em></strong>. We can use this label to query the cluster for a list of pods by adding -l flag.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5LKIcs9fn5GOtjTPsOKLqg.png" /></figure><p>The same can be done to services,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CttwpBWS_RxBbfUZQh7F-w.png" /></figure><p>Labels can also be used to filter pods and services. Let&#39;s apply a new label to test this out. Get the pods name and store it to POD_NAME environment variable. <strong><em>kubectl get pods | grep kubernetes-101 | awk ‘{ print $1 }&#39;</em></strong> , then apply the new label via the label command followed by the object type, object name, and the new label.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AU9PvTZ4Y8tb-mnuhM-Y_w.png" /></figure><p>This command will apply the new label (version=v1) to our pod. we can verify this fact by checking the pod&#39;s definition.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q3N6pVDduHPfH8YKJvgjqA.png" /></figure><p>As you can see the label version has been added to our pod definition.</p><blockquote>Note: When using yaml configuration labels and selectors are added on deployment and service definition.</blockquote><h3>Defining a Service</h3><p>Kubernetes Services are similar to Pods or Deployments. Like other objects services can be defined in JSON or YAML format. Below is an example of how a kubernetes service for this application would look like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IDklZFvyBzdH_Ru9tHyTIA.png" /></figure><p>We can apply this configuration to the cluster by running kubectl apply -f k8s/service.yaml . You can find this changes in <a href="https://github.com/rackbrainz/kubernetes-101/blob/master/k8s/service.yaml">our repository</a>.</p><p><strong><em>Yaml or JSON configuration objects are recommended for production deployments.</em></strong></p><h3>Deleting services</h3><p>Assuming that you don’t need a service anymore, you can delete a service by use of delete service command. Labels can be used to filter services.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SRReFgZhzeLpXlsRjY37SQ.png" /></figure><p>To confirm that the service has been deleted we can list services again, and we should only have one service.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7UFHb34YVm4LK_kssNQEmw.png" /></figure><p>As you can see only the default service is left. This confirms that the service was deleted. We can also check that the application is not exposed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*as4KThhLPl470gG0__Kl8A.png" /></figure><p>This proves that we cannot reach the application from the terminal or browser. We can further check if the application is running by executing a curl command inside the pod.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m_CRAcL02gzAkDk2Yvvr9Q.png" /></figure><p>There we go, our app is still running inside our pod.</p><h3>Conclusion</h3><p>Services are an important piece when working with Kubernetes deployments. They enable us to manage how we route traffic to our pods from within or outside the cluster. Due to the volatility of pods, services make service discovery simple enabling us to scale our deployments without worrying about how we are going to map each pod IP addresses to a proxy or external load balancers. We have only scratched the services of what services have to offer, therefore I will encourage you to invest time and tinker with them.</p><p>In our <a href="https://medium.com/rackbrains/kubernetes-101-part-5-scaling-the-application-and-rolling-updates-8d78f1b89852">next article</a> in this series, we will be looking into how we can scale our application. See you then.</p><p><em>Has this article been useful to you? please share extensively, we also welcome feedback on content you would like us to cover or how to improve our delivery. Engage us on </em><a href="https://www.facebook.com/rackbrainz"><em>facebook</em></a><em>,</em><a href="https://twitter.com/@rackbrainz"><em> twitter,</em></a><em> or send us an email on info.rackbrains@gmail.com.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4c42bd3d6e01" width="1" height="1" alt=""><hr><p><a href="https://medium.com/rackbrains/kubernetes-101-part-4-exposing-application-via-services-4c42bd3d6e01">Kubernetes 101 part 4: Exposing application via Services</a> was originally published in <a href="https://medium.com/rackbrains">Masked Weaver</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kubernetes 101 part 3: Deployments and pods]]></title>
            <link>https://medium.com/rackbrains/kubernetes-101-part-3-deployments-and-pods-217808afddfe?source=rss----231a6385b39d---4</link>
            <guid isPermaLink="false">https://medium.com/p/217808afddfe</guid>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[containerization]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Thomas Nyambati]]></dc:creator>
            <pubDate>Thu, 06 Dec 2018 10:01:01 GMT</pubDate>
            <atom:updated>2019-01-04T11:34:10.386Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-ZNAm7ibdwZ4j-SsxqIwOw.png" /></figure><p><em>In the previous article, we packaged a simple NodeJs application and deployed it to the cluster via a deployment. We went a step further and exposed the pods created via port forwarding. In this article, we will be exploring and troubleshooting the application to get a deeper understanding of deployment and pods.</em></p><p><a href="https://medium.com/rackbrains/kubernetes-101-part-2-deploying-apps-to-kubernetes-cluster-fcad2615d59">Kubernetes 101 part 2: Deploying apps to Kubernetes Cluster</a></p><p>So what is a pod? A pod is a kubernetes abstraction that represents a group of one or more application containers and any resources shared by the containers. Some of the resources that can be shared by containers in the same Pod include;</p><ul><li>Shared storage, as volumes</li><li>Networking, as unique cluster IP addresses</li><li>Information on how to run each container .ie version, or ports to use.</li></ul><p>A pod can contain different application containers which are relatively coupled. A good example is Node Js application container and a different container that feeds data to be published by the Node Js web server.</p><p>Pods are considered to be atomic units. When a Deployment is created, kubernetes will create a Pod with containers inside then instead of creating containers directly. Each Pod is tied to a specific Node where it is scheduled and remains there until termination or deletion. In the case of a Node failure, the Pods will be scheduled on other available Nodes in the cluster.</p><h3>Troubleshooting a Kubernetes Cluster with kubectl</h3><p>Once our application is deployed to the cluster, some troubleshooting is required to make sure that everything is working as expected. The most common operation can be done with the following kubectl commands</p><ul><li>kubectl get — Used to list resources</li><li>kubectl describe — Used to show detailed information about a resource</li><li>kubectl logs— Used to print logs from a container Pod</li><li>kubectl exec — Used to execute a command on a container pod</li></ul><p>These commands can be used to find the current status of the deployed applications, where they are running and their configurations. Having said that let us explore our application.</p><h3>Check Application configuration</h3><p>Having created a deployment earlier, let’s verify that it exists and is running. First, we will have to check if the deployment was created and confirm if the deployment, in turn, created Pods.</p><p>To view deployments, run the command below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-w2XKoHev_nIsaLuZT-dCA.png" /></figure><p>As you can see, we now have a deployment named kubernetes-101. Now that we have established that our deployment has been created, lets confirm if our pods are up and running.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B36MzjEPvYxJTlT05nFk_g.png" /></figure><p>Now that we know that our Pods are running, we can view what containers are inside the Pod and what images are used to build those containers. This is done by use of describe pods command.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z4hlMZkFfXybAlnedZz7cg.png" /></figure><p>This command will output details about the pods container Ip address, ports used and list of events related to the lifecycle of the Pod. This output provides more detailed data that we can use to verify if our deployment was successful or not when troubleshooting.</p><h3>Access the application via the browser</h3><p>Kubernetes Pods always run in an isolated private network and therefore cannot be accessed outside the cluster. To access the application on the browser, we are going to use port-forwarding to make the port accessible on our local machine. kubectl port-forward command will enable us to bind a local port to either a service or pod port.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L5X40aYuCBk13wYAiFPFHA.png" /></figure><p>First, we get the name of the pod, then use port forwarding command to map our local port 3000 to the pod’s internal port 3000. All the traffic directed to the local port will be redirected to the pod within our cluster. You can now access the application via the browser on <a href="http://localhost:3000/">http://localhost:3000</a>. You should be able to see “Hello World !” on your browser.</p><blockquote>If you have httpie installed on your machine you can run the command below. You can install httpie via pip, pip install httpie<em>.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1bmdoT4EkZmH5lOtiP_Vjw.png" /><figcaption>Result via command-line</figcaption></figure><h3>Accessing and viewing application logs</h3><p>All the outputs an application makes to the STDOUT is converted to logs for the container within the Pod. These logs can be accessed using kubectl logs command. We can use this logs to get important information on how the application is doing within our cluster.</p><p><em>When working with production level cluster, it’s advisable to deploy a centralized logging system such as ELK.</em></p><p>To access pod logs, we will use kubectl logs command and execute it against the current running pod. Since we already exported the pod name in our previous command, we can skip to running the command as shown below.</p><p><em>If you are running this on a new shell or terminal, start by exporting the pod name by running this command.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KRdI82qyVqR7r4qMt4QEtw.png" /><figcaption>Export pod name to the environment</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZJMRxEjMzDMvLtFWSJL6MQ.png" /><figcaption>Get logs of the specified pod</figcaption></figure><p>This command will output all the logs that belong to the specified Pod, in our case the pod created by kubernetes-101 Deployment.</p><h3>Executing commands against a Pod</h3><p>In some scenarios, one needs to run commands inside a pod during troubleshooting. This might include operations like checking the injected environment variables to manually running migration —<em> p</em><strong><em>s: Always automate database migration via jobs. </em></strong>Kubectl allows us to run commands against a specific Pod. This is done through the use of exec command. Let’s try this out by listing all the environment variables in our pod.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oY7-WeTEGJE2CEp7qWfqjQ.png" /></figure><p>As you can see our command was successful. However, in a more ideal situation you might need to access the shell terminal inside the container.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*due3VYgCqz_-Y-Hl5IkGqg.png" /></figure><p>Running the above command will start a bash session inside our pod giving us access to console on the container while the application is running. We can confirm this fact by listing the content of the index.js .The contents of the file should be the same as the one we created in the previous article.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Hs9355XKD3mcC83oixWs3g.png" /></figure><p>We can continue executing commands inside this container until it terminates or we decide to end the session.</p><h3>Conclusion</h3><p>The main focus of this article is to get you familiar with the common kubectl command you might need when you are troubleshooting your deployments. This always goes wrong all the time and being able to gather data and metrics from your deployments will enable you to fix and make relevant changes to get your application up and running.</p><p>There are more commands and methods one can employ when troubleshooting, I will leave this on you to explore further. In the <a href="https://medium.com/rackbrains/kubernetes-101-part-4-exposing-application-via-services-4c42bd3d6e01">next article</a>, we will be focusing on how we can be able to expose our application to the outside world without using port-forwarding.</p><p><em>Has this article been useful to you? please share extensively, we also welcome feedback on content you would like us to cover or how to improve our delivery. Engage us on </em><a href="https://www.facebook.com/rackbrainz"><em>facebook</em></a><em>,</em><a href="https://twitter.com/@rackbrainz"><em> twitter,</em></a><em> or send us an email on info.rackbrains@gmail.com.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=217808afddfe" width="1" height="1" alt=""><hr><p><a href="https://medium.com/rackbrains/kubernetes-101-part-3-deployments-and-pods-217808afddfe">Kubernetes 101 part 3: Deployments and pods</a> was originally published in <a href="https://medium.com/rackbrains">Masked Weaver</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting started with Docker]]></title>
            <link>https://medium.com/rackbrains/getting-started-with-docker-7ef142a2c17e?source=rss----231a6385b39d---4</link>
            <guid isPermaLink="false">https://medium.com/p/7ef142a2c17e</guid>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[containerization]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Businge scott]]></dc:creator>
            <pubDate>Tue, 04 Dec 2018 10:01:01 GMT</pubDate>
            <atom:updated>2018-12-04T10:01:00.923Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aVkQkoFUECdb4dYbpV2uSA.png" /></figure><p><em>Docker is an open platform for developing, shipping, and running applications.</em> Docker enables you to separate your apps from your infrastructure so you can deliver software quickly by reducing the delay between writing code and running it in production.</p><p>It provides the ability to package and run an application in a <em>loosely isolated environment</em> called a <em>Container</em> which is lightweight, flexible, portable, scalable and easily interchangeable as compared to VM’s. The isolation allows you to run many containers simultaneously on any given host.</p><p>Docker has revolutionalized DevOps in the past few years by streamlining IT operations and development teams. To deliver the consistent experience for developers and IT ops, teams began using Docker for Containers as a Service (CaaS) which is often described as a model where IT organizations and developers can work together to build, ship and run their applications anywhere.</p><p>Containers too are great for Continuous Integration and Continuous Delivery (CI/CD) workflows because they simply enable pushing the updated image to the production environment.</p><h4>Docker Concepts</h4><ul><li><strong><em>Image:</em></strong> It’s an executable package that contains all that is required to run an application. e.g the code, a runtime, libraries, environment variables, and configuration files.</li><li><strong><em>Container:</em></strong> is a running instance of an image, i.e what the image becomes in memory when executed.</li><li><strong><em>Dockerfile:</em></strong><em> </em>is a text document that contains all the commands a user could call on the command line to assemble an image.</li></ul><h4>Installing Docker Binaries</h4><p>Let’s start with installing Docker on your machine. We shall skip the details of this section but guidance is provided on the official Docker documentation. Depending on the system you’re running follow any of the links below to have it installed.</p><p>Docker for mac</p><p><a href="https://docs.docker.com/docker-for-mac/install/">Install Docker Desktop on Mac</a></p><p>Docker for windows</p><p><a href="https://docs.docker.com/docker-for-windows/install/">Install Docker Desktop on Windows</a></p><p>Docker for Ubuntu</p><p><a href="https://docs.docker.com/install/linux/docker-ce/ubuntu/">Get Docker Engine - Community for Ubuntu</a></p><p>You can test your docker installation by checking the installed version.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ML5HsWaEb010422Ks5J1xg.png" /><figcaption>Results of running docker version command</figcaption></figure><h4>Common docker CLI commands</h4><ul><li><a href="https://docs.docker.com/engine/reference/commandline/images">docker images</a> shows all images.</li><li><a href="https://docs.docker.com/engine/reference/commandline/build">docker build</a> creates an image from Dockerfile.</li><li><a href="https://docs.docker.com/engine/reference/commandline/ps">docker ps</a> shows running containers.</li><li><a href="https://docs.docker.com/engine/reference/commandline/start">docker start</a> starts a container</li><li><a href="https://docs.docker.com/engine/reference/commandline/stop">docker stop</a> stops a running container.</li><li><a href="https://docs.docker.com/engine/reference/commandline/create">docker create</a> creates a container but does not start it.</li><li><a href="https://docs.docker.com/engine/reference/commandline/run">docker run</a> creates and starts a container in one operation.</li><li><a href="https://docs.docker.com/engine/reference/commandline/restart">docker restart</a> stops and starts a container.</li><li><a href="https://docs.docker.com/engine/reference/commandline/pause/">docker pause</a> pauses a running container.</li><li><a href="https://docs.docker.com/engine/reference/commandline/rm">docker rm</a> deletes a container.</li><li><a href="https://docs.docker.com/engine/reference/commandline/inspect">docker inspect</a> looks at all the info on a container (including IP address).</li><li><a href="https://docs.docker.com/engine/reference/commandline/logs">docker logs</a> gets logs from the container.</li><li><a href="https://docs.docker.com/engine/reference/commandline/top">docker top</a> shows running processes in the container.</li><li>docker ps -a shows running and stopped containers.</li></ul><h4>Building a Docker Image</h4><p>As we have mentioned before, an image is an executable that contains all the required dependencies and resources to run an application. These dependencies are packaged into an image via instructions in a dockerfile. These instructions dictate what packages and binaries to be installed and how the images will be run during image build-time. The official documentation provides <a href="https://docs.docker.com/engine/articles/dockerfile_best-practices/">best practices </a>that should be followed when writing a dockerfile.</p><blockquote>It’s important to note that these images are read-only templates which consist of a series of layers which are combined by Docker into a single image.</blockquote><p>Let us write a simple dockerfile to have a glimpse of what we are talking about. First, create a directory and include a file to give our work context.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DBwn-PnJrpMFAPrjbx--UQ.png" /></figure><blockquote>PS: The above commands automatically uses vim as default editor . Feel free to free to substitute vim for your preffered editor.</blockquote><p>The default naming convention for a dockerfile is Dockerfile, this enables docker to automatically find the file during build-time within the same context. You can use an arbitrary name of your choice. However, you will have to inform docker where to locate this file.</p><p>Add the following steps to theDockerfile.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*23nHWF3HC5LTzzx100875g.png" /></figure><p>Building a docker image is done via docker buildcommand. This command builds a docker image from a Dockerile and a context. A build context is the set of files located within a specified path that the build process can refer to during build-time. The build command also requires you to specify a unique tag which is a human-readable identifier that can be used to reference the built image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BSBHsoOqGLCJCTatHWpBgw.png" /></figure><p>When you run this command, what docker will do is download the base image if it does not exist on your machine. Then will execute each instruction on the Dockerfile and when it is done, the image is taggedimage-1 as specified on the command. Each instruction will form a layer, so if you are looking to build an image with a few layers, you might need to consider chaining some of the instructions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3_rpb6TAcB1kQXN1csjU0g.png" /></figure><p>Based on the above result you will notice that our image has four layers which are equal to the number of instructions/steps on our Dockerfile. Once the command had done executing, we can prove that our image has been created by listing all the images as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qJKZwAJEBA0ZwuJVOITUPQ.png" /></figure><h4>Running a Docker Image as a container</h4><p>As per official documentation, a container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. In simple terms, a container is instances of an image. The command below shows how we can run our image as a container.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MAoEqdEfFzaNT5G01_gXXw.png" /></figure><p>We’ve run our container using the docker run command. The -i will run our container in interactive mode. Notice the app directory we specified in our Dockerfile.</p><h4>Dockerizing a simple Flask App</h4><p>Having looked at the basics of Docker, it would be more ideal if we tried for a more realistic example. In this section, we are going to create a simple flask application, package it and run it inside a Docker container.</p><p>In our current working directory, create a sub-folder called flask-app, and within it add <strong><em>app.py, requirements.txt,</em></strong> and a <strong><em>Dockerfile.</em></strong></p><blockquote>Please note that you have to have <em>python</em> binaries installed on your machine to proceed.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PqvWjwOAiLHSEOK3NE1QAA.png" /></figure><p>Add the following code snipped to <strong><em>app.py </em></strong>file and save the changes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q_7AumBi8bqN6ITrvjFaxQ.png" /></figure><p>Update <strong><em>requirements.txt</em></strong> file with the changes below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kvQOdPSutmZ-ytQLr4z1fQ.png" /></figure><p>Let us put together our Dockerfile and save.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ILobB6TLyjeyL_cC2crsDQ.png" /></figure><p>After completing the above steps, your current directory should look as below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jsHT3ABbZXmNGfsF1JIPIA.png" /></figure><blockquote>Note: You need to have tree binaries installed on your machine.</blockquote><p>Now that we have everything set up, we can build the image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XKb4q4js_h0BAoJt1lf3DQ.png" /></figure><p>The above command will build an image and tag it — flask-app as specified on the command We can prove this by listing all our images, flask-app should appear on the list of listed images.</p><p>Now that we have an image, its time to create a container from it. Containers always run on an isolated network, therefore we will need to bind our container port (80) to a machine port (5000).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*awCw63l-fkzfp7CL-CvSTg.png" /></figure><p>Remember we mapped port 80 of that container to 5000, making the correct URLhttp://localhost:5000. Let’s go to that URL in a web browser to see the display content served up on a web page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QCb5BWzafFvUH9a73VWnsQ.png" /><figcaption>served webpage</figcaption></figure><h4>Publishing Docker Images to Docker Hub</h4><p>After building and testing our image, we need to store these artifacts in a centralized location called container registry. Container Registry is a single place for your team to manage Docker images, perform vulnerability analysis, and decide who can access what with fine-grained access control. A good example of such a registry is Docker Hub. A registry is a collection of repositories, and a repository is a collection of images.</p><p>The <a href="https://hub.docker.com/">Docker Hub</a> is run and managed by Docker Inc. It is a place for centralized information about user accounts, images, and public namespaces. Let us publish one of the images we have built to docker hub.</p><ol><li>Go to <a href="https://hub.docker.com/">docker hub</a> and open an account if you didn’t have one.</li><li>After verifying your email address, you will get redirected to the login section of Docker Hub.</li><li>Submit your credentials and you will get redirected to your Dashboard.</li><li>Click on create a new repository, give it a name and a short description.</li><li>Choose the visibility ‘public or private’</li><li>Click Create.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PGJdviLXhHCG7M4H-bWeUg.png" /><figcaption>create new repository</figcaption></figure><p>7. Login into Docker Hub from the command line using your credentials.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1byfHcTzspgJZ2rB93v6yg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0tf9UOu8xPcfCXJZ1NOrSw.png" /><figcaption>docker hub login</figcaption></figure><p>8. Tag the image</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*naOxf4S4y4DSylUg5hqC8w.png" /></figure><p>10. Push the image</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1BoWTD3QqnBjIexsO-DGjg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4NVRn5YkmQZUFaLDLpKtWg.png" /></figure><p>10. Check out your image on docker hub.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IlCslTCaTjWwfpRNO2PfsQ.png" /></figure><h4>Conclusion</h4><p>In this article, we have explored docker basics, from basic docker commands, Dockerfiles, images, and container. We even took a step further and built a sample application of which we have been able to package and publish on our docker registry.</p><p>This isn’t all there is about docker, what we have learned so far are basics to get you started. In fact, we’ve just scratched the surface. I would recommend deeper dive into docker offering not limiting to docker cloud, docker swarm etc and understand how they simplify software deployment or delivery.</p><p><em>Has this article been useful to you? please share extensively, we also welcome feedback on content you would like us to cover or how to improve on our delivery. Engage us on </em><a href="https://www.facebook.com/rackbrainz"><em>facebook</em></a><em>,</em><a href="https://twitter.com/@rackbrainz"><em> twitter,</em></a><em> or send us an email on info.rackbrains@gmail.com.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7ef142a2c17e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/rackbrains/getting-started-with-docker-7ef142a2c17e">Getting started with Docker</a> was originally published in <a href="https://medium.com/rackbrains">Masked Weaver</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kubernetes 101 part 2: Deploying apps to Kubernetes Cluster]]></title>
            <link>https://medium.com/rackbrains/kubernetes-101-part-2-deploying-apps-to-kubernetes-cluster-fcad2615d59?source=rss----231a6385b39d---4</link>
            <guid isPermaLink="false">https://medium.com/p/fcad2615d59</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[containerization]]></category>
            <dc:creator><![CDATA[Thomas Nyambati]]></dc:creator>
            <pubDate>Wed, 28 Nov 2018 10:01:01 GMT</pubDate>
            <atom:updated>2019-01-04T11:31:32.551Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-ZNAm7ibdwZ4j-SsxqIwOw.png" /></figure><p><em>In our </em><a href="https://medium.com/rackbrains/kubernetes-101-part-1-8bd033f3ff33"><em>previous article</em></a><em> we looked at the kubernetes cluster and its components. We went a step further and created a local cluster using minikube.</em></p><p><a href="https://medium.com/rackbrains/kubernetes-101-part-1-8bd033f3ff33">Kubernetes 101 part 1</a></p><p><em>The objective of this section is to learn about application deployments, how it’s done with examples. We will deploy our app via the Kubectl CLI and configuration files. The code used in this section will be hosted on our </em><a href="https://github.com/rackbrainz/kubernetes-101"><em>GitHub repository</em></a><em>.</em></p><p><a href="https://github.com/rackbrainz/kubernetes-101">rackbrainz/kubernetes-101</a></p><h3>Kubernetes Deployments</h3><p>Now that we have a running Kubernetes cluster, we can deploy a containerized application on it. This can be achieved through use of Deployment configurations. The deployments instruct Kubernetes how to create and update instances of your application. Once a deployment has been created, the kubernetes master schedules mentioned application instances into individual Nodes in the cluster.</p><p>Once the application has been created, Kubernetes deployment Controller will continuously monitor instances to ensure they satisfy the desired state. If a node hosting the application goes down or is deleted, the deployment controller replaces it, thus providing a self-healing mechanism to address machine failure or maintenance.</p><h3><strong>Deploying An App to Kubernetes</strong></h3><p>Kubernetes deployments can be managed via kubernetes command line interface <strong>kubectl.</strong> Kubectl uses the Kubernetes API to interact with the cluster. In this section we will look into some of the commands you will need to create deployments that run an application on the kubernetes cluster.</p><p>When creating a deployment you will need to specify the container image for your application and the number of replicas that you need in your cluster. This information can be changed later by updating the Deployment.</p><h3>Create Nodejs Application</h3><p>Now that we have established how we can manage deployments, let us create the application we will be deploying to our cluster. Create a folder in your workspace and addindex.js.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*phV5br-iW9NeLxlvgDNKPQ.png" /></figure><p>Update the file with the following code. This will setup a simple server that we can use to test our deployments.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g5M8vRqq2tuU5AYoEN8QXQ.png" /></figure><p>Let&#39;s run our app to make sure it is working fine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vui7VZPIAbzIo8nwxyUIqQ.png" /></figure><p>When you visit <a href="http://localhost:3000/">http://localhost:3000</a>, you should be able to see “Hello, World” on your browser. Now that our app is working, let&#39;s package it into a container.</p><h3>Create a Docker container image</h3><p>Before we can make any deployments to our cluster we need to create an image we will be using. This image will contain the app we just built. In your current working directory, create a file named Dockerfile. A Dockerfile describes the image that you want to build. Images can be built by extending an existing image, for instance, our images will extend from Nodejs Image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x5faDdyA36b2K1xUs50O3w.png" /></figure><p>This image extends the official NodeJs image, exposes port 3000, copies index.jsfile to the image and starts the NodeJs server.</p><p>Because we are using minikube, instead of pushing our images to a registry, we can simply build the image using the same docker host as the minikube VM so that the images will be available automatically. To achieve this we will have to ensure that we are using Minikube Docker Daemon:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XY53nSPICbdkZIylHAgXpQ.png" /></figure><p>If in any case, you don’t wish to use minikube host, you can undo this change by running.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wg48oB16IOE4u0Sobyjlcg.png" /></figure><p>Having setup docker let&#39;s build our image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jO68h0I-cR0iMEStiC-fVA.png" /></figure><p>The command above will build our image taggedkubernetes-101:v1 using our Dockerfile. After the command is done executing, the image will be available within our minikube context.</p><h3>Create a Kubernetes Deployment</h3><p>Kubernetes deployments are responsible for creating and managing pods — a kubernetes pod is a group of one or more containers, tied together for the purpose of administration and networking. Deployments check on the health of the pods and restart the pods&#39; container when it terminates. For this reason, deployments are the recommended way to manage the creation and scaling of pods.</p><p>Kubernetes Deployments can be created in two ways, through kubectl run command or through YAML configuration- which is the recommended approach.</p><blockquote>Note: We will be using the command line approach, however, we will be including the yaml configuration in code repository accompanying this article.</blockquote><p>Run the command below to create a deployment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2lV-5w0k4_cpEyZQqxRigw.png" /></figure><p>This command will create a deployment that manages a pod. This pod will run a container based on the kubernetes-101:v1image we created earlier on port 3000.</p><p>We can check if our deployment has been created by running kubectl get deployments command.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eCcOhyodMJWU-lzRD2LQMg.png" /></figure><p>As you can see we now have a deployment with name kubernetes-101. If we need more details on this deployment we can use the describe command.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hHkz6wqgremW82cf6-X-Bg.png" /></figure><p>This command provides more detailed information about our deployment this includes the image, replicas, ports, status etc. We can use this information to audit our deployments.</p><p>So far we have created the deployments via command-line. We can still achieve the same results using yaml configuration.</p><p><em>Note: If you didn’t clone the code repository accompanying add the following yaml configuration, add the following code snippet under the directory named k8s within your current working directory.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nEYWXMFZ-Y35pw3qskWPaQ.png" /></figure><p>Before we proceed, let&#39;s delete the previous deployment. Run the following command against the cluster.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oi01xEiZ5sUCa6MKExpLHg.png" /></figure><p>After is done executing, run kubectl create -f k8s/deployment.yaml</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uvcBh4JsiWcNNK4g2qNlpg.png" /></figure><p>When you run the commands we did before, you will notice that we get the same details and information. Configuration files a more recommended approach to creating kubernetes objects. This approach embraces the concept of infrastructure as code with makes managing the cluster much easier.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DAPIRwKsJFV_eQ1wtcs1Zg.png" /></figure><p>Now that we have explored both approaches of creating deployments, let&#39;s test if our deployment is actually working. We can achieve this by via kubernetes port forwarding.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_y6vmoKRkOnkaTA4xv-0SQ.png" /></figure><p>First, we get the pod name, then use the kubectl port-forward command to forward traffic to the local port. We can now access our deployed application on <a href="http://localhost:3000/">http://localhost:3000</a> .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9UZT_z13RZZFF7wQ6r07pg.png" /></figure><p>There we go!</p><p><strong>Conclusion</strong></p><p>In this section, we have looked into kubernetes deployments. We have also looked at how to package our applications into images via docker which we later deployed to the cluster via the command-line and yaml configuration. At this point, you should be able to explain what a deployment controller is, what it does and both approaches to creating one.</p><p>In the <a href="https://medium.com/rackbrains/kubernetes-101-part-3-deployments-and-pods-217808afddfe">next article</a>, we shall explore the deployment we just made to better understand how it works and other commands we can use to audit progress.</p><p><em>Has this article been useful to you? please share extensively, we also welcome feedback on content you would like us to cover or how to improve on our delivery. Engage us on </em><a href="https://www.facebook.com/rackbrainz"><em>facebook</em></a><em>,</em><a href="https://twitter.com/@rackbrainz"><em> twitter,</em></a><em> or send us an email on info.rackbrains@gmail.com.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fcad2615d59" width="1" height="1" alt=""><hr><p><a href="https://medium.com/rackbrains/kubernetes-101-part-2-deploying-apps-to-kubernetes-cluster-fcad2615d59">Kubernetes 101 part 2: Deploying apps to Kubernetes Cluster</a> was originally published in <a href="https://medium.com/rackbrains">Masked Weaver</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kubernetes 101 part 1]]></title>
            <link>https://medium.com/rackbrains/kubernetes-101-part-1-8bd033f3ff33?source=rss----231a6385b39d---4</link>
            <guid isPermaLink="false">https://medium.com/p/8bd033f3ff33</guid>
            <category><![CDATA[containerization]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Thomas Nyambati]]></dc:creator>
            <pubDate>Mon, 19 Nov 2018 14:01:02 GMT</pubDate>
            <atom:updated>2019-10-18T14:58:00.067Z</atom:updated>
            <content:encoded><![CDATA[<h3>Kubernetes 101 part 1: Kubernetes Cluster</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-ZNAm7ibdwZ4j-SsxqIwOw.png" /></figure><p>With modern web services, users expect applications to be available 24/7, and developers expect to deploy new versions of those applications several times a day. Containerisation helps package software to serve these goals, enabling applications to be released and updated in an easy and fast way without downtime.</p><p><a href="https://kubernetes.io/">Production-Grade Container Orchestration</a></p><p><a href="https://kubernetes.io/docs/concepts/overview/what-is-kubernetes/">Kubernetes</a> is a production-ready, open-source platform designed with Google’s accumulated experience in container orchestration, combined with best-of-breed ideas from the community. It helps you make sure those containerized applications run where and when you want and helps them find the resources and tools they need to work.</p><h3>Prerequisites</h3><p>This tutorial assumes that you already have prior knowledge of container technology especially Docker. Kubernetes is built to manage containers and therefore having a prior understanding of containerization — Docker is required. If this is the first time you’ve heard of containerization, I will recommend taking time and going through docker materials before you continue.</p><p>The objectives of this article are to learn about the Kubernetes cluster and its components. We will be using Minikube to create a local cluster which we will be using in the remainder of this tutorial series.</p><h3>Kubernetes Cluster Architecture</h3><p>Kubernetes coordinates a highly available cluster of computers that are connected to work as a single unit. The abstractions in Kubernetes allow you to deploy containerized applications to a cluster without tying them specifically to an individual machine. Kubernetes automates the distribution and scheduling of application containers across a cluster in a more efficient way.</p><p>Kubernetes clusters consist of two types of resources, Master and Nodes. The master is responsible for managing the cluster operations, it coordinates all the activities such as scheduling applications, and rolling new updates.</p><p>A node is a Virtual Machine or physical computer that serves as a worker machine to the Kubernetes cluster. Each node has a Kubelet agent for managing the node and communicating with the Kubernetes master.</p><p>When an application is deployed to Kubernetes, the master is informed to start application containers. It will then schedule the containers to run on the cluster nodes. The nodes communicate to the master via the API exposed by the master. This API can also be used by users to interact with Kubernetes.</p><h3>Kubernetes Master Node</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/638/1*UvAp3BPQQ8qLriv8T6g6_w.jpeg" /></figure><p>Kubernetes master is responsible for managing the nodes. Master components provide the clusters control plane, they make global decisions about the cluster, detecting and responding to cluster events. Master components can be run in any node in the cluster, however, for simplicity, all master components are installed and run in the same VM ( Master node).</p><ul><li><strong>Etcd - </strong>This is a highly available key-value data store that can be distributed among the nodes in the cluster. It is used to store the configuration information which is shared and used by each node in the cluster. This store is only accessible by the API server.</li><li><strong>API Server - </strong>Kubernetes exposes its functionality via the API. The API implements an interface through which different tools and libraries can communicate with it. All the configuration for Kubernetes is communicated via the Kubernetes API server.</li><li><strong>Controller Manager - </strong>This component is responsible for regulating the state of the cluster and performing tasks. You can consider it as a daemon which runs in a non-terminating loop and collects and sends information to the API server. The main job of this component is to ensure that the desired state of the cluster is achieved. This is done by running different controllers to handle the nodes and endpoints. These controllers include; replication controller, endpoint controller, namespace controller, and service controller.</li><li><strong>Scheduler - </strong>The Scheduler is a service that is responsible for distributing the workload. It accepts the workload, tracking workload utilization in the cluster, and distributes the workload to available resources. The scheduler is responsible for workload utilization and allocation of pods to available nodes.</li></ul><h3>Kubernetes cluster Nodes</h3><p>A Node is a worker machine in Kubernetes which can be either a virtual or physical machine depending on the cluster. All Nodes in a cluster are managed by the master. Nodes are responsible for running pods. A Node can have multiple pods and the Kubernetes master automatically handles scheduling the pods across the nodes in the cluster. The Master’s automatic scheduling takes into account the available resources on each Node.</p><p>Each Kubernetes Node runs the following services</p><ul><li><strong>Kubernetes Kubelet Service - </strong>Each node has a kubelet. This service is responsible for relaying information to and from the control plane service - Master components. It interacts with an etcd store to read configuration details and also write values, this communicates with the master to receive commands and work. The kubelet process then assumes the responsibility of maintaining the state of work and the node server. It also manages network rules and port forwarding among many.</li><li><strong>Kubernetes Proxy Service -</strong> This a proxy service that makes all service in a node accessible to the external host. It forwards requests to the correct containers and can also perform primitive load balancing. The proxy service ensures that networking environments are predictable and accessible while maintaining its isolation. It manages pods on a node, volumes, secrets, health checks, etc.</li></ul><p>A Kubernetes Cluster can be deployed on either a physical machine or a virtual machine. In this tutorial, we will be using Minikube to create our cluster. <a href="https://github.com/kubernetes/minikube">Minikube</a> is a lightweight Kubernetes implementation that creates a VM on your local machine and deploys a simple cluster containing only one node. <a href="https://github.com/kubernetes/minikube">Minikube</a> is available on macOS, Linux and Windows systems.</p><h3>Installation</h3><p>To follow along, you have to ensure that you have the following tools installed on your machine.</p><ul><li><a href="https://github.com/kubernetes/minikube">Minikube</a></li><li><a href="https://www.virtualbox.org/wiki/Downloads">VirtualBox</a></li><li><a href="https://kubernetes.io/docs/tasks/tools/install-kubectl/">Kubernetes CLI</a></li></ul><p>For MacOs, the above tools can be installed via brew.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1YW1NnJMYJuUTi0ErjGPTQ.png" /></figure><p>If you are using Windows or Linux check on each tool installation guide in the documentation.</p><h3>Create a Kubernetes Cluster With Minikube</h3><p>Once you have Minikube installed, you can confirm by running minikube versioncommand on your terminal you should get a response similar to the one shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rtLZhfGmTzNxBnJrhzS0_g.png" /></figure><p>Ok, now that we have Minikube installed, we are ready to create our cluster. Start a cluster by running Minikube start command.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JIZkHRGoxsV8vt3kdTow1Q.png" /></figure><p>This command will start and configure a Kubernetes cluster. Once the configuration is complete, run minikube status to confirm if the cluster was well configured.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VbYE2xE5KDHML1w4DC4CpQ.png" /></figure><p>To view the cluster details we will make use of Kubernetes CLI. When Minikube creates a cluster it also configures Kubernetes CLI with its context, with this we can be able to access the cluster. Let&#39;s run kubectl cluster-infoto get cluster details.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1DGEOjO9AslRmrvWzrwzqA.png" /></figure><p>Now that we have our cluster up and running we can use kubectl to explore the cluster. To view the nodes of this cluster, run the kubectl command shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UasfmAbx5347K1vaMNGUgw.png" /></figure><p>This command shows all nodes that can be used to host our applications. Based on the response above, we can confirm that we have one node ready to accept application deployments. In most ideal cases you will have more than one node in your cluster based on the scale of your application or when configuring a fault-tolerant cluster.</p><h3>Conclusion</h3><p>In this article, we have looked at the Kubernetes cluster, its components and how they all work together to manage your containers. We have also managed to create a cluster locally using Minikube. At this point, you should be able to explain what Kubernetes and Kubernetes clusters are, and its components. In our <a href="https://medium.com/rackbrains/kubernetes-101-part-2-deploying-apps-to-kubernetes-cluster-fcad2615d59">next article</a>, we will be looking at how we can deploy an application to the cluster we just created.</p><p><em>Has this article been useful to you? please share extensively, we also welcome feedback on the content you would like us to cover or how to improve on our delivery. Engage us on </em><a href="https://www.facebook.com/rackbrainz"><em>facebook</em></a><em>,</em><a href="https://twitter.com/@rackbrainz"><em> twitter,</em></a><em> or send us an email on info.rackbrains@gmail.com.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8bd033f3ff33" width="1" height="1" alt=""><hr><p><a href="https://medium.com/rackbrains/kubernetes-101-part-1-8bd033f3ff33">Kubernetes 101 part 1</a> was originally published in <a href="https://medium.com/rackbrains">Masked Weaver</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Automate SSH Key Rotation With Ansible part 2]]></title>
            <link>https://medium.com/rackbrains/automate-ssh-key-rotation-with-ansible-part-2-7c6f599dd086?source=rss----231a6385b39d---4</link>
            <guid isPermaLink="false">https://medium.com/p/7c6f599dd086</guid>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[ansible]]></category>
            <category><![CDATA[ssh]]></category>
            <dc:creator><![CDATA[Thomas Nyambati]]></dc:creator>
            <pubDate>Sun, 09 Sep 2018 21:01:01 GMT</pubDate>
            <atom:updated>2018-09-10T23:29:45.863Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*azJTcJGp2skIrn0FCxvODg.png" /></figure><blockquote>In the <a href="https://medium.com/rackbrains/automate-ssh-key-rotation-with-ansible-part-1-a99934d2dfc9">previous article</a> we looked into SSH, SSH keys and the steps to follow when you are doing a key rotation. We also did a manual key rotation based on the steps. In this article we will be looking at how we can automate this process via Ansible and Bash scripts.</blockquote><p><a href="https://medium.com/rackbrains/automate-ssh-key-rotation-with-ansible-part-1-a99934d2dfc9">Automate SSH Key Rotation With Ansible part 1</a></p><p>So far we have managed to successfully rotate our keys manually. It has not been a pain yet and that’s because we are using one server. It might be fun now but when your servers grow to hundreds or thousands, you will not be smiling at all.</p><p>So what do we do? Automate. Bash scripts have been around for ages and since it comes bundled with most linux machines, it will be our first approach to automate this process.</p><p><strong>Automating the process with Bash Scripts</strong></p><blockquote>We will be using the previous article folder structure. If you jumped straight to this article, you can create a new workspace by running the command below. Full codebase for this article can be found on our <a href="https://github.com/rackbrainz/automate-ssh-key-rotation-with-ansible">GitHub repository.</a></blockquote><p><a href="https://github.com/rackbrainz/automate-ssh-key-rotation-with-ansible">rackbrainz/automate-ssh-key-rotation-with-ansible</a></p><blockquote>Feel free to think tinker with it as you see fit.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8XaF2RyS47x3JPdTyO6asw.png" /></figure><p>Update the vagrant file with the snippet below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2qNAXGRNr5WhL7T0qTtBYg.png" /></figure><p>Boot up the VM,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GzLnBH5znvFk3XcgpJMBgw.png" /></figure><p>Now that we have that taken care-of, lets get started.</p><p>In our working directory create a folder and call it bin and in it a file named rotate-keys.</p><blockquote>Keep in mind that all bash scripts file will be located in the <em>bin</em> folder for the remainder of this article.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d0lXyK89LCtfDVGrRRW7hQ.png" /></figure><p>Update rotate-keys.sh with the code snippet below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*00DAmQ-lLQsLSc8jmmsrQA.png" /></figure><p>The approach we are taking here is to be able to run our file as a CLI. This will give us the flexibility to pass arguments making our script dynamic. With <strong>getopts</strong> program we can create options based arguments that we can pass to our scripts. This argument values are assigned to variables and used later in the automation process. You can get more details on <a href="https://linux.die.net/man/1/getopt">getopts on linux man page</a>.</p><p>To ensure that we always the correct values at all times. We have to do a sanity check. The <strong><em>sanityCheck</em></strong>ensures that all the required variables have been assigned, if not, it assigns default values in their place. These variables are passed to three functions <strong><em>generateNewSshKeyPair</em></strong> which generates new key pair, <strong><em>addNewSshPublicKey</em></strong> which adds a new public key to the server and <strong><em>removeOldPublicKey</em></strong> which removes the old public from the server.</p><p>These three functions abstracts all the commands we did manually earlier into a bash script. when the script is run with the right arguments the rotation process will be done for us.</p><blockquote>Before running this script ensure that you have added the private key to the ssh agent and your VM is in running state. If you are using the VM from the last article, add the new generated keys in the ssh folder. If you are using a new VM use the private key located in <strong><em>.vagrant/machines/default/virtualbox/private_key</em></strong>.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cl9DoY__VFiNZ82dQPAWUw.png" /></figure><p>To keep our workspace tidy, move the generate key pairs from bin folder to the ssh folder . We can then access the VM using the new key as shown on the snippet below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3zrKuE9IV-x_1i0wfReDwQ.png" /></figure><p>Woohoo! Worked like a charm.</p><p>Even though we have been able to successfully automate this process, it is still hard to scale this script to the large fleet of servers. We can add a loop and more argument but the script will get more complex and hard to maintain. At this point, we need to look for a new alternative. Ansible.</p><p><strong>Automating the process with Ansible Scripts</strong></p><p>Manual process and bash script might have worked but when you are working with a large fleet of servers they may not be able to scale. At this point we need to find a tool that that is easy to work with and can scale to as many servers as possible and Ansible fits the bill. So let’s see how we can convert the above steps into Ansible scripts.</p><blockquote>If you are not familiar with Ansible, I will encourage to read <a href="https://scotch.io/tutorials/getting-started-with-ansible">Getting started with Ansibl</a>e on scotch.io, and the official documentation as well.</blockquote><p><a href="https://scotch.io/tutorials/getting-started-with-ansible">Getting started with Ansible</a></p><p><strong>Setup Ansible configuration</strong></p><p>Working with Ansible scripts in pretty easy, it enables you to structure your scripts in an easily readable and maintainable script using YAML syntax. Before we proceed to translate our bash script to Ansible scripts, we need to setup Ansible configuration.</p><blockquote>At this point, I will assume you have already installed Ansible on your machine and you have a basic understanding of how Ansible works.</blockquote><p>Run the command below to get started.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I_5FYCLGACr35vMtIYG1Bw.png" /></figure><blockquote>All the ansible related files will be located in the ansible folder for the remainder of this article.</blockquote><p>Before we start writing and testing our script we have to ensure that Ansible understand the hosts it will be provisioning and what credentials to use. This is achieved by using <strong>ansible.cfg</strong>,<strong> </strong><strong>ssh.config</strong>,<strong> </strong>and<strong> </strong><strong>inventory.ini</strong>. Since we already have the inventory file, lets create other files.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MNz04SqA9VpLCF_bgv0-Qw.png" /></figure><p>In the files created above, modify them with the code below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lcSnWt9AU8T_xEZ8ohAXTw.png" /></figure><p>This file contains the information on how to connect to the host machine via ssh.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*peqyQ0Fu6kVZS2JhkOuV-Q.png" /></figure><p>This file provides additional information to the ssh client on how to connect to the host machine.</p><p>The last thing we need to do is to tell ansible which hosts to provision. This is done via ansible inventory file. In this file we can place all the hosts that ansible will be provisioning.</p><p>Modify the inventory file with the code snippet below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G5OFEGsKNXvidhKarLUstw.png" /></figure><p>If you are using a different host you may need to change the IP address to match yours. At this point your folder structure should look as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AmVdTo81t7wscD1CTtsGDQ.png" /></figure><p>Having setup ansible, let’s test if our setup actually works. Run the command below to ping the server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qmce1nDz-DVRv9jIDxzpqA.png" /></figure><blockquote>Note: I recommend you use a new instance of vagrant for this section to avoid unpredictable outcomes due to changes made in previous article. To do this run vagrant halt &amp;&amp; vagrant destroy &amp;&amp; vagrant up. This will destroy the current VM and create a new instance. You might need to delete `.vagrant` folder before you run the above commands.</blockquote><p>If you don’t get the same output, make sure your configuration is correct and you have registered your private key with ssh-agent.</p><h4>Generate ssh key</h4><p>Now that we have tested that our connection works, it’s time to generate the ssh keys. Since we have done this in the previous section I will not explain how it works but rather convert the process to an Ansible script. Ansible has a module called command which enables you to run bash commands. Since ssh does not have its own module we will need to run the command itself.</p><p>Modify ssh-key-rotation.yml with the code snippet below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EhdF8VNUoUg0Ls-PEGmgUQ.png" /></figure><p>In the above snippet, we have used Ansible command module to run <strong>ssh-keygen</strong> command. We have used variable substitution in the command to make more dynamic. For now, I have passed the variables directly using vars directive but in you might want to abstract these into a different file for security reasons.</p><p>Since we need this command to be executed on our local machine it is important for us to use the <strong>delegate_to</strong> directive to inform Ansible of our intention.</p><p>And its show time !</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PaxpR7Jme8eUWQeu58UQmg.png" /></figure><p>One of the appealing attribute of Ansible is how it beautifully display real-time feedback as the script runs. When its done executing it also provides a summary of any changes that have been introduced by the script. And for us we should have our keys generated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LxC5PL2IXqS_lU-ooWCnEQ.png" /></figure><p>The good thing about Ansible is even if you run this script again, it will not generate new key since we already have one. This makes it easy to have consistent key even if the script is used several times.</p><h4>Add the Generated SSH public key to the authorized_keys file</h4><p>Now that we have the SSH key pair has been generated, we need to add it to the authorized keys file. As compared to the examples above, Ansible has a module called <a href="/service/ttp://docs.ansible.com/ansible/latest/authorized_key_module.html"><strong>authorized_key</strong></a>. This module abstracts all the complexities of changing authorized keys.</p><p>In our main playbook file , let’s add another task. Modify the file with the code snippet below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4nLRClSUevorjKoTq5wwGw.png" /></figure><p>In the code snippet above we have added two tasks. One to store the value of public ssh key. In this task we are using <strong>lookup(‘file, path/to/file) </strong>method to fetch the value of the public key.</p><p>In the next task, we are setting the value to the authorized key file. It’s important to note that this task has to be done as root since we are modifying files only accessed by the root user. The <strong>authorized_key</strong> modules do require more parameters than we have specified for now we can do with the default as long as <strong>exclusive</strong>, <strong>user </strong>and <strong>key</strong> are specified.</p><p>Let’s run the script again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PaxpR7Jme8eUWQeu58UQmg.png" /></figure><p>Awesome, our script works. If you ssh into you vm <strong>vagrant ssh</strong>, and cat the <strong>authorised_keys</strong> file you should see a new key with suffix of <a href="/service/mailto:domain@example.com">domain@example.com</a>.</p><p><strong>Remove old ssh Public Keys</strong></p><p>Now that we have added the key to our VM the next step is to remove the old key. How do we go about this? In the previous example, we had to ssh into the VM and edit the<strong> authorized_keys</strong> file using sed. The beauty of the <strong>authorized_key </strong>module is that we can add the key and remove the old one at the same time.</p><p>If you refer back to our script, you will see we added a variable called <strong>is_exclusive</strong>. This variable enables us to specify if we want our new key to be exclusive or it should be appended to the already existing keys. To remove the old key all we have to do is change the variable to <strong>yes</strong> from <strong>no.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jtOuUjoHat4O1VgjwmeN7Q.png" /></figure><p>Now, let’s test our script again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PaxpR7Jme8eUWQeu58UQmg.png" /></figure><p>If you try accessing the VM via <strong>vagrant ssh</strong> command, you will be prompted to enter a password as compared to the default seamless access. This is due to the fact that the script has replaced the default public key with the new generated ssh key. The only way to access this VM is via the new ssh key or password auth with vagrant as the password.</p><p>Let’s test if our new ssh key works.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iB4bra2oun7Nll4W7q5KSg.png" /></figure><p>Woohoo! it works</p><blockquote>You can find the final version of this code on our Github account. Feel free to tinker with it as much as you want</blockquote><h4>Conclusion</h4><p>In this article, We have looked into how we can simplify rotating SSH keys with different approaches, as you have noticed Ansible makes the process much simpler and easy. What we have done here is a simple example you can check my A<a href="https://github.com/nyambati/ssh-key-rotation">nsible role</a> on A<a href="https://galaxy.ansible.com/nyambati/ssh-key-rotation/">nsible Galaxy</a> with a little more features. What you can achieve with Ansible is limitless.</p><p>It’s time to get your tinker hat and simplify your processes with Ansible. If you have a question or suggestion feel free to leave a comment, happy tinkering.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7c6f599dd086" width="1" height="1" alt=""><hr><p><a href="https://medium.com/rackbrains/automate-ssh-key-rotation-with-ansible-part-2-7c6f599dd086">Automate SSH Key Rotation With Ansible part 2</a> was originally published in <a href="https://medium.com/rackbrains">Masked Weaver</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Automate SSH Key Rotation With Ansible part 1]]></title>
            <link>https://medium.com/rackbrains/automate-ssh-key-rotation-with-ansible-part-1-a99934d2dfc9?source=rss----231a6385b39d---4</link>
            <guid isPermaLink="false">https://medium.com/p/a99934d2dfc9</guid>
            <category><![CDATA[ssh]]></category>
            <category><![CDATA[ansible]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Thomas Nyambati]]></dc:creator>
            <pubDate>Thu, 06 Sep 2018 21:01:01 GMT</pubDate>
            <atom:updated>2018-09-10T23:32:15.209Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eydlhY_DSkghXKKuDfZB7g.jpeg" /></figure><blockquote>This article covers a brief introduction on ssh, ssh keys and the manual process of rotating ssh keys. It serves as a pre-requisite for the second part of this article. If you want to jump straight to implementation with ansible and bash scripts visit the second part of this article.</blockquote><p><a href="https://medium.com/rackbrains/automate-ssh-key-rotation-with-ansible-part-2-7c6f599dd086">Automate SSH Key Rotation With Ansible part 2</a></p><p>If you have worked with virtual machines, you’ve probably heard of the term “SSH”. So what does it actually mean? SSH — Secure Shell is a network protocol that provides system administrators a secure way of accessing remote servers. It provides strong authentication and encrypts communication between computers connecting over insecure networks such as the internet.</p><p>System administrators rely on this protocol to manage systems and applications remotely. Through SSH, you can log into another computer over the network, without being in the actual computer location. This means that as a system admin, you are not limited to doing maintenance at the actual server location and, you can manage your servers from any location as long as you have a connection to the network.</p><p><strong>How does SSH work?</strong></p><p>The SSH protocol uses the client-server model to authenticate servers and encrypt the data shared between them. For a connection to be established, the remote machine must be running a SSH daemon which listens for connections on a specific network port, authenticates connection requests and spawns the appropriate environment when provided with correct credentials.</p><p>The SSH clients can authenticate using passwords or SSH Keys. As much as passwords are easier to use, they can be easily compromised by bots trying gain access to remote servers. For this reason, it is recommended to use SSH key-based authentication at all times.</p><p>SSH Keys are a set of matching cryptographic keys used for authentication. They entail both private and public keys. Public keys can be shared freely while private keys must be vigilantly guarded and never exposed.<br>To successfully connect to a remote server, a user must have the private key on the local computer and the public key on a remote computer. Public keys are stored in the ~/ssh/authorized_keys. This file contains all the public keys that are authorized to log into the server.<br> <br><strong>Why does rotating ssh keys matter?</strong></p><p>If you are working with a large team, it’s reasonable to have each user have a key they can use to access the servers. With team members working on different projects, comes the possibility of constant changes in personnel and access privileges. This constant change exposes your infrastructure to vulnerabilities such as compromised keys from current users, off boarded ones or idle active keys that can be used to exploit the system.</p><p>To minimize such risks, it is recommended to always rotate the SSH keys based on a defined cycle. The cycle is usually arbitrary based on how your organisation handles security and audit policies. During rotation, new keys are generated and switched with the old ones, inactive keys are also removed. This constant change of keys decreases any chances of compromised keys being used to get access to the remote servers.<br>It is important to note that as much as rotation of ssh keys reduces the chances of your system being compromised, it is important to put in place security policies on how ssh keys will be shared and secured while in possession of the users.</p><h4>Rotating ssh keys</h4><p>A SSH key rotation process involves three simple steps,</p><ul><li>Create a new ssh key</li><li>Add the public key to an authorised keys file</li><li>Test the new keys and replace the old ones</li></ul><p>In this article, we shall be looking at how to implement these steps. We’ll also go a step further and automate this process with Ansible and bash.</p><p><strong>Pre-requisites</strong></p><p>It is my assumption that you are familiar with the Linux command line and Ansible as well. If you have not done either of the above I will recommend you brush up before proceeding. I also recommend reading the official Ansible documentation and this <a href="https://scotch.io/tutorials/getting-started-with-ansible">Getting started with Ansible</a> article on <strong>scotch.io</strong></p><p><a href="https://scotch.io/tutorials/getting-started-with-ansible">Getting started with Ansible</a></p><p>In this article, we shall be using the following technologies</p><ul><li><a href="https://www.vagrantup.com/">Vagrant</a></li><li><a href="https://www.ansible.com/">Ansible</a></li><li><a href="https://www.virtualbox.org/">Virtualbox</a></li></ul><p>To install the above tools on MacOs, run the commands listed below, for other operating systems, refer installation instruction from the respective websites.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nk1bzkv5GhkFKO08IX47-w.png" /></figure><p><strong>Setting up vagrant</strong></p><p>The instructions in this tutorial can be used on any hosted server, on the cloud or on a data center. On for this tutorial, we’ll be using Vagrant machines to simulate remote servers since it’s simple to work with and free. To confirm your vagrant installation was successful, run vagrant version.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-azmpVgb5q5lLGdEC29Kww.png" /></figure><p>Now that we have our vagrant installation working, we can go ahead and set up our working directory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qyJgX8CK6Uyl6YUpc7SwoQ.png" /></figure><p>This command will create a ssh-key-rotation directory and initialize vagrant, by creating a Vagrantfile. Vagrant uses this file to provision and configure its virtual machines. The initial vagrant file comes with defaults, modify the file with the code snippet below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ctlLdg99Xx9Jlf2ykFZh5g.png" /></figure><p>This configuration defines a private network accessible on IP address <strong>192.168.33.12</strong> and the hostname as octo. Boot the VM by running vagrant up.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YxBBMU4rJudnY-xB3YFkgQ.png" /></figure><blockquote>Note: If this is your first time using Vagrant and Ubuntu/trusty64 box, it might take some time downloading the box depending on your internet connection. This might the best time to get a cup of coffee.</blockquote><p>Once the command is done executing, your VMs should be ready to go. You can check the status by running vagrant status.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ajbMxnW3sZYLd8ijZVtnwg.png" /></figure><p>Now that our VM is up and running, we can ssh into it using vagrant ssh &lt;hostname&gt;. in our case octo.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IPPF4Byx36MnlYOh8FkCOA.png" /></figure><p><strong>Generating SSH Keys</strong></p><p>When vagrant boots up VMs by default it generates ssh keys for each each one of them. These generated keys enable us to ssh into the VM without use of a password . The first step of rotating keys, is generating new keys that would replace the old ones. We can generate new keys through ssh-keygen, the command below demonstrates.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7NoOzb0jRLKMtfHjNT68dg.png" /></figure><p>This command will generate a new key named octo-ssh-key and octo-ssh-key.pub. Move these two files into the ssh folder to keep things tidy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qyFiP14n6HyvC1zemQl4Eg.png" /></figure><p>If this is your first time using the ssh-keygen command, you might be wondering what the options passed to the command actually do. Let’s jump right into it, shall we?</p><ul><li><strong>-t </strong>Specifies the type of key to generate, this can be <strong>DSA</strong> or <strong>RSA</strong></li><li><strong>-b</strong> Specifies the type of bit encryption to use, I recommend using <strong>4096</strong> as the minimum.</li><li><strong>-C</strong> This option allows us to add a comment to accompany out a public key for easy identification.</li><li><strong>-N</strong> Specifies the passphrase to encrypt the keys with.</li><li><strong>-f</strong> Specifies the directory or path the keys will be stored.</li></ul><p>You can learn more about the options that can be passed to this command on the Linux Man Page at <a href="https://linux.die.net/man/1/ssh-keygen">die.net</a>.</p><p><strong>Add the Public key to the authorized keys file.</strong></p><p>After generating key pair, the next step is to add the public key to our Virtual Machine. One of the obvious options would be to ssh into the Machine and edit the authorized keys file. As much as that would work, it wouldn&#39;t be very scalable when working with alot of VMs</p><p>Lucky there is a there is a command that we can use to copy our public key to the host machine. <a href="https://linux.die.net/man/1/ssh-copy-id"><strong><em>ssh-copy-id</em></strong></a> logs into the remote server and modifies the remote users’ <strong>.ssh</strong> folder including the <strong><em>authorized_keys</em></strong> file. When the <strong><em>-i</em></strong> flag is specified, it will copy the public key and append it into the <strong><em>authorized_keys</em></strong> file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-4jdseiQR3svtCBfalWn5A.png" /></figure><p>When this command is done executing, ssh into the VM, and open the <strong><em>authorized_keys</em></strong> file in the .ssh folder. You should be able to see the public key we just generated appended to it. Excellent.</p><p><strong>Test the new key and replace the old ones</strong></p><p>Having added the public key to our server, let’s test it and ensure that we can use it to connect to the server. First thing first, let’s add our private key to the ssh-agent.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6Lg2CF57yyTorKHhG1pN8g.png" /></figure><p>When prompted with passphrase type the passkey we used to generate the key. The passphrase we used is `<strong>the-pass</strong>` which is very weak, insecure and therefore should not be used in a production setting. Since this is a demo, we can get away with it. At all times ensure you are using a strong and secure passphrase to generate your keys.</p><p>Now that we have added the private key to the ssh-agent let’s attempt to ssh into the machine, run ssh user@[hostname/ip] in our case, ssh vagrant@192.168.33.12.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XoyrlYu3oO-S77kFQuCXuQ.png" /></figure><p>Woohoo! yeah, it worked.</p><p>We have successfully generated new ssh keys, added the newly generated public key to our VM and also confirmed that we can actually access the VM using the new ssh key. At this point the old key is still in use and its about time to remove it from the system.</p><p>We could ssh into the VM and edit the authorized key file, but we ascertained that it’s not a scalable option. <a href="https://linux.die.net/man/1/sed">sed</a> to the rescue! If you have used sed before you can attest that it’s a powerful editor. Below is a template of how we can use sed to remove the old key from the system.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6LBaXFq9qLc78O4sz5fyRg.png" /></figure><p>For this command to work, it requires a unique identifier to match our key. Remember when creating the ssh keys we specified a comment, using this comment we can uniquely pick the key we want to remove from our server. All vagrant keys have a comment of vagrant, thus qualifying as our unique identifier. We can now substitute the command with actual values and it will be as below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*32NJQARjhXrW24yn2j8FYw.png" /></figure><p>If you are wondering what <strong>.bak</strong> means well when we use .bak sed will create a backup for our keys, if you don’t need them you can do away with it later. Let’s stick with it for now. To run this command against our VM, combine our commands as below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wp9TPejmRH9llPpjgB0SGQ.png" /></figure><p>When it’s done ssh into your VM and check the authorized_keys file. You should notice that the old public key has been removed. Awesome right?</p><p>If you try to ssh into the VM via vagrant ssh octo, it will prompt you for the password since it could not find the default public key on the server.</p><h4>Conclusion</h4><p>So far we have talked about SSH , SSH keys, SSH keys rotation and explored the steps to take when rotating keys. However, its important to note that the steps we have taken can also be used when issuing new keys to new members and removing old unused keys, application depends on you.</p><p>In <a href="https://medium.com/rackbrains/automate-ssh-key-rotation-with-ansible-part-2-7c6f599dd086">part 2</a> of this article we shall be taking a more hands-on approach where we’ll automate these steps using Ansible and bash scripts. See you then.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a99934d2dfc9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/rackbrains/automate-ssh-key-rotation-with-ansible-part-1-a99934d2dfc9">Automate SSH Key Rotation With Ansible part 1</a> was originally published in <a href="https://medium.com/rackbrains">Masked Weaver</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Manage Heroku Infrastructure with Terraform]]></title>
            <link>https://medium.com/rackbrains/manage-heroku-infrastructure-with-terraform-4a167b850300?source=rss----231a6385b39d---4</link>
            <guid isPermaLink="false">https://medium.com/p/4a167b850300</guid>
            <category><![CDATA[heroku]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Thomas Nyambati]]></dc:creator>
            <pubDate>Mon, 03 Sep 2018 21:01:01 GMT</pubDate>
            <atom:updated>2018-12-11T05:16:34.874Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kHC9Ob1xa6RQuhceigrPbA.png" /></figure><p>Heroku is the defacto Platform as a Service for many Developers. This popularity is based on the simplicity of provisioning and fast deployment cycles. As much as the Heroku team have done an admirable job of abstracting most of the complexities, managing the Heroku apps can be a little tedious as all the provisioning has to be done via the dashboard. When managing many apps, this approach might be slow and time-consuming.</p><p>With the advent of agile, the way we build and ship apps and systems has changed. System components can be built and shipped faster than before, but this has made managing and testing all the builds manually a nightmare. To be at par with the ever-changing industry, several methodologies have been adopted, to make the process more subtle, simple and efficient — IaC</p><h4><strong>Infrastructure as Code</strong></h4><p>According to Wikipedia, Infrastructure as Code (IAC) is a type of IT infrastructure that operations teams can automatically manage and provision programmatically, rather than using a manual process.</p><p>The concept of IaC is similar to programming scripts, which are used to automate important IT processes. However, scripts are primarily used to automate a series of static steps that must be repeated numerous times across multiple servers. Instead of scripts, IAC uses higher-level or descriptive language to code more versatile and adaptive provisioning and deployment processes.</p><p>Based on our use case Heroku, we can describe how our infrastructure should look like in code. This gives us the flexibility of updating, modifying or destroying it with few commands and time.<br>Below are some of the tools used in IaC.</p><ul><li>Ansible</li><li>Terraform</li><li>Chef</li><li>Puppet</li></ul><p>In this article we will be focusing on Heroku and Terraform.</p><h4>Terraform</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y0HZCbscmEJoWtG9LMaueA.png" /></figure><p>Terraform is an open source tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned. Terraform enables you to safely and predictably create, change, and improve your infrastructure.</p><p>Apart from the simplicity of Terraform, this tool can be used with different providers making it the best choice when working with different service. It also embraces the separation of concern (modularity), which makes it easy to break down the infrastructure into small components that can be re-used and easily maintained. You can find more details on Terraform on the official website.</p><h4>Heroku and Terraform</h4><p>Terraform works seamlessly with Heroku API. It simplifies how we interact with Heroku, making provisioning of Heroku infrastructure as simple as running terraform apply. Through its declarative language HCL, we can express the resources we need and terraform will take care of creating them for us.</p><h4>What we will be building</h4><p>In this article, we will be building a simple Heroku infrastructure as a demonstration. Below are the services we will be provisioning.</p><ul><li>Pipeline</li><li>3 apps (develop, staging, production)</li><li>3 databases(add-ons)</li></ul><h4>Pre-requisites and Installation</h4><p>Before we proceed, ensure you have a Heroku account and terraform binaries installed on your machine. Terraform binaries can be downloaded from the website. There is support for MacOs, Linux, FreeBSD, OpenBSD Solaris, and windows. For MacOs users you can install terraform via brew package manager.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YU0wRPUk-U61KP-BRHHA9w.png" /></figure><p>We would be using Heroku throughout the article, therefore if you don’t have an account ensure you signup before we proceed.</p><h4>Setting up the project</h4><p>At this point, you should have a Heroku account set up, and terraform binaries installed on your machine. To confirm that terraform has been correctly installed, run terraform version from the command line. You should get a response similar to the one below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LHny8TMVgMUKVn_ZfhoH7A.png" /></figure><p>The version may be different from mine depending on what version you have installed, nevertheless, the functionality will still be the same. Run the command below to create a working directory and initial terraform files.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ur0-1mS7o3tUaR-2KW-pCw.png" /></figure><p>Terraform files have an extension of <strong>.tf</strong> with and exception of <strong>.tfvars</strong> files that hold variable values. You can refer <strong>.tfvars</strong>file as <strong>.env</strong> for terraform and therefore should not be versioned at all unless they do not contain sensitive credentials that might compromise your infrastructure.</p><p>After running the commands your working directory should look as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6VEZ0w9npEyGZJFD0si9GA.png" /></figure><blockquote>Full code for this article can be found on our <a href="https://github.com/rackbrainz/manage-heroku-infrastructure-with-terraform">GitHub repository</a>. Feel free to tinker with it as you see fit.</blockquote><p><a href="https://github.com/rackbrainz/manage-heroku-infrastructure-with-terraform">rackbrainz/manage-heroku-infrastructure-with-terraform</a></p><h4>Setting up Heroku Provider</h4><p>Terraform is agnostic to the underlying platforms by supporting providers. A provider is responsible for communication with the API and exposing resources. Terraform supports a lot of providers which can be found in the <a href="https://www.terraform.io/docs/providers/">providers’ section</a> of the documentation.</p><p>In our case, we will be using the <strong>heroku</strong> provider which will be responsible for communicating our intent to through the Heroku API. Heroku provider docs can be found at the <a href="/service/ttps://www.terraform.io/docs/providers/heroku/index.html">Heroku section</a> under providers.</p><p>The snippet below shows the syntax of creating a Heroku provider, and it to the <strong>main.tf</strong> file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2ZsjVzSInHq2hEExr1tdsA.png" /></figure><p>Providers are defined using the <strong>provider</strong>keyword followed by the name of the provider in this case <strong>heroku</strong>. The Heroku provider requires two arguments <strong>email</strong> and <strong>api_key</strong> in order to communicate with your Heroku account.</p><p>We have used variable interpolation to dynamically pass this values to the provider. This not only makes our scripts dynamic it enables us to not expose sensitive values to the world. All variables used must be declared prior to being used. For more information refer to terraform <a href="https://www.terraform.io/docs/configuration/variables.html">variables </a>documentation</p><p>The snippet below shows variable declaration syntax.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Em5RcgoSMQ6qdZmtgQc0Pg.png" /></figure><p>All the above arguments are optional, however, when the variable type is not specified, terraform will assume string type by default.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZtiuESqeB3OTAGLVU-Euzw.png" /></figure><p>Before you can use terraform you need to run <strong>terraform init</strong>. This command will look at our configuration and download and install all the plugins required by Terraform.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T1hRbVVccTzS41WsQAj05w.png" /></figure><h4>Create Heroku Pipeline</h4><p>Heroku pipelines allow us to group our application environments. This means you can have your dev, staging and production apps grouped in the same place. This setup provides a much easier way to manage your applications, especially if you have many applications in one account.</p><p>Traditionally you will have to navigate through the Heroku dashboard to create a pipeline, which might take 2–3 minutes. But with terraform, it gets easier.</p><p>All terraform resources assume the following syntax.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*96It0x2YIc3waHusSlK6hw.png" /></figure><p>Add the code snippet below to <strong>pipeline.tf</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Snk9bHOomyKmXiSP5Dpl6g.png" /></figure><p>Update the <strong>variables.tf</strong> file with the newly added pipeline variable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yxGo4FbH4YBeO2YKGo169Q.png" /></figure><p>The code above instructs terraform to create a Heroku pipeline identified as a pipeline. The identifier here is arbitrary, you can name it anything you want. This resource takes one argument<strong> </strong><strong>name</strong> which is used to identify the pipeline created on Heroku.</p><blockquote>Note: All variables declaration will be done in <strong>variables.tf</strong> file. This makes it easier for us to track and change the variables when the project scales.</blockquote><h4>Create Heroku Apps</h4><p>Now that we have our pipeline setup, let&#39;s create apps that will be attached to this pipeline. Terraform creates Heroku apps via <strong><em>heroku_app</em></strong> resource. This resource takes only one required argument <strong><em>name</em></strong> , however, you can pass other arguments to customize your app further if required.</p><blockquote>Note: All terraform resource take arguments, these arguments can be found under Argument Reference on each resource documentation.</blockquote><p>Add the code snippet below to your <strong><em>apps.tf</em></strong> file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yP-pVuAszGAfAynGAdes5Q.png" /></figure><p>Update the variables file with the newly added variables</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pZ8ZstvXxEqQphN5gO-dXA.png" /></figure><p>Let&#39;s talk about what the code we have written above does. In <strong><em>apps.tf</em></strong> we have declared three apps resources identified as <strong><em>heroku_develop_app</em></strong>, <strong><em>heroku_staging_app</em></strong>, <strong><em>heroku_production_app</em></strong>. Terraform will create three Heroku apps when this code block is executed.</p><blockquote>Note: Its Important at all times to give your resources unique names to avoid conflicts and also make it easier for terraform to track your infrastructure changes.</blockquote><h3>Attach Database Add-ons to the apps</h3><p>Having setup up our apps, we need to create database add-ons that our deployed apps will use to persist data. To create a Heroku add-ons via Terraform we will use the <strong><em>heroku_addo</em></strong>n resource. This resource takes three arguments app, <strong>plan</strong> and <strong>config</strong> . More information can be found on the Argument Reference section in the documentation.</p><p>Add-ons syntax</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I1JeiyYDZFLons6FJGzZsg.png" /></figure><p>In our case, we are adding a Postgres database basic plan <strong><em>heroku-postgresql:hobby-basic</em></strong>. Add-ons plans can be found on each Heroku addon documentation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ODuXoKmNF7FHd0PjhDfoEg.png" /></figure><p>Update the variables file with the snippet below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GAihusJwdNJH1GtF2XGeCQ.png" /></figure><p>The code above creates three database add-ons and attaches them to the three apps we created earlier.</p><h4>Attach Heroku Apps to Heroku Pipeline</h4><p>Now that we have our apps, the only thing left is to attach them to the pipeline we created earlier. This is made possible by the <strong><em>heroku_pipeline_coupling</em></strong> resource. Once the apps have been created, they can be added to different stages in the pipeline.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zpbBfcIZ6DSnSH4DXmEjug.png" /></figure><p>One thing you will notice that the app and pipeline arguments have been interpolated by variables we have not defined. This is because these values are outputs of other resources. To reference the value of another resource, you have to follow the following syntax.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EJh0q651UGnBjJDbF35jeA.png" /></figure><p>Using develop example, we have referenced develop app name as <strong>heroku_app.develop.name</strong>, <strong><em>heroku_app</em></strong> being resource name, <strong><em>develop</em></strong> the unique identifier we gave the app earlier and the <strong><em>name</em></strong> which is the output value.</p><p>Resource outputs can be found under Attributes Reference in each resource documentation.</p><h4>Plan (Test) Changes</h4><p>So far we have been writing code but we have not even tested if they work or not. Through terraform plan we can dry run our scripts against the provider and check if your scripts are okay. Lets to that</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mc6GrUwoFoYMe8bxD8iGSA.png" /></figure><p>Once you run this command, you will get this prompt;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0UE2Ap-CkvJBcAL9mCkYrg.png" /></figure><p>At this point terraform is looking at your variables but it’s not aware where you get the values from. You can input these values one by one, however, this will not be a scalable solution. When your project grows it would be next to impossible to memorize all the values. Instead, we can add the values in a terraform.tfvars file. This will be a go-to file for terraform to look for answers (variables values).</p><p>Let’s create the file and add some values. Exit your current command and run the command below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*weB9B9F_Is5iZYUBp-xXcg.png" /></figure><p>In this file, add the following values. Modify the values below where necessary especially the email and API key.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fShhdqCpVCCHySqm7VyVLA.png" /></figure><p>Once you have this setup, run terraform plan again. If you configured everything correctly you should see something similar to the output below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4YXdRdySpErbpJfNMkqAUg.png" /></figure><p>When we run terraform plan, terraform will dry run your configuration against the provider API and verify whether what you have specified is possible. If what we declared is not possible you might get errors, which might range from the wrong resource to unique names such as Heroku apps.</p><p>Terraform plan also notifies us of how many resources will be created, updated or destroyed.</p><h4>Applying Changes</h4><p>Once we are content with the changes, we can run terraform apply. This command will run the configuration against the provider and provision the resources for us.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wE8_7fTdve5xZm8yteDgQg.png" /></figure><p>When terraform is applying your changes, it will report in real-time the progress of each resource declared. And once complete it also provides a report of resources created, changed, or destroyed.</p><p>Once the command has completed executing, visit your heroku dashboard, there should be a pipeline with three apps attached to it.</p><p><strong><em>The pipeline.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d6UN3YGCcxAie_vqPCFz3Q.png" /></figure><p><strong><em>The pipeline with 3 apps.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sHxdOH6yP614vnT0Cs7qPQ.png" /></figure><p>As you have noticed it took very little time from running the command to having your infrastructure up and running. As compared to the manual process. Since this scripts can versioned, we can quickly spin out the same infrastructure anytime and get the same results.</p><h4>Terraform Outputs</h4><p>Terraform outputs enables us to get specific values from resources that we might want to use later. This is important when you are working complex infrastructure and you need to share values or you are working with modules. Terraform outputs assume this syntax.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mIIGvdRA7GQdyWFQfzW6uw.png" /></figure><p>In our setup above we might need to get all the git URLs of our apps. This can be done as below. Place the code below in <strong><em>outputs.tf </em></strong>file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p6IwOYhXSKkMSDR-JQ52tg.png" /></figure><p>Since we had applied our changes, we will need to refresh terraform state in order for our outputs to take effect. When you run terraform refresh, terraform will read this file and produce its outputs for you. This outputs can also be displayed at the end of a terraform apply or accessed via terraform output command.</p><p>After refreshing terraform state you should see your outputs as below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JdGCsa-P0Grkyha_5LasuQ.png" /></figure><h4>Destroy Applied changes</h4><p>In one way or the other, you might want to delete your infrastructure. Terraform provides you will a command terraform destroy. This command will nuke all the resources that were created by terraform apply command</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GOHWpRkiH1A8OjuV-p4tMw.png" /></figure><blockquote>Note: changes made by this command are irreversible and therefore should be done when 100% sure that what you want.</blockquote><h4>Conclusion</h4><p>Automation is one of the key principles that DevOps engineers and Developers alike should learn to adopt it in their daily workflow. This approach helps to reduce the time spent on minor tasks and focus on improving and building products. It also introduces flexibility in that you can change and modify your infrastructure much easier as compared going the manual way.</p><p>Terraform works well with a lot of platforms and services, making it a perfect tool to use to manage your infrastructure. It can be used across most common services and cloud platforms.</p><p>If you would like to have further discussion on how to use terraform feel free to reach out to us on <a href="https://twitter.com/rackbrainz">twitter</a>, f<a href="https://www.facebook.com/rackbrainz">acebook</a> or via email.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4a167b850300" width="1" height="1" alt=""><hr><p><a href="https://medium.com/rackbrains/manage-heroku-infrastructure-with-terraform-4a167b850300">Manage Heroku Infrastructure with Terraform</a> was originally published in <a href="https://medium.com/rackbrains">Masked Weaver</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>