Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ spec:
ports:
- containerPort: 80
- containerPort: 443
readinessProbe:
# The entrypoint waits for cluster DNS to resolve the backend before
# starting nginx, so a running container is not yet a serving one.
tcpSocket:
port: 80
initialDelaySeconds: 5
periodSeconds: 5
resources:
requests:
cpu: 1m
Expand Down
32 changes: 32 additions & 0 deletions nginx/init/05-wait-for-backend-dns.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#! /bin/bash

###############################################################################
# nginx resolves the hostnames in its proxy_pass directives when it loads its
# configuration, and exits if any of them cannot be resolved:
#
# [emerg] host not found in upstream "backend"
#
# On a cold start the frontend container can be running before the cluster DNS
# can answer for the backend service, so wait for the name before starting.
###############################################################################

BACKEND_HOST=backend
MAX_WAIT_SECONDS=120

if getent hosts "${BACKEND_HOST}" > /dev/null 2>&1 ; then
exit 0
fi

echo "Waiting up to ${MAX_WAIT_SECONDS}s for '${BACKEND_HOST}' to resolve"
until getent hosts "${BACKEND_HOST}" > /dev/null 2>&1 ; do
# SECONDS is wall clock since this shell started, so the cap covers the time
# getent spends blocking on resolver timeouts, not just the sleeps.
if [ "${SECONDS}" -ge "${MAX_WAIT_SECONDS}" ] ; then
# Start nginx anyway so that it reports the problem itself, rather than
# leaving a container that is running but never serving.
echo "'${BACKEND_HOST}' did not resolve after ${SECONDS}s"
exit 0
fi
sleep 2
done
echo "'${BACKEND_HOST}' resolved after ${SECONDS}s"
3 changes: 3 additions & 0 deletions nginx/templates/default.conf.template
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Maintainer note: any new proxy_pass host must also be added to
# nginx/init/05-wait-for-backend-dns.sh, else nginx will exit at startup when
# that name is not yet resolvable.
server {
listen 80;
server_name ${SERVER_NAME};
Expand Down
Loading