How to use rewrite in traefik - kubernetes

I am using abc.com/foo and it is working fine, but whatever url like abc.com/foo/account-login I need to redirect to abc.com/account-login and it is not working. Please let me know how can I set rewrite or any other annotatios in traefik.
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
traefik.frontend.rule.type: PathPrefixStrip
kubernetes.io/ingress.class: traefik
name: dev-ingress
namespace: dev
spec:
rules:
- host: abc.com
http:
paths:
- backend:
serviceName: dev-service
servicePort: http
path: /foo
status:
loadBalancer: {}

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: traefik
traefik.ingress.kubernetes.io/rewrite-target: /account-login
name: dev-ingress
namespace: dev
spec:
rules:
- host: abc.com
http:
paths:
- backend:
serviceName: dev-service
servicePort: http
path: /foo
status:
loadBalancer: {}

It depends on your back-and configuration but probably you can try:
remove traefik.frontend.rule.type: PathPrefixStrip" and set path: /
Please let me know if its helps

Related

Is it possible to have placeholders in kong plugin k8s?

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: kong-ingress-test
annotations:
kubernetes.io/ingress.class: "kong-ingress"
konghq.com/strip-path: "true"
namespace: test
spec:
rules:
- host: test.com
http:
paths:
- backend:
serviceName: test
servicePort: 8080
path: /path/test/{id1}/{id2}/
I am wondering if is possible to have a path set like this in the plugin /path/test/{id1}/{id2}/
Solution for path was using regex
/path/test/[a-fA-F0-9]+/[a-fA-F0-9]+/

not able to create the rule for the ingress controller

What am I going wrong in the below query?
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /nginx
backend:
serviceName: nginx
servicePort: 80
The error I am getting:
error validating "ingress.yaml": error validating data: [ValidationError(Ingress.spec.rules[0].http.paths[0].backend): unknown field "serviceName" in io.k8s.api.networking.v1.IngressBackend, ValidationError(Ingress.spec.rules[0].http.paths[0].backend): unknown field "servicePort" in io.k8s.api.networking.v1.IngressBackend]; if you choose to ignore these errors, turn validation off with
--validate=false
Ingress spec has changed since updated to v1. Try:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /nginx
pathType: ImplementationSpecific
backend:
service:
name: nginx
port:
number: 80
According to this documentation you need to change ServiceName and ServicePort.
Each HTTP rule contains (...) a list of paths (for example, /testpath), each of which has an associated backend defined with a service.name and a service.port.name or service.port.number. Both the host and path must match the content of an incoming request before the load balancer directs traffic to the referenced Service.
Here is your yaml file with corrections:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /nginx
backend:
service:
name: nginx
port:
number: 8080
ServiceName and ServicePort no keywords are available like that in v1 .
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /nginx
backend:
service:
name: nginx
port:
number: 8080
service name and service port syntax is incorrect. Follow the below sample
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: minimal-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx-example
rules:
- http:
paths:
- path: /testpath
pathType: Prefix
backend:
service:
name: test
port:
number: 80

Rewrite rule for nginx-ingress in Kubernetes

I am struggling with making a rewrite/redirect rule on nginx-ingress on Kubernetes.
According to the doc https://kubernetes.github.io/ingress-nginx/examples/rewrite/ it says it is possible with the annotation "nginx.ingress.kubernetes.io/rewrite-target", but unable to implement on my ingress. Maybe I am doing it wrong.
An example, portion of nginx-ingress yaml file.
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx
example.com/deployment-name: frontend-page
example.com/ingress-hostnames: www.example.com
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
rules:
- host: www.example.com
http:
paths:
- backend:
serviceName: frontend-page
servicePort: 8080
path: /front(/|$)(.*)
Tried another way around as well but no luck:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx
example.com/deployment-name: frontend-page
example.com/ingress-hostnames: www.example.com
nginx.ingress.kubernetes.io/rewrite-target: /shop
spec:
rules:
- host: www.example.com
http:
paths:
- backend:
serviceName: frontend-page
servicePort: 8080
path: /
I want to redirect any traffic to the page www.example.com/front/checkdomain to www.example.com/checkdomain. I want to remove that "front" path comes in between.
try this with an updated API version, hope your ingress supports networking.k8s.io/v1beta1
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /$2
name: rewrite
namespace: default
spec:
rules:
- host: rewrite.bar.com
http:
paths:
- backend:
serviceName: http-svc
servicePort: 80
path: /something(/|$)(.*)
Example : https://kubernetes.github.io/ingress-nginx/examples/rewrite/#examples
The solution was
Changing from:
nginx.ingress.kubernetes.io/rewrite-target: /$2
To:
nginx.ingress.kubernetes.io/rewrite-target: /$2/
So the yaml looks like:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx
example.com/deployment-name: frontend-page
example.com/ingress-hostnames: www.example.com
nginx.ingress.kubernetes.io/rewrite-target: /$2/
spec:
rules:
- host: www.example.com
http:
paths:
- backend:
serviceName: frontend-page
servicePort: 8080
path: /front(/|$)(.*)

rewrites path on ingress kubernetes

i have tomcat backend service running on kubernetes cluster try to rewrite using ingress with path /blob/api/v1/test-backend > /api/v1/test-backend so
the configuration now is running so can hit to xx.somedomain.com/blob/api/v1/test-backend and i want change to xx.somedomain.com/api/v1/test-backend with rewrites
my basic ingress
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress-blob
namespace: blob-test
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/ssl-redirect: "true"
nginx.ingress.kubernetes.io/proxy-body-size: 50m
spec:
tls:
- hosts:
- xx.somedomain.com
secretName: cert-key
rules:
- host: xx.somedomain.com
http:
paths:
- path: /blob/
backend:
serviceName: blob-service
servicePort: 8080
- path: /
backend:
serviceName: web-service
servicePort: 80
and this is yaml for rewrite /blob/
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress-blob
namespace: blob-test
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/ssl-redirect: "true"
nginx.ingress.kubernetes.io/proxy-body-size: 50m
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
tls:
- hosts:
- xx.somedomain.com
secretName: cert-key
rules:
- host: xx.somedomain.com
http:
paths:
- path: /blob/api/v1/some-backend
backend:
serviceName: blob-service
servicePort: 8080
when i test with api tester like talend is got 405 error
Try this
apiVersion: networking.k8s.io/v1
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: http-svc
servicePort: 80
path: /something(/|$)(.*)
For example, the ingress definition above will result in the following rewrites:
rewrite.bar.com/something rewrites to rewrite.bar.com/
rewrite.bar.com/something/ rewrites to rewrite.bar.com/
rewrite.bar.com/something/new rewrites to rewrite.bar.com/new
Refer : https://kubernetes.github.io/ingress-nginx/examples/rewrite/
Try adding annotation like this
nginx.ingress.kubernetes.io/rewrite-target: "/$1"

Kubernetes nginx ingress rewrite issue

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
namespace: default
name: my-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: $2
spec:
rules:
- host: hostname.com
http:
paths:
- path: /
backend:
serviceName: frontend
servicePort: 80
- path: /api/v1(/|$)(.*)
backend:
serviceName: backend
servicePort: 80
What I am trying to accomplish here is:
hostname.com/api/v1/anyurl should become hostname.com/anyurl when it goes to the backend.
hostname.com/anyurl should remain hostname.com/anyurl and go to the frontend.
The /api/v1 rewrite seems to work, but any urls going to the frontend gets rewrited to /.
What I need is the rewrite rule to only apply to the /api/v1 path
I guess this should work for you -
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
namespace: default
name: my-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: $1
spec:
rules:
- host: hostname.com
http:
paths:
- path: /(.*)
backend:
serviceName: frontend
servicePort: 80
- path: /api/(.*)
backend:
serviceName: backend
servicePort: 80
I have just edited this, it works for me, please check for this. I guess we can troubleshoot