kubernetesterraformminikube

Kubernetes on Minikube: "The connection to the server localhost:8080 was refused" error despite Minikube being configured


I'm working on a project using Terraform, Docker, and Minikube to deploy a web application. My setup includes the following:

A Dockerfile to build my web application:

FROM nginx:latest

COPY . /usr/share/nginx/html

EXPOSE 80

A Terraform configuration to deploy the application to Minikube:

terraform {
  required_providers {
    kubernetes = {
      source  = "hashicorp/kubernetes"
      version = ">= 2.11.0"
    }
  }
}

provider "kubernetes" {
  host                   = "https://192.168.49.2:8443"
  cluster_ca_certificate = file("/home/pawel/.minikube/ca.crt")
  client_certificate     = file("/home/pawel/.minikube/profiles/minikube/client.crt")
  client_key             = file("/home/pawel/.minikube/profiles/minikube/client.key")
}

resource "kubernetes_deployment" "app" {
  metadata {
    name = "app-deployment"
    labels = {
      app = "my-app"
    }
  }

  spec {
    replicas = 1

    selector {
      match_labels = {
        app = "my-app"
      }
    }

    template {
      metadata {
        labels = {
          app = "my-app"
        }
      }

      spec {
        container {
          name  = "my-app-container"
          image = "todo-app"
          port {
            container_port = 80
          }
        }
      }
    }
  }
}

resource "kubernetes_service" "app_service" {
  metadata {
    name = "app-service"
  }

  spec {
    selector = {
      app = "my-app"
    }

    port {
      port        = 80
      target_port = 80
    }

    type = "NodePort"
  }
}

resource "kubernetes_ingress_v1" "app_ingress" {
  metadata {
    name = "app-ingress"
    annotations = {
      "nginx.ingress.kubernetes.io/rewrite-target" = "/"
    }
  }

  spec {
    rule {
      host = "my-app.local"

      http {
        path {
          path = "/"
          path_type = "Prefix"

          backend {
            service {
              name = kubernetes_service.app_service.metadata[0].name
              port {
                number = 80
              }
            }
          }
        }
      }
    }
  }
}

The problem I’m facing is that after running terraform init and terraform apply, everything seems to execute without errors. However it doesn't show my app that I've created in HTML,CSS and JS instead it shows Welcome to nginx!, but when I try opeining my page in docker it is working perfectly.
My Minikube setup also appears to be working:

minikube status:

minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured

kubectl config get-contexts shows the correct context:

objectivec
CURRENT   NAME       CLUSTER    AUTHINFO   NAMESPACE
 1.         minikube   minikube   minikube   default

kubectl get nodes works as expected:

NAME       STATUS   ROLES           AGE   VERSION
minikube   Ready    control-plane   22h   v1.31.0

What I've tried:

{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {},
  "status": "Failure",
  "message": "forbidden: User \"system:anonymous\" cannot get path \"/\"",
  "reason": "Forbidden",
  "details": {}
}

Solution

  • Hello please make sure to run the below commands to get the valid minikube url of your service, then use curl command to reach out to the service, during my test it worked as expected, please find the evidence below.

    $ minikube service app-service
        |-----------|-------------|-------------|---------------------------|
        | NAMESPACE |    NAME     | TARGET PORT |            URL            |
        |-----------|-------------|-------------|---------------------------|
        | default   | app-service |          80 | http://192.168.49.2:31089 |
        |-----------|-------------|-------------|---------------------------|
        🎉  Opening service default/app-service in default browser...
        👉  http://192.168.49.2:31089
    
    $ curl http://192.168.49.2:31089
        Hello stackoverflow