我完全按照这里所说的做:https://github.com/kubernetes/ingress-nginx/tree/master/docs/examples/rewrite#examples
$ echo '
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
name: rewrite
namespace: default
spec:
rules:
- host: rewrite.bar.com
http:
paths:
- backend:
serviceName: coffee-svc
servicePort: 80
path: /something(/|$)(.*)
' | kubectl create -f -
创建了入口,我将rewrite.bar.com
添加到了/etc/hosts
的minikube ip中。
coffee-svs
是此示例中的服务:https://github.com/nginxinc/NGINX-Demos/tree/master/kubernetes-demo-nginx-ingress-resources
然而,没有重写发生,我的使用案例与此处显示的完全相同:Another nginx ingress rewrite-target problem,但是建议的解决方案不起作用。
当我做curl rewrite.bar.com/something
时,会有404 Not Found
响应。
预期的行为是将rewrite.bar.com/something
重写为rewrite.bar.com/
,然后在路径/
上调用coffee-svc。
也进行curl -I -k rewrite.bar.com/
返回:
HTTP/1.1 404 Not Found
Server: nginx/1.17.9
Date: Sat, 02 May 2020 15:24:02 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
我使用stable/nginx-ingress
当前版本0.30.0
安装了带头盔的nginx入口。
Kubernetes版本为1.14
。
编辑: 添加入口控制器Pod日志
I0502 15:52:00.344425 7 flags.go:215] Watching for Ingress class: nginx
W0502 15:52:00.344820 7 flags.go:260] SSL certificate chain completion is disabled (--enable-ssl-chain-completion=false)
W0502 15:52:00.344909 7 client_config.go:543] Neither --kubeconfig nor --master was specified. Using the inClusterConfig. This might not work.
I0502 15:52:00.345341 7 main.go:193] Creating API client for https://10.96.0.1:443
I0502 15:52:00.355089 7 main.go:237] Running in Kubernetes cluster version v1.14 (v1.14.1) - git (clean) commit b7394102d6ef778017f2ca4046abbaa23b88c290 - platform linux/amd64
I0502 15:52:00.358184 7 main.go:91] Validated default/my-nginx-ingress-default-backend as the default backend.
I0502 15:52:00.836581 7 main.go:102] SSL fake certificate created /etc/ingress-controller/ssl/default-fake-certificate.pem
W0502 15:52:00.873934 7 store.go:628] Unexpected error reading configuration configmap: configmaps "my-nginx-ingress-controller" not found
I0502 15:52:00.911054 7 nginx.go:263] Starting NGINX Ingress controller
I0502 15:52:02.035037 7 event.go:281] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"default", Name:"rewrite", UID:"60b7853f-8c86-11ea-b3ad-080027f64399", APIVersion:"networking.k8s.io/v1beta1", ResourceVersion:"367651", FieldPath:""}): type: 'Normal' reason: 'CREATE' Ingress default/rewrite
I0502 15:52:02.113846 7 nginx.go:307] Starting NGINX process
I0502 15:52:02.116614 7 leaderelection.go:242] attempting to acquire leader lease default/ingress-controller-leader-nginx...
I0502 15:52:02.123127 7 controller.go:137] Configuration changes detected, backend reload required.
I0502 15:52:02.135944 7 status.go:86] new leader elected: my-nginx-ingress-controller-6fcc8d7f97-r9d8t
I0502 15:52:02.321762 7 controller.go:153] Backend successfully reloaded.
I0502 15:52:02.321833 7 controller.go:162] Initial sync, sleeping for 1 second.
I0502 15:52:55.451512 7 leaderelection.go:252] successfully acquired lease default/ingress-controller-leader-nginx
I0502 15:52:55.452458 7 status.go:86] new leader elected: my-nginx-ingress-controller-6fcc8d7f97-7p8g6
I0502 15:52:55.470245 7 status.go:274] updating Ingress default/rewrite status from [] to [{10.0.2.15 }]
I0502 15:52:55.475571 7 event.go:281] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"default", Name:"rewrite", UID:"60b7853f-8c86-11ea-b3ad-080027f64399", APIVersion:"networking.k8s.io/v1beta1", ResourceVersion:"367750", FieldPath:""}): type: 'Normal' reason: 'UPDATE' Ingress default/rewrite
I0502 15:53:18.288519 7 controller.go:137] Configuration changes detected, backend reload required.
I0502 15:53:18.291422 7 event.go:281] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"default", Name:"rewrite", UID:"60b7853f-8c86-11ea-b3ad-080027f64399", APIVersion:"networking.k8s.io/v1beta1", ResourceVersion:"367783", FieldPath:""}): type: 'Normal' reason: 'DELETE' Ingress default/rewrite
I0502 15:53:18.403753 7 controller.go:153] Backend successfully reloaded.
I0502 15:53:20.187998 7 event.go:281] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"default", Name:"rewrite", UID:"0ba43588-8c8d-11ea-b3ad-080027f64399", APIVersion:"networking.k8s.io/v1beta1", ResourceVersion:"367788", FieldPath:""}): type: 'Normal' reason: 'CREATE' Ingress default/rewrite
I0502 15:53:21.623215 7 controller.go:137] Configuration changes detected, backend reload required.
I0502 15:53:21.796798 7 controller.go:153] Backend successfully reloaded.
答案 0 :(得分:1)
最后我通过删除整个minikube集群解决了这个问题:
minikube delete
minikube start
某处存在某种影响进入的配置。然后工作正常。小子,我不知道罪魁祸首的配置或部署。
答案 1 :(得分:0)
curl命令应为
curl rewrite.bar.com/something --header "Host: rewrite.bar.com"
Host
标头是nginx用来了解哪个主机发送了请求并为主机rewrite.bar.com
应用入口规则,并且重写发生在规则中的定义。
如果您使用浏览器访问相同的URL,则浏览器会自动发送此标头。
在下面用于通过另一个吊舱的卷曲来测试服务
kubectl run curl --image=radial/busyboxplus:curl -i --tty
If you don't see a command prompt, try pressing enter.
[ root@curl:/ ]$ curl coffee-svc