Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Terraform (henceforth TF) is an open-source infrastructure as code software tool that provides a consistent CLI workflow to manage hundreds of cloud services. Terraform codifies cloud APIs into declarative configuration files. For this page we refer to this GitHub project.

Requirements

All the files and folders used here come from the same GitHub repository cloned in the KubeSpray chapter. That said, the necessary prerequisites are as follows:

  • Install TF; 
  • Install Ansible;
  • you already have a floating IP pool created;
  • you have security groups enabled;
  • you have a pair of keys generated that can be used to secure the new hosts.

Configuration

Inventory files

Create a directory for your cluster, mycluster for istance, by copying the existing sample-inventory and linking the hosts script, used to build the inventory based on TF state (this will be the base for subsequent Terraform commands).

Code Block
languagebash
titleInventory files
collapsetrue
# The following commands must be launched from inside the "kubespray" folder.
$ cp -LRp contrib/terraform/openstack/sample-inventory inventory/mycluster
$ cd inventory/mycluster
$ ln -s ../../contrib/terraform/openstack/hosts
$ ln -s ../../contrib

OpenStack access and credentials

TF supports various authentication methods for OpenStack. The recommended authentication method is to describe credentials in a YAML file clouds.yamlthat can be stored in the current directory (i.e. mycluster)

...

Code Block
languagebash
titleenvironment variable OS_CLOUD
collapsetrue
# Insert this line in the .bashrc file for variable persistence
export OS_CLOUD=openstack
# To apply the changes
$ source .bashrc

Cluster variables

The construction of the cluster is driven by values found in../../contrib/terraform/openstack/variables.tf. You can consult this file to find out which variables are available for configuration, accompanied by a brief description, which values they accept and their defaults. For your cluster, edit cluster.tfvars. Let's take a look at some parameters in this file, which can serve as an example

...

Note that the Ansible script will report an invalid configuration if you wind up with an even number of etcd instances since that is not a valid configuration. This restriction includes standalone etcd nodes that are deployed in a cluster along with master nodes with etcd replicas. As an example, if you have three master nodes with etcd replicas and three standalone etcd nodes, the script will fail since there are now six total etcd replicas. 

Provisioning VMs

Initialization

Before TF can operate on your cluster you need to install the required plugins. This is accomplished as follows (this should finish fairly quickly telling you TF has successfully initialized and loaded necessary modules)

Code Block
languagebash
titleInitialization
collapsetrue
# Launch from the path inventory/mycluster
$ terraform init ../../contrib/terraform/openstack

Provisioning cluster

You can apply the Terraform configuration to your cluster with the following command issued from the usual path. The same command can be used to apply changes to an existing cluster after modifying the cluster.tfvars configuration file.

...

Info
titleBastion host

If you chose to create a bastion host, this script will create ../../contrib/terraform/openstack/k8s-cluster.yml with an ssh command for Ansible to be able to access your machines tunneling through the bastion's IP address. If you want to manually handle the ssh tunneling to these machines, please delete or move that file. If you want to use this, just leave it there, as ansible will pick it up automatically.

Destroying cluster

You can destroy your new cluster with the following command

Code Block
languagebash
titleDestroying cluster
collapsetrue
$ terraform destroy -var-file=cluster.tfvars ../../contrib/terraform/openstack

Debugging

You can enable debugging output from Terraform by setting OS_DEBUG to 1 and TF_LOG to DEBUG before running the Terraform command.