Kubernetes NGINX入口重写目标注释被破坏 [英] Kubernetes NGINX ingress rewrite-target annotation breaking

查看:77
本文介绍了Kubernetes NGINX入口重写目标注释被破坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Windows的Docker桌面下运行kubernetes v1.16.我已经使用舵安装了nginx-ingress控制器v1.7.9.我已更新主机文件以包含以下条目

I am running kubernetes v1.16 under docker desktop for windows. I have installed the nginx-ingress controller v1.7.9 using helm. I have update my hosts file to have the following entry

127.0.0.1 application.local

我有一个名为 hedgehog-service 的后端服务.

I have a backend service named hedgehog-service.

以下入口定义将请求正确转发到后端.

The following ingress definition correctly forwards request to the backend.

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ml-zoo-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
  - host: application.local
    http:
      paths:
      - path: /hedgehog/
        backend:
          serviceName: hedgehog-service
          servicePort: 80

curl application.local/hedgehog 可以正常工作,并且可以访问后端服务.

curl application.local/hedgehog works as expected and hits the backend service.

但是,为了正确使用后端服务,我需要重写目标以删除URL前缀/hedgehog .因此,我有以下入口定义

However in order to correctly use the backend service I need to rewrite the target removing the url prefix /hedgehog. Hence I have the following ingress definition

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ml-zoo-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
  - host: application.local
    http:
      paths:
      - path: /hedgehog(/|$)(.*)
        backend:
          serviceName: hedgehog-service
          servicePort: 80

如此处所示: https://kubernetes.github.io/ingress-nginx/examples/rewrite/#rewrite-target

现在,当我 curl application.local/hedgehog/test 时,入口控制器不会与后端通信,但会根据日志尝试打开文件

Now when I curl application.local/hedgehog/test the ingress controller does not communicate with the backend but according to the logs attempts to open a file

2020/06/23 12:46:48 [error] 708#708: *792 open() "/etc/nginx/html/hedgehog/test" failed (2: No such file or directory), client: 192.168.65.3, server: application.local, request: "GET /hedgehog/test HTTP/1.1", host: "application.local"
192.168.65.3 - - [23/Jun/2020:12:46:48 +0000] "GET /hedgehog/test HTTP/1.1" 404 153 "-" "curl/7.65.3" "-"

这是 etc/nginx/conf.d/default-ml-zoo-ingress

# configuration for default/ml-zoo-ingress

upstream default-ml-zoo-ingress-application.local-hedgehog-service-80 {
        zone default-ml-zoo-ingress-application.local-hedgehog-service-80 256k;
        random two least_conn;

        server 10.1.0.48:80 max_fails=1 fail_timeout=10s max_conns=0;
}

server {
        listen 80;
        server_tokens on;
        server_name application.local;

        location /hedgehog(/|$)(.*) {

                proxy_http_version 1.1;
                proxy_connect_timeout 60s;
                proxy_read_timeout 60s;
                proxy_send_timeout 60s;
                client_max_body_size 1m;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Host $host;
                proxy_set_header X-Forwarded-Port $server_port;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_buffering on;

                proxy_pass http://default-ml-zoo-ingress-application.local-hedgehog-service-80;

        }
}

有人知道为什么我的URL不被重写并且请求被传递到后端服务吗?

Does anyone know why my URLs are not getting rewritten and the requsts delivered to the backend service?

提前谢谢!

推荐答案

好,我玩了几个小时后,我写下了问题,我的下一个Google提出了答案.

OK having played around with this for hours, once I had written the question my next google turned up an answer.

我使用头盔和以下存储库 stable/nginx-ingress 安装了nginx.但是根据此问题, https://github.com/kubernetes/ingress-nginx/issues/5756 实际上是旧版存储库.我卸载了控制器,并将存储库更改为 ingress-nginx

I installed nginx using helm with the following repo stable/nginx-ingress. However according to this issue https://github.com/kubernetes/ingress-nginx/issues/5756 that is in fact a legacy repository. I uninstalled my controller and changed the repository to ingress-nginx

helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx

,一切似乎都按预期工作.仍然不确定为什么以前的控制器安装失败,但是我可以恢复工作了:)

and everything appears to be working as expected. Still not sure why the previous controller installation failed but I can get back to work :)

为了可能在这里遇到的其他人的帮助-事后看来,我想知道重新安装是否只是意味着我删除并重新创建了可以解决原始问题的入口.换句话说,请确保在尝试重新安装带有头盔的入口控制器之前尝试重新创建入口.

For the aid of others that might end up here - in hindsight I am wondering if the reinstallation simply meant that I deleted and recreated my ingress which might have solved the original problem. In other words make sure you try recreating the ingress before reinstalling the ingress controller with helm.

这篇关于Kubernetes NGINX入口重写目标注释被破坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆