I did exactly as is it told here: 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 -
The ingress is created, I added rewrite.bar.com
to my /etc/hosts
to the ip of the minikube. coffee-svs
is the service from this example: https://github.com/nginxinc/NGINX-Demos/tree/master/kubernetes-demo-nginx-ingress-resources
Yet there is no rewrite happening, I have exactly the same use case as shown here: Another nginx ingress rewrite-target problem, yet the proposed solution does not work.
When I do curl rewrite.bar.com/something
there is a 404 Not Found
response.
The expected behavior would be that rewrite.bar.com/something
is rewritten as rewrite.bar.com/
and then call coffee-svc on path /
.
Also doing curl -I -k rewrite.bar.com/
returns:
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
I have nginx ingress installed with helm, using stable/nginx-ingress
, currently version 0.30.0
.
Kubernetes version is 1.14
.
Edit: Adding ingress-controller-pod logs
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.
The curl command should be
curl rewrite.bar.com/something --header "Host: rewrite.bar.com"
The Host
header is what nginx uses to know which host sent the request and apply the ingress rule for host rewrite.bar.com
and the rewrite happens as defined in the rule.
If you use a browser to access the same url then this header is automatically sent by the browser.
Use below to test service via curl from another pod
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
Finnally I solved it by removing the entire minikube cluster:
minikube delete
minikube start
There was somewhere, somehow a configuration affecting the ingress. Then it worked fine. Pitty I couldn't know the culprit config or deployment.