not able to create the rule for the ingress controller - kubernetes

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

Related

Kubernetes Ingress path route to different services in different namespaces

currently I'm trying the following setup:
I have:
one cluster
one Ingress Controller
one url (myapp.onazure.com)
two namespaces for two applications default and default-test
two deployments, ingress objects, services for the namespaces
I can easily reach my app from the default namespace with path based routing '/' as a prefix rule
Now i have tried to configure the second namespace and following rule: /testing to hit another service
Unfortunately i get an HTTP404 when i try to hit the following URL myapp.onazure.com/testing/openapi.json
What did I miss?
Working Ingress 1
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
name: liveapi-ingress-object
namespace: default
annotations:
kubernetes.io/ingress.class: public-nginx
spec:
tls:
- hosts:
- myapp-region1.onazure.com
- myapp-region2.onazure.com
secretName: ingress-tls-csi
rules:
- host: - myapp-region1.onazure.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: liveapi-svc
port:
number: 8080
- host: myapp-region2.onazure.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: liveapi-svc
port:
number: 8080
Not working Ingress 2
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
name: liveapi-ingress-object-testing
namespace: default-testing
annotations:
kubernetes.io/ingress.class: public-nginx
#nginx.ingress.kubernetes.io/rewrite-target: /testing
spec:
tls:
- hosts:
- myapp-region1.onazure.com
- myapp-region2.onazure.com
secretName: ingress-tls-csi-testing
rules:
- host: myapp-region1.onazure.com
http:
paths:
- path: /testing
#pathType: Prefix
backend:
service:
name: liveapi-svc-testing
port:
number: 8080
- host: myapp-region2.onazure.com
http:
paths:
- path: /testing
#pathType: Prefix
backend:
service:
name: liveapi-svc-testing
port:
number: 8080
Maybe I am missing a rewrite target to simply '/' in the testing namespace ingress?
Finally I figured out the missing part. I had to add the following statement to the not working ingress object:
annotations:
kubernetes.io/ingress.class: public-nginx
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/rewrite-target: /$1
Please see the complete ingress object:
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
name: liveapi-ingress-object
namespace: default-testing
annotations:
kubernetes.io/ingress.class: public-nginx
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
tls:
- hosts:
- myapp.onazure.com
secretName: ingress-tls-csi-testing
rules:
- host: myapp.onazure.com
http:
paths:
- path: /testing/(.*)
pathType: Prefix
backend:
service:
name: liveapi-svc-testing
port:
number: 8000
Use full DNS name of a service,
$SERVICE.$NAMESPACE.svc.cluster.local
But you need to make sure your ignress controller has acccess to the desired namespace.

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"

nginx ingress routing (default backend 404)

I am trying to write an ingress for a rest api service but I am seeing "default backend 404"
my rest service url is like this
https://myurl.com/restsvc/api/test/v1/app/hello?name=bob
below is my ingress yml:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: restservice-ingress
annotations:
kubernetes.io/ingress.class: nginx
spec:
tls:
- hosts:
- myurl.com
secretName: secret
rules:
- host: myurl.com
http:
paths:
- path: /restsvc/api/test/v1/app/(.*)
pathType: Prefix
backend:
serviceName: restservice-svc
servicePort: 8080
Any help appreciated, Thank you
try this -
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
kubernetes.io/ingress.class: nginx
name: staging-ingress
spec:
rules:
- host: myurl.com
http:
paths:
- path: /restsvc/api/test/v1/app/(.*)
backend:
serviceName: restservice-svc
servicePort: 8080
tls:
- hosts:
- myurl.com
secretName: secret
Fixed this by using the below rewrite target
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
kubernetes.io/ingress.class: nginx
name: staging-ingress
spec:
rules:
- host: myurl.com
http:
paths:
- path: /restsvc(/|$)(.*)
backend:
serviceName: restservice-svc
servicePort: 8080
tls:
- hosts:
- myurl.com
secretName: secret

How to create redirection url

I would to build following an service deployed to kubernetes to be accessed using an ingress object.
I would like to use https://mylocation.com/myprogram/doc to access the app but only https://mylocation.com/myprogram/doc/ works.
I have created the following entry in my yaml
# -----------------
# Ingress object
# -----------------
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: myapp-ingress
namespace: documentation
annotations:
kubernetes.io/ingress.class: nginx
#Default is 'true'
spec:
tls:
- hosts:
- mylocation.com
rules:
- host: mylocation.com
http:
paths:
- backend:
serviceName: myapp-service
servicePort: 80
path: /myapp/doc
- backend:
serviceName: myapp-service
servicePort: 80
path: /myapp/doc/(.*)
I have created the ingress object kubectl apply -f filename
When I browse http://mylocation.com//myapp/doc, I get HTTP ERROR 404
When I browse http://mylocation.com//myapp/doc/, It works
Can someone help me to get http://mylocation.com//myapp/doc working?
Thanks for your help.
Make sure to place appropriate regex
# -----------------
# Ingress object
# -----------------
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: myapp-ingress
namespace: documentation
annotations:
kubernetes.io/ingress.class: nginx
#Default is 'true'
spec:
tls:
- hosts:
- mylocation.com
rules:
- host: mylocation.com
http:
paths:
- backend:
serviceName: myapp-service
servicePort: 80
path: /myapp/doc(/|$)(.*)
`

How to use rewrite in traefik

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