VK Cloud logo
Updated at April 15, 2024   08:50 AM

Creating a cluster with Terraform

Description of how to create a cluster using Terraform is provided below. It is also possible to create a cluster via VK Cloud personal account.

Ready-to-use examples of configuration files to create different clusters are listed in the Terraform section.

Before creating cluster

  1. Familiarize yourself with the available resources and quotas for the region in which you plan to create the cluster. Different quotas may be configured for different regions.

    If you want to increase the quotas, write to technical support.

  2. Familiarize yourself with Terraform features in the container service.

  3. Install Terraform and configure the provider if not already done.

  4. Install the OpenStack CLI and authorize, if not already done.

  5. Create a Terraform configuration file.

1. Prepare the necessary data sources

  1. Determine what type of virtual machine will be used for the cluster master nodes:

    1. Run the command:

      openstack flavor list

      The available virtual machine types will be displayed.

    2. Select the desired virtual machine type and write its name from the Name column.

  2. Determine the version of Kubernetes you want to create the cluster with:

    1. Add the following lines to the configuration file:

      data "vkcs_kubernetes_clustertemplates" "k8s-template-list" {}output "k8s-version-list" {    value = data.vkcs_kubernetes_clustertemplates.k8s-template-list.cluster_templates.*.version}
    2. Run the command:

      terraform refresh
    3. Run the command:

      terraform output k8s-version-list

      A list of available Kubernetes versions will be displayed.

    4. Select the necessary Kubernetes version and write down its version number.

  3. Add data sources to the configuration file:

    1. Virtual machine template for master nodes. Example:

      data "vkcs_compute_flavor" "k8s-master-flavor" {    name = "STD3-2-6"}

      As the name of the template, specify the name obtained earlier.

    2. Cluster template. Example:

      data "vkcs_kubernetes_clustertemplate" "k8s-template" {    version = "<version of Kubernetes>"}

      As a version, specify the version number obtained earlier.

2. Describe the cluster configuration

Add the cluster resource to the configuration file:

resource "vkcs_kubernetes_cluster" "k8s-cluster" {  name                = "k8s-cluster"  cluster_template_id = data.vkcs_kubernetes_clustertemplate.k8s-template.id  master_flavor       = data.vkcs_compute_flavor.k8s-master-flavor.id  master_count        = <number of master nodes>  network_id          = "<network ID>"  subnet_id           = "<subnet ID>"  availability_zone   = "<availability zone>"  floating_ip_enabled = <true or false: whether to assign public IP address to the cluster's API endpoint>  labels = {    # Necessary addons    docker_registry_enabled = true    prometheus_monitoring = true    ingress_controller="nginx"  }}

Some clarification:

  • The number of master nodes master_count must be an odd number (1, 3, 5, and so on). See Architecture for details.

  • The network_id and subnet_id identifiers can be specified in different ways:

    If the required network and subnet already exist and you know their identifiers, specify the identifiers explicitly.

    Example:

    resource "vkcs_kubernetes_cluster" "k8s-cluster" {  name                = "k8s-cluster"  ...  network_id          = "sample-id-4212-a090-9f30519275e5"  subnet_id           = "sample-id-4bd6-bda4-f66dc7fbaa4f"  ...}
  • For the Moscow region, specify one of three availability zones in the availability_zone parameter: ME1, MS1 or GZ1.

  • It is recommended to assign a public IP address to the cluster when creating it, so that you can access the cluster from the Internet (floating_ip_enabled = true). To assing such an IP address, it is necessary for the subnet with the subnet_id identifier to be connected to the router which has access to the external network.

  • If some of the addons are not needed, delete the corresponding lines from the labels block. See Addons for details.

3. Describe the configuration of one or more worker node groups

This operation is described in detail in Worker node group management.

4. Run the cluster creation procedure

  1. Check the Terraform configuration file for correctness:

    terraform validate
  2. Familiarize yourself with the planned changes:

    terraform plan
  3. Apply the planned changes:

    terraform apply

    This will start creating the Kubernetes cluster. This process can take a long time, depending on the size of the cluster.

What's next?