Reader small image

You're reading from  Infrastructure as Code (IAC) Cookbook

Product typeBook
Published inFeb 2017
PublisherPackt
ISBN-139781786464910
Edition1st Edition
Right arrow
Authors (2):
Stephane Jourdan
Stephane Jourdan
author image
Stephane Jourdan

Stephane Jourdan is a passionate infrastructure engineer, enthusiastic entrepreneur, zealous trainer, and continuous learner, working on innovative infrastructures since the early 2000s. He focuses equally on tools and culture, in environments as different as startups, online audio/video media, e-commerce, and semi-conductors. The common point between all these experiences is that success comes with rigor, technical repeatability, communication, and a shared team culture. He co-founded an infrastructure automation consultancy (https://www.linkedin.com/company/green-alto), a web radio (http://phauneradio.com/), a container/serverless platform for developers (https://www.squarescale.com/), and a sound design studio (http://www.tarabust.com/). When Stephane isn't starting or contributing to new open source projects, he's usually found hiking in remote places with his camera.
Read more about Stephane Jourdan

Pierre Pomès
Pierre Pomès
author image
Pierre Pomès

Pierre Pomès is a senior enthusiastic engineer of open source technologies and a Linux adept since 1994. He has been working in the IT industry for the last twenty years mostly in C development, system administration, and security including PCI-DSS. He is currently an architect and a DevOps team leader for Reservit, an online hotel booking engine. He has also contributed to the pfSense project.
Read more about Pierre Pomès

View More author details
Right arrow

Chapter 4. Automating Complete Infrastructures with Terraform

In this chapter, we will cover the following recipes:

  • Provisioning a complete CoreOS infrastructure on Digital Ocean with Terraform

  • Provisioning a three-tier infrastructure on Google Compute Engine

  • Provisioning a GitLab CE + CI runners on OpenStack

  • Managing Heroku Apps and Add-ons using Terraform

  • Creating a scalable Docker Swarm cluster on bare metal with Packet

Introduction


In this chapter, we'll describe complete infrastructures using Terraform, how it looks when everything is tied together, with a real project in mind. Most examples from previous chapters on Terraform were on Amazon Web Services, so to try to be more diverse and complete, this chapter is dedicated to other infrastructure services, namely Digital Ocean, Google Cloud, Heroku, and Packet. On Digital Ocean, we'll build a fully working and monitored CoreOS cluster with DNS dynamically updated. On Google Cloud, we'll build a three-tier infrastructure with two HTTP nodes behind a load balancer and an isolated MySQL managed database. Using OpenStack, we'll deploy a GitLab CE and two GitLab CI runners, using different storage solutions. We'll see how we can integrate and automate a Heroku environment. We'll end this chapter with a powerful and scalable Docker Swarm cluster on bare metal using Packet, capable of scaling hundreds of containers.

Note

The Terraform version in use for this book...

Provisioning a complete CoreOS infrastructure on Digital Ocean with Terraform


In this recipe, we'll build from scratch a fully working CoreOS cluster on Digital Ocean in their New York region, using Terraform and cloud-init. We'll add some latency monitoring as well with StatusCake, so we have a good foundation of using Terraform on Digital Ocean.

Getting ready

To step through this recipe, you will need the following:

  • A working Terraform installation

  • A Digital Ocean account

  • A StatusCake account

  • An Internet connection

How to do it…

Let's start by creating the digitalocean provider (it only requires an API token) in a file named providers.tf:

provider "digitalocean" {
  token = "${var.do_token}"
}

Declare the do_token variable in a file named variables.tf:

variable "do_token" {
  description = "Digital Ocean Token"
}

Also, don't forget to set it in a private terraform.tfvars file:

do_token = "a1b2c3d4e5f6"

Handling the SSH key

We know that we'll need an SSH key to log into the cluster members. With Digital...

Provisioning a three-tier infrastructure on Google Compute Engine


We'll provision a ready to use, three-tier, load-balanced web infrastructure on Google Compute Engine, using two CentOS 7.2 servers for the web and one master Google MySQL instance. The MySQL instance will allow connections only from the two web servers (with valid credentials), and all three instances (SQL and HTTP) will be accessible from a single corporate network (our company's network). The topology looks like this:

Getting ready

To step through this recipe, you will need the following:

  • A working Terraform installation

  • A Google Compute Engine account with a project

  • An Internet connection

How to do it…

The first thing we need to do is to get our credentials from the console.

Generating API credentials for a Google project

Navigate to your Google Cloud project, and in the API Manager, select Credentials | Create credentials | Service Account Key. Now choose Compute Engine default service account from the dropdown list, in the JSON...

Provisioning a GitLab CE + CI runners on OpenStack


OpenStack is a very popular open source cloud computing solution. Many providers are based on it, and you can roll your own in your data center. In this example, we'll use the public OpenStack by OVH, located in Montreal, QC (Canada), but we can use any other OpenStack. There're differences in implementation for every custom deployment, but we'll stick with very stable features.

We'll launch one compute instance running Ubuntu LTS 16.04 for GitLab, with a dedicated block device for Docker, and two other compute instances for GitLab CI runners. Security will allow HTTP for everyone, but SSH only for a known IP from our corporate network. To store our builds or releases, we'll create a container, which is in OpenStack terminology—an object storage. The equivalent with AWS S3 is a bucket.

Getting ready

To step through this recipe, you will need the following:

  • A working Terraform installation.

  • An OpenStack account on any OpenStack provider (public...

Managing Heroku apps and add-ons using Terraform


Heroku is a popular Platform-as-a-Service (PaaS), where you have absolutely no control over the infrastructure. But even for such platforms, Terraform can automate and manage things for you, so Heroku can do the rest. We'll create an app (a simple GitHub Hubot: http://hubot.github.com/), but feel free to use your own. On top of this app, we'll automatically plug a Heroku add-on (redis) and deploy everything.

Getting ready

To step through this recipe, you will need the following:

  • A working Terraform installation

  • A Heroku account (https://www.heroku.com/)

  • An optional Slack Token

  • An Internet connection

How to do it…

First things first: we need to define the Heroku provider. It consists of an e-mail address and an API key. Let's create generic variables for that in variables.tf:

variable "heroku_email" {
  default     = "user@mail.com"
  description = "Heroku account email"
}

variable "heroku_api_key" {
  default     = "12345"
  description = "Heroku...

Creating a scalable Docker Swarm cluster on bare metal with Packet


IaaS clouds have been popularized through heavy usage of virtual machines. Recent initiatives are targeting bare metal servers with an API, so we get the best of both worlds—on-demand servers through an API and incredible performance through direct access to the hardware. https://www.packet.net/ is a bare metal IaaS provider (https://www.scaleway.com/ is another) very well supported by Terraform with an awesome global network. Within minutes we have new hardware ready and connected to the network.

We'll build a fully automated and scalable Docker Swarm cluster, so we can operate highly scalable and performant workloads on bare metal: this setup can scale thousands of containers in just a few minutes. This cluster is composed of Type 0 machines (4 cores and 8 GB RAM), for one manager and 2 nodes, totaling 12 cores and 24 GB of RAM, but we can use more performant machines if we want: the same cluster with Type 2 machines will...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Infrastructure as Code (IAC) Cookbook
Published in: Feb 2017Publisher: PacktISBN-13: 9781786464910
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

Authors (2)

author image
Stephane Jourdan

Stephane Jourdan is a passionate infrastructure engineer, enthusiastic entrepreneur, zealous trainer, and continuous learner, working on innovative infrastructures since the early 2000s. He focuses equally on tools and culture, in environments as different as startups, online audio/video media, e-commerce, and semi-conductors. The common point between all these experiences is that success comes with rigor, technical repeatability, communication, and a shared team culture. He co-founded an infrastructure automation consultancy (https://www.linkedin.com/company/green-alto), a web radio (http://phauneradio.com/), a container/serverless platform for developers (https://www.squarescale.com/), and a sound design studio (http://www.tarabust.com/). When Stephane isn't starting or contributing to new open source projects, he's usually found hiking in remote places with his camera.
Read more about Stephane Jourdan

author image
Pierre Pomès

Pierre Pomès is a senior enthusiastic engineer of open source technologies and a Linux adept since 1994. He has been working in the IT industry for the last twenty years mostly in C development, system administration, and security including PCI-DSS. He is currently an architect and a DevOps team leader for Reservit, an online hotel booking engine. He has also contributed to the pfSense project.
Read more about Pierre Pomès