Site icon Ron Ekins' – Oracle Technology, DevOps and Kubernetes Blog

Add storage to an OCI Oracle Kubernetes Engine (OKE) Cluster with Terraform

Introduction

So you have created your OKE cluster using the Oracle Cloud Infrastructure (OCI) WebUI, Terraform or maybe by following my Provisioning an Oracle Kubernetes Engine (OKE) cluster with Rancher. And now you need to add some additional block storage to each compute instance.

In this post I will show how we can use Terraform to automate this process.

Terraform for Oracle Cloud Infrastructure

The HashiCorp Terraform Registry includes documented providers for Cloud platforms including Oracle Cloud Infrastructure.

I have previously shared how can automate the deployment of an OKE cluster with Terraform in this post I will use some of the same and a few additional OCI modules to automate this process.

OKE Compute Instances

Before we start let’s logon to to OCI to review my OKE Cluster, from the hamburger menu navigate to Compute -> Instances.

From here you can see my OKE cluster is made up of 3 compute instances running in the 3 Availability Domains within my Region

Compute Instances

Terraform Process

My terraform script identifies the OKE compute instances, creates block volumes in their Availability Domain and then attaches the volumes to the compute instance.

The script uses the oci_core_volume and oci_core_volume_attachment modules to create and attach block storage volumes, let’s see in in action.

Terraform Init

  1. Run terraform init command to initialise working directory.
% terraform init

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/oci from the dependency lock file
- Using previously-installed hashicorp/oci v4.19.0

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Terraform Validate

2. Use terraform validate command to check the local configuration files.

% terraform validate
Success! The configuration is valid.

Terraform Plan

3. Use terraform plan to read current state and report actions which will be performed.

% terraform plan

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:
...
Changes to Outputs:
  + all-created-volume-names             = [
      + "oke-cfmmsx4tmza-na26kbyk67q-sunbst336ca-0",
      + "oke-cfmmsx4tmza-na26kbyk67q-sunbst336ca-1",
      + "oke-cfmmsx4tmza-na26kbyk67q-sunbst336ca-2",
    ]
  + all-created-volumes                  = [
      + (known after apply),
      + (known after apply),
      + (known after apply),
    ]
  + all-running-oke-availability-domains = [
      + "iXvO:UK-LONDON-1-AD-1",
      + "iXvO:UK-LONDON-1-AD-2",
      + "iXvO:UK-LONDON-1-AD-3",
    ]
  + all-running-oke-instance-names       = [
      + "oke-cfmmsx4tmza-na26kbyk67q-sunbst336ca-0",
      + "oke-cfmmsx4tmza-na26kbyk67q-sunbst336ca-1",
      + "oke-cfmmsx4tmza-na26kbyk67q-sunbst336ca-2",
    ]
  + number-of-oke-instances              = 3

Terraform Apply

4 . Use terraform apply to changes

% terraform apply 

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:
...

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes
...

Apply complete! Resources: 6 added, 0 changed, 0 destroyed.

Outputs:

all-created-volume-names = [
  "oke-cfmmsx4tmza-na26kbyk67q-sunbst336ca-0",
  "oke-cfmmsx4tmza-na26kbyk67q-sunbst336ca-1",
  "oke-cfmmsx4tmza-na26kbyk67q-sunbst336ca-2",
]
all-created-volumes = [
  "ocid1.volume.oc1.uk-london-1.abwgiljrw7jrr4mtg67otf766qyvqgocf65qqvsc4ugye5qcj7ywchzhpgaa",
  "ocid1.volume.oc1.uk-london-1.abwgiljty7wrc232v4ltxmviav2avkjbgizqtc4yo7psbqmjwdx4usvfecwq",
  "ocid1.volume.oc1.uk-london-1.abwgiljs6golqjiksnsnb7xx3xqza72foajfruy3htcfej5h46zwsyg3a23a",
]
all-running-oke-availability-domains = tolist([
  "iXvO:UK-LONDON-1-AD-1",
  "iXvO:UK-LONDON-1-AD-2",
  "iXvO:UK-LONDON-1-AD-3",
])
all-running-oke-instance-names = tolist([
  "oke-cfmmsx4tmza-na26kbyk67q-sunbst336ca-0",
  "oke-cfmmsx4tmza-na26kbyk67q-sunbst336ca-1",
  "oke-cfmmsx4tmza-na26kbyk67q-sunbst336ca-2",
])
number-of-oke-instances = 3

Oracle Cloud Infrastructure

Returning to OCI and navigating to Storage -> Block Volumes

We can now see we have created three 100GB block volumes in the three different Availability Domains.

Block Volumes

If we follow the link on the Name we can see that the storage has also been attached to OKE compute instance within the Availability Domain.

Terraform Destroy

If you no longer need the block storage we can remove and delete the storage using terraform destroy.

% terraform destroy
...
Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes
...
Destroy complete! Resources: 6 destroyed.

Summary

In this post I have shared how we can add additional storage to an OKE compute instance using Terraform.

In my next post I will return to Rancher and use the environment we created in Provisioning an Oracle Kubernetes Engine (OKE) cluster with Rancher and configure Portworx Essentials to use the storage we just created.


twitter-follow screen_name=’RonEkins’ show_count=’yes’]

Exit mobile version