Skip to content

Commit

Permalink
feat(examples): Add nginx-nodejs-redis compose example
Browse files Browse the repository at this point in the history
This example was derived from the analog example from docker/awesome-compose.

Signed-off-by: Luca Seritan <[email protected]>
  • Loading branch information
LucaSeri committed Jun 22, 2024
1 parent 3dd4c28 commit 8b75254
Show file tree
Hide file tree
Showing 10 changed files with 937 additions and 0 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/examples-nginx-nodejs-redis.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: examples/nginx-nodejs-redis

on:
repository_dispatch:
types: [core_merge]

workflow_dispatch:

schedule:
- cron: '0 0 * * *' # Everyday at 12AM

push:
branches: [main]
paths:
- 'examples/nginx-nodejs-redis/**'
- '.github/workflows/examples-nginx-nodejs-redis.yaml'
- '!examples/nginx-nodejs-redis/README.md'

pull_request:
types: [opened, synchronize, reopened]
branches: [main]
paths:
- 'examples/nginx-nodejs-redis/**'
- '.github/workflows/examples-nginx-nodejs-redis.yaml'
- '!examples/nginx-nodejs-redis/README.md'

jobs:
up:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: kraft compose up
uses: unikraft/kraftkit@staging
with:
loglevel: debug
workdir: examples/nginx-nodejs-redis
runtimedir: /github/workspace/.kraftkit
privileged: true
run: |
set -xe
sudo KRAFTKIT_NO_WARN_SUDO=1 kraft compose up -d
curl -s localhost:8080
curl -s localhost:8080
131 changes: 131 additions & 0 deletions examples/nginx-nodejs-redis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
## Compose sample application

## Node.js application with Nginx proxy and Redis database

This example was derived from the [nginx-nodejs-redis docker/awesome-compose example](https://github.com/docker/awesome-compose/tree/master/nginx-nodejs-redis).

Project structure:

```bash
.
├── README.md
├── compose.yaml
├── nginx
│   └── nginx.conf
└── web
├── Kraftfile
├── Dockerfile
├── package.json
└── server.js

2 directories, 7 files


```

[_compose.yaml_](compose.yaml)

```yaml
services:
redis:
image: redis:7.2
ports:
- '6379:6379'
networks:
internal:
ipv4_address: 172.23.0.2
mem_reservation: 512M

web1:
build: ./web
hostname: web1
networks:
internal:
ipv4_address: 172.23.0.3
mem_reservation: 512M
depends_on:
- redis

web2:
build: ./web
hostname: web2
networks:
internal:
ipv4_address: 172.23.0.4
mem_reservation: 512M
depends_on:
- redis

nginx:
image: nginx:1.25
ports:
- '8080:80'
volumes:
- ./nginx:/etc/nginx
depends_on:
- web1
- web2
networks:
internal:

networks:
internal:
ipam:
config:
- subnet: 172.23.0.1/16
```
The compose file defines an application with four services `redis`, `nginx`, `web1` and `web2`.
When deploying the application, docker compose maps port 80 of the nginx service VM to port 8080 of the host as specified in the file.

> **_INFO_**
> Redis runs on port 6379 by default.
> Make sure port 6379 on the host is not being used by another VM, otherwise the port should be changed.

## Deploy with kraft compose

```bash
$ sudo kraft compose up -d
nginx-nodejs-redis-redis
nginx-nodejs-redis-web1
nginx-nodejs-redis-web2
nginx-nodejs-redis-nginx
```

## Expected result

Listing VMs must show four VMs running and the port mapping as below:

```bash
$ sudo kraft compose ps
NAME KERNEL ARGS CREATED STATUS MEM PORTS PLAT
nginx-nodejs-redis-nginx oci://nginx:1.25 /usr/bin/nginx 2 minutes ago running 64M 0.0.0.0:8080->80/tcp qemu/x86_64
nginx-nodejs-redis-redis oci://redis:7.2 /usr/bin/redis-server /etc/redis/redis.conf 2 minutes ago running 512M 0.0.0.0:6379->6379/tcp qemu/x86_64
nginx-nodejs-redis-web1 oci://unikraft.org/node:21 /usr/bin/node /usr/src/server.js 2 minutes ago running 512M qemu/x86_64
nginx-nodejs-redis-web2 oci://unikraft.org/node:21 /usr/bin/node /usr/src/server.js 2 minutes ago running 512M qemu/x86_64
```

## Testing the app

After the application starts, navigate to `http://localhost:8080` in your web browser or run:

```bash
$ curl localhost:8080
web1: Total number of visits is: 1
```

```bash
$ curl localhost:8080
web2: Total number of visits is: 2
```

```bash
$ curl localhost:8080
web1: Total number of visits is: 3
```

## Stop and remove the VMs

```bash
sudo kraft compose down
```
52 changes: 52 additions & 0 deletions examples/nginx-nodejs-redis/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
services:
redis:
image: redis:7.2
ports:
- '6379:6379'
networks:
internal:
ipv4_address: 172.23.0.2
mem_reservation: 512M

web1:
build: ./web
hostname: web1
ports:
- '81:5000'
networks:
internal:
ipv4_address: 172.23.0.3
mem_reservation: 512M
depends_on:
- redis

web2:
build: ./web
hostname: web2
ports:
- '82:5000'
networks:
internal:
ipv4_address: 172.23.0.4
mem_reservation: 512M
depends_on:
- redis

nginx:
image: nginx:1.25
ports:
- '8080:80'
volumes:
- ./nginx:/etc/nginx
depends_on:
- web1
- web2
networks:
internal:

networks:
internal:
ipam:
config:
- subnet: 172.23.0.1/16

31 changes: 31 additions & 0 deletions examples/nginx-nodejs-redis/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
worker_processes 1;
daemon off;
master_process off;
user root root;

events {
worker_connections 32;
}

http {
error_log stderr error;
access_log off;

keepalive_timeout 10s;
keepalive_requests 10000;
send_timeout 10s;

upstream backend_servers {
server 172.23.0.3:5000;
server 172.23.0.4:5000;
}

server {
listen 80;
server_name localhost;

location / {
proxy_pass http://backend_servers;
}
}
}
1 change: 1 addition & 0 deletions examples/nginx-nodejs-redis/web/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
12 changes: 12 additions & 0 deletions examples/nginx-nodejs-redis/web/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM node:21-alpine AS build

WORKDIR /usr/src/app

COPY package.json package-lock.json ./
RUN npm ci

FROM scratch

COPY --from=build /etc/os-release /etc/os-release
COPY --from=build /usr/src/app/node_modules /usr/src/node_modules
COPY server.js /usr/src/server.js
7 changes: 7 additions & 0 deletions examples/nginx-nodejs-redis/web/Kraftfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
spec: v0.6

runtime: node:21

rootfs: ./Dockerfile

cmd: ["/usr/bin/node", "/usr/src/server.js"]
Loading

0 comments on commit 8b75254

Please sign in to comment.