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

Commit d680f24

Browse files
authored
Move to kubernetes (#100)
* Cleanup * Add cloudbuild.yaml file for angular client app * Add server Dockerfile and kubernetes files * Change readme
1 parent 4b2339d commit d680f24

14 files changed

+225
-205
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# Created by https://www.gitignore.io/api/python,django,node,sass,virtualenv,jetbrains
32

43
### Python ###

README.md

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,57 @@
1-
<img src="https://raw.githubusercontent.com/nirgn975/Angular-Django-Seed-Project/master/angular-django-seed-project.png" />
1+
<img src="https://raw.githubusercontent.com/nirgn975/Angular-Django-Seed-Project/master/art/angular-django-seed-project.png" />
22

33
[![license][license-image]][license-url] [![GitHub release][github-image]][github-url] [![Build Status][travis-image]][travis-url] [![codecov][codecov-image]][codecov-url] [![Dependency Status][dependencyci-image]][dependencyci-url] [![Codacy Badge][codacy-image]][codacy-url] [![Maintenance][maintenance-image]][maintenance-url] [![Donate][donate-image]][donate-url]
44

55
This repo is a production ready seed project. The app shows a list of users.
66

77
## Structure
88

9-
* The `client` service is a build of the `client` directory. It contain an [Angular](https://angular.io/) 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 `AOT` compiled. The app shows the users from the Django api.
10-
* The `server` service is a build of the `server` directory. It 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 Python serve through a [gunicorn](http://gunicorn.org/) server installed in the container.
11-
* There is a `postgres` service for the Django database. The `database` directory contains the automatic backup script.
12-
* There is an `nginx` service to serve static files (the client app).
13-
* There is an `haproxy` service to get all the HTTP requests and do load balancing between the containers in the services.
14-
* There are a separate containers for the [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.
15-
* There is a `visualizer` container to visualize where is each container is located at (on which server).
16-
17-
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.
18-
19-
## Pre Requirements
20-
21-
1. install [docker](https://www.docker.com/).
22-
23-
## Installation
24-
25-
Automatic installation of the project with docker.
26-
27-
0. If you work on `win` or `mac` please comment the lines of the `database -> volumes` in `docker-compose.yml`, this share volume with `linux` system only.
28-
1. In `client` directory run `docker build -t client .` to build the Docker image.
29-
2. In `server` directory run `docker build -t server .` to build the Docker image.
30-
3. To create a swarm `docker swarm init`.
31-
4. Download all docker images:
32-
* `docker pull dockercloud/haproxy`
33-
* `docker pull postgres`
34-
* `docker pull dockersamples/visualizer:stable`
35-
* `docker pull elasticsearch:5.4.3`
36-
* `docker pull kibana:5.4.3`
37-
* `docker pull logstash:5.4.3`
38-
5. Run `docker stack deploy --compose-file=docker-compose.yml prod`
39-
6. Open the browser at [http://localhost](http://localhost) to see your Angular (client) app.
40-
7. Open the browser at [http://localhost:8000](http://localhost:8000) to see your Django (server) app.
41-
8. Open the browser at [http://localhost:8080](http://localhost:8080) to see the visualizer.
42-
9. Open the browser at [http://localhost:5601](http://localhost:5601) to see Kibana and check your logs.
43-
44-
**If you want to install the project manually, go to the `/client` or `/server` directories and read the `README` file.**
9+
* The `client` contain an [Angular](https://angular.io/) app, built with [Angular-Cli](https://github.com/angular/angular-cli), and [ngrx](https://github.com/ngrx) to handle state, [Angular Material](https://github.com/angular/material2) as a design library, and have service worker, and `AOT` compiled. The app shows the users from the Sanic api.
10+
* The `server` contain a simple [Sanic](https://sanic.readthedocs.io) app that expose an `api` of `users`. The Python serve through a [gunicorn](http://gunicorn.org/) server installed in the container.
11+
* There is a `postgres` service for the database. The `database` directory contains the automatic backup script.
12+
* All the logs are going to `stdout` and can be collected through any service.
13+
* There are built in test that configured to run on `travis-ci`, and a code coverage analysis via `codecov`.
14+
15+
The `client` app is built via the cloud build CI on GCP and deployed to the GCP `storage`.
16+
The `server` app is built via the cloud build CI as a docker image and deployed to a `GKE cluster` on GCP (managed by Kubernetes).
17+
The PostgreSQL `database` is built via the cloud build CI as a docker image and deployed to a `GKE cluster` on GCP (managed by Kubernetes).
18+
19+
## Production Installation
20+
21+
Deploy the `client` app:
22+
1. Create a storage bucket with the name of the Domain you have.
23+
2. Create a cloud build trigger with the parameters in the [screenshot](art/client-trigger.jpg) (change the `_REGION_NAME` to the location of the bucket you created in the previous step).
24+
3. Now you can deploy your `client` app by creating a new tag in the `v0.0.1/prod/prod` format and push it to github (`git push --tags`).
25+
26+
Deploy the `server` app:
27+
1. Create a `GKE` cluster on GCP.
28+
2. Create a cloud build trigger with the parameters in the [screenshot](art/servier-trigger.jpg) (change the `_REGION_NAME` to the location of the `GKE` cluster you created in step 1).
29+
3. Connect to the `GKE` cluster using `gcloud container clusters get-credentials prod` and then create a `tiler` using the commands:
30+
1. `kubectl create serviceaccount --namespace kube-system tiller`
31+
2. `kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller`
32+
3. `kubectl patch deploy --namespace kube-system tiller-deploy -p '{"spec":{"template":{"spec":{"serviceAccount":"tiller"}}}}'`
33+
4. `helm init --service-account tiller --upgrade`
34+
4. Then change the `helm` permissions by navigate to `server/kubernetes` in the command line and then write `kubectl apply -f helm-permissions.yaml`.
35+
5. Now you can deploy your `server` app by create a new tag in the `v0.0.1/prod/prod` format and push it to github (`git push --tags`).
36+
37+
Create a Cloud DNS record:
38+
1. Create a Cloud DNS record on GCP. In this record you should add an `A` record to the kubernetes cluster (the server) and place there your load balancer ip address you get in the "Deploy the `server` app", and a `CNAME` record to our Storage bucket (client app) [screenshot](art/cloud_dns.jpg).
4539

4640
## Our Stack
4741

4842
* [Angular](https://angular.io/)
49-
* [Django](https://www.djangoproject.com/)
50-
* [PostgreSQL](http://www.postgresql.org/)
43+
* [Sanic](https://www.djangoproject.com/)
44+
* [PostgreSQL](https://sanic.readthedocs.io)
5145
* [Docker](https://www.docker.com/)
5246

5347
**Tools we use**
5448

55-
* [Angular Material](https://material.angular.io/)
49+
* [Angular Material](https://material.angular.io)
5650
* [ngrx](https://github.com/ngrx)
57-
* [Django REST framework](http://www.django-rest-framework.org/)
58-
* [django-admin-honeypot](http://django-admin-honeypot.readthedocs.io/en/latest/)
59-
* [ELK Stack](https://www.elastic.co/products)
60-
* [Docker Swarm](https://docs.docker.com/engine/swarm/)
61-
62-
## Django Admin
63-
64-
* When install the project with docker, there is an `entrypoint.sh` script that runs in the `server` container. It'll migrate the database and create a new super user with a username `admin` and a password `pass`.
65-
* We use [django-admin-honeypot](https://github.com/dmpayton/django-admin-honeypot) to fake the default Django admin login screen to log and notify admins of attempted unauthorized access. So the real Django admin login screen will be under `/secret-admin`.
66-
* We also use [django-flat-responsive](https://github.com/elky/django-flat-responsive) to make the Django admin screens responsive to mobile.
51+
* [Peewee](http://docs.peewee-orm.com)
52+
* [Kubernetes](https://kubernetes.io)
53+
* [Travis-CI](https://travis-ci.org)
54+
* [Codecov](https://codecov.io)
6755

6856
## Tests
6957

@@ -135,7 +123,7 @@ Just fork and do a pull request (;
135123
[dependencyci-url]: https://dependencyci.com/github/nirgn975/Angular-Django-Seed-Project
136124
[codacy-image]: https://api.codacy.com/project/badge/Grade/cdf4939e98804872b377a4120a4f4571
137125
[codacy-url]: https://www.codacy.com/app/nirgn975/Angular-Django-Seed-Project?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=nirgn975/Angular-Django-Seed-Project&amp;utm_campaign=Badge_Grade
138-
[maintenance-image]: https://img.shields.io/maintenance/yes/2017.svg
126+
[maintenance-image]: https://img.shields.io/maintenance/yes/2019.svg
139127
[maintenance-url]: https://github.com/nirgn975
140128
[donate-image]: https://img.shields.io/badge/PayPal-Donate-lightgrey.svg
141129
[donate-url]: https://www.paypal.me/nirgn/2
File renamed without changes.
File renamed without changes.

art/client-trigger.jpg

77.2 KB
Loading

art/cloud_dns.jpg

51.4 KB
Loading

art/server-trigger.jpg

76.1 KB
Loading

client/cloudbuild.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
steps:
2+
- name: 'gcr.io/cloud-builders/npm:current'
3+
args: ['install']
4+
- name: 'gcr.io/cloud-builders/npm:current'
5+
entrypoint: 'bash'
6+
args:
7+
- '-c'
8+
- |
9+
export APP_VERSION
10+
export NAMESPACE
11+
IFS=/ read -r APP_VERSION NAMESPACE <<< "$TAG_NAME"
12+
npm run build
13+
- name: 'gcr.io/cloud-builders/gsutil'
14+
entrypoint: 'bash'
15+
args:
16+
- '-c'
17+
- |
18+
export APP_VERSION
19+
export NAMESPACE
20+
IFS=/ read -r APP_VERSION NAMESPACE <<< "$TAG_NAME"
21+
gsutil mb -l $_REGION_NAME gs://client.example.com/
22+
gsutil cp -r dist/client/* gs://client.example.com/
23+
gsutil iam ch allUsers:objectViewer gs://client.example.com/
24+
gsutil web set -m index.html -e 404.html gs://client.example.com/

docker-compose.yml

Lines changed: 0 additions & 153 deletions
This file was deleted.

server/.flake8

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[flake8]
2+
max-line-length = 120

0 commit comments

Comments
 (0)