Skip to content

Commit 89d8de5

Browse files
committed
Support TCP/UDP load balancing
- Add stream-snippets configmap key - Add stream-log-format configmap key
1 parent 51a5962 commit 89d8de5

File tree

11 files changed

+64
-0
lines changed

11 files changed

+64
-0
lines changed

examples/customization/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ The table below summarizes all of the options. For some of them, there are examp
6767
| `nginx.com/health-checks-mandatory-queue` | N/A | When active health checks are mandatory, configures a queue for temporary storing incoming requests during the time when NGINX Plus is checking the health of the endpoints after a configuration reload. | `0` | [Support for Active Health Checks](../health-checks). |
6868
| `nginx.com/slow-start` | N/A | Sets the upstream server [slow-start period](https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-balancer/#server-slow-start). By default, slow-start is activated after a server becomes [available](https://docs.nginx.com/nginx/admin-guide/load-balancer/http-health-check/#passive-health-checks) or [healthy](https://docs.nginx.com/nginx/admin-guide/load-balancer/http-health-check/#active-health-checks). To enable slow-start for newly added servers, configure [mandatory active health checks](../health-checks). | `"0s"` | |
6969
| N/A | `external-status-address` | Sets the address to be reported in the status of Ingress resources. Requires the `-report-status` command-line argument. Overrides the `-external-service` argument. | N/A | [Report Ingress Status](../../docs/report-ingress-status.md). |
70+
| N/A | `stream-snippets` | Sets a custom snippet in stream context. | N/A | |
71+
| N/A | `stream-log-format` | Sets the custom [log format](http://nginx.org/en/docs/stream/ngx_stream_log_module.html#log_format) for TCP/UDP load balancing. | See the [template file](../../nginx-controller/nginx/nginx.conf.tmpl). | |
7072

7173
## Using ConfigMaps
7274

examples/customization/nginx-config.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,12 @@ data:
5757
keepalive: "32" # default is 0. When > 0, sets the value of the keepalive directive and adds 'proxy_set_header Connection "";' to a location block. See http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive
5858
max-fails: "0" # default is 1. Sets the value of the max_fails parameter of the `server` directive. See https://nginx.org/en/docs/http/ngx_http_upstream_module.html#max_fails
5959
fail-timeout: "5s" # default is 10s. Sets the value of the fail_timeout parameter of the `server` directive. See https://nginx.org/en/docs/http/ngx_http_upstream_module.html#fail_timeout
60+
stream-log-format: "$remote_addr $protocol" # stream-log-format default is set in the nginx.conf.tmpl file. Also see http://nginx.org/en/docs/stream/ngx_stream_log_module.html#log_format
61+
stream-snippets: |
62+
upstream tcp-coffee {
63+
server tcp-coffee-svc.default.svc.cluster.local:9944;
64+
}
65+
server {
66+
listen 4456;
67+
proxy_pass tcp-coffee;
68+
}

nginx-controller/Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ FROM nginx:1.15.2
33
# forward nginx access and error logs to stdout and stderr of the ingress
44
# controller process
55
RUN ln -sf /proc/1/fd/1 /var/log/nginx/access.log \
6+
&& ln -sf /proc/1/fd/1 /var/log/nginx/stream-access.log \
67
&& ln -sf /proc/1/fd/2 /var/log/nginx/error.log
78

89
COPY nginx-ingress nginx/templates/nginx.ingress.tmpl nginx/templates/nginx.tmpl /

nginx-controller/DockerfileForAlpine

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ FROM nginx:1.15.2-alpine
33
# forward nginx access and error logs to stdout and stderr of the ingress
44
# controller process
55
RUN ln -sf /proc/1/fd/1 /var/log/nginx/access.log \
6+
&& ln -sf /proc/1/fd/1 /var/log/nginx/stream-access.log \
67
&& ln -sf /proc/1/fd/2 /var/log/nginx/error.log
78

89
COPY nginx-ingress nginx/templates/nginx.ingress.tmpl nginx/templates/nginx.tmpl /

nginx-controller/DockerfileForPlus

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ RUN set -x \
4444
# forward nginx access and error logs to stdout and stderr of the ingress
4545
# controller process
4646
RUN ln -sf /proc/1/fd/1 /var/log/nginx/access.log \
47+
&& ln -sf /proc/1/fd/1 /var/log/nginx/stream-access.log \
4748
&& ln -sf /proc/1/fd/2 /var/log/nginx/error.log
4849

4950

nginx-controller/nginx/config.go

+12
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ type Config struct {
2121
SSLRedirect bool
2222
MainMainSnippets []string
2323
MainHTTPSnippets []string
24+
MainStreamSnippets []string
2425
MainServerNamesHashBucketSize string
2526
MainServerNamesHashMaxSize string
2627
MainLogFormat string
28+
MainStreamLogFormat string
2729
ProxyBuffering bool
2830
ProxyBuffers string
2931
ProxyBufferSize string
@@ -259,6 +261,9 @@ func ParseConfigMap(cfgm *api_v1.ConfigMap, nginxPlus bool) *Config {
259261
if logFormat, exists := cfgm.Data["log-format"]; exists {
260262
cfg.MainLogFormat = logFormat
261263
}
264+
if streamLogFormat, exists := cfgm.Data["stream-log-format"]; exists {
265+
cfg.MainStreamLogFormat = streamLogFormat
266+
}
262267
if proxyBuffering, exists, err := GetMapKeyAsBool(cfgm.Data, "proxy-buffering", cfgm); exists {
263268
if err != nil {
264269
glog.Error(err)
@@ -346,5 +351,12 @@ func ParseConfigMap(cfgm *api_v1.ConfigMap, nginxPlus bool) *Config {
346351
if ingressTemplate, exists := cfgm.Data["ingress-template"]; exists {
347352
cfg.IngressTemplate = &ingressTemplate
348353
}
354+
if mainStreamSnippets, exists, err := GetMapKeyAsStringSlice(cfgm.Data, "stream-snippets", cfgm, "\n"); exists {
355+
if err != nil {
356+
glog.Error(err)
357+
} else {
358+
cfg.MainStreamSnippets = mainStreamSnippets
359+
}
360+
}
349361
return cfg
350362
}

nginx-controller/nginx/configurator.go

+2
Original file line numberDiff line numberDiff line change
@@ -1082,9 +1082,11 @@ func GenerateNginxMainConfig(config *Config) *NginxMainConfig {
10821082
nginxCfg := &NginxMainConfig{
10831083
MainSnippets: config.MainMainSnippets,
10841084
HTTPSnippets: config.MainHTTPSnippets,
1085+
StreamSnippets: config.MainStreamSnippets,
10851086
ServerNamesHashBucketSize: config.MainServerNamesHashBucketSize,
10861087
ServerNamesHashMaxSize: config.MainServerNamesHashMaxSize,
10871088
LogFormat: config.MainLogFormat,
1089+
StreamLogFormat: config.MainStreamLogFormat,
10881090
SSLProtocols: config.MainServerSSLProtocols,
10891091
SSLCiphers: config.MainServerSSLCiphers,
10901092
SSLDHParam: config.MainServerSSLDHParam,

nginx-controller/nginx/nginx.go

+2
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,11 @@ type NginxMainConfig struct {
142142
ServerNamesHashBucketSize string
143143
ServerNamesHashMaxSize string
144144
LogFormat string
145+
StreamLogFormat string
145146
HealthStatus bool
146147
MainSnippets []string
147148
HTTPSnippets []string
149+
StreamSnippets []string
148150
// http://nginx.org/en/docs/http/ngx_http_ssl_module.html
149151
SSLProtocols string
150152
SSLPreferServerCiphers bool

nginx-controller/nginx/templates/nginx-plus.tmpl

+16
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,19 @@ http {
108108

109109
include /etc/nginx/conf.d/*.conf;
110110
}
111+
112+
stream {
113+
{{if .StreamLogFormat -}}
114+
log_format stream-main '{{.StreamLogFormat}}';
115+
{{- else -}}
116+
log_format stream-main '$remote_addr [$time_local] '
117+
'$protocol $status $bytes_sent $bytes_received '
118+
'$session_time';
119+
{{- end }}
120+
121+
access_log /var/log/nginx/stream-access.log stream-main;
122+
123+
{{range $value := .StreamSnippets}}
124+
{{$value}}
125+
{{end}}
126+
}

nginx-controller/nginx/templates/nginx.tmpl

+16
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,19 @@ http {
8484

8585
include /etc/nginx/conf.d/*.conf;
8686
}
87+
88+
stream {
89+
{{if .StreamLogFormat -}}
90+
log_format stream-main '{{.StreamLogFormat}}';
91+
{{- else -}}
92+
log_format stream-main '$remote_addr [$time_local] '
93+
'$protocol $status $bytes_sent $bytes_received '
94+
'$session_time';
95+
{{- end }}
96+
97+
access_log /var/log/nginx/stream-access.log stream-main;
98+
99+
{{range $value := .StreamSnippets}}
100+
{{$value}}
101+
{{end}}
102+
}

nginx-controller/nginx/templates/templates_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ var mainCfg = nginx.NginxMainConfig{
8888
WorkerShutdownTimeout: "1m",
8989
WorkerConnections: "1024",
9090
WorkerRlimitNofile: "65536",
91+
StreamSnippets: []string{"# comment"},
92+
StreamLogFormat: "$remote_addr",
9193
}
9294

9395
func TestIngressForNGINXPlus(t *testing.T) {

0 commit comments

Comments
 (0)