<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Cameron Gray</title>
    <description>The latest articles on DEV Community by Cameron Gray (@camerondgray).</description>
    <link>https://dev.to/camerondgray</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F401232%2Ffa415c4d-0ed1-4b22-a047-6e8f88c7ddc6.jpeg</url>
      <title>DEV Community: Cameron Gray</title>
      <link>https://dev.to/camerondgray</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/camerondgray"/>
    <language>en</language>
    <item>
      <title>Configuring Logging on Kubernetes</title>
      <dc:creator>Cameron Gray</dc:creator>
      <pubDate>Fri, 14 Aug 2020 14:26:47 +0000</pubDate>
      <link>https://dev.to/camerondgray/configuring-logging-on-kubernetes-2n87</link>
      <guid>https://dev.to/camerondgray/configuring-logging-on-kubernetes-2n87</guid>
      <description>&lt;p&gt;In my previous posts I gave &lt;a href="https://dev.to/camerondgray/monitoring-applications-on-kubernetes-an-overview-n91"&gt;an overview of Kubernetes monitoring&lt;/a&gt; as well as a deeper dive into &lt;a href="https://dev.to/camerondgray/configuring-apm-on-kubernetes-93f"&gt;application monitoring&lt;/a&gt;. In this post I am going to dig into the final piece of the monitoring puzzle which is log consolidation. To get a complete picture it's important to consolidate the logs from your applications and infrastructure into a single stream so you can get a detailed view, in real time if necessary, of everything that is happening in your cluster.&lt;/p&gt;

&lt;h2&gt;
  
  
  Consolidating Your logs
&lt;/h2&gt;

&lt;p&gt;At &lt;a href="https://convox.com"&gt;Convox&lt;/a&gt; we allow you to easily tail and search your application logs with the &lt;code&gt;convox logs&lt;/code&gt; CLI command, and we provide the same for your infrastructure with &lt;code&gt;convox rack logs&lt;/code&gt;.  While this works great for many situations we also recognize that sometimes you need a more robust solution. Seeing all your logs in one place can definitely help you gain a better understanding of what's happening in your environment. In addition, if you are using a third party service such as Datadog or New Relic, having your logs collected in the same platform can allow you to correlate changes in application performance or uptime to specific events in your logs. To this end, having a complete cluster-level logging solution can be extremely helpful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Kubernetes Logging Overview
&lt;/h2&gt;

&lt;p&gt;By default the logs from your containers will be written to &lt;code&gt;stdout&lt;/code&gt; and &lt;code&gt;stderr&lt;/code&gt; and Kubernetes will write these logs to the local file system on the Pods running your containers. You can retrieve the logs from a given Pod with the &lt;a href="https://kubectl.docs.kubernetes.io/pages/container_debugging/container_logs.html"&gt;&lt;code&gt;kubectl logs&lt;/code&gt;&lt;/a&gt;  command. While this can be useful for troubleshooting the initial configuration of your cluster or troubleshooting a new app, it doesn't provide the cluster-wide view you often need to manage apps in production. In addition, if a Pod or Node fails or is replaced the logs are lost.&lt;/p&gt;

&lt;p&gt;While Kubernetes does not provide a built-in method for cluster-level logging, the &lt;a href="https://kubernetes.io/docs/concepts/cluster-administration/logging/"&gt;official documentation&lt;/a&gt; does provide a few options for systems you can set up yourself:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://kubernetes.io/docs/concepts/cluster-administration/logging/#using-a-sidecar-container-with-the-logging-agent"&gt;run a dedicated sidecar container in your application pod&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://kubernetes.io/docs/concepts/cluster-administration/logging/#using-a-node-logging-agent"&gt;run a logging agent on every node&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://kubernetes.io/docs/concepts/cluster-administration/logging/#exposing-logs-directly-from-the-application"&gt;push logs directly from your application to a custom backend application or third party service&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Sidecar Logging
&lt;/h3&gt;

&lt;p&gt;We find that the sidecar approach is a bit cumbersome and generally difficult to set up and maintain, so I wont go into the details too much on this option. One benefit of the sidecar approach that is worth mentioning is because the sidecar container runs within each application's Pod, if you have multiple apps with logs in different formats you can use this per app sidecar to either reformat the logs to a common format or send different types of logs to different locations. You can even use the sidecar in conjunction with the logging agent approach if you want to pre-process your logs before sending them to a Node level agent. This option is definitely the most complex but also the most flexible.&lt;/p&gt;

&lt;h3&gt;
  
  
  Per Node Logging Agents
&lt;/h3&gt;

&lt;p&gt;Running a logging agent on every node is a great starting point. Once again we find using a &lt;a href="https://kubernetes.io/docs/concepts/workloads/controllers/daemonset/"&gt;DaemonSet&lt;/a&gt; is the right approach for this. At Convox we even offer this as a built-in solution using a Fluentd DaemonSet installed by default with every Rack. Fluentd provides three major functions. First it can gather all your logs from stdout or from an application specific stream. Secondly, it can store those logs in a cloud specific storage endpoint such as Elasticsearch or Stackdriver. Finally, it can stream those logs to an outside collector such as a syslog endpoint or even an S3 bucket. At Convox we also offer the option of enabling a &lt;a href="https://docs.convox.com/installation/production-rack/aws#install-rack"&gt;syslog forwarder&lt;/a&gt; on your cluster so you can easily send all these logs to a 3rd party provider like Papertrail. If you are not running on Convox and you want to deploy a Fluentd DaemonSet you can consult their official documentation &lt;a href="https://docs.fluentd.org/container-deployment/kubernetes"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using a 3rd Party Service
&lt;/h3&gt;

&lt;p&gt;One of the shortcomings of solely using the logging agent approach is that it will only collect application logs from &lt;code&gt;stdout&lt;/code&gt; and &lt;code&gt;stderr&lt;/code&gt;. While this is fine for most applications, sometimes you will want to push logs directly from your application to a third party service. While there are some specific use cases for this we have found most of the third party services you would use for this also provide a DaemonSet based logging agent which you can deploy to serve as both a per Node logging agent and a &lt;a href="https://docs.datadoghq.com/agent/logs/?tab=tailfiles#custom-log-collection"&gt;custom logging endpoint&lt;/a&gt;. With this approach, much like we previously outlined with &lt;a href="https://convox.com/blog/k8s-monitoring-APM"&gt;APM&lt;/a&gt;, you can push your custom application logs directly to the Agent running on the local Node and gain the benefits of tying Node and Application logs together.&lt;/p&gt;

&lt;p&gt;If you are using a service such as &lt;a href="https://docs.datadoghq.com/agent/kubernetes/log/?tab=daemonset"&gt;Datadog&lt;/a&gt; or &lt;a href="https://docs.logdna.com/docs/logdna-agent-kubernetes"&gt;LogDNA&lt;/a&gt; their logging agents will collect all the Node and Application logs from &lt;code&gt;stdout&lt;/code&gt; and &lt;code&gt;stderr&lt;/code&gt;. Also, in the case of Datadog if you are already using an Agent for infrastructure monitoring and/or APM you can enable logging with a simple &lt;a href="https://docs.datadoghq.com/agent/logs/?tab=tailfiles#activate-log-collection"&gt;configuration option&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Hopefully, this has given you a good overview of the various options for collecting logs for both your Kubernetes cluster and the applications you are hosting on it. As always if you don't want to worry about all this stuff you can just use &lt;a href="https://docs.convox.com/getting-started/introduction"&gt;Convox&lt;/a&gt; and we will take care of it for you!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Configuring APM on Kubernetes</title>
      <dc:creator>Cameron Gray</dc:creator>
      <pubDate>Wed, 12 Aug 2020 16:59:17 +0000</pubDate>
      <link>https://dev.to/camerondgray/configuring-apm-on-kubernetes-93f</link>
      <guid>https://dev.to/camerondgray/configuring-apm-on-kubernetes-93f</guid>
      <description>&lt;h1&gt;
  
  
  Monitoring your apps
&lt;/h1&gt;

&lt;p&gt;In our &lt;a href="https://dev.to/camerondgray/monitoring-applications-on-kubernetes-an-overview-n91"&gt;previous post&lt;/a&gt; we gave you a general overview of infrastructure and application monitoring for Kubernetes. In this follow up post we are going to take a deeper dive specifically into application monitoring. &lt;/p&gt;

&lt;p&gt;The term used by most of the industry for application monitoring is APM which is an acronym for either Application Performance Monitoring or Application Performance Management depending on who you ask. Whether the terms are actually synonymous or not is the source of &lt;a href="https://www.appdynamics.com/blog/product/monitoring-versus-management/"&gt;much debate&lt;/a&gt; but I am not going to get into that here. Many vendors offer "APM" solutions and the feature sets of these offerings do have some variety but also a great deal of overlap. At a high level, an APM tool instruments your apps and allows you to dig into actual code performance, stack traces, crashes, etc...&lt;/p&gt;

&lt;p&gt;Digging into the specifics, &lt;a href="https://www.gartner.com/en/documents/3983892/magic-quadrant-for-application-performance-monitoring"&gt;Gartner&lt;/a&gt; identifies the following core components of APM:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Front-end monitoring (page load times, browser render time, etc...)&lt;/li&gt;
&lt;li&gt;Application discovery, tracing and diagnostics (tracing your running applications through your code all the way down to your underlying infrastructure)&lt;/li&gt;
&lt;li&gt;Analytics (providing overview and analysis of the collected data)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some of the major APM vendors you may have heard of include&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://newrelic.com/products/application-monitoring"&gt;New Relic&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.datadoghq.com/dg/apm/benefits-os/"&gt;Datadog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.appdynamics.com/"&gt;AppDynamics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.dynatrace.com/platform/application-performance-monitoring/"&gt;Dynatrace&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Odds are if you have an application in production you are using one or more of these services already.  As you make the move to Kubernetes it's important to understand the core components of installing and configuring an APM solution. There are typically three pieces to implementing any APM solution for Kubernetes&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Installing and configuring a collection and reporting agent on every Node running in your cluster&lt;/li&gt;
&lt;li&gt;Instrumenting your applications with a tracing library or module to collect and report trace data&lt;/li&gt;
&lt;li&gt;Configuring your application trace component to communicate with the collection agent running on the local Node&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once you get this setup, all of your nodes should have an APM agent running on them. Your application code will then be collecting trace data and sending it to the agent running on it's node which will allow you to tie application performance to the underlying host performance. Finally your agents will be streaming all the collected data back to a central ingress endpoint for your APM provider so it can be aggregated and analyzed. Once you have all of this in place you can create dashboards, reports, alerts, etc...&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing a Collection Agent on Every Node
&lt;/h2&gt;

&lt;p&gt;We covered this in some detail in our &lt;a href="https://convox.com/blog/k8s-monitoring-overview"&gt;previous post&lt;/a&gt; but the short story is to use a Kubernetes DaemonSet to set this up. You can read our detailed instructions of how to do this with DataDog &lt;a href="https://docs.convox.com/integrations/monitoring/datadog"&gt;here&lt;/a&gt;. For most providers these collection agents perform many different monitoring tasks and you will need to set a &lt;a href="https://docs.datadoghq.com/agent/kubernetes/apm/?tab=daemonset"&gt;configuration option&lt;/a&gt; to enable APM trace collection. Here are a few links to the DaemonSet configuration instructions for some of the other providers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.newrelic.com/docs/integrations/kubernetes-integration/installation/kubernetes-integration-install-configure#customized-manifest"&gt;New Relic&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.appdynamics.com/display/PRO45/Monitoring+Kubernetes+with+the+Cluster+Agent"&gt;AppDynamics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.dynatrace.com/support/help/technology-support/cloud-platforms/kubernetes/deploy-oneagent-k8/"&gt;Dynatrace&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Instrumenting Your App
&lt;/h2&gt;

&lt;p&gt;Once you have your collection agents up and running the next step is to start collecting data from your running code. Most of the APM providers have libraries or modules for all the major languages and frameworks.&lt;/p&gt;

&lt;p&gt;Let's look at the simple example of a Node.js application that we want to monitor using Datadog's APM offering. First we will need to install the Datadog tracer module and save it in a NPM requirements.txt.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;install&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;save&lt;/span&gt; &lt;span class="nx"&gt;dd&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;trace&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Next we will need to add the tracer to our code and initialize it. Typically we will want to do this as part of starting up our app server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;host&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;INSTANCE_IP&lt;/span&gt; &lt;span class="c1"&gt;//we will get to this&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;trace&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dd-trace&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;hostname&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;host&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;8126&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For each provider there are several configuration options for the tracer libraries so I strongly recommend you consult the documentation for the particular provider and language/framework you are using. Here a few links to the various provider's APM libraries:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.newrelic.com/docs/agents"&gt;New Relic&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.datadoghq.com/tracing/compatibility_requirements/"&gt;Datadog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.appdynamics.com/display/PRO45/Application+Monitoring"&gt;AppDynamics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.dynatrace.com/support/help/technology-support/application-software/"&gt;Dynatrace&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Configuring Your Tracer to Communicate With the Local Agent
&lt;/h2&gt;

&lt;p&gt;As you can see above we are using the &lt;code&gt;INSTANCE_IP&lt;/code&gt; environment variable to find the IP address of the current instance that the agent is listening on. If you are used to using APM services on traditional servers, VMs, or instances you are likely using &lt;code&gt;localhost&lt;/code&gt; or &lt;code&gt;127.0.0.1&lt;/code&gt; to communicate with the local agent. In a Kubernetes cluster that approach is not going to work. You will need to find the actual instance IP as &lt;code&gt;localhost&lt;/code&gt; would refer to the container your App is running in, rather than the underlying Node that hosts your container.&lt;/p&gt;

&lt;p&gt;If you are deploying your application on a &lt;a href="https://docs.convox.com/reference/primitives/rack"&gt;Convox Rack&lt;/a&gt; then every container will have the &lt;code&gt;INSTANCE_IP&lt;/code&gt; environment variable automatically injected into your App’s container for your convenience. Otherwise you will need to use the Kubernetes API or another third party utility to determine the IP address. As an example you can find the Datadog documentation for this &lt;a href="https://docs.datadoghq.com/agent/kubernetes/apm/?tab=daemonset"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pulling it All Together
&lt;/h2&gt;

&lt;p&gt;Once you have completed the steps outlined above you should have a collection agent running on every Node in your cluster. Your applications should be fully instrumented and streaming data to the collection agent on their respective Nodes. With this in place, you should be able to keep a close eye on the performance of your applications as well any impact those applications may be having on your Nodes. As an example if your infrastructure monitoring indicates that one of your Nodes is consuming all of it's available memory, your APM service should be able to pinpoint what application, and potentially even what line of code is causing the problem.&lt;/p&gt;

&lt;p&gt;At Convox we strive to make not only &lt;a href="https://docs.convox.com/getting-started/introduction#install-a-rack"&gt;setting up a Kubernetes cluster&lt;/a&gt; easy but we also ensure you have all the tools you need to run a production application on Kubernetes such as &lt;a href="https://docs.convox.com/deployment/scaling"&gt;auto-scaling&lt;/a&gt; and &lt;a href="https://docs.convox.com/integrations/monitoring/datadog"&gt;monitoring&lt;/a&gt;. If you haven't already tried setting up a Kubernetes cluster with Convox it only take a few minutes and it works on all major clouds so give it a &lt;a href="https://docs.convox.com/getting-started/introduction"&gt;try&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>kubernetes</category>
      <category>apm</category>
      <category>datadog</category>
      <category>devops</category>
    </item>
    <item>
      <title>Monitoring Applications on Kubernetes an Overview</title>
      <dc:creator>Cameron Gray</dc:creator>
      <pubDate>Thu, 06 Aug 2020 16:47:54 +0000</pubDate>
      <link>https://dev.to/camerondgray/monitoring-applications-on-kubernetes-an-overview-n91</link>
      <guid>https://dev.to/camerondgray/monitoring-applications-on-kubernetes-an-overview-n91</guid>
      <description>&lt;p&gt;As many teams make the move to Kubernetes one of the challenges is how to monitor your infrastructure and apps. Most of the same monitoring tools that people are already familiar with are available for Kubernetes but it does require a slightly different approach. In this post my hope is to give those of you who are new to Kubernetes a little bit of an overview on the core components as well as the key things to monitor. I will also cover some specific strategies for implementing a variety of third party monitoring services. This is the first of a multi-part series of posts and we will dig into specific examples in subsequent posts.&lt;/p&gt;

&lt;h1&gt;
  
  
  Kubernetes Architecture
&lt;/h1&gt;

&lt;p&gt;If you are new to deploying onto Kubernetes it's helpful to have a basic understanding of how a Kubernetes cluster is structured before we dive into the details of monitoring. While a complete overview of Kubernetes is definitely outside of the scope of this post, I will try and cover the basics. For the simple case of deploying an application that runs in a Docker container using the typical &lt;a href="https://kubernetes.io/docs/concepts/workloads/pods/#using-pods"&gt;"one-container-per-Pod"&lt;/a&gt; model that is the most common for Kubernetes, you really only need to understand a few core concepts.&lt;/p&gt;

&lt;p&gt;The biggest philosophical difference between Kubernetes and more traditional deployment options such as dedicated servers, or virtual machines like EC2 is the often mentioned pets vs cattle analogy. While pets are beloved individuals that are generally thought of as unique and irreplaceable, cattle are generally thought of as a herd. You may not give much consideration to an individual cow, what is important is the total amount of cattle you have and if one is to expire for whatever reason it can be replaced by any other cow.&lt;/p&gt;

&lt;p&gt;In a typical server or VM based deployment, you may have a bunch of task specific instances, like the backend server or the mail server (pets). If you have ever had the one EC2 instance that's running your MySQL database suddenly fail you know exactly what I am talking about! Kubernetes takes a different approach and runs your cluster on a group of identical instances that can be terminated and replaced at any time (cattle). When you deploy onto a Kubernetes cluster you don't spend much time at all thinking about the individual instances. You are more concerned with the total capacity and load on your cluster as a whole. Kubernetes will automatically handle replacing any instances and you generally don't spend much, if any, time interacting with individual instances as an end user.&lt;/p&gt;

&lt;p&gt;Your cluster is going to be running on a group of underlying physical or virtual hosts, such as AWS EC2 instances, and Kubernetes refers to those hosts as Nodes. Every instance of a container that you deploy (in the simple and most common use case) is referred to as a Pod. When you deploy a Pod onto a cluster the Kubernetes scheduler will attempt to find a Node with sufficient resources (typically RAM and CPU) to host that Pod.  For example if you tell Kubernetes to deploy three copies of your main web service and two copies of your backend API you are telling Kubernetes to run five Pods in total and Kubernetes will figure out which Nodes to deploy those Pods on to without you needing to worry about it. &lt;/p&gt;

&lt;p&gt;There are two things you do need to worry about however when it comes to the Nodes in your Kubernetes cluster. The first is how the required resources for your individual Pods align with the available resources on any one Node and the second is the total number of Nodes you will need to run to have the total capacity required to meet your demand. If a single instance of your backend web service requires 8gb of memory and your hosts have 2gb of memory each, your cluster will be unable to host your application because no single Node is able to fit your Pod. Alternatively, if your backend web service only requires 1gb of memory and your hosts have 32gb of memory each you can run almost 32 copies of your service (allow room for overhead) on each Node. &lt;/p&gt;

&lt;p&gt;While this seems like a very simple concept it gets a little more interesting when you are running multiple types of services (Pods) each with their own resource requirements and you start thinking about scaling up and scaling down to meet changing demand. The important concept to keep in mind is that it's much faster to find a Node with spare capacity and deploy an additional Pod there than it is to spin up a new Node to create additional capacity but it's also expensive to run more or larger Nodes than you need because you are paying for resources that you aren't using. &lt;/p&gt;

&lt;p&gt;Kubernetes will try and make the most efficient use possible of the available Node capacity which means if you have different types of Pods with different resource requirements (typically CPU and Memory) that need to be run they will not necessarily be evenly distributed across your Nodes. This is called Bin Packing, and is sometimes referred to as Kubernetes Tetris because of this great &lt;a href="https://youtu.be/u_iAXzy3xBA?t=1033"&gt;talk&lt;/a&gt; given by the always awesome Kelsey Hightower.  The important thing is that you don't need to concern yourself with which Pods are running on which Nodes and if a Node should fail, Kubernetes is going to automatically find a new place to put those pods and spin up more Nodes if necessary. There is obviously a lot more to Kubernetes under the hood but as an app developer this hopefully gives you a good overview.&lt;/p&gt;

&lt;h1&gt;
  
  
  What Do You Need to Monitor
&lt;/h1&gt;

&lt;p&gt;Now that we have a basic understanding of how our apps run on a Kubernetes cluster, let's dig into what we need to keep an eye on once we have our apps deployed.&lt;/p&gt;

&lt;p&gt;There are many facets to monitoring a set of web applications and their underlying infrastructure, but for the purposes of this post I am going to focus on three main elements&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Infrastructure Monitoring (monitor your Nodes)&lt;/li&gt;
&lt;li&gt;APM (monitor your apps)&lt;/li&gt;
&lt;li&gt;Log collection (consolidating your infrastructure and app logs)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Infrastructure Monitoring
&lt;/h2&gt;

&lt;p&gt;While I spent much of the previous section of this post explaining why you don't need to concern yourself with your individual Nodes, there are two important reasons to monitor the health of the Nodes in your cluster. First, only by monitoring all your individual Nodes will you be able to get a complete picture of the health and capacity of your cluster. While you don't care if one Node crashes and needs to be replaced it is important to know if all your Nodes are running at 99% CPU usage. Secondly, if you have individual Nodes that are spiking or crashing it's important to know what Pods are running on those Nodes. For example if you have a bug in your application code that consumes excessive amounts of memory you may only learn that if you see one of your Pods spiking memory usage and you have a record of which Pods are running on that Node.&lt;/p&gt;

&lt;p&gt;One of the easiest ways to keep an eye on all your Nodes is to run a monitoring agent, such as the &lt;a href="https://docs.datadoghq.com/agent/kubernetes/?tab=daemonset"&gt;Datadog Agent&lt;/a&gt;, as a &lt;a href="https://kubernetes.io/docs/concepts/workloads/controllers/daemonset/"&gt;DaemonSet&lt;/a&gt; on your cluster.  In its simplest and most common form, a DaemonSet is a way to instruct Kubernetes to run exactly one copy of a given container (Pod) on each of your Nodes. This is an ideal configuration for a monitoring agent application because it ensures that each Node in your cluster will be monitored. Once you have a monitoring agent running as a DaemonSet on your cluster you can start collecting important information such as memory and CPU usage on all your Nodes and therefore your cluster as a whole.&lt;/p&gt;

&lt;p&gt;It's important when running a monitoring agent like Datadog, to make sure that your other containers are labeled with something that will help you identify the app that is running in that container. This will ensure you can tie the load on your underlying Nodes back to what containers are running on them. At Convox we automatically name all containers with the name of the app that is running in them as well as adding labels to provide an even more granular view.&lt;/p&gt;

&lt;p&gt;With a monitoring agent running on each of your nodes you can start to make informed decisions about how to best configure your cluster for your particular demands. Scaling decisions in Kubernetes are a little different than in a typical containerized hosting environment. You need to consider the factors we mentioned earlier such as bin packing and headroom as well as understanding how quickly you will need to scale up or down which will significantly impact whether you want to run many smaller nodes or fewer larger nodes. Of course having a smart autoscaler &lt;a href="https://docs.convox.com/deployment/scaling"&gt;(such as Convox provides)&lt;/a&gt; can help a lot.&lt;/p&gt;

&lt;h2&gt;
  
  
  Application Performance Monitoring (APM)
&lt;/h2&gt;

&lt;p&gt;APM is a concept that has been around for some time and like infrastructure monitoring the major APM providers (Datadog, New Relic, etc..) have solutions for most major hosting configurations including Kubernetes. &lt;/p&gt;

&lt;p&gt;While Infrastructure monitoring gives you an important view into the health and capacity of your underlying physical or virtual infrastructure, APM gives you insight into the health and performance of your applications. If you introduced a new bug in your application code that is causing crashes or slow response times, APM is typically the tool that you will use to find the cause of those issues. APM can often pinpoint the performance of individual modules or even lines of code. In production this type of monitoring can also often find the dreaded "works on my machine" problem where a particular application may work just fine on an individual developer's laptop but behaves completely differently in a production environment.&lt;/p&gt;

&lt;p&gt;Fortunately the same DaemonSet strategy that you use to deploy an infrastructure monitoring agent in your Kubernetes cluster works for APM. In fact, for many providers such as Datadog it is the same agent and you simply enable the APM features with a &lt;a href="https://docs.datadoghq.com/agent/kubernetes/apm/?tab=daemonset"&gt;configuration option&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Once you have your agent Pods running on all your Nodes you have two important steps to get an APM solution up and running. First you will need to add the actual APM instrumentation code to your application which is typically a fairly lightweight code change and all the major APM providers have libraries for most popular languages and frameworks. This will allow your application to start collecting trace data and now all you need is send that data to the Agent running on the local Node.&lt;/p&gt;

&lt;p&gt;This can be a little tricky and depends on how your Kubernetes cluster is configured and which APM service you are using. Once you have it all wired up however your application should be sending trace data to the Agent running on it's Node and in turn the Agent should be reporting that data back to your APM service. We will get into the details of configuring this in a follow up post.&lt;/p&gt;

&lt;h2&gt;
  
  
  Log Collection
&lt;/h2&gt;

&lt;p&gt;The final piece of the monitoring puzzle is to consolidate the logs from your applications and infrastructure into a single stream so you can get a detailed view, in real time if necessary, of everything that is happening in your cluster.&lt;/p&gt;

&lt;p&gt;While Infrastructure and Application monitoring provides you with a real time view of what is happening, the logs provide the details and historical view of everything that has happened in your environment. While not a perfect analogy, you can think of the monitors as the instrument cluster in an airplane cockpit and the logs are the black box in the tail that is recording everything that happens. Oftentimes when something goes wrong you will find you need to pour through the logs to really figure out what happened. &lt;/p&gt;

&lt;p&gt;The flaw in the airplane analogy is that monitoring applications and logs each have both realtime and after the fact benefits because they provide different views of your environment and applications and often you will need to look at both to get a complete picture of what is happening or has happened. The key with logging is that every piece of your environment from individual hosts, to the Kubernetes cluster components, up to your custom applications all spit out logs and often you need a consolidated, cluster-level, view of these logs to get a complete picture of what is going on.&lt;/p&gt;

&lt;p&gt;Kubernetes does not provide a built-in solution for cluster-level logging, although the documentation does provide some guidance on a few options for setting up cluster-level logging yourself. In what has become a common theme for this post, we once again find that leveraging a DaemonSet to run a logging agent on every node is a really good solution. If you are already running an Agent such as Datadog or &lt;a href="https://docs.logdna.com/docs/logdna-agent-kubernetes"&gt;LogDNA&lt;/a&gt; for the purposes of Node monitoring and/or APM, typically those agents will also provide a solution for gathering logs from stdout or from a file system logging driver, such as the Docker json logging driver, and directing those logs to an outside ingest for analysis. I will also dig into some detailed implementation examples in a follow up post. &lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Hopefully this post has given you some insight into the important aspects of monitoring your Kubernetes clusters and the applications you deploy onto them. While it can seem daunting at first, you can generally use the same tools you are already familiar with as long as you understand some basic strategies that are unique to Kubernetes. Watch this space for some follow up posts that dig into the details. Meanwhile, if you want to spin up your first Kubernetes cluster on any cloud and deploy an application in just a few clicks and a few minutes there is no easier way than &lt;a href="https://docs.convox.com/getting-started/introduction"&gt;Convox&lt;/a&gt; so give it a try!&lt;/p&gt;

</description>
      <category>kubernetes</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
