VK Cloud logo
Updated at April 15, 2024   11:13 AM

Creating Kubernetes cluster

The examples of creating different Kubernetes clusters using Terraform are provided below. The cluster creation procedure is described in details in the Creating a cluster with Terraform section.

Before creating a cluster

  1. Check out the available resources and quotas for the region in which you plan to create a cluster. Different quotas can be set up for different regions.

    If you want to increase quotas, contact technical support.

  2. Learn about the features of using Terraform with the Cloud Containers service.

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

    Place the provider settings in the provider.tf Terraform configuration file.

1. Create a file describing the cluster network infrastructure

Create the network.tf Terraform configuration file which describes the cluster network infrastructure:

data "vkcs_networking_network" "extnet" {  name = "internet"}resource "vkcs_networking_network" "k8s" {  name           = "k8s-net"  admin_state_up = true}resource "vkcs_networking_subnet" "k8s" {  name            = "k8s-subnet"  network_id      = vkcs_networking_network.k8s.id  cidr            = "192.168.199.0/24"  dns_nameservers = ["8.8.8.8", "8.8.4.4"]}resource "vkcs_networking_router" "k8s" {  name                = "k8s-router"  admin_state_up      = true  external_network_id = data.vkcs_networking_network.extnet.id}resource "vkcs_networking_router_interface" "k8s" {  router_id = vkcs_networking_router.k8s.id  subnet_id = vkcs_networking_subnet.k8s.id}

2. Create a file describing the Kubernetes cluster

In the examples below, clusters with the following configuration are created:

  • The Moscow region, the GZ1 availability zone.

  • Kubernetes version: 1.25.

  • One STD3-4-8 master node.

  • Two STD2-2-4 worker nodes:

    • with two labels configured:

      • env:test
      • disktype:ssd
    • with two taints configured:

      • taintkey1:taintvalue1: PreferNoSchedule
      • taintkey2:taintvalue2: PreferNoSchedule
  • External public IP address assigned to each cluster.

Select one of the cluster creation examples and create the main.tf Terraform configuration file with the necessary content:

data "vkcs_compute_flavor" "k8s-master-flavor" {    name = "STD3-4-8"}data "vkcs_compute_flavor" "k8s-node-group-flavor" { name = "STD2-2-4"}data "vkcs_kubernetes_clustertemplate" "k8s-template" {    version = "1.25"}resource "vkcs_kubernetes_cluster" "k8s-cluster" {  depends_on = [    vkcs_networking_router_interface.k8s,  ]  name                = "k8s-cluster-tf"  cluster_template_id = data.vkcs_kubernetes_clustertemplate.k8s-template.id  master_flavor       = data.vkcs_compute_flavor.k8s-master-flavor.id  master_count        = 1  network_id          = vkcs_networking_network.k8s.id  subnet_id           = vkcs_networking_subnet.k8s.id  availability_zone   = "GZ1"  floating_ip_enabled = true}resource "vkcs_kubernetes_node_group" "k8s-node-group" {  name = "k8s-node-group"  cluster_id = vkcs_kubernetes_cluster.k8s-cluster.id  flavor_id = data.vkcs_compute_flavor.k8s-node-group-flavor.id  node_count = 2  labels {        key = "env"        value = "test"    }  labels {        key = "disktype"        value = "ssd"    }  taints {        key = "taintkey1"        value = "taintvalue1"        effect = "PreferNoSchedule"    }  taints {        key = "taintkey2"        value = "taintvalue2"        effect = "PreferNoSchedule"    }}

3. Create the necessary resources using Terraform

  1. Place the created Terraform configuration files provider.tf, network.tf and main.tf in one directory.

  2. Go to this directory.

  3. Run the command:

    terraform init
  4. Run the command:

    terraform apply

    When prompted for confirmation, enter yes.

  5. Wait for the operation to complete.

Delete unused resources

If you no longer need the resources created with Terraform, delete them:

  1. Switch to the directory that contains the Terraform configuration files.

  2. Run the command:

    terraform destroy

    When prompted for confirmation, enter yes.

  3. Wait for the operation to complete.