Skip to content

Commit 0ca388f

Browse files
authored
Add readiness probe for NGINX on pod startup (#3629)
Add readiness probe for NGINX on startup Problem: Users want to have a readiness probe for NGINX on startup to verify pod health. Solution: Expose readiness probe configuration fields in the NginxProxy spec, allowing users to customize them as needed. By default, deploy NGINX with a readiness probe at the /readyz endpoint.
1 parent da6ad1e commit 0ca388f

File tree

15 files changed

+563
-41
lines changed

15 files changed

+563
-41
lines changed

apis/v1alpha2/nginxproxy_types.go

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,11 @@ type KubernetesSpec struct {
390390

391391
// Deployment is the configuration for the NGINX Deployment.
392392
type DeploymentSpec struct {
393+
// Container defines container fields for the NGINX container.
394+
//
395+
// +optional
396+
Container ContainerSpec `json:"container"`
397+
393398
// Number of desired Pods.
394399
//
395400
// +optional
@@ -399,24 +404,19 @@ type DeploymentSpec struct {
399404
//
400405
// +optional
401406
Pod PodSpec `json:"pod"`
407+
}
402408

409+
// DaemonSet is the configuration for the NGINX DaemonSet.
410+
type DaemonSetSpec struct {
403411
// Container defines container fields for the NGINX container.
404412
//
405413
// +optional
406414
Container ContainerSpec `json:"container"`
407-
}
408415

409-
// DaemonSet is the configuration for the NGINX DaemonSet.
410-
type DaemonSetSpec struct {
411416
// Pod defines Pod-specific fields.
412417
//
413418
// +optional
414419
Pod PodSpec `json:"pod"`
415-
416-
// Container defines container fields for the NGINX container.
417-
//
418-
// +optional
419-
Container ContainerSpec `json:"container"`
420420
}
421421

422422
// PodSpec defines Pod-specific fields.
@@ -486,6 +486,11 @@ type ContainerSpec struct {
486486
// +optional
487487
Lifecycle *corev1.Lifecycle `json:"lifecycle,omitempty"`
488488

489+
// ReadinessProbe defines the readiness probe for the NGINX container.
490+
//
491+
// +optional
492+
ReadinessProbe *ReadinessProbeSpec `json:"readinessProbe,omitempty"`
493+
489494
// HostPorts are the list of ports to expose on the host.
490495
//
491496
// +optional
@@ -497,6 +502,26 @@ type ContainerSpec struct {
497502
VolumeMounts []corev1.VolumeMount `json:"volumeMounts,omitempty"`
498503
}
499504

505+
// ReadinessProbeSpec defines the configuration for the NGINX readiness probe.
506+
type ReadinessProbeSpec struct {
507+
// Port is the port on which the readiness endpoint is exposed.
508+
// If not specified, the default port is 8081.
509+
//
510+
// +optional
511+
// +kubebuilder:validation:Minimum=1
512+
// +kubebuilder:validation:Maximum=65535
513+
Port *int32 `json:"port,omitempty"`
514+
515+
// InitialDelaySeconds is the number of seconds after the container has
516+
// started before the readiness probe is initiated.
517+
// If not specified, the default is 3 seconds.
518+
//
519+
// +optional
520+
// +kubebuilder:validation:Minimum=0
521+
// +kubebuilder:validation:Maximum=3600
522+
InitialDelaySeconds *int32 `json:"initialDelaySeconds,omitempty"`
523+
}
524+
500525
// Image is the NGINX image to use.
501526
type Image struct {
502527
// Repository is the image path.

apis/v1alpha2/zz_generated.deepcopy.go

Lines changed: 32 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

charts/nginx-gateway-fabric/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,9 @@ The following table lists the configurable parameters of the NGINX Gateway Fabri
264264
| `certGenerator.ttlSecondsAfterFinished` | How long to wait after the cert generator job has finished before it is removed by the job controller. | int | `30` |
265265
| `clusterDomain` | The DNS cluster domain of your Kubernetes cluster. | string | `"cluster.local"` |
266266
| `gateways` | A list of Gateway objects. View https://gateway-api.sigs.k8s.io/reference/spec/#gateway for full Gateway reference. | list | `[]` |
267-
| `nginx` | The nginx section contains the configuration for all NGINX data plane deployments installed by the NGINX Gateway Fabric control plane. | object | `{"config":{},"container":{"hostPorts":[],"lifecycle":{},"resources":{},"volumeMounts":[]},"debug":false,"image":{"pullPolicy":"Always","repository":"ghcr.io/nginx/nginx-gateway-fabric/nginx","tag":"edge"},"imagePullSecret":"","imagePullSecrets":[],"kind":"deployment","plus":false,"pod":{},"replicas":1,"service":{"externalTrafficPolicy":"Local","loadBalancerClass":"","loadBalancerIP":"","loadBalancerSourceRanges":[],"nodePorts":[],"type":"LoadBalancer"},"usage":{"caSecretName":"","clientSSLSecretName":"","endpoint":"","resolver":"","secretName":"nplus-license","skipVerify":false}}` |
267+
| `nginx` | The nginx section contains the configuration for all NGINX data plane deployments installed by the NGINX Gateway Fabric control plane. | object | `{"config":{},"container":{"hostPorts":[],"lifecycle":{},"readinessProbe":{},"resources":{},"volumeMounts":[]},"debug":false,"image":{"pullPolicy":"Always","repository":"ghcr.io/nginx/nginx-gateway-fabric/nginx","tag":"edge"},"imagePullSecret":"","imagePullSecrets":[],"kind":"deployment","plus":false,"pod":{},"replicas":1,"service":{"externalTrafficPolicy":"Local","loadBalancerClass":"","loadBalancerIP":"","loadBalancerSourceRanges":[],"nodePorts":[],"type":"LoadBalancer"},"usage":{"caSecretName":"","clientSSLSecretName":"","endpoint":"","resolver":"","secretName":"nplus-license","skipVerify":false}}` |
268268
| `nginx.config` | The configuration for the data plane that is contained in the NginxProxy resource. This is applied globally to all Gateways managed by this instance of NGINX Gateway Fabric. | object | `{}` |
269-
| `nginx.container` | The container configuration for the NGINX container. This is applied globally to all Gateways managed by this instance of NGINX Gateway Fabric. | object | `{"hostPorts":[],"lifecycle":{},"resources":{},"volumeMounts":[]}` |
269+
| `nginx.container` | The container configuration for the NGINX container. This is applied globally to all Gateways managed by this instance of NGINX Gateway Fabric. | object | `{"hostPorts":[],"lifecycle":{},"readinessProbe":{},"resources":{},"volumeMounts":[]}` |
270270
| `nginx.container.hostPorts` | A list of HostPorts to expose on the host. This configuration allows containers to bind to a specific port on the host node, enabling external network traffic to reach the container directly through the host's IP address and port. Use this option when you need to expose container ports on the host for direct access, such as for debugging, legacy integrations, or when NodePort/LoadBalancer services are not suitable. Note: Using hostPort may have security and scheduling implications, as it ties pods to specific nodes and ports. | list | `[]` |
271271
| `nginx.container.lifecycle` | The lifecycle of the NGINX container. | object | `{}` |
272272
| `nginx.container.resources` | The resource requirements of the NGINX container. | object | `{}` |

charts/nginx-gateway-fabric/values.schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,12 @@
351351
"title": "lifecycle",
352352
"type": "object"
353353
},
354+
"readinessProbe": {
355+
"description": "# -- Defines the settings for the data plane readiness probe. This probe returns Ready when the NGINX data plane is ready to serve traffic.",
356+
"required": [],
357+
"title": "readinessProbe",
358+
"type": "object"
359+
},
354360
"resources": {
355361
"description": "The resource requirements of the NGINX container.",
356362
"required": [],

charts/nginx-gateway-fabric/values.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,19 @@ nginx:
441441
# -- volumeMounts are the additional volume mounts for the NGINX container.
442442
volumeMounts: []
443443

444+
## -- Defines the settings for the data plane readiness probe. This probe returns Ready when the NGINX data plane is ready to serve traffic.
445+
readinessProbe: {}
446+
# @schema
447+
# type: integer
448+
# minimum: 1
449+
# maximum: 65535
450+
# @schema
451+
# -- Port in which the readiness endpoint is exposed.
452+
# port: 8081
453+
454+
# -- The number of seconds after the Pod has started before the readiness probes are initiated.
455+
# initialDelaySeconds: 3
456+
444457
# -- The service configuration for the NGINX data plane. This is applied globally to all Gateways managed by this
445458
# instance of NGINX Gateway Fabric.
446459
service:

config/crd/bases/gateway.nginx.org_nginxproxies.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,28 @@ spec:
362362
StopSignal can only be set for Pods with a non-empty .spec.os.name
363363
type: string
364364
type: object
365+
readinessProbe:
366+
description: ReadinessProbe defines the readiness probe
367+
for the NGINX container.
368+
properties:
369+
initialDelaySeconds:
370+
description: |-
371+
InitialDelaySeconds is the number of seconds after the container has
372+
started before the readiness probe is initiated.
373+
If not specified, the default is 3 seconds.
374+
format: int32
375+
maximum: 3600
376+
minimum: 0
377+
type: integer
378+
port:
379+
description: |-
380+
Port is the port on which the readiness endpoint is exposed.
381+
If not specified, the default port is 8081.
382+
format: int32
383+
maximum: 65535
384+
minimum: 1
385+
type: integer
386+
type: object
365387
resources:
366388
description: Resources describes the compute resource
367389
requirements.
@@ -3773,6 +3795,28 @@ spec:
37733795
StopSignal can only be set for Pods with a non-empty .spec.os.name
37743796
type: string
37753797
type: object
3798+
readinessProbe:
3799+
description: ReadinessProbe defines the readiness probe
3800+
for the NGINX container.
3801+
properties:
3802+
initialDelaySeconds:
3803+
description: |-
3804+
InitialDelaySeconds is the number of seconds after the container has
3805+
started before the readiness probe is initiated.
3806+
If not specified, the default is 3 seconds.
3807+
format: int32
3808+
maximum: 3600
3809+
minimum: 0
3810+
type: integer
3811+
port:
3812+
description: |-
3813+
Port is the port on which the readiness endpoint is exposed.
3814+
If not specified, the default port is 8081.
3815+
format: int32
3816+
maximum: 65535
3817+
minimum: 1
3818+
type: integer
3819+
type: object
37763820
resources:
37773821
description: Resources describes the compute resource
37783822
requirements.

deploy/crds.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,28 @@ spec:
947947
StopSignal can only be set for Pods with a non-empty .spec.os.name
948948
type: string
949949
type: object
950+
readinessProbe:
951+
description: ReadinessProbe defines the readiness probe
952+
for the NGINX container.
953+
properties:
954+
initialDelaySeconds:
955+
description: |-
956+
InitialDelaySeconds is the number of seconds after the container has
957+
started before the readiness probe is initiated.
958+
If not specified, the default is 3 seconds.
959+
format: int32
960+
maximum: 3600
961+
minimum: 0
962+
type: integer
963+
port:
964+
description: |-
965+
Port is the port on which the readiness endpoint is exposed.
966+
If not specified, the default port is 8081.
967+
format: int32
968+
maximum: 65535
969+
minimum: 1
970+
type: integer
971+
type: object
950972
resources:
951973
description: Resources describes the compute resource
952974
requirements.
@@ -4358,6 +4380,28 @@ spec:
43584380
StopSignal can only be set for Pods with a non-empty .spec.os.name
43594381
type: string
43604382
type: object
4383+
readinessProbe:
4384+
description: ReadinessProbe defines the readiness probe
4385+
for the NGINX container.
4386+
properties:
4387+
initialDelaySeconds:
4388+
description: |-
4389+
InitialDelaySeconds is the number of seconds after the container has
4390+
started before the readiness probe is initiated.
4391+
If not specified, the default is 3 seconds.
4392+
format: int32
4393+
maximum: 3600
4394+
minimum: 0
4395+
type: integer
4396+
port:
4397+
description: |-
4398+
Port is the port on which the readiness endpoint is exposed.
4399+
If not specified, the default port is 8081.
4400+
format: int32
4401+
maximum: 65535
4402+
minimum: 1
4403+
type: integer
4404+
type: object
43614405
resources:
43624406
description: Resources describes the compute resource
43634407
requirements.

internal/controller/nginx/config/base_http_config.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ import (
1111
var baseHTTPTemplate = gotemplate.Must(gotemplate.New("baseHttp").Parse(baseHTTPTemplateText))
1212

1313
type httpConfig struct {
14-
Includes []shared.Include
15-
HTTP2 bool
14+
Includes []shared.Include
15+
HTTP2 bool
16+
NginxReadinessProbePort int32
1617
}
1718

1819
func executeBaseHTTPConfig(conf dataplane.Configuration) []executeResult {
1920
includes := createIncludesFromSnippets(conf.BaseHTTPConfig.Snippets)
2021

2122
hc := httpConfig{
22-
HTTP2: conf.BaseHTTPConfig.HTTP2,
23-
Includes: includes,
23+
HTTP2: conf.BaseHTTPConfig.HTTP2,
24+
Includes: includes,
25+
NginxReadinessProbePort: conf.BaseHTTPConfig.NginxReadinessProbePort,
2426
}
2527

2628
results := make([]executeResult, 0, len(includes)+1)

internal/controller/nginx/config/base_http_config_template.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ map $request_uri $request_uri_path {
2424
"~^(?P<path>[^?]*)(\?.*)?$" $path;
2525
}
2626
27+
# NGINX health check server block.
28+
server {
29+
listen {{ .NginxReadinessProbePort }};
30+
31+
location = /readyz {
32+
access_log off;
33+
return 200;
34+
}
35+
}
36+
2737
{{ range $i := .Includes -}}
2838
include {{ $i.Name }};
2939
{{ end -}}

0 commit comments

Comments
 (0)