Skip to content

Commit cb7d80c

Browse files
magdaleneDarla Shockley
and
Darla Shockley
authored
Add docker-compose setup example (superfly#6)
* gitignore text editor backup files * Move fly.io specific config to subdirectory * Add Docker/docker-compose config * Update READMEs/Dockerfile comments explaining the docker setup * Remove fly proxy config from docker-compose litefs yml files * Set candidate based on env var for docker setup --------- Co-authored-by: Darla Shockley <[email protected]>
1 parent 5aeea71 commit cb7d80c

File tree

10 files changed

+241
-93
lines changed

10 files changed

+241
-93
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
fly.toml
2+
*~
3+
*#

Dockerfile

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
# Build our application using a Go builder.
22
FROM golang:1.20 AS builder
3+
34
WORKDIR /src/litefs-example
45
COPY . .
56
RUN go build -buildvcs=false -ldflags "-s -w -extldflags '-static'" -tags osusergo,netgo -o /usr/local/bin/litefs-example ./cmd/litefs-example
67

78

89
# Our final Docker image stage starts here.
910
FROM alpine
11+
ARG LITEFS_CONFIG=litefs.yml
1012

1113
# Copy binaries from the previous build stages.
1214
COPY --from=flyio/litefs:0.4 /usr/local/bin/litefs /usr/local/bin/litefs
1315
COPY --from=builder /usr/local/bin/litefs-example /usr/local/bin/litefs-example
1416

15-
# Copy our LiteFS configuration.
16-
ADD etc/litefs.yml /etc/litefs.yml
17+
# Copy the possible LiteFS configurations.
18+
ADD fly-io-config/etc/litefs.yml /tmp/litefs.yml
19+
ADD docker-config/etc/litefs.static-lease.yml /tmp/litefs.static-lease.yml
20+
21+
# Move the appropriate LiteFS config file to /etc/ (this one will be
22+
# used by LiteFS). By default this is the config file used on Fly.io,
23+
# but it's set appropriately to other files for the docker setup in
24+
# docker-compose.yml
25+
RUN cp /tmp/$LITEFS_CONFIG /etc/litefs.yml
1726

1827
# Setup our environment to include FUSE & SQLite. We install ca-certificates
1928
# so we can communicate with the Consul server over HTTPS. cURL is added so

README.md

+6-91
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,11 @@
11
LiteFS Example Application
22
==========================
33

4-
This repository is an example of a toy application running on LiteFS on Fly.io.
4+
This repository is an example of a toy application running on LiteFS. You can
5+
test it out by deploying to Fly.io, or locally with a docker-compose setup.
56

6-
## Prerequisites
7+
* [Fly.io instructions](./fly-io-config)
8+
* [Docker-compose instructions](./docker-config)
79

8-
First, you'll need to install [`flyctl`][] and then [sign up for a free account][signup].
9-
10-
[`flyctl`]: https://fly.io/docs/hands-on/install-flyctl/
11-
[signup]: https://fly.io/docs/hands-on/sign-up/
12-
13-
14-
## Usage
15-
16-
### Creating the application
17-
18-
First, we'll need to create our application on [Fly.io](https://fly.io). This
19-
will prompt you for a name or you can autogenerate one for this example.
20-
Remember this name as we'll need it later.
21-
22-
```sh
23-
fly launch --region ord --no-deploy
24-
```
25-
26-
The launch command will create a `fly.toml` file and set the primary region to
27-
Chicago (`ord`) but will not launch the app.
28-
29-
30-
### Creating a volume
31-
32-
Next, we need to set up a persistent volume in our primary region so that our
33-
data is not lost between restarts.
34-
35-
```sh
36-
fly volumes create -r ord --size 1 litefs
37-
```
38-
39-
And add a mount to this volume in your `fly.toml` file:
40-
41-
```toml
42-
[mounts]
43-
source = "litefs"
44-
destination = "/var/lib/litefs"
45-
```
46-
47-
### Setting up Consul
48-
49-
LiteFS uses [Consul](https://consul.io) for its distributed lease. You can find
50-
instructions for using Fly.io's free multi-tenant Consul in the
51-
[Lease Management][] section of the Getting Started guide.
52-
53-
[Lease Management]: https://fly.io/docs/litefs/getting-started/#lease-configuration
54-
55-
56-
### Launching your app
57-
58-
The next step is to launch & deploy your app with the following command:
59-
60-
```sh
61-
fly deploy
62-
```
63-
64-
The application should build and deploy and you should see it up in running
65-
after a minute or so. You can go to `https://$APPNAME.fly.dev/` and see your
66-
application running live.
67-
68-
The application is a simple interface for generating fake records. It's just
69-
for illustrating how LiteFS can easily replicate your data between nodes.
70-
71-
When you click the _"Generate Record"_ button, it will create that row in a
72-
local SQLite database that is running on a LiteFS file system. Any other node
73-
running LiteFS will automatically get those updates and apply them to their
74-
local copy of the database. That lets every node keep an exact copy of the same
75-
database.
76-
77-
78-
### Launching more regions
79-
80-
This example application is configured to run as a primary only in the
81-
`PRIMARY_REGION` (which is Chicago). It's best practice to run two or more
82-
instances in the primary region and then you can add instances in additional
83-
regions to reduce latency for your users.
84-
85-
You can clone the configuration of the machine to other regions by using the
86-
`fly m clone` command. The `--select` flag lets you choose from a list of
87-
existing machines to clone.
88-
89-
```sh
90-
# Make a second instance in your primary region.
91-
fly m clone --select --region ord
92-
93-
# Make additional instances in regions around the world (London, Sydney, etc).
94-
fly m clone --select --region lhr
95-
fly m clone --select --region syd
96-
```
10+
**Note: commands should be run from the top-level directory for both Fly.io and
11+
local docker-compose (not from the location of the README files above).**

docker-compose.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
version: '3'
2+
services:
3+
nginx:
4+
build:
5+
context: ./docker-config/nginx
6+
ports:
7+
- "8080:80"
8+
primary:
9+
build:
10+
context: .
11+
args:
12+
- "LITEFS_CONFIG=litefs.static-lease.yml"
13+
ports:
14+
- "8081:8081"
15+
privileged: true
16+
environment:
17+
FLY_REGION: primary
18+
IS_PRIMARY: "true"
19+
replica1:
20+
build:
21+
context: .
22+
args:
23+
- "LITEFS_CONFIG=litefs.static-lease.yml"
24+
ports:
25+
- "8082:8081"
26+
privileged: true
27+
environment:
28+
FLY_REGION: replica1
29+
IS_PRIMARY: "false"

docker-config/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
LiteFS Example Application with Docker
2+
======================================
3+
4+
## Prerequisites
5+
6+
You need to [install Docker][] (and docker-compose, which is included in all
7+
recent releases of Docker).
8+
9+
[install Docker]: https://docs.docker.com/engine/install/
10+
11+
## Usage
12+
13+
### Running the app
14+
15+
You can run the application with this command (from the top-level directory
16+
of the repo):
17+
18+
```bash
19+
docker-compose up
20+
```
21+
22+
### Using the app locally
23+
24+
The app is configured with the following containers:
25+
26+
* `primary` - this is the application with LiteFS running as primary
27+
(all writes happen on this node)
28+
* `replica1` - this is the application running with LiteFS as a replica
29+
* `nginx` - nginx configured as a load balancer, and also configured to
30+
route all `POST` requests to the primary node
31+
32+
Each of these is available to test out locally:
33+
34+
* http://localhost:8080 - the load balancer
35+
* http://localhost:8081 - the primary
36+
* http://localhost:8082 - the replica
37+
38+
You can test generating records on each node (it will fail if attempted on
39+
the replica).
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# note: there should be one template for this, which you can update as necessary.
2+
# note: had to leave the getting started guide and go to the lease management section bc not using fly
3+
# note: lease.hostname and lease.advertise-url are not clear
4+
# note: we should have an example docker-compose setup with everything working both with consul
5+
# and static leases
6+
# This directory is where your application will access the database.
7+
fuse:
8+
dir: "/litefs"
9+
10+
# This directory is where LiteFS will store internal data.
11+
# You must place this directory on a persistent volume.
12+
data:
13+
dir: "/var/lib/litefs"
14+
15+
# The lease section defines how LiteFS creates a cluster and
16+
# implements leader election. For dynamic clusters, use the
17+
# "consul". This allows the primary to change automatically when
18+
# the current primary goes down. For a simpler setup, use
19+
# "static" which assigns a single node to be the primary and does
20+
# not failover.
21+
lease:
22+
# Required. Must be either "consul" or "static".
23+
type: "static"
24+
25+
# Required. The URL for this node's LiteFS API.
26+
# Should match HTTP port.
27+
advertise-url: "http://primary:20202"
28+
29+
# Specifies whether the node can become the primary. If using
30+
# "static" leasing, this should be set to true on the primary
31+
# and false on the replicas.
32+
candidate: $IS_PRIMARY
33+
34+
exec:
35+
- cmd: "litefs-example -addr :8081 -dsn /litefs/db"

docker-config/nginx/Dockerfile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM nginx:alpine
2+
3+
COPY ./nginx.conf /etc/nginx/nginx.conf
4+
EXPOSE 80
5+
CMD ["nginx", "-g", "daemon off;"]

docker-config/nginx/nginx.conf

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
http {
2+
upstream primary {
3+
server primary:8081;
4+
}
5+
upstream all {
6+
server primary:8081;
7+
server replica1:8081;
8+
}
9+
server {
10+
listen 80;
11+
location / {
12+
if ($request_method ~ "(PUT|POST|PATCH|DELETE)") {
13+
proxy_pass http://primary;
14+
}
15+
proxy_pass http://all;
16+
}
17+
}
18+
}
19+
20+
events { }

fly-io-config/README.md

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
LiteFS Example Application on Fly.io
2+
====================================
3+
4+
## Prerequisites
5+
6+
First, you'll need to install [`flyctl`][] and then [sign up for a free account][signup].
7+
8+
[`flyctl`]: https://fly.io/docs/hands-on/install-flyctl/
9+
[signup]: https://fly.io/docs/hands-on/sign-up/
10+
11+
12+
## Usage
13+
14+
### Creating the application
15+
16+
First, we'll need to create our application on [Fly.io](https://fly.io). This
17+
will prompt you for a name or you can autogenerate one for this example.
18+
Remember this name as we'll need it later.
19+
20+
```sh
21+
fly launch --region ord --no-deploy
22+
```
23+
24+
The launch command will create a `fly.toml` file and set the primary region to
25+
Chicago (`ord`) but will not launch the app.
26+
27+
28+
### Creating a volume
29+
30+
Next, we need to set up a persistent volume in our primary region so that our
31+
data is not lost between restarts.
32+
33+
```sh
34+
fly volumes create -r ord --size 1 litefs
35+
```
36+
37+
And add a mount to this volume in your `fly.toml` file:
38+
39+
```toml
40+
[mounts]
41+
source = "litefs"
42+
destination = "/var/lib/litefs"
43+
```
44+
45+
### Setting up Consul
46+
47+
LiteFS uses [Consul](https://consul.io) for its distributed lease. You can find
48+
instructions for using Fly.io's free multi-tenant Consul in the
49+
[Lease Management][] section of the Getting Started guide.
50+
51+
[Lease Management]: https://fly.io/docs/litefs/getting-started/#lease-configuration
52+
53+
54+
### Launching your app
55+
56+
The next step is to launch & deploy your app with the following command:
57+
58+
```sh
59+
fly deploy
60+
```
61+
62+
The application should build and deploy and you should see it up in running
63+
after a minute or so. You can go to `https://$APPNAME.fly.dev/` and see your
64+
application running live.
65+
66+
The application is a simple interface for generating fake records. It's just
67+
for illustrating how LiteFS can easily replicate your data between nodes.
68+
69+
When you click the _"Generate Record"_ button, it will create that row in a
70+
local SQLite database that is running on a LiteFS file system. Any other node
71+
running LiteFS will automatically get those updates and apply them to their
72+
local copy of the database. That lets every node keep an exact copy of the same
73+
database.
74+
75+
76+
### Launching more regions
77+
78+
This example application is configured to run as a primary only in the
79+
`PRIMARY_REGION` (which is Chicago). It's best practice to run two or more
80+
instances in the primary region and then you can add instances in additional
81+
regions to reduce latency for your users.
82+
83+
You can clone the configuration of the machine to other regions by using the
84+
`fly m clone` command. The `--select` flag lets you choose from a list of
85+
existing machines to clone.
86+
87+
```sh
88+
# Make a second instance in your primary region.
89+
fly m clone --select --region ord
90+
91+
# Make additional instances in regions around the world (London, Sydney, etc).
92+
fly m clone --select --region lhr
93+
fly m clone --select --region syd
94+
```
File renamed without changes.

0 commit comments

Comments
 (0)