本文介绍了在Kubernetes入口Ngnix中编辑max_conns吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试限制Nginx入口中与服务器的并发连接数.

Im trying to limit the number of concurrent connection to servers in my Nginx ingress.

Ngnix入口支持max_conns吗?我该如何编辑或添加它?

is max_conns supported in Ngnix ingress? how can i edit or add it?

http://nginx.org/en/docs/http/ngx_http_upstream_module .html#upstream

使用max_conn的Nginx conf的示例

exmple of an Nginx conf using max_conn

upstream backend {
server backend1.example.com  max_conns=3;
server backend2.example.com;}

谢谢

推荐答案

因此,要添加max_conns(或入口configmap不支持的任何其他参数),需要做的是更改模板.

So, what needed to be done in order to add max_conns (or any other parameter that is not supported by the ingress configmap) - is to change the template.

像这样更改模板/etc/nginx/template/nginx.tmpl:

changing the template /etc/nginx/template/nginx.tmpl like this:

upstream {{ $upstream.Name }} {
    # Load balance algorithm; empty for round robin, which is the default
    {{ if ne $cfg.LoadBalanceAlgorithm "round_robin" }}
    {{ $cfg.LoadBalanceAlgorithm }};
    {{ end }}

    {{ if $upstream.UpstreamHashBy }}
    hash {{ $upstream.UpstreamHashBy }} consistent;
    {{ end }}

    {{ if (gt $cfg.UpstreamKeepaliveConnections 0) }}
    keepalive {{ $cfg.UpstreamKeepaliveConnections }};
    {{ end }}

    {{ range $server := $upstream.Endpoints }}server {{ $server.Address | formatIP }}:{{ $server.Port }} max_fails={{ $server.MaxFails }} fail_timeout={{ $server.FailTimeout }} max_conns=1;
    {{ end }}
}

(您可以从pod nginx-ingress-controller获取完整文件,只需在pod上运行bash并对其进行分类即可)会成功的现在使用本地nginx.tmpl创建一个configmap:

(you can get the full file from the pod nginx-ingress-controller, just run bash on the pod and cat it)will do the trick.now create a configmap with the local nginx.tmpl:

kubectl create configmap nginx-template --from-file=nginx.tmpl=/localpath/nginx.tmpl

,然后使用此yaml将卷安装到部署中:

and then mount a volume to the deployment with this yaml:

        volumeMounts:
      - mountPath: /etc/nginx/template
        name: nginx-template-volume
        readOnly: true
  volumes:
    - name: nginx-template-volume
      configMap:
        name: nginx-template
        items:
        - key: nginx.tmpl
          path: nginx.tmpl

  • 我需要手动重新启动NGINX入口,但是由于没有部署(我想这是因为我在minikube上),所以我编辑了Re​​plicationController
  • 这篇关于在Kubernetes入口Ngnix中编辑max_conns吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 18:10