nginx path routing not working on ingress - kubernetes

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

Related

Issue with Kubernetes apiVersion: networking.k8s.io/v1 with K8 Ingress

We are using the networking.k8s.io/v1beta1 and we trying to move to networking.k8s.io/v1 with the following changes and my ingress.YAML code looks like this now.
Version:"v1.20.15-gke.2500"
When I applied the "kubectl apply" I got to see the following error, could someone help me why I am facing this error?
error: error validating "ingress.yaml": error validating data: [ValidationError(Ingress.spec.rules[0].http.paths[0].backend.service.port): invalid type for io.k8s.api.networking.v1.ServiceBackendPort: got "integer", expected "map", ValidationError(Ingress.spec.rules[0].http.paths[1].backend.service.port): invalid type for io.k8s.api.networking.v1.ServiceBackendPort: got "integer", expected "map", ValidationError(Ingress.spec.rules[0].http.paths[2].backend.service.port): invalid type for io.k8s.api.networking.v1.ServiceBackendPort: got "integer", expected "map"; if you choose to ignore these errors, turn validation off with --validate=false
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ***
annotations:
kubernetes.io/ingress.global-static-ip-name: ****
networking.gke.io/managed-certificates: *****
spec:
rules:
- host: ***
http:
paths:
- path: /****/*
pathType: Prefix
backend:
service:
name: *********
port: 50000
- path: /****/*
pathType: Prefix
backend:
service:
name: *********
port: 50000
- path: /****/*
pathType: Prefix
backend:
service:
name: *********
port: 50000
- path: /****/*
pathType: Prefix
backend:
service:
name: *********
port: 50000
- path: /****/*
pathType: Prefix
backend:
service:
name: *********
port: 50000
- path: /****/*
pathType: Prefix
backend:
service:
name: *********
port: 50000
- path: /****/*
pathType: Prefix
backend:
service:
name: *********
port: 50000
- path: /****/*
pathType: Prefix
backend:
service:
name: *********
port: 50000
- host: *********
http:
paths:
- path: /****/*
pathType: Prefix
backend:
service:
name: *********
port: 50000
- path: /****/*
pathType: Prefix
backend:
service:
name: *********
port: 50000
- path: /****/*
pathType: Prefix
backend:
service:
name: *********
port: 50000
You are using the Old format of YAML with API networking.k8s.io/v1 it's updated now.
You can read more here : https://kubernetes.io/docs/concepts/services-networking/ingress/#the-ingress-resource
Change in service port
backend:
service:
name: test
port:
number: 80
Ref YAML block :
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress
spec:
ingressClassName: class-name
rules:
- http:
paths:
- path: /testpath
pathType: Prefix
backend:
service:
name: test
port:
number: 80
You are using incorrect syntax for networking.k8s.io/v1 Ingress. The error is saying that the backend.service.port is no longer an integer, it expects a map.
In apiVersion: networking.k8s.io/v1beta1, the backend section looks like below:
backend:
serviceName: service1
servicePort: 80
Now in apiVersion: networking.k8s.io/v1:
backend:
service:
name: test
port:
number: 80
Sample YAML:
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

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.

I had a web application and I would like to separate some service into another subdomain (k8s)

I hosted my web application on AKS cluster in default namespaces.
The cluster uses Nginx Ingress, with host: example.com
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress
namespace: default
spec:
tls:
- hosts:
- example.com
- www.example.com
secretName: tls
defaultBackend:
service:
name: service1
port:
number: 80
rules:
- host: example.com
http:
paths:
- backend:
service:
name: service1
port:
number: 80
path: /
pathType: Prefix
- backend:
service:
name: service1
port:
number: 8080
path: /api
pathType: Prefix
- backend:
service:
name: service1
port:
number: 8080
path: /services
pathType: Prefix
- backend:
service:
name: service1
port:
number: 8080
path: /auth
pathType: Prefix
- backend:
service:
name: service1
port:
number: 8080
path: /websocket
pathType: Prefix
It works well with the simple setting. Nonetheless, some service needs to be separated to another namespaces. We use the same loadbalancer and would like to separate them just with another ingress in another namespaces. No changes were made to the original setting, I have deployed the required services into the new namespace togehter with the ingress as below
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-2
namespace: ns2
spec:
tls:
- hosts:
- new.example.com
- www.new.example.com
secretName: new-tls
defaultBackend:
service:
name: service2
port:
number: 8080
rules:
- host: new.example.com
http:
paths:
- backend:
service:
name: service2
port:
number: 8080
path: /
pathType: Prefix
- backend:
service:
name: service2
port:
number: 8080
path: /api
pathType: Prefix
- backend:
service:
name: service2
port:
number: 8080
path: /services
pathType: Prefix
- backend:
service:
name: service2
port:
number: 8080
path: /auth
pathType: Prefix
- backend:
service:
name: service2
port:
number: 8080
path: /websocket
pathType: Prefix
When I tried to do some test on the config, It turns out that the request always go back to default namespaces service. Any reason?
p.s. service1 and service2 are basically the same, I just need them to handle different request from different domain/subdomain.
Thanks.

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?