Skip to content

Deploying a MicroK8s HA cluster on IBM Cloud VPC using Terraform and Ansible

License

Notifications You must be signed in to change notification settings

cloud-design-dev/microk8s-ibmcloud-vpc

Repository files navigation

Deploy a microk8s cluster on IBM Cloud VPC 🚀

In this guide we will deploy a testing microk8s cluster in to an IBM Cloud VPC ☁️. MicroK8s allows you to deploy fully functional, highly available Kubernetes clusters on a wide variety of infrastructure and platforms. With the ability to quickly toggle features on and off, microk8s is a great tool for testing and learning Kubernetes.

In this guide we will be using Terraform to provision the VPC infrastructure and Ansible to handle the post provisioning configuration of the the cluster.

Microk8s on IBM Cloud VPC

This guide is written to utilize IBM Cloud Shell as the development environment. Cloud Shell is a free, browser-based shell environment that includes Terraform, Ansible, and the IBM Cloud CLI. If you prefer to use your own environment, see the Local Prerequisites section below to ensure you have all the required software installed.

Prerequisites

To deploy our microk8s cluster, you will need to ensure you have the following prerequisites:

  • IBM Cloud API Key. See here for instructions on how to create one via the Portal. Alternately you can use the following CLI command to create one:

    ibmcloud iam api-key-create "$(whoami)-microk8s-vpc-apikey" -d "API key for microk8s deployment" --file "$(whoami)-microk8s-vpc-apikey.json"
  • Recent version of Terraform installed. This guide was tested on terraform 1.5.3.

  • A recent version of Ansible installed. This guide was tested on ansible 2.15.2.

  • (Optional) - tfswitch installed. The tfswitch utility allows you to run multiple versions of Terraform on the same system. If you do not have a particular version of terraform installed, tfswitch will download the version you select from an interactive menu.

Deploying the cluster

With the prerequisites installed and up to date, you can start deploying the cluster.

  1. Clone this repo and cd into the microk8s-ibmcloud-vpc directory.

    git clone https://github.com/cloud-design-dev/microk8s-ibmcloud-vpc.git
    cd microk8s-ibmcloud-vpc
  2. Copy the tfvars-example to terraform.tfvars file and update the values as needed. See here for full list of variables.:

    cp tfvars-example terraform.tfvars
  3. Initialize Terraform:

    terraform init -upgrade
  4. Run a Terraform plan:

    terraform plan -out "$(terraform workspace show).tfplan" 
  5. Run Terraform apply:

    terraform apply "$(terraform workspace show).tfplan"

Once the cluster is deployed, you should see some output about your deployed infrastructure, as well as the commands for running the Ansible playbooks.

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

Outputs:

bastion_ip = "<your bastion ip>"
step_01_ping_hosts = "ansible-playbook -i ansible/inventory.ini  ansible/playbooks/ping-all.yml"
step_02_update_hosts = "ansible-playbook -i ansible/inventory.ini ansible/playbooks/update-systems.yml"
step_03_deploy_cluster = "ansible-playbook -i ansible/inventory.ini ansible/playbooks/configure-microk8s.yml"

Run Ansible playbooks

  1. Run the ping-all.yml playbook to ensure all hosts are reachable:

    ansible-playbook -i ansible/inventory.ini ansible/playbooks/ping-all.yml
  2. Run the update-systems.yml playbook to update the hosts and insure required packages are installed:

    ansible-playbook -i ansible/inventory.ini ansible/playbooks/update-systems.yml
  3. Finally, run the configure-microk8s.yml playbook to configure our microk8s cluster:

    ansible-playbook -i ansible/inventory.ini ansible/playbooks/configure-microk8s.yml

When the configure-microk8s.yml playbook completes, you can run the following command to get the clusters compute nodes:

ansible -m shell -b -a "microk8s kubectl get nodes" CONTROL_PLANE_NODE -i ansible/inventory.ini 

If everything went as planned you should see output similar to the following:

[$] <git:(main*)> ansible -m shell -b -a "microk8s kubectl get nodes" qywx-controller-1 -i ansible/inventory.ini
qywx-controller-1 | CHANGED | rc=0 >>
NAME                STATUS   ROLES       AGE    VERSION
qywx-worker-1       Ready    worker      7m5s   v1.27.2
qywx-worker-3       Ready    worker      7m4s   v1.27.2
qywx-worker-2       Ready    worker      7m3s   v1.27.2
qywx-controller-1   Ready    contoller   11m    v1.27.2

Running the devcontainer

This repository comes with a devcontainer configuration file. If you would like to use this containerized development environment, ensure you have the following installed:

To reopen this repository in the development container, open the command pallete on VS Code and select Rebuild and Reopen in Container

About

Deploying a MicroK8s HA cluster on IBM Cloud VPC using Terraform and Ansible

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published