Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(examples): Add nginx-nodejs-redis compose example #125

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading