Question

Nginx ingress with custom rules

Posted December 22, 2021 132 views
NginxDockerKubernetesDigitalOcean Managed KubernetesDigitalOcean Managed Load Balancers

We have a pretty big monolithic web app built in rails with background jobs, recurrent workers, websockets and content delivery. After 4 years it is impossible to maintain such a big structure and updates have been a problem. So I’m migrating the code base to kubernetes and splitting the services by docker containers.

The one web server would be split into 4 different containers, each serving a web app split by different domain. The anycable server (and gRPC provider) for each domain would be in a container on its own. The workers would also be on containers… etc

Since kubernetes have a private network for the containers I’m not too worried about internal communication. My problem is with the load balancer and web servers.

My first attempt was to use nginx-ingress. From what I understand the DOKS automatically maps it to a DO load balancer, which doesn’t allow custom rules that I need, specifically this:

location /cable {
proxypass http://app1upstream;
proxyhttpversion 1.1;
proxysetheader Upgrade $httpupgrade;
proxy
setheader Connection “Upgrade”;
proxy
set_header Host $host;
}

My second option would be to create a droplet, in the same VPC, that will run nginx, create an upstream for each pod that contains a web app and manually handle the “Connection: Upgrade” header

upstream app1upstream {
server 192.168.100.10:8010;
}
upstream app2
upstream {
server 192.168.100.11:8010;
}
upstream app3_upstream {
server 192.168.100.12:8010;
}

The problem with that is that I can’t seem to find a reliable way to update the upstream when a container is replaced (for update or failure) or scaled (add or remove replicas)

I’m new to docker and kubernetes. How can I do this structure? Or at least, how can I find materials that can help me with this?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi,
Looks like you are trying to use DO Loadbanancer with ingress-nginx controller.

“My first attempt was to use nginx-ingress. From what I understand the DOKS automatically maps it to a DO load balancer, which doesn’t allow custom rules that I need, specifically this:

You do not create or use Loadbalanacer to create your routing rules. After you deploy your Ingress Controller with a Loadbalancer, you have to create a Kubernetes Ingress. Kubernetes Ingress will allow you to do what you are looking for.

After you deploy your Ingress Controller, You can create your Ingress
Example:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: rails
  namespace: your_namespace
  annotations:
    nginx.ingress.kubernetes.io/proxy-read-timeout: "3600"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "3600"
    kubernetes.io/ingress.class: nginx
spec:
  rules:
  - host: your_domain
    http:
      paths:
        - path: /
          backend:
            serviceName: name_of_your_rails_service
            servicePort: port_of_your_rails_service
        - path: /events
          backend:
            serviceName: "gateway-alias-in-club"
            servicePort: 8080
        - path: /cable
          backend:
            serviceName: name_of_your_cable_service
            servicePort: port_of_your_cable_service
        - path: /your_other_paths
          backend:
            serviceName: name_of_your_other_service
            servicePort: port_of_your_other_service