Skip to content

Commit

Permalink
feat: add LiteFS example for Flipt in the examples directory (#2297)
Browse files Browse the repository at this point in the history
* feat: add LiteFS example for Flipt in the examples directory

* chore: add details section

* chore: add newline to end of README file

* chore: make changes to address comments on exposing ports
  • Loading branch information
yquansah authored Nov 1, 2023
1 parent 428c237 commit 85e5f3a
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 0 deletions.
16 changes: 16 additions & 0 deletions examples/database/litefs/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM alpine

COPY --from=flyio/litefs:0.5 /usr/local/bin/litefs /usr/local/bin/litefs
COPY --from=flipt/flipt:latest /flipt /usr/local/bin/flipt

RUN mkdir -p /etc/flipt/config && \
mkdir -p /var/opt/flipt

COPY --from=flipt/flipt:latest /etc/flipt/config/default.yml /etc/flipt/config/default.yml
RUN chown -R root:root /var/opt/flipt /etc/flipt

ADD litefs.yml /etc/litefs.yml

RUN apk add bash fuse3 sqlite ca-certificates

ENTRYPOINT litefs mount
25 changes: 25 additions & 0 deletions examples/database/litefs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# LiteFS

LiteFS is a distributed file system that replicates SQLite databases to other nodes at the file system level. It works by using [FUSE](https://www.kernel.org/doc/html/next/filesystems/fuse.html) to detect writes to the file system and determines how and if those should be replicated.

This example will demonstrate how to run Flipt over LiteFS.

## Requirements

- [Docker](https://www.docker.com/)
- [docker-compose](https://docs.docker.com/compose/install/)

## Running the Example

1. Run `docker-compose up` from this directory
1. Open the Flipt UI (default: [http://localhost:8080](http://localhost:8080))

## Details

`docker compose` will spin up two instances of Flipt with embedded SQLite databases. On top of these instances is an nginx proxy that will forward "write" requests, anything but `GET`, to the primary. `GET` requests will be served from the instance's embedded SQLite database.

LiteFS describes a few [caveats](https://fly.io/docs/litefs/proxy/#how-it-works) that must be kept in mind.

## Data

You can view the data on any one of the instances located at [primary](http://localhost:8081) or the [replica](http://localhost:8082), but remember that writes will only happen on the primary or the nginx proxy, the replica instance will not be able to accpet writes.
21 changes: 21 additions & 0 deletions examples/database/litefs/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: '3'
services:
nginx:
build: ./nginx
ports:
- "8080:80"
primary:
build: .
privileged: true
ports:
- "8081:8080"
environment:
IS_PRIMARY: "true"
replica:
build: .
ports:
- "8082:8080"
restart: on-failure
privileged: true
environment:
IS_PRIMARY: "false"
18 changes: 18 additions & 0 deletions examples/database/litefs/litefs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
log:
debug: true

fuse:
dir: "/var/opt/flipt"

data:
dir: "/var/lib/litefs"

lease:
type: "static"

advertise-url: "http://primary:20202"

candidate: $IS_PRIMARY

exec:
- cmd: "flipt"
5 changes: 5 additions & 0 deletions examples/database/litefs/nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM nginx:alpine

COPY ./nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
22 changes: 22 additions & 0 deletions examples/database/litefs/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
http {
upstream primary {
server primary:8080;
}
upstream all {
server primary:8080;
server replica:8080;
}
server {
listen 80;
location / {
# Basically all writes should go to this primary,
# while reads go to everything else.
if ($request_method ~ "(PUT|POST|PATCH|DELETE)") {
proxy_pass http://primary;
}
proxy_pass http://all;
}
}
}

events { }

0 comments on commit 85e5f3a

Please sign in to comment.