Skip to content

Commit

Permalink
Fix MaxWorkerOpenFiles calculation on high cores nodes (kubernetes#7107)
Browse files Browse the repository at this point in the history
* Fix MaxWorkerOpenFiles calculation on high cores nodes

* Add e2e test for rlimit_nofile

* Fix doc for max-worker-open-files
  • Loading branch information
nanorobocop authored and rikatz committed Aug 21, 2021
1 parent ebdc71c commit 358e497
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/user-guide/nginx-configuration/configmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ _**default:**_ 16384
## max-worker-open-files

Sets the [maximum number of files](http://nginx.org/en/docs/ngx_core_module.html#worker_rlimit_nofile) that can be opened by each worker process.
The default of 0 means "max open files (system's limit) / [worker-processes](#worker-processes) - 1024".
The default of 0 means "max open files (system's limit) - 1024".
_**default:**_ 0

## map-hash-bucket-size
Expand Down
7 changes: 1 addition & 6 deletions internal/ingress/controller/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,12 +511,7 @@ func (n NGINXController) generateTemplate(cfg ngx_config.Configuration, ingressC
if cfg.MaxWorkerOpenFiles == 0 {
// the limit of open files is per worker process
// and we leave some room to avoid consuming all the FDs available
wp, err := strconv.Atoi(cfg.WorkerProcesses)
klog.V(3).InfoS("Worker processes", "count", wp)
if err != nil {
wp = 1
}
maxOpenFiles := (rlimitMaxNumFiles() / wp) - 1024
maxOpenFiles := rlimitMaxNumFiles() - 1024
klog.V(3).InfoS("Maximum number of open file descriptors", "value", maxOpenFiles)
if maxOpenFiles < 1024 {
// this means the value of RLIMIT_NOFILE is too low.
Expand Down
58 changes: 58 additions & 0 deletions test/e2e/settings/global_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package settings

import (
"fmt"
"strings"
"syscall"

"github.com/onsi/ginkgo"
"k8s.io/ingress-nginx/test/e2e/framework"
)

var _ = framework.IngressNginxDescribe("global-options", func() {
f := framework.NewDefaultFramework("global-options")

ginkgo.It("should have worker_rlimit_nofile option", func() {
f.WaitForNginxConfiguration(func(server string) bool {
return strings.Contains(server, fmt.Sprintf("worker_rlimit_nofile %d;", rlimitMaxNumFiles()-1024))

})
})

ginkgo.It("should have worker_rlimit_nofile option and be independent on amount of worker processes", func() {
f.SetNginxConfigMapData(map[string]string{
"worker-processes": "11",
})

f.WaitForNginxConfiguration(func(server string) bool {
return strings.Contains(server, "worker_processes 11;") &&
strings.Contains(server, fmt.Sprintf("worker_rlimit_nofile %d;", rlimitMaxNumFiles()-1024))
})
})
})

// rlimitMaxNumFiles returns hard limit for RLIMIT_NOFILE
func rlimitMaxNumFiles() int {
var rLimit syscall.Rlimit
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
return 0
}
return int(rLimit.Max)
}

0 comments on commit 358e497

Please sign in to comment.