Ingress don't load the website assets (Css files and Javascript files) - kubernetes

I have a kubeadm cluster and i am trying to deploy to static websites using ingress (after installing the metalLB and nginx-ingress controller )
After deploying the ingress, i find that the ingress don't load the website assets (the html file only)
Please any help !
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
name: ingress-test
spec:
ingressClassName: nginx
rules:
- host: k8s.example.k8s
http:
paths:
- path: /app1
pathType: Prefix
backend:
service:
name: app1-svc
port:
number: 80
- path: /app2
pathType: Prefix
backend:
service:
name: app2-svc
port:
number: 80

Make sure your routing working properly with ingress
Example and it's not 404 for css:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-service
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- http:
paths:
- path: /?(.*)
backend:
serviceName: service
servicePort: 3000
- path: /api/?(.*)
backend:
serviceName: service
servicePort: 5000
Read more at : https://github.com/kubernetes/ingress-nginx/issues/2557

Related

Kubernetes how to run multiple services at the same address

I have several sites available at http://service1/api and http://service2/api
I want them to work on the same external address and routing occurs inside the kube
http:/exturi/service1/api
http:/exturi/service2/api
Ingress configuration
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: haproxy
name: ingress-api
spec:
rules:
- host: api.sample.com
http:
paths:
- backend:
serviceName: edpcore-db-api
servicePort: 13001
path: /db-api/
pathType: Prefix
tls:
- hosts:
- api.sample.com
secretName: tls-api-secret
how to configure ingress
If you are using the Nginx ingress you can do routing using
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: service-1
servicePort: 80
path: /service-1(/|$)(.*)
- backend:
serviceName: service-2
servicePort: 80
path: /service-2(/|$)(.*)
For example, the ingress definition above will result in the following rewrites:
rewrite.bar.com/service1 rewrites to rewrite.bar.com/
rewrite.bar.com/service1/ rewrites to rewrite.bar.com/
rewrite.bar.com/service1/new rewrites to rewrite.bar.com/new
Refer : https://kubernetes.github.io/ingress-nginx/examples/rewrite/
Extra
If you don't want to remove service name or rewrite simple use this config
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: haproxy
name: simple
namespace: default
spec:
rules:
- host: simple.bar.com
http:
paths:
- backend:
serviceName: service-1
servicePort: 80
path: /service-1
- backend:
serviceName: service-2
servicePort: 80
path: /service-2
Thats how you should do it (nginx ingress):
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
namespace: my-ingress-namespace
spec:
rules:
- host: exturi
http:
paths:
- backend:
service:
name: service1
port:
number: 80
path: /service1/(.*)
pathType: ImplementationSpecific
- backend:
service:
name: service2
port:
number: 80
path: /service2/(.*)
pathType: ImplementationSpecific
status:
loadBalancer:
ingress:
- ip: {}

Redirect trafic from one app (workload) to other if there is prefix?

I have two apps (workloads): app1 and app2. My goal is to redirect trafic from app1 to app2 if there is the diona prefix. For example:
app1/diona > app2/<api>
Now I have such rules:
rules:
- host: host
http:
paths:
- backend:
serviceName: ingress-37ce1ad1e8214375784d1e50805c056c
servicePort: 80
path: /diona
When I check app1/diona endpoint in logs of app2 there is an error:
Not Found: /diona
How can I redirect trafic correctly without realizing diona prefix in app2?
You can achieve it using nginx ingress controller. You code will look like this
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
name: rewrite
spec:
rules:
- host: host
http:
paths:
- backend:
serviceName: ingress-37ce1ad1e8214375784d1e50805c056c
servicePort: 80
path: /diona
If you want to redirect app1/diona/* to app2/*, change annotations to
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
name: rewrite
spec:
rules:
- host: host
http:
paths:
- backend:
serviceName: ingress-37ce1ad1e8214375784d1e50805c056c
servicePort: 80
path: /diona/(.*)
Reference

Creating a Kubernetes ingress pointing two services

Requirement: Want to deploy Minio and another backend service using an ingress with HTTPS (Not for production purposes)
I have been trying to create an ingress to access two services externally from the Kubernetes cluster in GKE. These are the attempts I tried.
Attempt One
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: lightning-ingress
namespace: default
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
rules:
- http:
paths:
- path: /storage
backend:
serviceName: minio
servicePort: 9000
- path: /portal
backend:
serviceName: oscar
servicePort: 8080
Attempt Two
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: oscar
annotations:
# nginx.ingress.kubernetes.io/rewrite-target: /
kubernetes.io/ingress.class: nginx
spec:
rules:
- http:
paths:
- backend:
serviceName: oscar
servicePort: 8080
- host: storage.lightningfaas.tech
http:
paths:
- backend:
serviceName: minio
servicePort: 9000
Attempt Three
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: lightning-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- http:
paths:
- backend:
serviceName: minio
servicePort: 9000
path: /minio(/|$)(.*)
- backend:
serviceName: oscar
servicePort: 8080
path: /portal(/|$)(.*)
Attempt Four
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: minio-ingress
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: minio.lightningfaas.tech
http:
paths:
- backend:
serviceName: minio
servicePort: 9000
- host: portal.lightningfaas.tech
http:
paths:
- backend:
serviceName: oscar
servicePort: 8080
However, none of the above attempts suites for my requirement. Either it gives a 404 0r a 503. But I can confirm that creating an individual ingress for each service works fine as below.
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: oscar
annotations:
kubernetes.io/ingress.class: nginx
spec:
rules:
- http:
paths:
- backend:
serviceName: oscar
servicePort: 8080
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: minio-ingress
annotations:
kubernetes.io/ingress.class: nginx
spec:
rules:
- http:
paths:
- backend:
serviceName: minio
servicePort: 9000
Changing domain servers takes a huge time to test as well, therefore creating hosts are very annoying since I have to wait a massive time to test my code. Is there anything more that I can try?
Something like below would be ideal:
https://34.452.234.45:9000 > will access minio
https://34.452.234.45:8080 > will access oscar
Your suggestions and opinions will be really helpful for me.
Minio helm chart: https://github.com/minio/charts
Minio deployment
helm install --namespace oscar minio minio/minio --set accessKey=minio --set secretKey=password --set persistence.existingClaim=lightnig --set resources.requests.memory=256Mi
Oscar helm chart: https://github.com/grycap/helm-charts/tree/master/oscar
Oscar deployment
helm install --namespace=oscar oscar oscar --set authPass=password --set service.type=ClusterIP --set createIngress=false --set volume.storageClassName=nfs --set minIO.endpoint=http://104.197.173.174 --set minIO.TLSVerify=false --set minIO.accessKey=minio --set minIO.secretKey=password --set serverlessBackend=openfaas
According to kubernetes doc, simple fan-out example should solve your problem.
A simple fan-out example is given below where same host has two different paths for two different services.
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
So your manifest file might look like this:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: lightning-ingress
namespace: default
spec:
rules:
- host: [your host name here]
http:
paths:
- path: /storage
pathType: Prefix
backend:
service:
name: minio
port:
number: 9000
- path: /portal
pathType: Prefix
backend:
service:
name: oscar
port:
number: 8080
Ref: Kubernetes doc

Kubernetes Nginx Ingress not redirect properly to path routing services

I'm using ingress for kubernetes. My ingress is kubernetes nginx ingress. And routing configs don't work and redirect me root path in every request in https section All configs is given below
cat frontapi-ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: swagger-ingress
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: preprod-ops.kblab.local
http:
paths:
- backend:
serviceName: kb-workplace
servicePort: 8080
path: /
- backend:
serviceName: gw-branch
servicePort: 8443
path: /api
$ cat swagger-portal.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: swagger-portal
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: preprod-ops.kblab.local
http:
paths:
- backend:
serviceName: swagger-portal
servicePort: 9001
path: /swagger-portal
Above config works properly with http config but, below config doesn't work with https config. It only works swagger-portal section
$cat frontapi-ingress-https.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
ingress.kubernetes.io/secure-verify-ca-secret: preprod.kblab.local
kubernetes.io/ingress.class: nginx
generation: 1
name: app-ingress
spec:
rules:
- host: preprod.kblab.local
http:
paths:
- backend:
serviceName: gw-branch
servicePort: 8443
path: /api
- backend:
serviceName: kb-workplace
servicePort: 8080
path: /
- backend:
serviceName: acs-alfresco
servicePort: 8080
path: /acs
tls:
- hosts:
- preprod.kblab.local
- secretName: preprod.kblab.local
$ cat swagger-portal-https.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /
name: swagger-portal
spec:
rules:
- host: preprod.kblab.local
http:
paths:
- backend:
serviceName: swagger-portal
servicePort: 9001
path: /swagger-portal/a
tls:
- hosts:
- preprod.kblab.local
- secretName: preprod.kblab.local
Thanks in advance.
You are setting the nginx.ingress.kubernetes.io/rewrite-target: / annotation on your swagger-portal-https.yaml Ingress, which explicitly implies the redirection to the root path.
If you want to preserve the request path, you need to remove this annotation.

Kubernetes nginx ingress rewrite issue

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
namespace: default
name: my-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: $2
spec:
rules:
- host: hostname.com
http:
paths:
- path: /
backend:
serviceName: frontend
servicePort: 80
- path: /api/v1(/|$)(.*)
backend:
serviceName: backend
servicePort: 80
What I am trying to accomplish here is:
hostname.com/api/v1/anyurl should become hostname.com/anyurl when it goes to the backend.
hostname.com/anyurl should remain hostname.com/anyurl and go to the frontend.
The /api/v1 rewrite seems to work, but any urls going to the frontend gets rewrited to /.
What I need is the rewrite rule to only apply to the /api/v1 path
I guess this should work for you -
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
namespace: default
name: my-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: $1
spec:
rules:
- host: hostname.com
http:
paths:
- path: /(.*)
backend:
serviceName: frontend
servicePort: 80
- path: /api/(.*)
backend:
serviceName: backend
servicePort: 80
I have just edited this, it works for me, please check for this. I guess we can troubleshoot