Reader small image

You're reading from  Kubernetes in Production Best Practices

Product typeBook
Published inMar 2021
PublisherPackt
ISBN-139781800202450
Edition1st Edition
Right arrow
Authors (2):
Aly Saleh
Aly Saleh
author image
Aly Saleh

Aly Saleh is a technology entrepreneur, cloud transformation leader, and architect. He has worked for the past 2 decades on building large-scale software solutions and cloud-based platforms and services that are used by millions of users. He is a co-founder of MAVS Cloud, a start-up that empowers organizations to leverage the power of the cloud. He also played various technical roles at Oracle, Vodafone, FreshBooks, Aurea Software, and Ceros. Aly holds degrees in computer science, and he has gained multiple credentials in AWS, GCP, and Kubernetes, with a focus on building cloud platforms, app modernization, containerization, and architecting distributed systems. He is an advocate for cloud best practices, remote work, and globally distributed teams.
Read more about Aly Saleh

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

View More author details
Right arrow

Chapter 4: Managing Cluster Configuration with Ansible

In Chapter 3, Provisioning Kubernetes Clusters Using AWS and Terraform, you learned how to create a Kubernetes infrastructure with Terraform and AWS, and you also learned how to develop infrastructure as code and provisioned your first production-like cluster.

This was just the first step towards building operational and production-ready Kubernetes clusters. By now, you should have an up-and-running cluster with Terraform infrastructure modules to provision other similar clusters.

These clusters are still plain; they're not configured or optimized to run production workloads. To make these clusters fully operational, we simply need to deploy and configure the required Kubernetes services for them.

In this chapter, you will design and develop a configuration management solution that you can use to manage the configuration of Kubernetes clusters and their supporting services. This solution is automated and scalable...

Technical requirements

In addition to the tools that you installed in Chapter 3, Provisioning Kubernetes Clusters Using AWS and Terraform, you will need to install the following tools:

  • python3
  • pip3
  • virtualenv

I will go into the specifics of these tools' installation and configuration in the next section. If you already know how to do this, you can go ahead and set them up now.

You need to have an up-and-running Kubernetes cluster as per the instructions in Chapter 3, Provisioning Kubernetes Clusters Using AWS and Terraform.

The code for this chapter is located at https://github.com/PacktPublishing/Kubernetes-in-Production-Best-Practices/tree/master/Chapter04.

Check out the following link to see the Code in Action video:

https://bit.ly/3cGtqjx

Installing the required tools

python3, pip3, and virtualenv are the prerequisites to execute the Ansible configuration playbooks that we will develop in this chapter. If you do not have these tools installed on your system, you can follow these instructions:

  • Execute the following commands to install python3, pip3, and virtualenv on Ubuntu Linux:
    $ sudo apt-get update
    $ sudo apt-get install python3
    $ sudo apt-get install python3-pip
    $ sudo pip3 install virtualenv
  • Execute the following commands to install python3, pip3, and virtualenv on Amazon Linux 2:
    $ sudo yum update
    $ sudo yum install python3
    $ sudo python3 -m pip install --upgrade pip
    $ sudo python3 -m pip install virtualenv
  • Execute the following commands to install python3, pip3, and virtualenv on macOS:
    $ brew install python3
    $ curl -O https://bootstrap.pypa.io/get-pip.py
    $ sudo python3 get-pip.py
    $ sudo -H pip3 install virtualenv
  • Execute the following commands to install python3, pip3, and virtualenv on Windows...

Implementation principles

In Chapter 1, Introduction to Kubernetes Infrastructure and Production-Readiness, you learned about the infrastructure design principles that we will follow in this book. I would like to start this chapter by highlighting the notable principles that influenced the configuration management solution and the technical decisions in this chapter:

  • Everything as code: In this chapter, we will keep our commitment to having everything in the infrastructure as code – cluster configuration is not an exception. You will use Ansible to achieve this goal by creating a configuration management solution for your Kubernetes cluster.
  • Automation: In the previous chapter, we used Terraform tool to automate infrastructure provisioning. We designed a solution around Terraform that can scale to serve a growing number of clusters without the need to scale up your infrastructure teams. Here, you will create a similar solution to manage the Kubernetes configuration...

Kubernetes configuration management

The beauty of Kubernetes is that every part of it is abstracted as an object that can be managed and configured declaratively with YAML or JSON through its API server. This makes Kubernetes configuration easier to manage as code. However, it is still challenging to manage this configuration when you have groups of clusters that run hundreds of add-ons and services.

Imagine a scenario where you manage a company's infrastructure with Kubernetes, and you have multiple clusters for development, testing, and production. Add to them the cluster add-ons that run on the Kubernetes services layer as per the following diagram:

Figure 4.1 – Kubernetes infrastructure layers

This means that you can have N clusters with a growing number of add-ons and different environment types, such as development, QA, and production. If we put these together, we end up with a complex and redundant configuration to manage.

The recommended...

Configuring the clusters

Now we put the solution we designed in the previous section into action. We will start by developing the Ansible framework skeleton, which will consist of the following parts:

  • group_vars: This directory contains the manifest configuration files with variables' default unless a cluster defines its own private variables in its own inventory.
  • inventories: This directory contains the configuration files with variables' values, which are specific to each cluster or cluster group, meaning that variables defined here override default variables defined under the groups_vars directory.
  • tasks: In this directory, we define a separate task for each cluster service and add-on that we need to deploy and configure; the task definition file is standard across tasks, as we will use Ansible's k8s module and pass to it the YAML templates to deploy against the target cluster.
  • templates: This directory contains the Kubernetes manifest YAMLs and...

Destroying the cluster's resources

You can follow the instructions in the Destroying the network and cluster infrastructure section of Chapter 3, Provisioning Kubernetes Clusters Using AWS and Terraform, to destroy the Kubernetes cluster and its related AWS resources. Please be sure to destroy the resources in the following order:

  1. Cluster packtclusters resources
  2. Cluster VPC resources
  3. Terraform shared state resources

After executing the previous steps, all of the cluster AWS resources should be destroyed successfully. You can still log in to the AWS web console and double-check the destruction of the resources to avoid any unwanted AWS charges.

Summary

In this chapter, you learned about Kubernetes configuration management challenges and how to scale your configuration management solution to manage multiple clusters and environments. We designed and developed a solution that is based on Ansible, and we went through practical hands-on examples to deploy this code.

We started by creating Ansible templates for Kubernetes objects and add-ons. Then, we developed the tasks and the playbook to execute the Ansible configuration in sequence against the targeted clusters.

This chapter introduced you to Ansible basic concepts. It showed you how to use the best practices of infrastructure and configuration as code, automation, and Ansible development.

This sets up the base for the coming chapters, where you will use this configuration management solution to configure and deploy clusters' add-ons and services where these add-ons are essential to reach production-readiness.

In the next chapter, you will learn about Kubernetes...

Further reading

You can refer to the following links for more information on the topics covered in this chapter:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Kubernetes in Production Best Practices
Published in: Mar 2021Publisher: PacktISBN-13: 9781800202450
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 €14.99/month. Cancel anytime

Authors (2)

author image
Aly Saleh

Aly Saleh is a technology entrepreneur, cloud transformation leader, and architect. He has worked for the past 2 decades on building large-scale software solutions and cloud-based platforms and services that are used by millions of users. He is a co-founder of MAVS Cloud, a start-up that empowers organizations to leverage the power of the cloud. He also played various technical roles at Oracle, Vodafone, FreshBooks, Aurea Software, and Ceros. Aly holds degrees in computer science, and he has gained multiple credentials in AWS, GCP, and Kubernetes, with a focus on building cloud platforms, app modernization, containerization, and architecting distributed systems. He is an advocate for cloud best practices, remote work, and globally distributed teams.
Read more about Aly Saleh

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