Skip to content

Commit

Permalink
feat: nginx kube zero-downtime wip (#1039)
Browse files Browse the repository at this point in the history
  • Loading branch information
devthejo authored Aug 28, 2023
1 parent 70976e1 commit d231475
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 5 deletions.
4 changes: 2 additions & 2 deletions nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM nginxinc/nginx-unprivileged:1.25-alpine@sha256:557e9af4afa7a36462e313906fe42fba39c307ae2a72d5323d49963eb2883b45

COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./entrypoint.sh /entrypoint.sh
COPY ./nginx.conf ./ready_response.conf /etc/nginx/
COPY ./entrypoint.sh ./pre-stop.sh /
COPY ./404.html /usr/share/nginx/errors/

## adjust permissions
Expand Down
25 changes: 24 additions & 1 deletion nginx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ add_header X-Content-Type-Options "nosniff";

> For a single-page-applications nginx image, see [../nginx4spa](../nginx4spa)

Notes:

- `PORT` is set to `8080`
Expand All @@ -28,3 +27,27 @@ COPY ./dist /usr/share/nginx/html
```

**Note**: follow security recommendations here: <https://socialgouv.github.io/support/#/securite>


## kubernetes integration with zero-downtime

```yaml
spec:
containers:
- name: my-app
image: ghcr.io/socialgouv/docker/nginx
ports:
- containerPort: 8080
livenessProbe:
httpGet:
path: /live
port: 8080
readinessProbe:
httpGet:
path: /ready
port: 8080
lifecycle:
preStop:
exec:
command: ["/pre-stop.sh"]
```
14 changes: 14 additions & 0 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,19 @@ http {
root /var/lib/nginx/html;
}

location /live {
default_type text/plain;
return 200 'OK';
}

include /etc/nginx/ready_response.conf;
location /ready {
default_type text/plain;
if ($ready_response = 'OK') {
return 200 $ready_response;
}
return 500 'Not Ready';
}

}
}
20 changes: 20 additions & 0 deletions nginx/pre-stop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env sh

echo "set \$ready_response 'Not Ready';" > /etc/nginx/conf.d/ready_response.conf
nginx -s reload

WAIT_TIME=30
if [ "$1" ]; then
WAIT_TIME="$1"
fi

for i in $(seq 1 $WAIT_TIME); do
if [ "$(nginx -s status | grep 'Active connections' | awk '{print $3}')" -eq "0" ]; then
exit 0
fi
sleep 1
done

nginx -s stop

exit 0
1 change: 1 addition & 0 deletions nginx/ready_response.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
set $ready_response 'OK';
4 changes: 2 additions & 2 deletions nginx4spa/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM nginxinc/nginx-unprivileged:1.25-alpine@sha256:557e9af4afa7a36462e313906fe42fba39c307ae2a72d5323d49963eb2883b45

COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./entrypoint.sh /entrypoint.sh
COPY ./nginx.conf ./ready_response.conf /etc/nginx/
COPY ./entrypoint.sh ./pre-stop.sh /

USER 0
RUN chown -R nginx:nginx /usr/share/nginx/html && chmod -R 755 /usr/share/nginx/html && \
Expand Down
24 changes: 24 additions & 0 deletions nginx4spa/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,27 @@ COPY ./dist /usr/share/nginx/html
```

**Note**: follow security recommendations here: <https://socialgouv.github.io/support/#/securite>


## kubernetes integration with zero-downtime

```yaml
spec:
containers:
- name: my-app
image: ghcr.io/socialgouv/docker/nginx
ports:
- containerPort: 8080
livenessProbe:
httpGet:
path: /live
port: 8080
readinessProbe:
httpGet:
path: /ready
port: 8080
lifecycle:
preStop:
exec:
command: ["/pre-stop.sh"]
```
14 changes: 14 additions & 0 deletions nginx4spa/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,19 @@ http {
root /var/lib/nginx/html;
}

location /live {
default_type text/plain;
return 200 'OK';
}

include /etc/nginx/ready_response.conf;
location /ready {
default_type text/plain;
if ($ready_response = 'OK') {
return 200 $ready_response;
}
return 500 'Not Ready';
}

}
}
20 changes: 20 additions & 0 deletions nginx4spa/pre-stop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env sh

echo "set \$ready_response 'Not Ready';" > /etc/nginx/conf.d/ready_response.conf
nginx -s reload

WAIT_TIME=30
if [ "$1" ]; then
WAIT_TIME="$1"
fi

for i in $(seq 1 $WAIT_TIME); do
if [ "$(nginx -s status | grep 'Active connections' | awk '{print $3}')" -eq "0" ]; then
exit 0
fi
sleep 1
done

nginx -s stop

exit 0
1 change: 1 addition & 0 deletions nginx4spa/ready_response.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
set $ready_response 'OK';

0 comments on commit d231475

Please sign in to comment.