skip to Main Content

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:

  • Ensured Minikube is running (minikube status).
  • Switched to the correct context (minikube kubectl — config use-context minikube).
  • Verified kubectl get nodes works fine.
  • Attempted to run kubectl commands directly via Minikube:
  • Verified the Minikube server is reachable:
{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {},
  "status": "Failure",
  "message": "forbidden: User "system:anonymous" cannot get path "/"",
  "reason": "Forbidden",
  "details": {}
}

2

Answers


  1. Chosen as BEST ANSWER

    Problem 1: "The connection to the server localhost:8080 was refused"

    This error indicated that kubectl was trying to connect to an incorrect Kubernetes API server address (localhost:8080). Hereโ€™s how I fixed it:

    Verified that Minikube is running:

    minikube status
    

    If Minikube was not running, I started it with:

    minikube start
    

    Updated the kubectl configuration:

    kubectl config use-context minikube
    

    Set the Minikube Docker environment:

    eval $(minikube docker-env)
    docker build -t todo-app .
    

    Problem 2: "Welcome to nginx" instead of my application

    Updated the Deployment in Kubernetes to use the local image: In main.tf, I added image_pull_policy = "IfNotPresent":

    resource "kubernetes_deployment" "app" {
      spec {
        template {
          spec {
            container {
              name  = "my-app-container"
              image = "todo-app"
              image_pull_policy = "IfNotPresent"
            }
          }
        }
      }
    }
    

    Restarted the Deployment: To force Kubernetes to refresh the pods with the updated image:

    kubectl rollout restart deployment app-deployment
    

    And opened the page, and my own application showed up


  2. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search