Minikube 上的 Kubernetes:尽管配置了 Minikube,但仍出现“与服务器 localhost:8080 的连接被拒绝”错误

问题描述 投票:0回答:1

我正在开发一个使用 Terraform、Docker 和 Minikube 来部署 Web 应用程序的项目。我的设置包括以下内容:

用于构建我的 Web 应用程序的 Dockerfile:

FROM nginx:latest

COPY . /usr/share/nginx/html

EXPOSE 80

将应用程序部署到 Minikube 的 Terraform 配置:

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
              }
            }
          }
        }
      }
    }
  }
}

我面临的问题是,运行 terraform initterraform apply 后,一切似乎都执行没有错误。然而,它没有显示我用 HTML、CSS 和 JS 创建的应用程序,而是显示欢迎使用 nginx!,但是当我尝试在 docker 中打开我的页面时,它工作正常。
我的 Minikube 设置似乎也正常工作:

minikube 状态:

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

kubectl config get-contexts 显示正确的上下文:

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

kubectl get 节点按预期工作:

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

我尝试过的:

  • 确保 Minikube 正在运行(minikube 状态)。
  • 切换到正确的上下文(minikube kubectl -- config use-context minikube)。
  • 已验证 kubectl get 节点工作正常。
  • 尝试直接通过 Minikube 运行 kubectl 命令:
  • 验证 Minikube 服务器可访问:
{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {},
  "status": "Failure",
  "message": "forbidden: User \"system:anonymous\" cannot get path \"/\"",
  "reason": "Forbidden",
  "details": {}
}
kubernetes terraform minikube
1个回答
0
投票

您好,请确保运行以下命令来获取您服务的有效

minikube
url,然后使用
curl
命令联系该服务,在我的测试过程中,它按预期工作,请在下面找到证据。

$ 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
© www.soinside.com 2019 - 2024. All rights reserved.