Embed fly clustering into the release (#2519)

This commit is contained in:
José Valim 2024-03-22 17:22:18 +01:00 committed by GitHub
parent 1038ef8d24
commit e892d9dea0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 28 additions and 21 deletions

View file

@ -201,8 +201,9 @@ The following environment variables can be used to configure Livebook on boot:
accesses files from external sources. accesses files from external sources.
* `LIVEBOOK_CLUSTER` - configures clustering strategy when running multiple * `LIVEBOOK_CLUSTER` - configures clustering strategy when running multiple
instances of Livebook. See the "Clustering" section of our Docker Deployment instances of Livebook using either the Docker image or an Elixir release.
guide for more information: https://hexdocs.pm/livebook/docker.html See the "Clustering" section of our Docker Deployment guide for more
information: https://hexdocs.pm/livebook/docker.html
* `LIVEBOOK_COOKIE` - sets the cookie for running Livebook in a cluster. * `LIVEBOOK_COOKIE` - sets the cookie for running Livebook in a cluster.
Defaults to a random string that is generated on boot. Defaults to a random string that is generated on boot.

View file

@ -50,16 +50,18 @@ If you are using [Livebook Teams](https://livebook.dev/teams/), you will also ha
## Clustering ## Clustering
If you plan to run several Livebook instances behind a load balancer, you need to enable clustering via the `LIVEBOOK_CLUSTER` environment variable. Currently the only supported value is `dns:QUERY`, in which case nodes ask DNS for A/AAAA records using the given query and try to connect to peer nodes on the discovered IPs. If you plan to run several Livebook instances behind a load balancer, you need to enable clustering via the `LIVEBOOK_CLUSTER` environment variable. Depending on the strategy of your choice, you must set additional environment variables, oftentimes, at runtime. When using the Livebook Docker image, you can create a file at `/app/user/env.sh` that exports the necessary environment variables. This file is invoked right before booting Livebook. `LIVEBOOK_DISTRIBUTION` is automatically set to `name` if clustering is enabled.
When clustering is enabled, you must additionally set the following env vars: ### `LIVEBOOK_CLUSTER=fly`
It automatically sets up a cluster to run on Fly using DNS configuration. It automatically sets up the environment variables based on your Fly Application name and enables IPv6 support.
### `LIVEBOOK_CLUSTER=dns:QUERY`
Sets up a cluster using DNS for queries for A/AAAA records to discover new nodes. Additionally, you must additionally set the following env vars:
* `LIVEBOOK_NODE=livebook_server@IP`, where `IP` is the machine IP of each deployed node * `LIVEBOOK_NODE=livebook_server@IP`, where `IP` is the machine IP of each deployed node
* You must set `LIVEBOOK_SECRET_KEY_BASE` and `LIVEBOOK_COOKIE` to different random values (use `openssl rand -base64 48` to generate said values) * You must set `LIVEBOOK_SECRET_KEY_BASE` and `LIVEBOOK_COOKIE` to different random values (use `openssl rand -base64 48` to generate said values)
* If your cloud requires IPv6, also set `ERL_AFLAGS="-proto_dist inet6_tcp"` * If your cloud requires IPv6, also set `ERL_AFLAGS="-proto_dist inet6_tcp"`
`LIVEBOOK_DISTRIBUTION` is automatically set to `name` if clustering is enabled.
Some variables, like `LIVEBOOK_NODE`, are oftentimes computed at runtime. When using the Livebook Docker image, you can create a file at `/app/user/env.sh` that exports the necessary environment variables. This file is invoked right before booting Livebook.

View file

@ -132,16 +132,8 @@ defmodule Livebook.Hubs.Dockerfile do
# Set the same Livebook secrets across all nodes # Set the same Livebook secrets across all nodes
ENV LIVEBOOK_SECRET_KEY_BASE "#{random_secret_key_base}" ENV LIVEBOOK_SECRET_KEY_BASE "#{random_secret_key_base}"
ENV LIVEBOOK_COOKIE "#{random_cookie}" ENV LIVEBOOK_COOKIE "#{random_cookie}"
ENV LIVEBOOK_CLUSTER "fly"
""" <> """
~S"""
# Runtime configuration to cluster multiple Livebook nodes on Fly.io
RUN printf '\
export ERL_AFLAGS="-proto_dist inet6_tcp"\n\
export LIVEBOOK_NODE="${FLY_APP_NAME}-${FLY_IMAGE_REF##*-}@${FLY_PRIVATE_IP}"\n\
export LIVEBOOK_CLUSTER="dns:${FLY_APP_NAME}.internal"\n\
' > /app/user/env.sh
"""
end end
[ [

View file

@ -1,10 +1,22 @@
DISTRIBUTION_DEFAULT="sname"
NODE_DEFAULT="livebook_server"
if [ ! -z "$LIVEBOOK_CLUSTER" ]; then
DISTRIBUTION_DEFAULT="name";
fi
if [ "$LIVEBOOK_CLUSTER" = "fly" ]; then
export ERL_AFLAGS="-proto_dist inet6_tcp"
export LIVEBOOK_CLUSTER="dns:${FLY_APP_NAME}.internal"
NODE_DEFAULT="${FLY_APP_NAME}-${FLY_IMAGE_REF##*-}@${FLY_PRIVATE_IP}"
fi
if [ -f "${RELEASE_ROOT}/user/env.sh" ]; then if [ -f "${RELEASE_ROOT}/user/env.sh" ]; then
. "${RELEASE_ROOT}/user/env.sh" . "${RELEASE_ROOT}/user/env.sh"
fi fi
export RELEASE_MODE=interactive export RELEASE_MODE=interactive
export RELEASE_NODE=${LIVEBOOK_NODE:-${RELEASE_NODE:-livebook_server}} export RELEASE_NODE=${LIVEBOOK_NODE:-${RELEASE_NODE:-${NODE_DEFAULT}}}
if [ -z "${LIVEBOOK_CLUSTER}" ]; then DISTRIBUTION_DEFAULT="sname"; else DISTRIBUTION_DEFAULT="name"; fi
export RELEASE_DISTRIBUTION=${LIVEBOOK_DISTRIBUTION:-${RELEASE_DISTRIBUTION:-${DISTRIBUTION_DEFAULT}}} export RELEASE_DISTRIBUTION=${LIVEBOOK_DISTRIBUTION:-${RELEASE_DISTRIBUTION:-${DISTRIBUTION_DEFAULT}}}
if [ ! -z "${LIVEBOOK_COOKIE}" ]; then export RELEASE_COOKIE=${LIVEBOOK_COOKIE}; fi if [ ! -z "${LIVEBOOK_COOKIE}" ]; then export RELEASE_COOKIE=${LIVEBOOK_COOKIE}; fi

View file

@ -202,7 +202,7 @@ defmodule Livebook.Hubs.DockerfileTest do
dockerfile = Dockerfile.build_dockerfile(config, hub, [], [], file, [], %{}) dockerfile = Dockerfile.build_dockerfile(config, hub, [], [], file, [], %{})
assert dockerfile =~ ~s/export LIVEBOOK_CLUSTER="dns:${FLY_APP_NAME}.internal"/ assert dockerfile =~ ~s/ENV LIVEBOOK_CLUSTER "fly"/
end end
end end