无法从浏览器访问kubernetes NodePort服务

Unable to access kubernetes NodePort service from browser

我在 AWS EC2 上通过 MinikubeKubernetes 上进行练习。作为其中的一部分,我创建了 deployment 并通过 NodePort 服务公开了它,然后检查了:

curl http://<node-ip>:<service-port>

在 EC2 机器中运行良好。但是当我在浏览器上点击相同的 URL 时,给了我:

This site can't be reached

谁能帮我解决问题,我该如何访问它?

谢谢。

这是我的部署 YAML 文件:


apiVersion: apps/v1
kind: Deployment
metadata:
  name: myfirstdeployment
  labels:
    app: web
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      name: myfirstpod
      labels:
        app: web
    spec:
      containers:
      - name: myfirstpod-1
        image: nginx
        command: ["sleep","3600"]
        ports:
        - containerPort: 80

这是我的服务 YAML 文件


apiVersion: v1
kind: Service
metadata:
  name: myfirstservice
spec:
  selector:
    app: web
  ports:
  - targetPort: 80 #target container's port
    port: 80 #service port
    nodePort: 30030 #node port that we access to
  type: NodePort

我强烈建议阅读显示 Minikube 中 Accessing apps 选项的官方教程:

How to access applications running within minikube There are two major categories of services in Kubernetes:

  • NodePort
  • LoadBalancer

minikube supports either. Read on!

在那里你会找到如何使用两者,NodePort access:

A NodePort service is the most basic way to get external traffic directly to your service. NodePort, as the name implies, opens a specific port, and any traffic that is sent to this port is forwarded to the service.

注意这里必须使用minikube ip

还有 LoadBalancer access:

A LoadBalancer service is the standard way to expose a service to the internet. With this method, each service gets its own IP address.

此方法使用minikube tunnel命令。

也可以用these docs作为补充。