Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit feb7708

Browse files
authored
Finish Docker Swarm (#54)
* Add files * Change nginx to haproxy * Update dependencies * Update README file * Server is working * Fix test errors * Show data from server * Serve static files for server * Cleanup
1 parent e06a09c commit feb7708

File tree

16 files changed

+366
-46
lines changed

16 files changed

+366
-46
lines changed

README.md

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,27 @@
44

55
The `server` directory contain a simple [Django](https://www.djangoproject.com/) app that expose an `api` of Django `users` with [Django REST framework](http://www.django-rest-framework.org/). The `client` directory contain an [Angular](https://angular.io/) simple app, built with [Angular-Cli](https://github.com/angular/angular-cli), [ngrx](https://github.com/ngrx) to handle state, [Angular Material](https://github.com/angular/material2) as a design library, have service worker, and ready to `AOT` compilation. The simple Angular app show the users from the Django api.
66

7-
The repo is a production ready app, that uses `nginx` to serve static files (the client app and static files from the server), and `gunicorn` for the server (python) stuff. All the parts are in a separate [Docker](https://www.docker.com/) containers and we use [kubernetes](https://kubernetes.io/) to manage them.
7+
The repo is a production ready app, that uses `nginx` to serve static files (the client app and static files from the server), and `gunicorn` for the server (python) stuff. All the parts are in a separate [Docker](https://www.docker.com/) containers and we use [Docker Swarm](https://docs.docker.com/engine/swarm/) to manage them.
8+
9+
We use [ELK Stack](https://www.elastic.co/products) for logging. The `server` and the `client` logs sent to logstash, and saved in elasticsearch. There is also a kibana instance to check and analyze all the logs.
810

911
## Pre Requirements
1012

1113
1. install [docker](https://www.docker.com/).
12-
2. Don't know yet.
1314

1415
## Installation
1516

1617
Automatic installation of the project with docker, for development.
1718

1819
1. In `client` directory run `docker build -t client .` to build the Docker image.
19-
2. Run ```docker run -dit -v `pwd`:/usr/src -p 4200:4200 --name=client-con client``` to run a container from that image.
20-
3. Open the browser at [http://localhost:4200](http://localhost:4200) to see your Angular (client) app.
21-
4. In `server` directory run `docker build -t server .` to build the Docker image.
22-
5. Run ```docker run -dit -v `pwd`:/usr/src -p 8000:8000 --name=server-con server``` to run a container from that image.
20+
2. In `server` directory run `docker build -t server .` to build the Docker image.
21+
3. To create a swarm `docker swarm init`.
22+
4. Run `docker stack deploy --compose-file=docker-compose.yml prod`
23+
5. Open the browser at [http://localhost](http://localhost) to see your Angular (client) app.
2324
6. Open the browser at [http://localhost:8000](http://localhost:8000) to see your Django (server) app.
25+
7. Open the browser at [http://localhost:5601](http://localhost:5601) to see Kibana and check your logs.
2426

25-
If you want to install the project manually, go to the `/client` or `/server` directories and read the `README` file.
27+
**If you want to install the project manually, go to the `/client` or `/server` directories and read the `README` file.**
2628

2729
## Our Stack
2830

@@ -36,7 +38,35 @@ If you want to install the project manually, go to the `/client` or `/server` di
3638
* [Angular Material](https://material.angular.io/)
3739
* [ngrx](https://github.com/ngrx)
3840
* [Django REST framework](http://www.django-rest-framework.org/)
39-
* [kubernetes](https://kubernetes.io/)
41+
* [django-admin-honeypot](http://django-admin-honeypot.readthedocs.io/en/latest/)
42+
* [ELK Stack](https://www.elastic.co/products)
43+
* [Docker Swarm](https://docs.docker.com/engine/swarm/)
44+
45+
## Tests
46+
47+
There is already tests for the `server` and the `client`, we currently at **+90** percent coverage.
48+
49+
We also write some tests for doing load test with [locust](http://locust.io/), you can find it under `server/stress_tests/`. To do a load test just install locust and write
50+
51+
```
52+
locust --host=http://localhost
53+
```
54+
55+
Then open up Locust’s web interface [http://localhost:8089](http://localhost:8089).
56+
57+
## Rolling Updates
58+
59+
To update the any of the containers that are in a service with a new image just create a new image, for example
60+
61+
```
62+
docker build -t server:v2 .
63+
```
64+
65+
And then update the service with the new image
66+
67+
```
68+
docker service update --image server:v2 prod_server
69+
```
4070

4171
## Contribute
4272

client/Dockerfile

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
FROM node:latest
1+
FROM node:8.1.2
22
MAINTAINER Nir Galon <[email protected]>
33

4-
RUN useradd --user-group --create-home --shell /bin/false app
4+
# Install nginx
5+
RUN apt-get update && apt-get install -y --no-install-recommends \
6+
nginx-light
57

6-
ENV APP_NAME "client"
7-
ENV APP_USER "app"
8-
ENV HOME /home/$APP_USER
9-
ENV APP_DIR $HOME/$APP_NAME
8+
# nginx files
9+
COPY ./nginx /etc/nginx
1010

11-
RUN npm install --global --silent angular-cli
11+
# Create app directory
12+
RUN mkdir -p /usr/src/app
13+
WORKDIR /usr/src/app
1214

13-
WORKDIR $APP_DIR
14-
COPY package.json $APP_DIR/package.json
15-
RUN npm install --silent && npm cache clean
16-
COPY . $APP_DIR
17-
RUN chown -R $APP_USER:$APP_USER $HOME/*
15+
# Install app dependencies
16+
COPY package.json /usr/src/app/
17+
RUN npm install
1818

19-
USER $APP_USER
20-
WORKDIR $APP_DIR
19+
# Bundle app source
20+
COPY . /usr/src/app
21+
RUN npm run build -- --prod
22+
RUN npm run sw
2123

2224
EXPOSE 4200
23-
24-
CMD ["npm", "start"]
25+
CMD ["nginx", "-g", "daemon off;"]

client/nginx/mime.types

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
types {
2+
text/html html htm shtml;
3+
text/css css;
4+
text/xml xml;
5+
image/gif gif;
6+
image/jpeg jpeg jpg;
7+
application/x-javascript js;
8+
application/atom+xml atom;
9+
application/rss+xml rss;
10+
11+
text/mathml mml;
12+
text/plain txt;
13+
text/vnd.sun.j2me.app-descriptor jad;
14+
text/vnd.wap.wml wml;
15+
text/x-component htc;
16+
17+
image/png png;
18+
image/tiff tif tiff;
19+
image/vnd.wap.wbmp wbmp;
20+
image/x-icon ico;
21+
image/x-jng jng;
22+
image/x-ms-bmp bmp;
23+
image/svg+xml svg svgz;
24+
image/webp webp;
25+
26+
application/java-archive jar war ear;
27+
application/mac-binhex40 hqx;
28+
application/msword doc;
29+
application/pdf pdf;
30+
application/postscript ps eps ai;
31+
application/rtf rtf;
32+
application/vnd.ms-excel xls;
33+
application/vnd.ms-powerpoint ppt;
34+
application/vnd.wap.wmlc wmlc;
35+
application/vnd.google-earth.kml+xml kml;
36+
application/vnd.google-earth.kmz kmz;
37+
application/x-7z-compressed 7z;
38+
application/x-cocoa cco;
39+
application/x-java-archive-diff jardiff;
40+
application/x-java-jnlp-file jnlp;
41+
application/x-makeself run;
42+
application/x-perl pl pm;
43+
application/x-pilot prc pdb;
44+
application/x-rar-compressed rar;
45+
application/x-redhat-package-manager rpm;
46+
application/x-sea sea;
47+
application/x-shockwave-flash swf;
48+
application/x-stuffit sit;
49+
application/x-tcl tcl tk;
50+
application/x-x509-ca-cert der pem crt;
51+
application/x-xpinstall xpi;
52+
application/xhtml+xml xhtml;
53+
application/zip zip;
54+
55+
application/octet-stream bin exe dll;
56+
application/octet-stream deb;
57+
application/octet-stream dmg;
58+
application/octet-stream eot;
59+
application/octet-stream iso img;
60+
application/octet-stream msi msp msm;
61+
62+
audio/midi mid midi kar;
63+
audio/mpeg mp3;
64+
audio/ogg ogg;
65+
audio/x-m4a m4a;
66+
audio/x-realaudio ra;
67+
68+
video/3gpp 3gpp 3gp;
69+
video/mp4 mp4;
70+
video/mpeg mpeg mpg;
71+
video/quicktime mov;
72+
video/webm webm;
73+
video/x-flv flv;
74+
video/x-m4v m4v;
75+
video/x-mng mng;
76+
video/x-ms-asf asx asf;
77+
video/x-ms-wmv wmv;
78+
video/x-msvideo avi;
79+
}

client/nginx/nginx.conf

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- mode: nginx; mode: flyspell-prog; ispell-local-dictionary: "american" -*-
2+
3+
### Very basic Nginx configuration.
4+
user www-data;
5+
worker_processes auto;
6+
error_log /var/log/nginx/error.log;
7+
8+
events {
9+
worker_connections 4096;
10+
multi_accept on;
11+
}
12+
13+
http {
14+
include mime.types;
15+
default_type application/octet-stream;
16+
17+
access_log /var/log/nginx/access.log;
18+
19+
sendfile on;
20+
tcp_nopush on;
21+
22+
keepalive_timeout 10;
23+
tcp_nodelay on;
24+
gzip on;
25+
gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css;
26+
27+
server {
28+
listen 4200;
29+
30+
access_log /var/log/nginx/angular_access.log;
31+
error_log /var/log/nginx/angular_error.log;
32+
root /usr/src/app/dist;
33+
34+
location /favicon.ico {
35+
alias static/favicon.ico;
36+
}
37+
38+
}
39+
40+
}

client/package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,28 @@
1313
},
1414
"private": false,
1515
"dependencies": {
16-
"@angular/animations": "^4.1.1",
16+
"@angular/animations": "^4.2.5",
1717
"@angular/common": "^4.0.0",
1818
"@angular/compiler": "^4.0.0",
1919
"@angular/core": "^4.0.0",
2020
"@angular/forms": "^4.0.0",
2121
"@angular/http": "^4.0.0",
22-
"@angular/material": "^2.0.0-beta.3",
22+
"@angular/material": "^2.0.0-beta.8",
2323
"@angular/platform-browser": "^4.0.0",
2424
"@angular/platform-browser-dynamic": "^4.0.0",
2525
"@angular/router": "^4.0.0",
2626
"@ngrx/core": "^1.2.0",
2727
"@ngrx/effects": "^2.0.3",
2828
"@ngrx/store": "^2.2.2",
29+
"@ngrx/store-devtools": "^3.2.4",
2930
"core-js": "^2.4.1",
3031
"hammerjs": "^2.0.8",
31-
"reselect": "^3.0.0",
32-
"rxjs": "^5.1.0",
32+
"reselect": "^3.0.1",
33+
"rxjs": "^5.4.2",
3334
"zone.js": "^0.8.4"
3435
},
3536
"devDependencies": {
37+
"@angular/cdk": "^2.0.0-beta.8",
3638
"@angular/cli": "1.0.2",
3739
"@angular/compiler-cli": "^4.0.0",
3840
"@ngrx/store-devtools": "^3.2.4",

client/src/app/components/users/users.component.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Observable } from 'rxjs/Observable';
77
import { MaterialModule } from '@angular/material';
88
import { reducer } from '../../reducers';
99
import { StoreModule } from '@ngrx/store';
10+
import 'rxjs/add/observable/of';
1011

1112
import { UsersComponent } from './users.component';
1213

client/src/app/services/user.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Injectable } from '@angular/core';
22
import { Http, Response } from '@angular/http';
33
import { Observable } from 'rxjs/Rx';
44

5+
import { environment } from '../../environments/environment';
56
import { User } from '../models/user';
67

78
@Injectable()
@@ -12,7 +13,7 @@ export class UserService {
1213
) { }
1314

1415
getUsers(): Observable<User[]> {
15-
return this.http.get('/api/users')
16+
return this.http.get(`${environment.server}/api/users`)
1617
.map(res => res.json())
1718
.catch(this.handleError);
1819
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export const environment = {
2-
production: true
2+
production: true,
3+
server: 'http://localhost:8000'
34
};

client/src/environments/environment.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
// The list of which env maps to which file can be found in `.angular-cli.json`.
55

66
export const environment = {
7-
production: false
7+
production: false,
8+
server: 'http://localhost:8000'
89
};

codecov.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ parsers:
3131
javascript:
3232
enable_partials: false
3333
ignore:
34-
- "client/src/app/util.ts"
3534
- "server/manage.py"
3635
- "server/config"
3736
- "server/**/apps.py"

0 commit comments

Comments
 (0)