Reader small image

You're reading from  Kubernetes - A Complete DevOps Cookbook

Product typeBook
Published inMar 2020
PublisherPackt
ISBN-139781838828042
Edition1st Edition
Concepts
Right arrow
Author (1)
Murat Karslioglu
Murat Karslioglu
author image
Murat Karslioglu

Murat Karslioglu is a distinguished technologist with years of experience using infrastructure tools and technologies. Murat is currently the VP of products at MayaData, a start-up that builds data agility platform for stateful applications, and a maintainer of open source projects, namely OpenEBS and Litmus. In his free time, Murat is busy writing practical articles about DevOps best practices, CI/CD, Kubernetes, and running stateful applications on popular Kubernetes platforms on his blog, Containerized Me. Murat also runs a cloud-native news curator site, The Containerized Today, where he regularly publishes updates on the Kubernetes ecosystem.
Read more about Murat Karslioglu

Right arrow
Logging with Kubernetes

In this chapter, we will discuss cluster logging for Kubernetes clusters. We will talk about setting up a cluster to ingest logs, as well as how to view them using both self-managed and hosted solutions.

In this chapter, we will cover the following recipes:

  • Accessing Kubernetes logs locally
  • Accessing application-specific logs
  • Building centralized logging in Kubernetes using the EFK stack
  • Logging with Kubernetes using Google Stackdriver
  • Using a managed Kubernetes logging service
  • Logging for your Jenkins CI/CD environment

Technical requirements

The recipes in this chapter expect you to have a functional Kubernetes cluster deployed by following one of the recommended methods described in Chapter 1, Building Production-Ready Kubernetes Clusters.

The Logging for your Jenkins CI/CD environment recipe in this chapter expects you to have a functional Jenkins server with an existing CI pipeline created by following one of the recommended methods described in Chapter 3, Building CI/CD Pipelines.

The Kubernetes command-line tool kubectl will be used for the rest of the recipes in this chapter since it's the main command-line interface for running commands against Kubernetes clusters. We will also use helm where Helm charts are available in order to deploy solutions.

Accessing Kubernetes logs locally

In Kubernetes, logs can be used for debugging and monitoring activities to a certain level. Basic logging can be used to detect configuration problems, but for cluster-level logging, an external backend is required to store and query logs. Cluster-level logging will be covered in the Building centralized logging in Kubernetes using the EFK stack and Logging Kubernetes using Google Stackdriver recipes.

In this section, we will learn how to access basic logs based on the options that are available in Kubernetes.

Getting ready

Clone the k8sdevopscookbook/src repository to your workstation to use the manifest files in the chapter10 directory, as follows:

$ git clone https://github.com/k8sdevopscookbook/src.git
$ cd src/chapter10

Make sure you have a Kubernetes cluster ready and kubectl and helm configured to manage the cluster resources.

How to do it…

This section is further divided into the following subsections to make this process easier:

  • Accessing...

Accessing application-specific logs

In Kubernetes, pod and deployment logs that are related to how pods and containers are scheduled can be accessed through the kubectl logs command, but not all application logs and commands are exposed through Kubernetes APIs. Getting access to these logs and shell commands inside a container may be required.

In this section, we will learn how to access a container shell, extract logs, and update binaries for troubleshooting.

Getting ready

Clone the k8sdevopscookbook/src repository to your workstation to use the manifest files under the chapter10 directory, as follows:

$ git clone https://github.com/k8sdevopscookbook/src.git
$ cd src/chapter10

Make sure you have a Kubernetes cluster ready and kubectl configured to manage the cluster resources.

How to do it…

This section is further divided into the following subsections to make this process easier:

  • Getting shell access in a container
  • Accessing PostgreSQL logs inside a container

Getting shell access...

Building centralized logging in Kubernetes using the EFK stack

As described in the the Accessing Kubernetes logs locally section, basic logging can be used to detect configuration problems, but for cluster-level logging, an external backend is required to store and query logs. A cluster-level logging stack can help you quickly sort through and analyze the high volume of production log data that's produced by your application in the Kubernetes cluster. One of the most popular centralized logging solutions in the Kubernetes ecosystem is the Elasticsearch, Logstash, and Kibana (ELK) stack.

In the ELK stack, Logstash is used as the log collector. Logstash uses slightly more memory than Fluent Bit, which is a low-footprint version of Fluentd. Therefore, in this recipe, we will use the Elasticsearch, Fluent-bit, and Kibana (EFK) stack. If you have an application that has Logstash dependencies, you can always replace Fluentd/Fluent Bit with Logstash.

In this section, we will learn how...

Logging Kubernetes using Google Stackdriver

In this section, we will use Google Stackdriver Kubernetes Engine Monitoring to monitor, isolate, and diagnose our containerized applications and microservices environments. You will learn how to use Stackdriver Kubernetes Engine Monitoring to aggregate logs, events, and metrics from your Kubernetes environment on GKE to help you understand your application's behavior in production.

Getting ready

Make sure you have a Google Kubernetes Engine (GKE) cluster ready and kubectl configured to manage the cluster resources. If you don't have one, you can follow the instructions in Chapter 1, Building Production-Ready Kubernetes Clusters, in the Configuring a Kubernetes cluster on Google Cloud Platform recipe.

How to do it…

This section is further divided into the following subsections to make this process easier:

  • Installing Stackdriver Kubernetes Engine Monitoring support for GKE
  • Configuring a workspace on Stackdriver
  • Viewing GKE...

Using a managed Kubernetes logging service

Running an EFK stack to store and maintain Kubernetes logs in your cluster is useful until something goes wrong with your cluster. It is recommended that you keep your log management system and production cluster separate so that you have access in case of cluster failure.

In this section, we will learn how to use some of the freely available SaaS solutions to keep your cluster logs accessible, even if your cluster is not available.

Getting ready

Make sure you have a Kubernetes cluster ready and kubectl configured to manage the cluster resources.

How to do it…

This section is further divided into the following subsections to make this process easier:

  • Adding clusters to Director Online
  • Accessing logs using Director Online

Connecting clusters to Director Online

OpenEBS Director provides a freely managed EFK stack as a SaaS solution so that you can store and manage your Kubernetes cluster logs. In this recipe, we will add our Kubernetes...

Logging for your Jenkins CI/CD environment

CI/CD pipelines can generate a great amount of metadata every day in busy build environments. Elasticsearch is the perfect platform for feeding this kind of data from Jenkins.

In this section, we will learn how to enable and access the logs of our Jenkins instance and analyze team efficiency.

Getting ready

All the operations mentioned in this recipe require a fully functional Jenkins deployment, as described in Chapter 3, Building CI/CD Pipelines, in the Setting up a CI/CD pipeline in Jenkins X section.

Clone the k8sdevopscookbook/src repository to your workstation to use the manifest files in the chapter10 directory:

$ git clone https://github.com/k8sdevopscookbook/src.git
$ cd src/chapter10

Make sure you have a Kubernetes cluster, Jenkins X, and an EFK stack ready and that kubectl has been configured so that you can manage the cluster resources.

How to do it…

This section will show you how to feed Jenkins logs to Elasticsearch. This section...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Kubernetes - A Complete DevOps Cookbook
Published in: Mar 2020Publisher: PacktISBN-13: 9781838828042
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Murat Karslioglu

Murat Karslioglu is a distinguished technologist with years of experience using infrastructure tools and technologies. Murat is currently the VP of products at MayaData, a start-up that builds data agility platform for stateful applications, and a maintainer of open source projects, namely OpenEBS and Litmus. In his free time, Murat is busy writing practical articles about DevOps best practices, CI/CD, Kubernetes, and running stateful applications on popular Kubernetes platforms on his blog, Containerized Me. Murat also runs a cloud-native news curator site, The Containerized Today, where he regularly publishes updates on the Kubernetes ecosystem.
Read more about Murat Karslioglu