I am using the nginx ingress (community edition) to serve different services within my Kubernetes cluster while trying to optimize costs.
I encountered issues when trying to add a websocket rule that although it’s supported out of the box from the controller it actually requires (if I understood correctly) setting with annotations:
nginx.ingress.kubernetes.io/proxy-read-timeout: 3600
nginx.ingress.kubernetes.io/proxy-send-timeout: 3600
Now I want to use this setting only for the http path exploited by the websocket and not for the others. How can I accomplish that?
— Extra Info —
I tried to look in the documentation, but I didn’t find any way to fine tune this and, changing the nginx.conf
manually doesn’t seem an option here. Moreover, the nginx inc controller has "mergeable" ingresses with master and minions that I guess can get different configurations easily through annotations, but it’s not supported in the community edition (and I don’t wanna switch to the limited free nginx inc one).
As an extra detail the nginx inc version lets you specify the websockets services with an annotation nginx.org/websocket-services: "ws-svc"
that it’s not available in the community edition (and I am not sure what it does).
I will paste here for the sake of completeness a similar structure to my ingress without including everything unrelated
apiVersion: networking.k8s.io/v1
kind:Ingress
metadata:
name: nlb-ingress
namespace: test
annotations:
nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
nginx.ingress.kubernetes.io/rewrite-target: "/$2"
spec:
ingressClassName: "nginx"
rules
- host = cafe.example.com
http:
paths:
- path: /banana(/|$)(?:.*)
pathType: Prefix
backend:
service:
name: banana-svc
port:
number: 5678
- path: /webapp(/|$)(.*)
pathType: Prefix
backend:
service:
name: webapp
port:
number: 80
- path: /ws
pathType: Prefix
backend:
service:
name: ws-svc
port:
number: 8008
Moreover, being able to customize differently the paths within an ingress resource would also help me for other things/parameters like having better rewrite targets, so I guess that whatever solution applies here it will apply also to other configurations through annotations.
2
Answers
You’re right that this behaviour is possible with NGINX Ingress Controller via Mergeable Ingresses. Mergeable Ingresses are supported in both OSS and Plus NGINX Ingress Controllers.
If you want to use the
proxy_send_timeout
orproxy_read_timeout
directives in NGINX Ingress Controller you can use these annotationsnginx.org/proxy-send-timeout
nginx.org/proxy-read-timeout
You can see the full list of supported annotations in the docs.
This is an option, and can be achieved in two ways, one is with snippets, the other is with templating (if its the
nginx.conf
you want to change, this is themain-template
). Snippets is the easier one to maintain so always consider that first. And you can target thehttp
orstream
blocks using a config map, or theserver
andlocation
blocks using either config maps or annotations (annotations if you want them to be only on a specific ingress). I don’t think you will need snippets or templating for the behaviour you are asking about however.What this
nginx.org/websocket-services
annotation does is inform NGINX Ingress Controller which services are websocket services. Under the hood what happens is these two directives are added to the location block which uses the websocket service.You can see an example of using this directive here. I have copied this below. As you can see, the service which the
/ws
path uses,ws-svc
is referenced in thenginx.org/websocket-services
annotation, telling NGINX Ingress Controller to insert the twoproxy_set_header
directives, inlocation /ws
in the generated config.Lastly, to put these two annotations below which you mentioned on a specific route, you will need to use Mergeable Ingresses.
nginx.org/proxy-send-timeout
nginx.org/proxy-read-timeout
You can use the Mergeable Ingresses example for this. Modify the master and minion(s) to get the behaviour you want. It seems you will have a minion on the path
/ws
with thenginx.org/proxy-send-timeout
,nginx.org/proxy-read-timeout
andnginx.org/websocket-services
annotations set.