Skip to content

Commit

Permalink
migrate to kubernetes and helm
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-koba-mobidev committed Dec 15, 2018
1 parent 8c1d333 commit c671822
Show file tree
Hide file tree
Showing 21 changed files with 419 additions and 63 deletions.
34 changes: 34 additions & 0 deletions admin/Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
FROM ruby:2.5.0-alpine as builder

# Pg
RUN apk --update --upgrade add postgresql-dev nodejs git build-base libffi-dev libgcc
RUN npm install -g bower
RUN echo '{ "allow_root": true }' > /root/.bowerrc

ENV APP_ROOT /app
RUN mkdir $APP_ROOT
WORKDIR $APP_ROOT

# Bundle
COPY . /app
RUN gem update bundler && bundle install --jobs 4 && rm -rf /usr/local/bundle/cache/*.gem \
&& find /usr/local/bundle/gems/ -name "*.c" -delete \
&& find /usr/local/bundle/gems/ -name "*.o" -delete

# Run bower
WORKDIR $APP_ROOT/public
RUN bower install

FROM ruby:2.5.0-alpine

#Pg
RUN apk --update --upgrade add postgresql-dev && rm -rf /var/cache/apk/*

ENV APP_ROOT /app
RUN mkdir $APP_ROOT
WORKDIR $APP_ROOT

COPY --from=builder /usr/local/bundle /usr/local/bundle
COPY --from=builder /app /app

EXPOSE 3000
5 changes: 5 additions & 0 deletions admin/init/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
db_config_file = File.join(File.dirname(__FILE__), 'config', 'database.yml')
if File.exist?(db_config_file)
config = YAML.load(File.read(db_config_file))
# Override db config with env vars
config["host"] = ENV['POSTGRES_HOST'] unless ENV['POSTGRES_HOST'] == nil
config["database"] = ENV['POSTGRES_DB'] unless ENV['POSTGRES_DB'] == nil
config["user"] = ENV['POSTGRES_USER'] unless ENV['POSTGRES_USER'] == nil
config["password"] = ENV['POSTGRES_PASSWORD'] unless ENV['POSTGRES_PASSWORD'] == nil
DB = Sequel.connect(config)
Sequel.extension :migration
Sequel.extension :pagination
Expand Down
4 changes: 2 additions & 2 deletions admin/lib/constants.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module Constants
def self.frontend_url
return 'http://1devblog.org' if ENV['RACK_ENV'] == 'production'
return 'https://1devblog.org' if ENV['RACK_ENV'] == 'production'
return 'http://localhost:3001'
end

def self.frontend_api_url
return 'http://1devblog.org/api' if ENV['RACK_ENV'] == 'production'
return 'https://1devblog.org/api' if ENV['RACK_ENV'] == 'production'
return 'http://front:3001/api'
end

Expand Down
18 changes: 10 additions & 8 deletions bin/deploy.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#!/usr/bin/env bash

UNIXTIME=$(date +%s)

cd ./front
docker build -f Dockerfile -t sergkoba/blog:latest .
docker push sergkoba/blog:latest
docker build -f Dockerfile.prod -t repo.treescale.com/skoba/blog:build-$UNIXTIME .
docker push repo.treescale.com/skoba/blog:build-$UNIXTIME

cd ../admin
docker build -f Dockerfile -t sergkoba/blog:latest-admin .
docker push sergkoba/blog:latest-admin
docker build -f Dockerfile.prod -t repo.treescale.com/skoba/blog-admin:build-$UNIXTIME .
docker push repo.treescale.com/skoba/blog-admin:build-$UNIXTIME

cd ..
rancher up -d -u -c -p

rancher exec admin bundle exec ./cli db_migrate
rancher exec front bundle exec ./cli sitemap_generate
helm upgrade -i --set image.tag=build-$UNIXTIME --wait --namespace default blog ./chart
ADMIN_POD=$(kubectl get pods -l app.kubernetes.io/name=blog-admin --output=jsonpath={.items..metadata.name})
kubectl exec $ADMIN_POD bundle exec ./cli db_migrate
21 changes: 21 additions & 0 deletions chart/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 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
*~
# Various IDEs
.project
.idea/
*.tmproj
5 changes: 5 additions & 0 deletions chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: v1
appVersion: "1.0"
description: A Helm chart for 1devblog
name: blog
version: 0.1.0
Empty file added chart/templates/NOTES.txt
Empty file.
32 changes: 32 additions & 0 deletions chart/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "chart.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 "chart.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 "chart.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}
50 changes: 50 additions & 0 deletions chart/templates/admin_deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: "{{ include "chart.fullname" . }}-admin-deployment"
labels:
app.kubernetes.io/name: "{{ include "chart.name" . }}-admin"
helm.sh/chart: {{ include "chart.chart" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app.kubernetes.io/name: "{{ include "chart.name" . }}-admin"
app.kubernetes.io/instance: {{ .Release.Name }}
template:
metadata:
labels:
app.kubernetes.io/name: "{{ include "chart.name" . }}-admin"
app.kubernetes.io/instance: {{ .Release.Name }}
spec:
containers:
- name: "{{ .Chart.Name }}-admin"
image: "{{ .Values.image.repository }}-admin:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
command: ["rackup", "--port", "3000", "--host", "0.0.0.0"]
env:
- name: RACK_ENV
value: "production"
- name: POSTGRES_HOST
value: "{{ .Chart.Name }}-postgres-service"
envFrom:
- secretRef:
name: "{{ include "chart.name" . }}-postgres-secret"
- secretRef:
name: "{{ include "chart.name" . }}-api-secret"
ports:
- name: http
containerPort: 3000
protocol: TCP
livenessProbe:
httpGet:
path: /
port: http
readinessProbe:
httpGet:
path: /
port: http
imagePullSecrets:
- name: regcred
19 changes: 19 additions & 0 deletions chart/templates/admin_service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: v1
kind: Service
metadata:
name: "{{ include "chart.fullname" . }}-admin-service"
labels:
app.kubernetes.io/name: "{{ include "chart.name" . }}-admin-service"
helm.sh/chart: {{ include "chart.chart" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: 3000
protocol: TCP
name: http
selector:
app.kubernetes.io/name: "{{ include "chart.name" . }}-admin"
app.kubernetes.io/instance: {{ .Release.Name }}
12 changes: 12 additions & 0 deletions chart/templates/api_secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: Secret
metadata:
name: "{{ include "chart.name" . }}-api-secret"
labels:
app.kubernetes.io/name: "{{ include "chart.name" . }}-api-secret"
helm.sh/chart: {{ include "chart.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
type: Opaque
data:
API_TOKEN: "RnJPTU9SMlhKTlNCMVloeDFNRDRDNGt0NGRTek1lRDQ="
50 changes: 50 additions & 0 deletions chart/templates/front_deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: "{{ include "chart.fullname" . }}-front-deployment"
labels:
app.kubernetes.io/name: "{{ include "chart.name" . }}-front"
helm.sh/chart: {{ include "chart.chart" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app.kubernetes.io/name: "{{ include "chart.name" . }}-front"
app.kubernetes.io/instance: {{ .Release.Name }}
template:
metadata:
labels:
app.kubernetes.io/name: "{{ include "chart.name" . }}-front"
app.kubernetes.io/instance: {{ .Release.Name }}
spec:
containers:
- name: "{{ .Chart.Name }}-front"
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
command: ["rackup", "--port", "3001", "--host", "0.0.0.0"]
env:
- name: RACK_ENV
value: "production"
- name: POSTGRES_HOST
value: "{{ .Chart.Name }}-postgres-service"
envFrom:
- secretRef:
name: "{{ include "chart.name" . }}-postgres-secret"
- secretRef:
name: "{{ include "chart.name" . }}-api-secret"
ports:
- name: http
containerPort: 3001
protocol: TCP
livenessProbe:
httpGet:
path: /
port: http
readinessProbe:
httpGet:
path: /
port: http
imagePullSecrets:
- name: regcred
19 changes: 19 additions & 0 deletions chart/templates/front_service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: v1
kind: Service
metadata:
name: "{{ include "chart.fullname" . }}-front-service"
labels:
app.kubernetes.io/name: "{{ include "chart.name" . }}-front-service"
helm.sh/chart: {{ include "chart.chart" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: 3001
protocol: TCP
name: http
selector:
app.kubernetes.io/name: "{{ include "chart.name" . }}-front"
app.kubernetes.io/instance: {{ .Release.Name }}
54 changes: 54 additions & 0 deletions chart/templates/postgres_deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
apiVersion: v1
kind: Service
metadata:
name: "{{ include "chart.fullname" . }}-postgres-service"
labels:
app.kubernetes.io/name: "{{ include "chart.name" . }}-postgres-service"
helm.sh/chart: {{ include "chart.chart" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
ports:
- port: 5432
selector:
app.kubernetes.io/name: "{{ include "chart.name" . }}-postgres"
app.kubernetes.io/instance: {{ .Release.Name }}
---
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: "{{ include "chart.fullname" . }}-postgres-deployment"
labels:
app.kubernetes.io/name: "{{ include "chart.name" . }}-postgres"
helm.sh/chart: {{ include "chart.chart" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
selector:
matchLabels:
app.kubernetes.io/name: "{{ include "chart.name" . }}-postgres"
app.kubernetes.io/instance: {{ .Release.Name }}
strategy:
type: Recreate
template:
metadata:
labels:
app.kubernetes.io/name: "{{ include "chart.name" . }}-postgres"
app.kubernetes.io/instance: {{ .Release.Name }}
spec:
containers:
- image: postgres:11.1
name: "{{ .Chart.Name }}-postgres"
ports:
- containerPort: 5432
name: postgres
volumeMounts:
- name: "{{ include "chart.name" . }}-postgres-persistent-storage"
mountPath: /var/lib/postgresql/data
envFrom:
- secretRef:
name: "{{ include "chart.name" . }}-postgres-secret"
volumes:
- name: "{{ include "chart.name" . }}-postgres-persistent-storage"
persistentVolumeClaim:
claimName: "{{ include "chart.name" . }}-postgres-pv-claim"
35 changes: 35 additions & 0 deletions chart/templates/postgres_pv.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
kind: PersistentVolume
apiVersion: v1
metadata:
name: "{{ include "chart.name" . }}-postgres-pv-volume"
labels:
type: local
app.kubernetes.io/name: "{{ include "chart.name" . }}-postgres-pv-volume"
helm.sh/chart: {{ include "chart.chart" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
storageClassName: manual
capacity:
storage: 3Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: "{{ include "chart.name" . }}-postgres-pv-claim"
labels:
app.kubernetes.io/name: "{{ include "chart.name" . }}-postgres-pv-claim"
helm.sh/chart: {{ include "chart.chart" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 3Gi
Loading

0 comments on commit c671822

Please sign in to comment.