Automate Kubernetes Infrastructure with IBM Cloud Schematics

5 min read

By: Sai Vennam

Every developer should automate their infrastructure—here’s why.

Imagine you want to start writing an application on Kubernetes. You go to your favorite cloud, spin up a cluster, choose all the config options you need. Over the next few weeks, you iteratively develop your application and add more pieces to the puzzle, like third-party service integrations and a database. After a while, you decide it’s time to move this into production.

You know the best approach is to create a new production environment and keep “dev” separate. Although your code has changed drastically, its history is neatly kept in GitHub—no issues there. But what about your Kubernetes cluster, attached databases, service integrations, and volume claims? Are you certain that you can recreate the state of your dev environment’s infrastructure?

Here’s where Infrastructure as Code (IaC) comes in—it allows you to codify your cloud environments so you can automate provisioning of your infrastructure. To learn more about IaC, check out my video below, and if you want to get hands-on with, keep scrolling down.

IBM Cloud Schematics (Terraform-as-a-Service)

IBM Cloud Schematics is IBM’s cloud automation tool. The best thing about it? It’s based entirely on open source Terraform! See the blog post "IBM Cloud Schematics: Enabling Infrastructure as Code" for more info on the offering.

Today, let’s deploy a simple Kubernetes cluster on IBM Cloud using Schematics.

Navigate to the Schematics Dashboard and click Create a workspace.

Choose a name for your workspace, and input the GitHub repository URL: https://github.com/svennam92/ibmcloud-terraform

Note: You could also fork this if you want your own repo.

Navigate to the Schematics Dashboard and click Create a workspace.

When you hit Retrieve input variables, it’ll pull the Terraform files from GitHub and you’ll see a list of configurable variables. As you input these variables, let’s break down the main.tf Terraform file that Schematics is using to automate the infrastructure.

Using the IBM Cloud provider for Terraform

The first thing in the main.tf Terraform file is the provider setting for using IBM Cloud:

provider "ibm" {
  ibmcloud_api_key = "${var.ibmcloud_api_key}"
}

As part of this, you’ll need to configure the API key that Terraform will use to authenticate with IBM Cloud. Create an API key using the API Keys dashboard and copy it into your Schematics workspace creation.

For each Terraform variable, Schematics allows you to configure it directly in the creation flow.

For each Terraform variable, Schematics allows you to configure it directly in the creation flow.

Next, you’ll need to configure the Kubernetes cluster resource you’re creating. A full reference is available in the docs, but the following is the basic required resource config:

# The Kubernetes Cluster Resource
resource "ibm_container_cluster" "cluster" {
  name              = "${var.cluster_name}"
  datacenter        = "${var.datacenter}"
  hardware          = "${var.hardware}"
  default_pool_size = "${var.poolsize}"
  machine_type      = "${var.machine_type}"
  public_vlan_id    = "${var.public_vlan_id}"
  private_vlan_id   = "${var.private_vlan_id}"
  kube_version      = "${var.kube_version}"
}

Defaults are available for most fields, but you will need to manually configure the VLANs.

Note: If you’ve never created a cluster before, these VLANs are created for you, and you can simply input null.

Once completed, it should look something like this:

Defaults are available for most fields, but you will need to manually configure the VLANs.

To customize most of these fields, you may want help from the CLI. Download the IBM Cloud CLI here.

Finally, hit Create to provision the Schematics workspace.

Navigating IBM Cloud Schematics

Schematics allows you to fully manage your Terraform-based infrastructure automation. This includes making changes to the configuration, getting the latest Terraform config from GitHub, tracking provisioned resources, and finding resources to get started. Joining our Slack is a great way to have your questions answered or raise any issues:

Schematics allows you to fully manage your Terraform-based infrastructure automation.

With Terraform, there’s a “planning” stage before you start provisioning your infrastructure. This lets you ensure all the knobs are in the right position before launching. To do so, hit Generate Plan:

With Terraform, there’s a “planning” stage before you start provisioning your infrastructure. This lets you ensure all the knobs are in the right position before launching. To do so, hit Generate Plan:

Verify the logs:

Verify the logs.

Once you’ve verified the infrastructure that will be provisioned, hit Apply Plan. It generally takes around 20 minutes while your cluster is being provisioned and set-up:

ks7

That’s it! What’s next?

Once completed, you can access your Kubernetes cluster directly from your Schematics workspace or by navigating to your IBM Cloud Kubernetes Dashboard. You’ve successfully “codified” and automated the creation of a simple Kubernetes cluster. 

But, we’re really just scratching the surface for using Terraform in Schematics. Stay tuned for more content where I’ll show how to set up networking, service creation/binding, VPCs, and more. 

Be the first to hear about news, product updates, and innovation from IBM Cloud