A walkthrough of "Bootstrapping Microservices with Docker, Kubernetes and Terraform"
Published: 2022-02-01 04:25:07

<Article under construction>

"Bootstrapping Microservices with Docker, Kubernetes and Terraform: A project-based guide" is a great way to learn all of these tools, if you can get it working. In this article, I attempt to document the workarounds and tricks that were helpful to me. 

_Chapter 1-3: Publishing your first microservice and learning Docker_. Here the book does a great job in articulating the differences between concepts like a docker image, docker container, Dockerfile, docker compose and the different commands associated with each. You've probably seen Dockerfiles in the root of your project when you clone one from github. You may have also seen other types of docker-related files like Makefile and .dockerignore. Don't worry about those yet. If you're unfamiliar with github at this point, I would recommend reading through the git scm before starting this book and possibly installing the desktop version of Git for Windows/Mac. In the later chapters on Terraform, you'll be iteratively working alongside the code with commits to your own git account. 

Know that the Dockerfile is a script file which creates an image and contains all the commands needed to start a container. The Docker image created from the Dockerfile consists of a series read-only layers (commits) that are stacked and each one is a delta of the changes from the previous layer.

The flow of containers goes like this: the command "docker build" packages our microservice as an image. Then, "push" publishes our image. Then, the "run" command uses our published image to execute a container. Commands like "docker build" and "docker run" are associated with a single container but "docker-compose up" can build and run multiple containers at once. You'll get familiar with all these commands in the first three chapters and also running them in combination with a cloud provider.

Running a Docker container directly from the Azure container registry presented some authentication challenges. You'll need to set up the Azure CLI according to the Azure docs. If you get authentication errors, remember to Docker login <registry name> with the username and password in your Container Registry > Access Key. Although it won't come up in the book until after you need it, a service principal is an authentication mechanism for Azure. It allows our clusters to authenticate with Azure so it can create load balancers as external endpoints for our customer-facing microservices. I linked the file for creating this principal later in this article.

_Chapter 4-5: Databases and Communication between microservices_

The book uses the MEAN stack, meaning that we'll be using mongoDB as our database. You'll have to upload information to mongoDB through the use of a tool called Robo3T. I was only successfully able to install this tool with the brew command and not from the website. After loading the json file from your example folder into Robo3T, you're supposed to be able to see your video with in localhost with the id appended, but this didn't work for me- no idea why so far.

Ch5 introduces the concept of volumes and message queues. Application code should be ephemeral and tied to the lifecycle of our container. But what about persistent data that we want to stick around and lives outside of the container? This is achieved with volumes. This allows us to upgrade or delete our database container without losing any database data that is tied to the application. Note that the book doesn't talk about bind mounts specifically- you'll have to learn those separately. The rest of the chapter is briefly devoted to message queues. In this, you're still executing much of the already established code in these chapters vs writing the code yourself. This is one negative I would have with the tutorial except the book is covering a lot of ground to begin with.

_Chapter 6+: Creating Infrastructure_

Ch 6 goes over the basics of Infrastructure as Code and how you'll want to use declarative over procedural languages. The book defines infrastructure as code is a form of executable documentation. Terraform is a tool/language for configuring infrastructure for cloud based applications. Kubernetes is a container orchestration platform (which is why we started the book with Docker). It's a good idea that if you don't know Docker/Kubernetes/TF to start with Docker. 

There can be a large number of Terraform files ending with .tf in your repository. Providers.tf is where you'll set up configuration having to do with your cloud provider. The choice of Azure configuration again presented more challenges that I don't remember having when practicing Terraform with AWS. To set up Terraform/Azure correctly, I had to follow the instructions on creating/using a service principal: https://docs.microsoft.com/en-us/azure/developer/terraform/get-started-cloud-shell-bash?tabs=bash. My Mac natively with zshell so just replace .bashrc with .zshrc. 

There are several caveats here when getting errors. Always terraform init in any new directory, to change your app_name/resource group to a unique name, and remember to change your kubernetes version to a working version for your region.