Azure Create K8 Cluster

Here is a Terraform file that you can use to create a Kubernetes cluster in Azure:

provider "azurerm" {
  version = "~> 3.70.0"
  subscription_id = var.azure_subscription_id
  client_id = var.azure_client_id
  client_secret = var.azure_client_secret
  tenant_id = var.azure_tenant_id
}

resource "azurerm_resource_group" "aks_cluster" {
  name = var.resource_group_name
  location = var.location
}

resource "azurerm_kubernetes_cluster" "aks_cluster" {
  name = var.aks_cluster_name
  location = azurerm_resource_group.aks_cluster.location
  resource_group_name = azurerm_resource_group.aks_cluster.name
  node_count = 3
  vm_size = "Standard_D2s_v3"
  network_profile {
    kubernetes_network_interface_id = azurerm_network_interface.aks_cluster_nic.id
  }
  default_node_pool {
    name = "default"
    node_count = 3
    vm_size = "Standard_D2s_v3"
  }
}

resource "azurerm_network_interface" "aks_cluster_nic" {
  name = var.aks_cluster_nic_name
  location = var.location
  resource_group_name = azurerm_resource_group.aks_cluster.name

  ip_configuration {
    name = "primary"
    subnet_id = azurerm_subnet.aks_cluster_subnet.id
    address_prefix = "10.0.0.0/24"
  }
}

resource "azurerm_subnet" "aks_cluster_subnet" {
  name = var.aks_cluster_subnet_name
  resource_group_name = azurerm_resource_group.aks_cluster.name
  virtual_network_name = var.virtual_network_name
  address_prefix = "10.0.0.0/24"
}

resource "azurerm_virtual_network" "aks_cluster_vnet" {
  name = var.virtual_network_name
  location = var.location
  resource_group_name = azurerm_resource_group.aks_cluster.name

  address_space = ["10.0.0.0/16"]
}

This Terraform file will create a new Azure resource group, a Kubernetes cluster, a virtual network, and a subnet. The Kubernetes cluster will have three nodes, each of which will be a Standard_D2s_v3 VM. The virtual network and subnet will be created in the same region and resource group as the Kubernetes cluster.

To use this Terraform file, you will need to provide the following variables:

  • azure_subscription_id: Your Azure subscription ID.
  • azure_client_id: Your Azure client ID.
  • azure_client_secret: Your Azure client secret.
  • azure_tenant_id: Your Azure tenant ID.
  • resource_group_name: The name of the resource group that you want to create the Kubernetes cluster in.
  • location: The location of the resource group that you want to create the Kubernetes cluster in.
  • aks_cluster_name: The name of the Kubernetes cluster that you want to create.
  • virtual_network_name: The name of the virtual network that you want to create.
  • aks_cluster_subnet_name: The name of the subnet that you want to create for the Kubernetes cluster.

Once you have provided these variables, you can run the following command to create the Kubernetes cluster:

terraform apply

This command will create the Kubernetes cluster and all of the associated resources. You can then connect to the Kubernetes cluster using the kubectl command-line tool.