{"id":695,"date":"2019-08-27T04:31:59","date_gmt":"2019-08-27T04:31:59","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=695"},"modified":"2019-08-27T04:31:59","modified_gmt":"2019-08-27T04:31:59","slug":"kubernetes-monitoring-sensu","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=695","title":{"rendered":"How to Setup Kubernetes Monitoring With Sensu Sidecar Agent"},"content":{"rendered":"<p>The rise of containerized infrastructure has caused us to rethink the way we build and deploy our applications. But, with all that speed and flexibility comes challenges, especially when it comes to maintaining visibility into your (often multi-generational) infrastructure.<\/p>\n<p>In this post, I\u2019ll discuss some of the challenges to monitoring containers and <a href=\"https:\/\/devopscube.com\/kubernetes-deployment-tutorial\/\" rel=\"noreferrer noopener\">Kubernetes<\/a> (the go-to for container orchestration), talk about some of the data sources and a few methods for monitoring Kubernetes, and walk you through a tutorial on monitoring containers with <a href=\"https:\/\/sensu.io\/?ref=devopscube.com\" rel=\"noreferrer noopener\">Sensu<\/a>, a multi-cloud monitoring tool.<\/p>\n<h2 id=\"container-monitoring-the-challenges\">Container monitoring: the challenges<\/h2>\n<p>In a containerized world, your applications are constantly moving, making it even more difficult to keep tabs on them. It\u2019s also the norm to have distributed applications, which causes a whole new set of problems when it comes to observability. Said another way, this new reality of distributed, constantly moving infrastructure means there are many smaller pieces to monitor. Similarly, it\u2019s important to keep track of the labels and annotations associated with pods and containers.<\/p>\n<h2 id=\"monitoring-kubernetes-the-data-sources\">Monitoring Kubernetes: the data sources<\/h2>\n<p>When it comes to <a href=\"https:\/\/devopscube.com\/setup-prometheus-monitoring-on-kubernetes\/\" rel=\"noreferrer noopener\">monitoring Kubernetes<\/a>, there are essentially four data sources you\u2019re plugging into your monitoring tool:<\/p>\n<ol>\n<li>The Kubernetes hosts running the Kubelet. The most common way to get data out of these hosts is to use the<a href=\"https:\/\/github.com\/prometheus\/node_exporter?ref=devopscube.com\"> Prometheus node exporter<\/a> to scrape data from Kubernetes and expose system resource telemetry data on an HTTP endpoint.<\/li>\n<li>The Kubelet metrics, which includes metrics for apiserver, kube-scheduler, and kube-controller-manager.<\/li>\n<li>The Kubelet\u2019s built-in cAdvisor, which collects, aggregates, processes, and exports metrics for your running containers.<\/li>\n<li>kube-state-metrics, which gives you data at the cluster level, such as all the pods you have configured and their current state.<\/li>\n<\/ol>\n<h2 id=\"monitoring-kubernetes-the-sidecar-pattern\">Monitoring Kubernetes: the sidecar pattern<\/h2>\n<p>Of these data sources, we tend to prefer kube-state-metrics, as it gives you most of what you need without overloading you with information. I\u2019ll use that for my tutorial of container monitoring with Sensu, but wanted to note that Sensu can also scrape Prometheus metrics (but that\u2019s a whole other post \u2014 for now, feel free to check out our <a href=\"https:\/\/bonsai.sensu.io\/assets\/sensu\/sensu-prometheus-collector?ref=devopscube.com\">Prometheus collector asset<\/a>).<\/p>\n<p>Using the sidecar pattern, you can deploy a Sensu agent alongside your application container. A sidecar approach makes it possible for each Kubernetes pod to host your application container alongside other containers running support processes, such as the Sensu agent. Since all containers running inside a pod all share the same network space, your applications can talk to Sensu as if they were running in the same container.<\/p>\n<h2 id=\"tutorial\">Tutorial<\/h2>\n<p>In this tutorial, we\u2019ll install and configure sensuctl (Sensu\u2019s command-line tool), deploy the Sensu backend using a Kubernetes deployment, and deploy Sensu agent sidecars. This tutorial assumes you have Kubernetes installed. If you don\u2019t, take a look at setting up <a href=\"https:\/\/kubernetes.io\/docs\/setup\/learning-environment\/minikube\/?ref=devopscube.com\">Minikube<\/a> which will make things easier for the tutorial.<\/p>\n<h3 id=\"1-deploy-the-sensu-backend-using-a-kubernetes-deployment\">1. Deploy the Sensu backend using a Kubernetes deployment<\/h3>\n<p>Download the <a href=\"https:\/\/github.com\/sensu\/sensu-kube-demo?ref=devopscube.com\">sensu-kube-demo repo<\/a>. Use this <a href=\"https:\/\/github.com\/sensu\/sensu-kube-demo\/blob\/master\/go\/deploy\/sensu-backend.yaml?ref=devopscube.com\">sensu-backend.yaml file<\/a> from the repo and the following command to deploy Sensu:<\/p>\n<pre><code>kubectl create -f go\/deploy\/sensu-backend.yaml<\/code><\/pre>\n<p>The <code>go\/deploy\/<\/code> path reference is relative to the top directory of the repository checkout.<\/p>\n<p> You will also want to establish a Kubernetes ingress policy and host DNS configuration that will allow you to communicate with the sensu-backend running inside of the Kubernetes node. For the rest of the tutorial, I will assume you have configured things such that sensu.local tcp port 80 maps to the sensu-backend pod\u2019s TCP port 8080, the default port for the sensu-backend API service. The <a href=\"https:\/\/github.com\/sensu\/sensu-kube-demo?ref=devopscube.com\">sensu-kube-demo repository<\/a> includes an appropriate nginx ingress policy example.<\/p>\n<h3 id=\"2-optional-install-sensuctl-on-your-workstation\">2. [Optional] Install sensuctl on your workstation<\/h3>\n<p>sensuctl is a command-line tool for managing resources within Sensu. It works by calling Sensu\u2019s HTTP API to create, read, update, and delete resources, events, and entities. The below instructions are for Ubuntu \u2014 check out our documentation for sensuctl installation on CentOS, Windows, and macOS.<\/p>\n<p>To install sensuctl on Ubuntu:<\/p>\n<ol>\n<li>Add the Sensu repository<\/li>\n<\/ol>\n<pre><code>curl -s https:\/\/packagecloud.io\/install\/repositories\/sensu\/stable\/script.deb.sh | sudo bash<\/code><\/pre>\n<ol start=\"2\">\n<li>Install the sensu-go-cli package<\/li>\n<\/ol>\n<pre><code>sudo apt-get install sensu-go-cli<\/code><\/pre>\n<p>In order to use sensuctl from your workstation to communicate to the containerized sensu-backend, you\u2019ll need to make sure to configure your Kubernetes ingress controller and network DNS settings to allow access to the sensu-backend API outside of the Kubernetes cluster.<\/p>\n<p>To save time, for this tutorial you can alternatively use the sensuctl provided in the sensu-backend container. In general you\u2019ll want to use sensuctl installed natively in your workstation environment for ease of operation.<\/p>\n<h3 id=\"3a-configure-sensuctl-to-use-the-built-in-admin-user-on-your-workstation\">3a. Configure sensuctl to use the built-in admin user on your workstation<\/h3>\n<p>If you are using the sensuctl configured on your workstation, enter the following to configure sensuctl:<\/p>\n<pre><code>sensuctl configure -n \\\n--username 'admin'\\\n--password 'P@assw0rd!'\\\n--namespace default\\\n--url 'http:\/\/sensu.local' <\/code><\/pre>\n<p>Please note you\u2019ll need to adjust the URL to match your Kubernetes cluster ingest configuration and DNS configuration.<\/p>\n<h3 id=\"3b-configure-sensuctl-to-use-the-built-in-admin-user-inside-the-cluster\">3b. Configure sensuctl to use the built-in admin user inside the cluster<\/h3>\n<p>Open up a new terminal window and start a shell inside the sensu-backend pod:<\/p>\n<pre><code>kubectl exec -it sensu-backend-&lt;replace&gt; -- \/bin\/sh<\/code><\/pre>\n<p>You can discover the correct pod name with:<\/p>\n<pre><code>kubectl get pods -l app=sensu-backend<\/code><\/pre>\n<pre><code>sensuctl configure -n \\\n--username 'admin'\\\n--password 'P@assw0rd!'\\\n--namespace default\\\n--url 'http:\/\/localhost:8080' <\/code><\/pre>\n<p>Here you can use the localhost URL as if the sensu-backend were running locally, because your sensuctl is operating in the same pod, sharing the pod network with the sensu-backend container.<\/p>\n<h3 id=\"4-create-the-sensu-namespace-for-the-dummy-application\">4. Create the Sensu namespace for the dummy application<\/h3>\n<p>All the agents used in this tutorial operate in a Sensu namespace named demo.  We\u2019ll need to create the namespace now using sensuctl:<\/p>\n<pre><code>sensuctl namespace create demo<\/code><\/pre>\n<p>We can optionally reconfigure sensuctl to use the demo namespace as default:<\/p>\n<pre><code>sensuctl configure -n \\\n--username 'admin'\\\n--password 'P@assw0rd!'\\\n--namespace demo\\<\/code><\/pre>\n<h3 id=\"5-deploy-sensu-agent-sidecars\">5. Deploy Sensu agent sidecars<\/h3>\n<p>Using the aforementioned sidecar pattern and the <a href=\"https:\/\/github.com\/sensu\/sensu-kube-demo\/blob\/master\/go\/deploy\/dummy.sensu.yaml?ref=devopscube.com\">dummy.sensu.yaml file<\/a>, enter the following to deploy Sensu agent sidecars for two example app instances using a Kubernetes deployment:<\/p>\n<pre><code>kubectl apply -f go\/deploy\/dummy.sensu.yaml<\/code><\/pre>\n<p>If you have set the default namespace to demo, use sensuctl entity list to see the agent containers, otherwise, use sensuctl entity list &#8211;namespace demo to set the namespace explicitly. Each agent is running in a separate pod as a sidecar to support the dummy application as per the deployment configuration in the dummy.sensu.yaml file. From here you can use sensuctl to create Sensu checks to examine the running dummy service in each pod by using the dummy subscription, or a specific pod using the specific agent associated with each agent.<\/p>\n<p>For more on container monitoring with Sensu \u2014 including using the Sensu web UI to view events and setting up workflows to Slack and InfluxDB \u2014 <a href=\"https:\/\/docs.sensu.io\/sensu-go\/latest\/getting-started\/tutorial\/?ref=devopscube.com\">check out our interactive tutorial<\/a>. Thanks for reading, and happy monitoring!<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/kubernetes-monitoring-sensu\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Setup Kubernetes Monitoring With Sensu Sidecar Agent \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/kubernetes-monitoring-sensu\/<\/p>\n","protected":false},"author":1,"featured_media":696,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-695","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=695"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/695\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/696"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}