4

我在 VMware 的 k8s 集群中部署了一个入口。问题是 DNS 在容器内响应:

[root@k8s-cluster ~]# kubectl exec -it -n test ingress-nginx-controller-848bfcb64d-n796z  -- curl localhost/banana -H 'Host: k8s-cluster.lab.buch.int'
banana

但是,如果我从集群中尝试:

[root@k8s-cluster ~]# curl localhost/banana
curl: (7) Failed connect to localhost:80; Connection refused

即使我使用 DNS 而不是 localhost 或 metallb 分配给入口控制器的 IP,它也不起作用。

[root@k8s-cluster ~]# kubectl get services
NAME                                 TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
apple-service                        ClusterIP      10.97.109.66     <none>        5678/TCP                     6d15h
banana-service                       ClusterIP      10.110.129.29    <none>        5678/TCP                     6d15h
ingress-nginx-controller             LoadBalancer   10.99.151.233    10.133.2.21   80:31981/TCP,443:30293/TCP   6d15h
ingress-nginx-controller-admission   ClusterIP      10.108.116.127   <none>        443/TCP                      6d15h
[root@k8s-cluster ~]# kubectl describe ing
Warning: extensions/v1beta1 Ingress is deprecated in v1.14+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
Name:             example-ingress
Namespace:        test
Address:          10.133.65.148
Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
  Host                            Path  Backends
  ----                            ----  --------
  k8s-cluster.lab.buch.int
                                  /apple(/|$)(.*)    apple-service:5678 (192.168.86.184:5678)
                                  /banana(/|$)(.*)   banana-service:5678 (192.168.76.157:5678)
Annotations:                      kubernetes.io/ingress.class: nginx
                                  nginx.ingress.kubernetes.io/rewrite-target: /$2
Events:                           <none>

如果我 curl 由 metallb 分配的外部 ip: 10.133.2.21

[root@k8s-cluster ~]# curl 10.133.2.21
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>

所以,这意味着 nginx 以某种方式工作,但如果我尝试 curl 10.133.2.21/banana 或 10.133.2.21/apple 它不起作用。

有任何想法吗?

4

1 回答 1

0

在您的Ingress配置中,您指定了 k8s-cluster.lab.buch.int主机,因此您可以使用此特定主机名访问apple和服务:banana

curl k8s-cluster.lab.buch.int/apple

curl k8s-cluster.lab.buch.int/banana

解决方案

如果您将Ingress主机更改为*(所有主机)这是默认配置,或者从您的 YAML 中删除此字段。

...
rules:
- host: "k8s-cluster.lab.buch.int" ### Here you can change to "*" or remove this field
  http:
    paths:
    - path: "/apple(/|$)(.*)"
      backend:
...

通过上述更改,您将能够使用LoadBalancerIP 地址访问您的服务:

$ curl 10.133.2.21/apple

$ curl 10.133.2.21/banana

正如您在 Kubernetes Ingress 规则文档中所读到的:

在负载均衡器将流量定向到引用的服务之前,主机和路径都必须匹配传入请求的内容。

此外,404 Not Found当您尝试$ curl 10.133.2.21并且$ curl k8s-cluster.lab.buch.int是预期的行为时,因为您没有设置default-backend

默认后端:default-http-backend:80(<错误:未找到端点“default-http-backend”>)

于 2021-01-11T13:31:26.550 回答