Data Engineering

The busy developer’s guide to Kubernetes

Posted by Chris

TLDR:  Kubernetes is a container orchestration system.  You define K8’s API objects in a configuration file and then the Kubernetes Controller ensures your infrastructure matches this desired state.  Kubernetes automatically manages your application’s infrastructure.

Why use Kubernetes?

You’re a busy developer.  You’ve heard of Kubernetes (aka K8s), perhaps your organization uses Kubernetes.  It’s time for a quick overview of what Kubernetes is so you don’t have to just smile and nod next time someone mentions it.

Applications today need to be automatically scalable and highly available.  This can be a real pain.

Kubernetes to the Rescue

Kubernetes manages your infrastructure for you.  It can even manage your deployments too.

You define the desired state of your infrastructure and the Kubernetes Controller takes care of the rest.  Specify the number of Replicas you’d like for each Pod (think 1 Pod = 1 instance of an application). Then the K8s controller will automatically self-heal anytime a Pod becomes unusable.  No more restarting processes to revive a server in the middle of the night.

Deployments are another type of K8s object. In the event of a failed deployment, Kubernetes makes it easy to revert.  No more scrambling at deploy time if something accidentally breaks.

Read here in the Kubernetes documentation for more details on what Kubernetes can do.

Basic Terms & Architecture of Kubernetes

Architecture Overview

  • Kubernetes Cluster – is a set of Nodes grouped together.
  • Nodes – are a physical or virtual machine that launches containers.  Think of these as the worker machines. 
  • Master Node – one of the Nodes above is designated Master Node by Kubernetes.  This node is the brains of the orchestration and watches over the cluster of Nodes.
  • PODs – are the smallest creatable object.  Think of a POD as representing one instance of your application.  A single Node can have 1 or many PODs.
  • Containers – The Docker image (or another container type) that runs inside your POD.  PODs can have 1 or more Docker images but the Docker images should not be duplicated inside POD. 

The section Kubernetes Components from the documentation provides more details on the architecture.

Basic Terms / Common API Objects

Objects are defined in a configuration file to tell the K8’s API what to do.  You define the specification, the K8’s controller makes it happen. A POD, mentioned above, is an example of an object you can create with the K8’s API.  

Here are some common API Objects:

  • PODs – Represent a single instance of your application.  A POD can contain multiple containers (ie. frontend and backend containers).  To scale an application simply add a POD.
  • ReplicaSets – specify the count and selector on which POD this pertains to.  This will ensure the number of healthy PODs running always matches your specified count.
  • Namespaces – a virtual cluster inside a physical cluster.  This allows you to set the scope for certain K8’s resources.  The name of a resource needs to be unique inside a Namespace. Read more about Namespaces in the documentation.
  • Labels and Selectors – Labels are a key:value pair specified by you for each Object.  These help you organize groups of resources. Selectors allow you to specify which groups of resources an Object will act on.  Read more about Labels and Selectors.
    • Using labels and selectors you can specify a label like:
      • Environment = production
  • Deployments – specifies how to roll out updates.  Can take all the PODs down and then bring back up or can do rolling updates, modifying one at a time.  Easily reversible.
  • Services – Provides an interface for PODs to communicate with each other.  POD IPs are not static.

Read more to Understand Kubernetes Objects from the documentation.

How to use Kubernetes

Using Kubernetes means you are interacting with the Kubernetes API.  You send the configurations you’ve defined and Kubernetes creates the objects.

  • Kubectl is the command-line tool to interact with the Kubernetes API.  This is where your interaction with the cluster will take place. Kubectl Cheat Sheet.
  • YAML files contain the configuration for Kubernetes Objects.  Use yamllint.com or and linter plug-in for your IDE.
  • Helm helps you manage your applications that run in Kubernetes.  This tool lets you define a Helm Chart.  
  • Helm Charts allow you to group all the Kubernetes objects for a single application together. A Helm Chart is a collection of all the individual resources needed for a single application.  Think 1 Helm Chart for 1 Application. Check out the Helm Chart – Getting Started Guide.
  • Kubectx is an open-source tool to easily switch between Kubernetes clusters.  In case your organization has multiple clusters running. This is not for Namespace switching, but for dealing with multiple physical clusters. 

Next Steps with Kubernetes

You don’t need access to a Kubernetes cluster to get some practice!  Minikube allows you to set up a K8’s cluster locally and Katacoda offers free, in-browser Kubernetes tutorials.

  1. Start Here – Do the Basic Kubernetes Interactive Tutorial right in your browser.  Over 6 modules you will create a cluster then deploy and scale an application.  
  2. Install Minikube on your local machine to play around with Kubernetes.
  3. Try out some more Kubernetes Tutorials.
  4. Set up Kubectl to connect to a cluster you have access to at work.  Try out some basic read-only commands like:
    1. Kubetctl get all
    2. Kubectl get pods
    3. Kubectl describe pods

Have fun playing around with Kubernetes!