Skip to main content

Introduction

In the world of cloud-native applications, Kubernetes has become the de facto standard for container orchestration. However, setting up and managing Kubernetes clusters can be a complex task, especially in a dynamic cloud environment like OpenStack. Automating this process not only saves time but also ensures consistency across environments. In this post, we’ll explore how to automatically provision and create Kubernetes clusters using OpenStack and Rancher, making the entire process seamless and efficient.

Prerequisites

Before we dive into the automation process, make sure you have the following:

  1. OpenStack Environment: A running OpenStack instance with necessary permissions to create networks, VMs, and security groups.
  2. Rancher: A Rancher server instance (version 2.0 or later) set up and accessible.
  3. Terraform: For infrastructure as code (IaC) to manage OpenStack resources.
  4. kubectl: Command-line tool for Kubernetes.

Step 1: Set Up OpenStack Infrastructure Using Terraform

The first step is to automate the creation of the infrastructure on OpenStack, which includes VMs, networks, and security groups. Terraform is an excellent tool for this purpose.

  1. Install Terraform:
    • If not already installed, download and install Terraform from the official website.
  2. Create a Terraform Configuration File:
    • Create a file named main.tf in your working directory. Define the OpenStack provider and resources like networks, security groups, and instances.
    • Example configuration:
hcl
provider "openstack" {
auth_url = "https://openstack.example.com:5000/v3"
tenant_name = "your-tenant-name"
user_name = "your-username"
password = "your-password"
region = "RegionOne"
}

resource "openstack_networking_network_v2" "k8s_network" {
name = "k8s-network"
}

resource "openstack_compute_instance_v2" "k8s_master" {
name = "k8s-master"
image_id = "your-image-id"
flavor_id = "your-flavor-id"
key_pair = "your-keypair"
network {
uuid = "${openstack_networking_network_v2.k8s_network.id}"
}
}

resource "openstack_compute_instance_v2" "k8s_worker" {
count = 3
name = "k8s-worker-${count.index}"
image_id = "your-image-id"
flavor_id = "your-flavor-id"
key_pair = "your-keypair"
network {
uuid = "${openstack_networking_network_v2.k8s_network.id}"
}
}

  1. Initialize and Apply Terraform Configuration:
    • Run terraform init to initialize the configuration.
    • Run terraform apply to create the infrastructure. Review and confirm the changes.

Step 2: Install Rancher on OpenStack

Rancher provides a powerful and intuitive interface for managing Kubernetes clusters. Deploying Rancher in your OpenStack environment can be done as follows:

  1. Deploy Rancher using Helm:
    • Install Helm if it’s not already installed:
      bash
      curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
    • Add the Rancher Helm repository and install Rancher:
      bash
      helm repo add rancher-latest https://releases.rancher.com/server-charts/latest
      helm repo update

      helm install rancher rancher-latest/rancher \
      --namespace cattle-system \
      --create-namespace \
      --set hostname=rancher.yourdomain.com \
      --set replicas=1

  2. Access Rancher:
    • Once Rancher is deployed, access it using the provided hostname. Set up the admin user and secure the installation.

Step 3: Automate Kubernetes Cluster Creation in Rancher

With Rancher running, you can now automate the creation of Kubernetes clusters:

  1. Create a Cloud Credential for OpenStack in Rancher:
    • Navigate to the Rancher UI, go to Global Settings > Cloud Credentials.
    • Add a new credential for OpenStack, providing the necessary details like Auth URL, Username, Password, Tenant Name, etc.
  2. Define a Cluster Template in Rancher:
    • Go to Global Settings > Cluster Management > Cluster Templates.
    • Create a new template for your Kubernetes cluster, specifying the OpenStack credential and any custom configurations (e.g., number of nodes, VM flavors).
  3. Automate Cluster Creation:
    • Use Rancher’s API or CLI to automate the creation of clusters based on the defined template.
    • Example API call using curl:
      bash
      curl -u "username:password" -X POST -H "Content-Type: application/json" \
      -d '{"type":"cluster","name":"k8s-cluster","clusterTemplateId":"cattle-global-nt:template-id"}' \
      "https://rancher.yourdomain.com/v3/clusters"

Step 4: Post-Deployment Configuration

After the cluster is created, you might need to configure networking, storage, and other integrations:

  1. Set Up Networking (Optional):
    • If using Calico or other CNI plugins, configure networking according to your needs.
  2. Integrate Storage (Optional):
    • Set up persistent storage using OpenStack Cinder or other storage backends.
  3. Deploy Applications:
    • Use Rancher’s UI or kubectl to deploy applications on your new Kubernetes cluster.

Conclusion

By leveraging OpenStack, Rancher, and Terraform, you can automate the provisioning and creation of Kubernetes clusters, streamlining the process and ensuring consistency across deployments. This approach not only saves time but also allows for scalable and repeatable deployments, making it ideal for both development and production environments.