rewrite URI with ingress for different k8s services - kubernetes

Basically I need to achieve the workflow as below. I've already deployed the official nginx helm-chart without any custom-values.
The flow I'm trying to achieve:
• https://test-api.foo.com/ >>> http://k8s-service-A/
• https://test-api.foo.com/bar >>> http://k8s-service-B/bar
• https://test-api.foo.com/sos >>> http://k8s-service-C/sos
Here is my service-A-ingress.yaml configuration:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/proxy-ssl-verify: "false"
nginx.ingress.kubernetes.io/rewrite-target: /$1
nginx.org/client-max-body-size: 1024m
nginx.org/proxy-connect-timeout: 350s
nginx.org/proxy-read-timeout: 4260s
name: service-A-ingress
spec:
ingressClassName: nginx
rules:
- host: test-api.foo.com
http:
paths:
- backend:
service:
name: service-A
port:
number: 3001
path: /
pathType: ImplementationSpecific
- backend:
service:
name: Service-B
port:
number: 3002
path: /bar
pathType: ImplementationSpecific
- backend:
service:
name: Service-C
port:
number: 3003
path: /sos
pathType: ImplementationSpecific
Assume that all 3 services and their respective deployments are already there working fine. For all 3 services I'm getting response as below:
• https://test-api.foo.com/ >>> http://k8s-service-A/ (working fine)
• https://test-api.foo.com/bar >>> http://k8s-service-B/bar (Got 404)
• https://test-api.foo.com/sos >>> http://k8s-service-C/sos (Got 404)
I'm not an nginx expert but what it looks like is, `rewrite-target' annotation in the ingress doesn't work.
Also let me know if I'm doing something wrong or understanding it differently. Any help would be appreciated.

As #xirehat mentioned, the rewrite annotation can be removed. Because the request will be rewritten to match the URI that the associated services anticipate. Because occasionally the exposed URL for the backend service is different from the path indicated in the Ingress rule. Unless a request is rebuilt, it will always return 404.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/proxy-ssl-verify: "false"
nginx.org/client-max-body-size: 1024m
nginx.org/proxy-connect-timeout: 350s
nginx.org/proxy-read-timeout: 4260s
name: service-A-ingress
spec:
ingressClassName: nginx
rules:
- host: test-api.foo.com
http:
paths:
- backend:
service:
name: service-A
port:
number: 3001
path: /
pathType: ImplementationSpecific
- backend:
service:
name: Service-B
port:
number: 3002
path: /bar
pathType: ImplementationSpecific
- backend:
service:
name: Service-C
port:
number: 3003
path: /sos
pathType: ImplementationSpecific
In case if still it doesn't work then try with pathType: Prefix. You can refer to these links to learn more about Rewrite annotation and Ingress annotations.

Related

How to run kubernetes ingress for bultiple api

I want to organize my web apis iwth kubernetes ingress tool.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: myapp-api-ingress
annotations:
kubernetes.io/ingress.class: nginx
# nginx.ingress.kubernetes.io/use-regex: "true"
# nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- host: api.myapp.com
http:
paths:
- pathType: Prefix
path: /catalog
backend:
service:
name: myapp-catalog-service
port:
number: 80
- pathType: Prefix
path: /identity
backend:
service:
name: myapp-identity-service
port:
number: 80
With this configuration, I can access the "api.myapp.com/catalog".
But "api.myapp.com/catalog" is 404 not found. How can fix this configuration?
Seems to be an issue with rewrite annotation that might cause the 404 error. Can you give the below annotation in the yaml and give a try :
nginx.ingress.kubernetes.io/rewrite-target: /$2
As per this rewrite target example , These $2 placeholders can be used as parameters in the rewrite-target annotation. This Target URI where the traffic must be redirected.
As per Kubernetes ingress update your yaml as below example which can be accessed from foo.bar.com/foo from port 4200 and foo.bar.com/bar from port 8080.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: simple-fanout-example
spec:
rules:
- host: foo.bar.com
http:
paths:
- path: /foo
pathType: Prefix
backend:
service:
name: service1
port:
number: 4200
- path: /bar
pathType: Prefix
backend:
service:
name: service2
port:
number: 8080
Refer to this ingress path matching doc and SO

I have two react applications under the same host from nginx ingress controller

I am having an ingress resource as below
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-staging
namespace: staging
annotations:
kubernetes.io/ingress.class: "nginx"
cert-manager.io/issuer: letsencrypt-staging-production
# nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/rewrite-target: /$1
kubernetes.io/ingress.allow-http: "true"
nginx.ingress.kubernetes.io/configuration-snippet: |
if ($request_uri ~* "^/\?.*") {
add_header Content-Type text/javascript;
}
spec:
tls:
- hosts:
- afriven.com
secretName: ingress-staging-secret
rules:
- host: app.com
http:
paths:
- path: /?(.*)
pathType: Prefix
backend:
service:
name: afriven-frontend-service-staging
port:
number: 80
- path: /vendor/?(.*)
pathType: Prefix
backend:
service:
name: afriven-vendor-service-staging
port:
number: 80
- path: /backend/?(.*)
pathType: Prefix
backend:
service:
name: afriven-backend-service-staging
port:
number: 5000
However only app.com and app.com/backend are working.
afriven-frontend-service-staging and afriven-vendor-service-staging server react build that from an nginx docker containers.
If I comment out
- path: /?(.*)
pathType: Prefix
backend:
service:
name: afriven-frontend-service-staging
port:
number: 80
and change the path of this
- path: /vendor/?(.*)
pathType: Prefix
backend:
service:
name: afriven-vendor-service-staging
port:
number: 80
to /?(.*) it works, other than that, i keep getting Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec
This error usually happens because your deployment was into a subfolder, not directly on your base-URL.
To fix it
Build your angular app with
ng build --prod --base-href yoursubfolder
Or set the base path in your index.html
<head>
<base href="/yoursubfolder/">
</head>
Refer to this stack post for more information.

Kubernetes Ingress redirect setup

I have a avi Kubernetes ingress and want to redirect / to /ui . Is it possible to do on Ingress routing rules.
poc.xxx.com/ --> How to redirect it to poc.xxx.com/ui
poc.xxx.com/ui --> ui-service
poc.xxx.com/backend --> backend-service
My ingress Yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: poc-ingress
spec:
rules:
- host: poc.xxx.com
http:
paths:
- path: /ui
pathType: Prefix
backend:
service:
name: ui-service
port:
number: 443
- path: /backend
pathType: Prefix
backend:
service:
name: backend-service
port:
number: 443
What if you do something like this, any request at / will get moved to ui service
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: poc-ingress
spec:
rules:
- host: poc.xxx.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: ui-service
port:
number: 443
- path: /backend
pathType: Prefix
backend:
service:
name: backend-service
port:
number: 443
However still if you are looking for a redirect solution you can follow below option
Add this annotation in ingress :
nginx.ingress.kubernetes.io/server-snippet: |
location ~ / {
rewrite / https://test.example.com/ui permanent;
}
if request comes at / it will get redirected to another domain or ui path as you wish.
You can also create the two ingress looks like this, first one check backend and / while another one handles ui :
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: poc-ingress
annotation:
nginx.ingress.kubernetes.io/server-snippet: |
location ~ / {
rewrite / https://test.example.com/ui permanent;
}
spec:
rules:
- host: poc.xxx.com
http:
paths:
- path: /backend
pathType: Prefix
backend:
service:
name: backend-service
port:
number: 443
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ui-ingress
spec:
rules:
- host: poc.xxx.com
http:
paths:
- path: /ui
pathType: Prefix
backend:
service:
name: ui-service
port:
number: 443
Do not forget to use the ingress class annotation in ingress.

Kubernetesingress can't find multiple path resources

My ingress file like this and I cannot get my resources when it comes up multiple paths
And my ingress file like this:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite: /
spec:
ingressClassName: nginx
rules:
- host: foo.test.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: service1
port:
number: 80
- path: /service2
pathType: Prefix
backend:
service:
name: service2
port:
number: 80
Is there anybody can help me find solutions?

Getting blank page when exposing ReactJS app using Ingress-controller

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: hello-world-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /$2
nginx.ingress.kubernetes.io/add-base-url: "true"
nginx.ingress.kubernetes.io/use-regex: "true"
cert-manager.io/cluster-issuer: letsencrypt
spec:
tls:
- hosts:
- mints.cf
secretName: tls-secret
rules:
- host: mints.cf
http:
paths:
- path: /amq(/|$)(.*)
pathType: Prefix
backend:
service:
name: activemq-artemis
port:
number: 8161
- path: /*(/|$)(.*)
pathType: Prefix
backend:
service:
name: mints-ui
port:
number: 3000
- path: /nginx(/|$)(.*)
pathType: Prefix
backend:
service:
name: nginx-deployment
port:
number: 80
- path: /kafka(/|$)(.*)
pathType: Prefix
backend:
service:
name: kafka-manager
port:
number: 9000
- path: /kibana(/|$)(.*)
pathType: Prefix
backend:
service:
name: kibana
port:
number: 5601
I Deployed some React JS application into my aks cluster by using deploymentyaml file, and created an Ingress Controller for exposing service with public IP and DNS configuration. Ingress Working other services but it is showing blank page for ReactJS applications. screenshot from browser for reference
Can anyone help me out from this issue?