Search code examples
azurekubernetesterraformazure-aks

How to fix Azure Kubernetes Services with Terraform 'error dial tcp 127.0.0.1:80: connect: connection refused'?


What is the cause of 'terraform apply' giving me the error below on my local machine? It seems to run fine on the build server.

I've also checked the related stackoverflow messages:

  • Windows Firewall is disabled, thus 80 is allowed on the private network
  • config_path in AKS is not used, no kubeconfig seems to be configured anywhere
Plan: 3 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

kubernetes_namespace.azurevotefront-namespace: Creating...
kubernetes_service.azurevotefront-metadata: Creating...
kubernetes_deployment.azurevotefront-namespace: Creating...
╷
│ Error: Post "http://localhost/api/v1/namespaces": dial tcp 127.0.0.1:80: connect: connection refused   
│
│   with kubernetes_namespace.azurevotefront-namespace,
│   on kubernetes.tf line 1, in resource "kubernetes_namespace" "azurevotefront-namespace":
│    1: resource "kubernetes_namespace" "azurevotefront-namespace" {
│
╵
╷
│ Error: Failed to create deployment: Post "http://localhost/apis/apps/v1/namespaces/azurevotefront-namespace/deployments": dial tcp 127.0.0.1:80: connect: connection refused
│
│   with kubernetes_deployment.azurevotefront-namespace,
│   on main.tf line 1, in resource "kubernetes_deployment" "azurevotefront-namespace":
│    1: resource "kubernetes_deployment" "azurevotefront-namespace" {
│
╵
╷
│ Error: Post "http://localhost/api/v1/namespaces/azurevotefront-namespace/services": dial tcp 127.0.0.1:80: connect: connection refused
│
│   with kubernetes_service.azurevotefront-metadata,
│   on main.tf line 47, in resource "kubernetes_service" "azurevotefront-metadata":
│   47: resource "kubernetes_service" "azurevotefront-metadata" {

Kubernetes.tf

resource "kubernetes_namespace" "azurevotefront-namespace" {
  metadata {
    annotations = {
      name = "azurevotefront-annotation"
    }

    labels = {
      mylabel = "azurevotefront-value"
    }

    name = "azurevotefront-namespace"
  }
}

Provider.tf

terraform {
  backend "azurerm" {
    key = "terraform.tfstate"
    resource_group_name = "MASKED"
    storage_account_name = "MASKED"
    access_key = "MASKED"
    container_name = "MASKED"
  }
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~>2.68"
    }
    kubernetes = {
      source  = "hashicorp/kubernetes"
      version = "~> 2.4"
    }
  }
}

provider "azurerm" {
  tenant_id = "MASKED"
  subscription_id = "MASKED"
  client_id = "MASKED"
  client_secret = "MASKED"
  features {}
}


Solution

  • as mentioned in the comments you are missing the kubernetes provider config:

    provider "kubernetes" {
      host                   = azurerm_kubernetes_cluster.aks.kube_admin_config.0.host
      client_certificate     = base64decode(azurerm_kubernetes_cluster.aks.kube_admin_config.0.client_certificate)
      client_key             = base64decode(azurerm_kubernetes_cluster.aks.kube_admin_config.0.client_key)
      cluster_ca_certificate = base64decode(azurerm_kubernetes_cluster.aks.kube_admin_config.0.cluster_ca_certificate)
    }