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

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.

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

rewrite URI with ingress for different k8s services

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.

Ingress manifest with same host but different paths

I've encountered an odd problem, I think. I'm not sure if my rewrite-target is correct.
I got two URLs that I want to reach, both URLs are going to different backend and frontends. The /login endpoint will be added automatically when you hit the login-page, which we will do.
first one: dev.app.com/login
second one: dev.app.com/path2/login
The first URL is always working, even if I put /login after the host directly or just let it redirect from dev.app.com to dev.app.com/login.
But the second URL is working only if I let it redirect from dev.app.com/path2 to dev.app.com/path2/login. If I put dev.app.com/path2/login directly in the browser I will get an 404 not found nginx error. The same goes if I first put dev.app.com/path2/ and gets redirected to dev.app.com/path2/login (which works), then I reload the page. Then I get an 404 not found error.
I have setup an Ingress file with the following code:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-fe
annotations:
nginx.ingress.kubernetes.io/use-regex: 'true'
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
tls:
- hosts:
- dev.app.com
secretName: tls-secret-con
rules:
- host: dev.app.com
http:
paths:
- path: /(.*)
pathType: ImplementationSpecific
backend:
service:
name: path1svc
port:
number: 80
- path: /path2(/|$)(.*)
pathType: ImplementationSpecific
backend:
service:
name: path2svc
port:
number: 80
What could cause this?
In the Yaml Can you change the PathType to Prefix and have a check?. Can you refer to this link1 and Link2 and Let me know if this resolves your issue. Find sample examples for ingress Path matching.
Here's the example from the Kubernetes docs:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-ingress-1
spec:
ingressClassName: nginx
rules:
- host: test.com
http:
paths:
- path: /foo/bar
pathType: Prefix
backend:
service:
name: service1
port:
number: 80
- path: /foo/bar/
pathType: Prefix
backend:
service:
name: service2
port:
number: 80

nginx path routing not working on ingress

i cannot get pth base nginx routing working. I got host based working. Here is the yaml config
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
name: ingress-resource-3
spec:
ingressClassName: nginx
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-deploy-main
port:
number: 80
- path: /blue
pathType: Prefix
backend:
service:
name: nginx-deploy-blue
port:
number: 80
- path: /green
pathType: Prefix
backend:
service:
name: nginx-deploy-green
port:
number: 80
however host routing works below
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-resource-2
spec:
ingressClassName: nginx
rules:
- host: testingress.cluster.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-deploy-main
port:
number: 80
- host: blue.testingress.cluster.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-deploy-blue
port:
number: 80
- host: green.testingress.cluster.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-deploy-green
port:
number: 80
any idea why path base routing is not working?
also I dont see my hostname when i try to get ingress
[rke#k8gui ingress-demo]$ kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
ingress-resource-3 nginx * 80 11m

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?