Skip to content

Commit

Permalink
fix: use address 0.0.0.0 in Kubernetes env (fastify#579)
Browse files Browse the repository at this point in the history
  • Loading branch information
sinedied committed Jan 12, 2023
1 parent 94e5d7a commit 004fecc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ You can pass the following options via CLI arguments. You can also use `--config
| Path to logging configuration module to use | `-L` | `--logging-module` | `FASTIFY_LOGGING_MODULE` |
| Start Fastify app in debug mode with nodejs inspector | `-d` | `--debug` | `FASTIFY_DEBUG` |
| Set the inspector port (default: 9320) | `-I` | `--debug-port` | `FASTIFY_DEBUG_PORT` |
| Set the inspector host to listen on (default: loopback address or `0.0.0.0` inside Docker) | | `--debug-host` | `FASTIFY_DEBUG_HOST` |
| Set the inspector host to listen on (default: loopback address or `0.0.0.0` inside Docker or Kubernetes) | | `--debug-host` | `FASTIFY_DEBUG_HOST` |
| Prints pretty logs | `-P` | `--pretty-logs` | `FASTIFY_PRETTY_LOGS` |
| Watch process.cwd() directory for changes, recursively; when that happens, the process will auto reload | `-w` | `--watch` | `FASTIFY_WATCH` |
| Ignore changes to the specified files or directories when watch is enabled. (e.g. `--ignore-watch='node_modules .git logs/error.log'` ) | | `--ignore-watch` | `FASTIFY_IGNORE_WATCH` |
Expand All @@ -175,9 +175,9 @@ By default `--ignore-watch` flag is set to ignore `node_modules build dist .git

#### Containerization

When deploying to a Docker, and potentially other, containers, it is advisable to set a fastify address of `0.0.0.0` because these containers do not default to exposing mapped ports to localhost.
When deploying to a Docker container, and potentially other, containers, it is advisable to set a fastify address of `0.0.0.0` because these containers do not default to exposing mapped ports to localhost.

For containers built and run specifically by the Docker Daemon, fastify-cli is able to detect that the server process is running within a Docker container and the `0.0.0.0` listen address is set automatically.
For containers built and run specifically by the Docker Daemon or inside a Kubernetes cluster, fastify-cli is able to detect that the server process is running within a container and the `0.0.0.0` listen address is set automatically.

Other containerization tools (eg. Buildah and Podman) are not detected automatically, so the `0.0.0.0` listen address must be set explicitly with either the `--address` flag or the `FASTIFY_ADDRESS` environment variable.

Expand Down
7 changes: 4 additions & 3 deletions start.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const {
requireModule,
requireFastifyForModule,
requireServerPluginFromPath,
showHelpForCommand
showHelpForCommand,
isKubernetes
} = require('./util')

let Fastify = null
Expand Down Expand Up @@ -125,7 +126,7 @@ async function runFastify (args, additionalOptions, serverOptions) {
} else {
require('inspector').open(
opts.debugPort,
opts.debugHost || isDocker() ? listenAddressDocker : undefined
opts.debugHost || isDocker() || isKubernetes() ? listenAddressDocker : undefined
)
}
}
Expand Down Expand Up @@ -165,7 +166,7 @@ async function runFastify (args, additionalOptions, serverOptions) {
await fastify.listen({ port: opts.port, host: opts.address })
} else if (opts.socket) {
await fastify.listen({ path: opts.socket })
} else if (isDocker()) {
} else if (isDocker() || isKubernetes()) {
await fastify.listen({ port: opts.port, host: listenAddressDocker })
} else {
await fastify.listen({ port: opts.port })
Expand Down
14 changes: 14 additions & 0 deletions test/start.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,20 @@ test('should start the server listening on 0.0.0.0 when running in docker', asyn
t.pass('server closed')
})

t.only('should start the server listening on 0.0.0.0 when running in kubernetes', async t => {
t.plan(2)
process.env.KUBERNETES_SERVICE_HOST = '1.2.3.4'

const argv = ['-p', getPort(), './examples/plugin.js']
const fastify = await start.start(argv)

t.equal(fastify.server.address().address, '0.0.0.0')

await fastify.close()
delete process.env.KUBERNETES_SERVICE_HOST
t.pass('server closed')
})

test('should start the server with watch options that the child process restart when directory changed', { skip: process.platform === 'win32' }, async (t) => {
t.plan(3)
const tmpjs = path.resolve(baseFilename + '.js')
Expand Down
4 changes: 3 additions & 1 deletion util.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const resolveFrom = require('resolve-from')

const moduleSupport = semver.satisfies(process.version, '>= 14 || >= 12.17.0 < 13.0.0')

const isKubernetes = () => Boolean(process.env.KUBERNETES_SERVICE_HOST)

function exit (message) {
if (message instanceof Error) {
console.log(message)
Expand Down Expand Up @@ -98,4 +100,4 @@ function showHelpForCommand (commandName) {
}
}

module.exports = { exit, requireModule, requireFastifyForModule, showHelpForCommand, requireServerPluginFromPath }
module.exports = { isKubernetes, exit, requireModule, requireFastifyForModule, showHelpForCommand, requireServerPluginFromPath }

0 comments on commit 004fecc

Please sign in to comment.