getting 503 service not available after doing configuration - kubernetes

I have application "test.abc.com". I want to transfer request between different service.
Example or Expected output
when user hit test.abc.com --> it will go to abc-demo-frontend-external service
when user hit test.abc.com/main.js or test.abc.com/main.css or any .css/ .js files --> it will go to abc-demo-frontend service
I have added below configuration
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-demo-frontend-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/ssl-redirect: 'false'
nginx.ingress.kubernetes.io/use-regex: "true"
spec:
rules:
- host: "test.abc.com"
http:
paths:
- path: /[^\/#?]+\.(?:css|js)(?![^\/?#])
pathType: ImplementationSpecific
backend:
service:
name: abc-demo-frontend
port:
number: 80
- path: /
pathType: ImplementationSpecific
backend:
service:
name: abc-demo-frontend-external
port:
number: 80
after adding [^\/#?]+\.(?:css|js)(?![^\/?#]) path I am getting 503 why ?
when I add below path I am getting 503 why ?
- path: /[^\/#?]+\.(?:css|js)(?![^\/?#])
pathType: ImplementationSpecific
backend:
service:
name: abc-demo-frontend
port:
number: 80

The Regex used in the spec.rules.http.paths.path for K8s ingress-nginx should be comply with RE2 engine syntax; see here. Seems the Regex that you are using does not comply with the RE2 engine syntax. Check it with the supported syntax in https://github.com/google/re2/wiki/Syntax.
Because of this, your ingress-nginx pods should not be ready. Pods are not being ready should be the reason to get 503 - service unavailable issue. Try updating the Regex according to the RE2 syntax and then check it again.

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.

Unable to access ingress k8s resources after using rewrite-target annotation

I have a K8s cluster, with two services - one service serving the front end static files and the other for backend. To differentiate between the two, I am using an ingress and a rest call to xyz.com/ is directed to the frontend service and rest calls to xyz.com/rest/abc are directed to the backend service (which is an api gateway and collection of services behind it). When creating an ingress, if i use the rewrite target annotation to rewrite xyz.com/rest/abc to xyz/com/abc, then i start getting errors when trying to access the front end service..
The errors that I have faced are
Did not parse stylesheet .... because non css mime types are not allowed in strict mode
Syntax error: Unexpected token < (while parsing bundle.js)
but if i am not using the rewrite annotation, then all files are transferred without any issue and the page is rendered as it should
annotation being used:
nginx.ingress.kubernetes.io/rewrite-target: /$2
using nginx ingress controller
Not sure why this is happening, could anyone help, any pointers, or any error that I am making.
Instead of using the path: "/" for the frontend service, i tried giving a more exact path "/ui" but it still gives the same issue.
New to K8s and ingress, so stuck here, and not sure what to do.
Ingress ->
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: sample-ingress
labels:
name: sample-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
nginx.ingress.kubernetes.io/use-regex: "true"
spec:
rules:
- host: xyz.com
http:
paths:
- pathType: Prefix
path: /rest(/|$)(.*)
backend:
service:
name: backend-service
port:
number: 8080
- pathType: Prefix
path: /()
backend:
service:
name: frontend-service
port:
number: 3000
For me it's still not clear what the desired behavior is. If you want to have
/rest/restpath -> backend-service/restpath
and
/uipath -> frontend-service/uipath
then your ingress could look like this:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: sample-ingress
labels:
name: sample-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
nginx.ingress.kubernetes.io/use-regex: "true"
spec:
rules:
- host: xyz.com
http:
paths:
- pathType: Prefix
path: /rest/(.*)
backend:
service:
name: backend-service
port:
number: 8080
- pathType: Prefix
path: /(.*)
backend:
service:
name: frontend-service
port:
number: 3000

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

Remove path with ingress nginx kubernetes 404

I have 2 services running behind Kubernetes Ingress Nginx that I'm not able to get it to work together. And the issue is I get is a 404.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
cert-manager.io/cluster-issuer: "letsencrypt-staging"
name: rewrite
namespace: default
spec:
ingressClassName: nginx
rules:
- host: www.example.com
http:
paths:
- path: /bar/static(/|$)(.*)
pathType: Prefix
backend:
service:
name: service-1
port:
number: 80
- path: /foo/bar
pathType: Prefix
backend:
service:
name: service-1
port:
number: 80
- path: /
pathType: Prefix
backend:
service:
name: service-2
port:
number: 80
tls:
- hosts:
- www.example.com
secretName: www.example.com-tls
For the first path I'm trying to remove bar from the path so that it becomes it becomes www.example.com/static without changing the URL if that makes sense. This works fine and all static files get a 200 however with this configuration I will get a 404 on the second path rule /foo/bar.
If I disable the nginx.ingress.kubernetes.io/rewrite-target: /$2 line, the second path rule /foo/bar gets a 200 and routes to the service correctly but now as expected since I have removed the capture group, the first rule /bar/static(/|$)(.*) doesn't work anymore and I get a 404 on all static content.
How can I make these work together?