Skip to content

Commit

Permalink
feature: create helm charts(#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuoZhuoCrayon committed Sep 9, 2021
1 parent 4567303 commit 8826f93
Show file tree
Hide file tree
Showing 32 changed files with 1,048 additions and 8 deletions.
18 changes: 18 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## Project Part
.git/
.idea/
.DS_Store
.tmp/
.vscode/
db.sqlite3

## Python Part

__pycache__/

## Extra Part
.helm/
docs/
logs/
support-files/deploy/*
!support-files/deploy/prod/
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ __pycache__/


## Settings
scripts/deploy/local/*
scripts/deploy/prod/*
scripts/deploy/stag/*
support-files/deploy/local/*
support-files/deploy/prod/*
support-files/deploy/stag/*

logs/
6 changes: 6 additions & 0 deletions .helm/djangocli/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Chart.lock
charts/*.tgz
tests/
*.log
values-private.yaml
static-pvc.yaml
23 changes: 23 additions & 0 deletions .helm/djangocli/.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/
45 changes: 45 additions & 0 deletions .helm/djangocli/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
apiVersion: v2
name: "djangocli"
description: A Helm chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "latest"

dependencies:
- name: common
version: 1.7.1
repository: https://charts.bitnami.com/bitnami
#- name: nginx-ingress-controller
# version: 7.6.17
# repository: https://charts.bitnami.com/bitnami
# condition: nginx-ingress-controller.enabled
- name: mariadb
version: 9.4.0
repository: https://charts.bitnami.com/bitnami
condition: mariadb.enabled
- name: redis
version: 14.8.7
repository: https://charts.bitnami.com/bitnami
condition: redis.enabled
#- name: nginx
# version: 9.4.2
# repository: https://charts.bitnami.com/bitnami
# condition: nginx.enabled
11 changes: 11 additions & 0 deletions .helm/djangocli/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
1. Please check all pods is running: kubectl get pod -A

2. Get the application URL by running these commands:
- minikube: minikube service {{ include "djangocli.nginx.fullname" .}} --url
- k8s: http://127.0.0.1:{{ .Values.nginx.service.nodePort }}

3. List services and access from outer: minikube get svc -A

4. Enter container to debug: kubectl exec -it <POD> -c <CONTAINER> -- sh

5. Get container's log: kubectl logs <POD> -c <CONTAINER>
145 changes: 145 additions & 0 deletions .helm/djangocli/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
{{- define "djangocli.backend.fullname" -}}
{{- printf "%s-%s" (include "common.names.fullname" .) "backend" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{- define "djangocli.nginx.fullname" -}}
{{- printf "%s-%s" (include "common.names.fullname" .) "nginx" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{- define "djangocli.celeryworker.fullname" -}}
{{- printf "%s-%s" (include "common.names.fullname" .) "celeryworker" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{- define "djangocli.secret.mariadb.name" -}}
{{- printf "%s-%s" (include "common.names.fullname" .) "secret-mariadb" -}}
{{- end -}}

{{- define "djangocli.secret.redis.name" -}}
{{- printf "%s-%s" (include "common.names.fullname" .) "secret-redis" -}}
{{- end -}}

{{- define "djangocli.secret.image.name" -}}
{{- printf "%s-%s" (include "common.names.fullname" .) "secret-image" -}}
{{- end -}}

{{- define "djangocli.secret.app.name" -}}
{{- printf "%s-%s" (include "common.names.fullname" .) "secret-app" -}}
{{- end -}}

{{- define "imagePullSecret" }}
{{- with .Values.imageCredentials }}
{{- printf "{\"auths\":{\"%s\":{\"username\":\"%s\",\"password\":\"%s\",\"email\":\"%s\",\"auth\":\"%s\"}}}" .registry .username .password .email (printf "%s:%s" .username .password | b64enc) | b64enc }}
{{- end }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "djangocli.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "common.names.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}


{{/*
Fully qualified app name for MariaDB
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
*/}}
{{- define "djangocli.mariadb.fullname" -}}
{{- printf "%s-%s" .Release.Name "mariadb" | trunc 63 | trimSuffix "-" -}}
{{- end -}}


{{/*
Fully qualified app name for Redis
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
*/}}
{{- define "djangocli.redis.fullname" -}}
{{- printf "%s-%s" .Release.Name "redis" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Return the Redis host
*/}}
{{- define "djangocli.redis.host" -}}
{{- if .Values.redis.enabled }}
{{- printf "%s-master" (include "djangocli.redis.fullname" .) -}}
{{- else -}}
{{- printf "%s" .Values.externalRedis.host -}}
{{- end -}}
{{- end -}}


{{- define "djangocli.commonEnv" -}}
- name: DC_ENV
value: prod
- name: DC_MYSQL_HOST
valueFrom:
secretKeyRef:
name: {{ include "djangocli.secret.mariadb.name" . }}
key: host
- name: DC_MYSQL_PORT
valueFrom:
secretKeyRef:
name: {{ include "djangocli.secret.mariadb.name" . }}
key: port
- name: DC_MYSQL_USER
valueFrom:
secretKeyRef:
name: {{ include "djangocli.secret.mariadb.name" . }}
key: username
- name: DC_MYSQL_PASSWORD
valueFrom:
secretKeyRef:
name: {{ include "djangocli.secret.mariadb.name" . }}
key: password

- name: DC_REDIS_HOST
valueFrom:
secretKeyRef:
name: {{ include "djangocli.secret.redis.name" . }}
key: host
- name: DC_REDIS_PORT
valueFrom:
secretKeyRef:
name: {{ include "djangocli.secret.redis.name" . }}
key: port
- name: DC_REDIS_PASSWORD
valueFrom:
secretKeyRef:
name: {{ include "djangocli.secret.redis.name" . }}
key: password
- name: SECRET_KEY
valueFrom:
secretKeyRef:
name: {{ include "djangocli.secret.app.name" . }}
key: secretKey
- name: APP_NAME
valueFrom:
secretKeyRef:
name: {{ include "djangocli.secret.app.name" . }}
key: appName
- name: APP_VERSION
valueFrom:
secretKeyRef:
name: {{ include "djangocli.secret.app.name" . }}
key: appVersion
- name: DJANGO_SUPERUSER_PASSWORD
valueFrom:
secretKeyRef:
name: {{ include "djangocli.secret.app.name" . }}
key: superUserPassword
- name: DJANGO_SUPERUSER_USERNAME
valueFrom:
secretKeyRef:
name: {{ include "djangocli.secret.app.name" . }}
key: superUserName
- name: DJANGO_SUPERUSER_EMAIL
valueFrom:
secretKeyRef:
name: {{ include "djangocli.secret.app.name" . }}
key: superUserEmail
{{- end -}}
69 changes: 69 additions & 0 deletions .helm/djangocli/templates/backend/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "djangocli.backend.fullname" . }}
labels:
{{- include "common.labels.standard" . | nindent 4 }}
app.kubernetes.io/component: {{ include "djangocli.backend.fullname" . }}
{{- if .Values.commonLabels }}
{{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
{{- end }}
spec:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
{{- end }}
selector:
matchLabels: {{ include "common.labels.matchLabels" . | nindent 6 }}
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels: {{ include "common.labels.matchLabels" . | nindent 8 }}
app.kubernetes.io/component: {{ include "djangocli.backend.fullname" . }}
spec:
imagePullSecrets:
- name: {{ include "djangocli.secret.image.name" . }}
serviceAccountName: {{ include "djangocli.serviceAccountName" . }}
securityContext:
{{- toYaml .Values.podSecurityContext | nindent 8 }}

initContainers:
- name: {{ include "djangocli.backend.fullname" . }}-migrate
image: "{{ .Values.backend.image.repository }}:{{ .Values.backend.image.tag }}"
imagePullPolicy: {{ .Values.backend.image.imagePullPolicy | default "IfNotPresent" | quote }}
command: ["python", "manage.py", "migrate"]
env:
{{- include "djangocli.commonEnv" . | nindent 12 }}

containers:
- name: {{ include "djangocli.backend.fullname" . }}
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.backend.image.repository }}:{{ .Values.backend.image.tag }}"
imagePullPolicy: {{ .Values.backend.image.imagePullPolicy | default "IfNotPresent" | quote }}
command:
- "gunicorn"
- "wsgi:application"
- "--bind"
- {{ printf "%s:%d" "0.0.0.0" (int .Values.backend.service.targetPort) }}
ports:
- containerPort: {{ int .Values.backend.service.targetPort }}
env:
{{- include "djangocli.commonEnv" . | nindent 12 }}

resources:
{{- toYaml .Values.resources | nindent 12 }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
29 changes: 29 additions & 0 deletions .helm/djangocli/templates/backend/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
apiVersion: v1
kind: Service
metadata:
name: {{ include "djangocli.backend.fullname" . }}
namespace: {{ .Release.Namespace }}
labels: {{- include "common.labels.standard" . | nindent 4 }}
app.kubernetes.io/component: {{ include "djangocli.backend.fullname" . }}
{{- if .Values.commonLabels }}
{{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
{{- end }}
{{- if .Values.commonAnnotations }}
annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
{{- end }}
spec:
type: {{ .Values.backend.service.type }}
ports:
- name: http
port: {{ .Values.backend.service.port }}
{{- if eq .Values.backend.service.type "NodePort" }}
{{- if .Values.backend.service.nodePort }}
nodePort: {{ .Values.backend.service.nodePort }}
{{- else }}
nodePort: null
{{- end }}
{{- end }}
targetPort: {{ .Values.backend.service.targetPort }}
protocol: TCP
selector: {{- include "common.labels.matchLabels" . | nindent 4 }}
app.kubernetes.io/component: {{ include "djangocli.backend.fullname" . }}
Loading

0 comments on commit 8826f93

Please sign in to comment.