Skip to content

Commit

Permalink
feat(examples): Add nginx-golang compose example
Browse files Browse the repository at this point in the history
This example is dervied from the nginx-golang
docker/awesome-compose example.

Signed-off-by: Luca Seritan <[email protected]>
  • Loading branch information
LucaSeri committed Jun 20, 2024
1 parent 3dd4c28 commit 932591a
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 0 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/examples-nginx-golang.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: examples/nginx-golang

on:
repository_dispatch:
types: [core_merge]

workflow_dispatch:

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

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

pull_request:
types: [opened, synchronize, reopened]
branches: [main]
paths:
- 'examples/nginx-golang/**'
- '.github/workflows/examples-nginx-golang.yaml'
- '!examples/nginx-golang/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-golang
runtimedir: /github/workspace/.kraftkit
privileged: true
run: |
set -xe
sudo KRAFTKIT_NO_WARN_SUDO=1 kraft compose up -d
curl localhost:8080
95 changes: 95 additions & 0 deletions examples/nginx-golang/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
## Compose sample application

### NGINX proxy with Go backend

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

Project structure:

```bash
.
├── backend
│   ├── Kraftfile
│   ├── Dockerfile
│   └── main.go
├── compose.yaml
├── proxy
│   └── nginx.conf
└── README.md
```

[`compose.yaml`](compose.yaml)

```yml
services:
proxy:
image: nginx:1.25
volumes:
- ./proxy:/etc/nginx
ports:
- 8080:80
depends_on:
- backend
networks:
internal:

backend:
build:
context: backend
networks:
internal:
ipv4_address: 172.23.0.2

networks:
internal:
ipam:
config:
- subnet: 172.23.0.1/16
```
The compose file defines an application with two services `proxy` and `backend`.
When deploying the application, kraft compose maps port 80 of the frontend service VM to port 8080 of the host as specified in the file.
Make sure port 8080 on the host is not already in use.

## Deploy with kraft compose

```bash
$ kraft compose up -d
creating network nginx-golang_internal...
nginx-golang_internal
i building service backend...
...
nginx-golang-backend
nginx-golang-proxy
```

## Expected result

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

```bash
$ kraft compose ps
NAME KERNEL ARGS CREATED STATUS MEM PORTS PLAT
nginx-golang-backend oci://unikraft.org/base:latest /server 23 seconds ago running 64M qemu/x86_64
nginx-golang-proxy oci://nginx:1.25 /usr/bin/nginx 22 seconds ago running 64M 0.0.0.0:8080->80/tcp qemu/x86_64
```

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

```bash
$ curl localhost:8080
.--------------------.
( Hello from Unikraft! )
'--------------------'
\\
\\
_
c'o'o .--.
(| |)_/
```

Stop and remove the VMs

```bash
kraft compose down
```
29 changes: 29 additions & 0 deletions examples/nginx-golang/backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
FROM golang:1.21.3-bookworm AS build

WORKDIR /src

COPY ./main.go /src/server.go

COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod/cache \
go mod download

COPY . .

RUN set -xe; \
CGO_ENABLED=1 \
go build \
-buildmode=pie \
-ldflags "-linkmode external -extldflags '-static-pie'" \
-tags netgo \
-o /server server.go \
;

FROM scratch

COPY --from=build /server /server
COPY --from=build /lib/x86_64-linux-gnu/libc.so.6 /lib/x86_64-linux-gnu/
COPY --from=build /lib64/ld-linux-x86-64.so.2 /lib64/

CMD ["/server"]

8 changes: 8 additions & 0 deletions examples/nginx-golang/backend/Kraftfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
spec: v0.6

runtime: base:latest

rootfs: ./Dockerfile

cmd: ["/server"]

5 changes: 5 additions & 0 deletions examples/nginx-golang/backend/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/unikraft/catalog/examples/nginx-golang/backend

go 1.18

require github.com/go-chi/chi/v5 v5.0.7
2 changes: 2 additions & 0 deletions examples/nginx-golang/backend/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8=
github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
35 changes: 35 additions & 0 deletions examples/nginx-golang/backend/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"fmt"
"log"
"net/http"

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)

func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(
w, `
.--------------------.
( Hello from Unikraft! )
'--------------------'
\\
\\
_
c'o'o .--.
(| |)_/
`,
)
}

func main() {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Get("/", handler)

fmt.Println("Go backend started!")
log.Fatal(http.ListenAndServe(":80", r))
}
25 changes: 25 additions & 0 deletions examples/nginx-golang/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
services:
proxy:
image: nginx:1.25
volumes:
- ./proxy:/etc/nginx
ports:
- 8080:80
depends_on:
- backend
networks:
internal:

backend:
build:
context: backend
networks:
internal:
ipv4_address: 172.23.0.2

networks:
internal:
ipam:
config:
- subnet: 172.23.0.1/16
26 changes: 26 additions & 0 deletions examples/nginx-golang/proxy/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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;

server {
listen 80;
server_name localhost;

location / {
proxy_pass http://172.23.0.2;
}
}
}

0 comments on commit 932591a

Please sign in to comment.