Skip to content

Commit

Permalink
Add helm chart (alfredfrancis#156)
Browse files Browse the repository at this point in the history
* Add ready endpoint

* update gunicorn error op

* Add config for helm

* add helm chart

* update nginx conf to handle upstream unavilability while using k8

* add migration in docker-compose

* update Documenation
  • Loading branch information
alfredfrancis authored May 30, 2022
1 parent 5db8e73 commit 2993350
Show file tree
Hide file tree
Showing 15 changed files with 346 additions and 30 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

EXPOSE 8080
EXPOSE 80

COPY . .

CMD ["gunicorn", "run:app" ,"--bind", "0.0.0.0:8080" ,"--access-logfile=logs/gunicorn-access.log" ,"--error-logfile" ,"logs/gunicorn-error.log"]
CMD ["gunicorn", "run:app" ,"--log-level=debug", "--timeout", "90","--bind", "0.0.0.0:80" ]
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,39 @@ You don’t need to be an expert at artificial intelligence to create an awesome

### Using docker-compose (Recommended)
```sh
docker-compose build
docker-compose up -d
docker-compose exec iky_backend python manage.py migrate
```

### Using Helm

```sh
helm dep update helm/ai-chatbot-framework

helm upgrade --install --create-namespace -n ai-chatbot-framework ai-chatbot-framework helm/ai-chatbot-framework

# port forward for local installation
kubectl port-forward --namespace=ai-chatbot-framework service/ingress-nginx-controller 8080:80
```

### Using Docker
```sh

# build docker images
docker build -t iky_backend:2.0.0 .
docker build -t iky_gateway:2.0.0 frontend/.
# pull docker images
docker pull alfredfrancis/ai-chatbot-framework_backend:latest
docker pull alfredfrancis/ai-chatbot-framework_frontend:latest

# start a mongodb server
docker run --name mongodb -d mongo:3.6

# start iky backend
docker run -d --name=iky_backend --link mongodb:mongodb -e="APPLICATION_ENV=Production" iky_backend:2.0.0
docker run -d --name=iky_backend --link mongodb:mongodb -e="APPLICATION_ENV=Production" alfredfrancis/ai-chatbot-framework_backend:latest

# setup default intents
docker exec -it iky_backend python manage.py migrate

# start iky gateway with frontend
docker run -d --name=iky_gateway --link iky_backend:iky_backend -p 8080:80 iky_gateway:2.0.0
docker run -d --name=iky_gateway --link iky_backend:iky_backend -p 8080:80 alfredfrancis/ai-chatbot-framework_frontend:latest

```

Expand Down
4 changes: 4 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def create_app(env = 'Development'):

admin_panel_dist = os.path.join(APP_ROOT, 'frontend/dist/')

@app.route('/ready')
def ready():
return "ok",200

@app.route('/<path:path>', methods=['GET'])
def static_proxy(path):
return send_from_directory(admin_panel_dist, path)
Expand Down
3 changes: 3 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ class Production(Config):
WEB_SERVER_PORT = 8001

class Heroku(Production):
MONGODB_HOST = os.environ.get('MONGO_URL')

class Helm(Production):
MONGODB_HOST = os.environ.get('MONGO_URL')
17 changes: 14 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
version: '2'
services:
migrate:
container_name: iky_migrate
image: alfredfrancis/ai-chatbot-framework_backend:latest
command: python manage.py migrate
environment:
APPLICATION_ENV: Production
depends_on:
- mongodb

iky_backend:
container_name: iky_backend
build: .
image: alfredfrancis/ai-chatbot-framework_backend:latest
hostname: iky_backend
environment:
APPLICATION_ENV: Production
depends_on:
- migrate
- mongodb

iky_gateway:
container_name: iky_gateway
build: frontend/
image: alfredfrancis/ai-chatbot-framework_frontend:latest
ports:
- "8080:80"
depends_on:
- iky_backend

mongodb:
container_name: mongodb
image: mongo
image: mongo:4.2.20
hostname: mongodb
ports:
- "27017:27017"
Expand Down
25 changes: 6 additions & 19 deletions frontend/nginx/default.conf
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@



server {

listen 80;

sendfile on;

default_type application/octet-stream;


gzip on;
gzip_http_version 1.1;
gzip_disable "MSIE [1-6]\.";
gzip_min_length 256;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_comp_level 9;


root /usr/share/nginx/html;
resolver 127.0.0.11 valid=30s;

set $backend http://iky_backend;

location / {
try_files $uri $uri/ /index.html =404;
}
location /gateway {
proxy_pass http://iky_backend:8080/;

location /gateway/ {
rewrite ^/gateway/?(.*)$ /$1 break;
proxy_pass $backend;
proxy_set_header Host $host;
}
}
2 changes: 2 additions & 0 deletions helm/ai-chatbot-framework/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
charts/*
Chart.lock
23 changes: 23 additions & 0 deletions helm/ai-chatbot-framework/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
14 changes: 14 additions & 0 deletions helm/ai-chatbot-framework/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v2
name: ai-chatbot-framework
description: A python chatbot framework with Natural Language Understanding and Artificial Intelligence.
type: application
version: 0.1.0
appVersion: "1.0.0"
dependencies:
- name: mongodb
version: 10.3.3
repository: "https://charts.bitnami.com/bitnami"
alias: mongodb
condition: mongodb.enabled


62 changes: 62 additions & 0 deletions helm/ai-chatbot-framework/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "ai-chatbot-framework.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "ai-chatbot-framework.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "ai-chatbot-framework.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "ai-chatbot-framework.labels" -}}
helm.sh/chart: {{ include "ai-chatbot-framework.chart" . }}
{{ include "ai-chatbot-framework.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "ai-chatbot-framework.selectorLabels" -}}
app.kubernetes.io/name: {{ include "ai-chatbot-framework.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "ai-chatbot-framework.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "ai-chatbot-framework.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
56 changes: 56 additions & 0 deletions helm/ai-chatbot-framework/templates/deployment-backend.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "ai-chatbot-framework.fullname" . }}-backend
labels:
app: "ai-chatbot-framework"
name: "ai-chatbot-framework-backend"
{{- include "ai-chatbot-framework.labels" . | nindent 4 }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: "ai-chatbot-framework"
name: "ai-chatbot-framework-backend"
template:
metadata:
labels:
app: "ai-chatbot-framework"
name: "ai-chatbot-framework-backend"
spec:
initContainers:
- name: {{ .Chart.Name }}-init
image: "{{ .Values.image.backend.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
command: ["python","manage.py","migrate"]
env:
- name: MONGO_URL
value: {{.Values.mongodb.uri}}
- name: APPLICATION_ENV
value: 'Helm'
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.backend.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
command: ["gunicorn", "run:app" ,"--log-level=debug", "--timeout", "90","--bind", "0.0.0.0:80" ]
env:
- name: MONGO_URL
value: {{.Values.mongodb.uri}}
- name: APPLICATION_ENV
value: 'Helm'
ports:
- name: http
containerPort: 80
protocol: TCP
livenessProbe:
initialDelaySeconds: 120
httpGet:
path: /ready
port: http
readinessProbe:
initialDelaySeconds: 120
httpGet:
path: /ready
port: http
resources:
{{- toYaml .Values.resources.backend | nindent 12 }}
33 changes: 33 additions & 0 deletions helm/ai-chatbot-framework/templates/deployment-frontend.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "ai-chatbot-framework.fullname" . }}-frontend
labels:
app: "ai-chatbot-framework"
name: "ai-chatbot-framework-frontend"
{{- include "ai-chatbot-framework.labels" . | nindent 4 }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: "ai-chatbot-framework"
name: "ai-chatbot-framework-frontend"
template:
metadata:
{{- with .Values.podAnnotations }}
{{- end }}
labels:
app: "ai-chatbot-framework"
name: "ai-chatbot-framework-frontend"
spec:
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.frontend.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
command: ["nginx", "-g", "daemon off;"]
ports:
- name: http
containerPort: 80
protocol: TCP
resources:
{{- toYaml .Values.resources.frontend | nindent 12 }}
29 changes: 29 additions & 0 deletions helm/ai-chatbot-framework/templates/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ include "ai-chatbot-framework.fullname" . }}
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
labels:
app: "ai-chatbot-framework"
name: "ai-chatbot-framework-ingress"
{{- include "ai-chatbot-framework.labels" . | nindent 4 }}
spec:
ingressClassName: nginx
rules:
- http:
paths:
- pathType: Prefix
path: /(.*)
backend:
service:
name: {{ include "ai-chatbot-framework.fullname" . }}-frontend
port:
number: 80
- pathType: Prefix
path: /gateway/(.*)
backend:
service:
name: {{ include "ai-chatbot-framework.fullname" . }}-backend
port:
number: 80
Loading

0 comments on commit 2993350

Please sign in to comment.