Kubernetes ingress nginx controller routing using HTTP headers - nginx

I have installed Kubernetes ingress nginx controller as described here. I am looking further to customize the routing logic by parsing HTTP header and altering the base ingress route accordingly. I know that it can be done using Lua script to be executed as part of the server routing configuration.
Can anyone advise how I inject the lua script into Kubernetes ingress nginx controller configuration?

I am doing a quite similar task. Previously we have a separate Nginx instance which has openresty installed.
My solution is to build our own customized image based on kubernetes nginx-ingress, and also include the openresty module inside the module.

Related

How can I use ingress to control the routing between two instances?

I have a service deployed on Kubernetes and it has url app.io (using ingress).
What if I need a user to every time go to app.io and:
if it's running okay with no errors, it redirects to the app.io (on k8s)
and if not running well or have an error, it would redirect on a backup service on Heroku for example with url backup.io.
How can I do that?
Thanks in advance
Fallback routing like you describe is not part of the Ingress standard. It only does routing based on incoming Host header and request path. It's possible some specific Ingress Controller supports this as a custom extension but I don't know of any that do.
I think you may need to put a L7 load balancer like HAproxy in front. Configure your backup location in backend pool, and HAProxy will take care of the rest.
You may want to configure ingress befault-backendto be some sort of fallback service. With most of the cases people tend to use that for some custom 404 but you might just direct it to another service, for example backup-io:
kind: Ingress
metadata:
name: my-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: "/"
nginx.ingress.kubernetes.io/default-backend: backup-io
That's of course assuming you're using nginx controller. Kong has also fallback service instructions.

Nginx Ingress Controller with Nginx Reverse Proxy

I am a bit confused with the architecture of load-balancing K8s traffic with Nginx ingress controller.
I learned that an ingress controller is supposed to configure the load-balancer you're using according to ingress configurations.
So if I want to use Nginx ingress controller and I have a Physical server that is running Nginx that stands in front of my network, how can I make the ingress controller configure it?
Ingress exposes HTTP and HTTPS routes from outside the cluster to services within the cluster. Traffic routing is controlled by rules defined on the Ingress resource. You must have an Ingress controller to satisfy an Ingress. Only creating an Ingress resource has no effect. Take a look at the example graph below:
Nginx Ingress Controller is using service of type LoadBalancer to get the traffic enter the controller and then to get rerouted to particular services.
I strongly suggest going through the official documentation in order to get a good understanding of the topic and see some examples of using it.
is the nginx ingress controller supposed to (or can) configure an
Nginx machine?
NGINX Ingress Controller works with both NGINX and NGINX Plus and supports the standard Ingress features - content-based routing and TLS/SSL termination.

HTTP is not disabled after adding allow-http configuration in Ingress

I have tried to block http and allow only https for the application which deployed in GCP. For the routing, using ningx ingress and tcp loadbalancer to route the traffic from client to the application.
Even after adding the below configuration in ingress yaml , still application serves http.
kubernetes.io/ingress.allow-http: "false"
Thanks in advance.
I reproduced your issue and solved it by deleting ingress resource and deploying new one with kubernetes.io/ingress.allow-http: "false" annotation. According to GCP docs:
Note: For an existing Ingress, the HTTP load balancer resources are not deleted automatically on adding the
kubernetes.io/ingress.allow-http annotation with its value set to
false. A workaround for this is to delete the Ingress and recreate
it with the annotation added. Beginning with GKE version
1.16.4-gke.25, the HTTP load balancer resources are automatically deleted on updating an Ingress to disable HTTP load balancing.
So if you have GKE version prior to 1.16.4-gke.25 ingress resource has to be deleted and then new ingress has to be created with this annotation.

traefik vs nginx ingress controller

Hi I am using kubernetes and now need to make a choice between traefik and nginx ingress controller for ingress expose. I have googled a lot, but seems no such big difference, especially nginx just announce support grpc now. Anyone can give a advice? Thanks very very much!
Found this comment at stackshare by Howie Zhao
The reasons for choosing Traefik over Nginx are as follows:
Traefik built-in Let’s Encrypt and supports automatic renewal
Traefik automatically enables HTTP/2
Prometheus can be supported through simple Traefik configuration
cookiecutter django integrates Traefik's configuration by default

Using nginx as Forward proxy in kubernetes

I am really new to kubernetes and nginx. I am able to use it as a reverse-proxy by setting up ingress resource, however, I am not sure about how should I use it to forward the request from kubernetes to a particular host.
My case is as follows:
I have a container running in kubernetes pod which access an external api url (example www.xxx.com) with some parameters, however, because I have blocked the outgoing requests for all the pods, it can not access that api url.
To solve this I want to setup nginx proxy which will forward my request to the actual api url.
Being new to this and having lack of proper steps documented anywhere to achieve this, I am really stuck. How can I do this?
What you could do is to define a Service object that points to your external API endpoint. This is done by creating an Endpoint object and a Service object both with the same name.
https://kubernetes.io/docs/concepts/services-networking/service/#services-without-selectors
Once you have your service, you could create an Ingress rule that would forward the traffic to that service. Make sure that the Ingress controller can access your API endpoint.

Resources