带有入口示例的 Minikube 不起作用

Minikube with ingress example not working

我正在尝试让入口控制器在 Minikube 中工作,并按照 K8s 文档中的步骤进行操作 here,但是我看到了不同的结果,因为入口控制器的 IP 地址与对于 Minikube(该示例似乎表明它们应该相同):

$ kubectl get ingress
NAME              HOSTS              ADDRESS     PORTS   AGE
example-ingress   hello-world.info   10.0.2.15   80      12m

$ minikube ip
192.168.99.101

当我尝试连接到 Minikube IP 地址(直接使用该地址与将其添加到我的本地主机文件)时,我收到来自 NGINX 的 "Not found" 响应:

$ curl http://`minikube ip`/
<html>
    <head><title>404 Not Found</title></head>
    <body>
        <center><h1>404 Not Found</h1></center>
        <hr><center>openresty/1.15.8.1</center>
    </body>
</html>

当我尝试连接到与入口控制器关联的 IP 地址时,它只是挂起。

我应该期望地址与 K8s 文档指示的相同吗?

一些附加信息:

$ kubectl get nodes -o wide
NAME       STATUS   ROLES    AGE     VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE              KERNEL-VERSION   CONTAINER-RUNTIME
minikube   Ready    master   2d23h   v1.16.0   10.0.2.15     <none>        Buildroot 2018.05.3   4.15.0           docker://18.9.9

$ kubectl get ingresses example-ingress -o yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"networking.k8s.io/v1beta1","kind":"Ingress","metadata":{"annotations":{"nginx.ingress.kubernetes.io/rewrite-target":"/"},"name":"example-ingress","namespace":"default"},"spec":{"rules":[{"host":"hello-world.info","http":{"paths":[{"backend":{"serviceName":"web","servicePort":8080},"path":"/"}]}}]}}
    nginx.ingress.kubernetes.io/rewrite-target: /
  creationTimestamp: "2019-10-28T15:36:57Z"
  generation: 1
  name: example-ingress
  namespace: default
  resourceVersion: "25609"
  selfLink: /apis/extensions/v1beta1/namespaces/default/ingresses/example-ingress
  uid: 5e96c378-fbb1-4e8f-9738-3693cbce7d9b
spec:
  rules:
  - host: hello-world.info
    http:
      paths:
      - backend:
          serviceName: web
          servicePort: 8080
        path: /
status:
  loadBalancer:
    ingress:
    - ip: 10.0.2.15

我已经在 Linux 环境(在 GCP 上)中重现了您的场景,我也有不同的 IP:

user@bf:~$ minikube ip
192.168.39.144

user@bf:~$ kubectl get ingresses
NAME              HOSTS   ADDRESS           PORTS   AGE
example-ingress   *       192.168.122.173   80      30m

您的问题与您拥有不同的 IP 无关。该指南指示我们使用以下规则创建入口:

spec:
  rules:
  - host: hello-world.info

此规则告诉入口服务需要具有 hello-world.info 名称的 DNS 记录。 如果您进一步按照指南进行操作,它会指示您在主机文件上创建一个条目,指向您的入口 IP 或 Minikube IP。

Note: If you are running Minikube locally, use minikube ip to get the external IP. The IP address displayed within the ingress list will be the internal IP.
Source: Set up Ingress on Minikube with the NGINX Ingress Controller

(如果您想卷曲 IP 而不是 DNS 名称,则需要从入口中删除主机规则)

它应该是这样的:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
     paths:
     - path: /
       backend:
         serviceName: web
         servicePort: 8080

应用您的更改:

user@bf:~$ kubectl apply -f example-ingress.yaml

并使用 -Lk 选项卷曲 IP 以解决与安全连接相关的问题。

user@bf:~$ curl -Lk 192.168.39.144
Hello, world!
Version: 1.0.0
Hostname: web-9bbd7b488-l5gc9

除了已接受的答案外,minikube 现在有一个 tunnel 命令,它允许您为您的服务生成外部 IP 地址,可以直接在您的主机上访问这些地址,而无需使用一般 minikube ip.

运行 minikube tunnel 在单独的终端中。它作为守护进程在前台运行。 在不同的终端中,执行 kubectl apply -f <file_name> 命令以部署所需的服务。它应该为您生成一个 IP 地址,该地址直接路由到您的服务,并可在该地址的端口 80 上使用。

关于 minikube 文档的更多信息:https://minikube.sigs.k8s.io/docs/tasks/loadbalancer/