Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CI/CD] [example] build, publish and deploy application into kubernetes with werf #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .helm/templates/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: {{ .Chart.Name }}
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: evil-chat.kubernetes.docker.internal
http:
paths:
- path: /
backend:
serviceName: {{ .Chart.Name }}-rails
servicePort: 3000
- path: /packs
backend:
serviceName: {{ .Chart.Name }}-assets
servicePort: 80
34 changes: 34 additions & 0 deletions .helm/templates/job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
apiVersion: batch/v1
kind: Job
metadata:
name: {{ .Chart.Name }}-create-db
annotations:
"helm.sh/hook": post-install,post-upgrade
"helm.sh/hook-weight": "0"
spec:
backoffLimit: 0
template:
metadata:
name: {{ .Chart.Name }}-create-db
spec:
initContainers:
- name: wait-postgres
image: postgres:11
command:
- "sh"
- "-c"
- "until pg_isready -h {{ pluck .Values.global.env .Values.postgresql.host | first | default .Values.postgresql.host._default }} -U {{ pluck .Values.global.env .Values.postgresql.user | first | default .Values.postgresql.user._default }}; do sleep 2; done;"
containers:
- name: rails
{{ tuple "rails" . | include "werf_container_image" | indent 8 }}
command: ["bundle", "exec", "rake", "db:migrate"]
env:
- name: RAILS_MASTER_KEY
value: {{ .Values.rails.master_key}}
- name: RAILS_ENV
value: production
- name: DATABASE_URL
value: "postgres://{{ first (pluck .Values.global.env .Values.postgresql.user) | default .Values.postgresql.user._default }}:{{ first (pluck .Values.global.env .Values.postgresql.password) | default .Values.postgresql.password._default }}@{{ first (pluck .Values.global.env .Values.postgresql.host) | default .Values.postgresql.host._default }}:5432"
{{ tuple "rails" . | include "werf_container_env" | indent 8 }}
restartPolicy: Never
51 changes: 51 additions & 0 deletions .helm/templates/postgresql.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: {{ .Chart.Name }}-pgsql
spec:
serviceName: {{ .Chart.Name }}-pgsql
replicas: 1
selector:
matchLabels:
name: {{ .Chart.Name }}-pgsql
template:
metadata:
labels:
name: {{ .Chart.Name }}-pgsql
spec:
containers:
- name: postgres
image: postgres:11
env:
- name: POSTGRES_USER
value: {{ first (pluck .Values.global.env .Values.postgresql.user) | default .Values.postgresql.user._default }}
- name: POSTGRES_PASSWORD
value: {{ first (pluck .Values.global.env .Values.postgresql.password) | default .Values.postgresql.password._default }}
lifecycle:
preStop:
exec:
command: ["/etc/init.d/postgresql", "stop"]
ports:
- containerPort: 5432
name: pgsql
protocol: TCP
volumeMounts:
- name: pgsql
mountPath: /var/lib/postgresql/data
volumes:
- name: pgsql
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
name: {{ .Chart.Name }}-pgsql
spec:
clusterIP: None
selector:
name: {{ .Chart.Name }}-pgsql
ports:
- name: pgsql
port: 5432
protocol: TCP
89 changes: 89 additions & 0 deletions .helm/templates/rails.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Chart.Name }}
spec:
selector:
matchLabels:
service: {{ .Chart.Name }}
template:
metadata:
labels:
service: {{ .Chart.Name }}
spec:
containers:
# контейнер с rails приложением
- name: rails
command: ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]
# werf при делое подставится нужный url до registry
{{ tuple "rails" . | include "werf_container_image" | indent 8 }}
env:
# переменные образа
{{ tuple "rails" . | include "werf_container_env" | indent 8 }}
# подстановка необходимых переменных согласно нужному окружению
- name: RAILS_MASTER_KEY
value: {{ .Values.rails.master_key}}
- name: RAILS_ENV
value: production
- name: RAILS_LOG_TO_STDOUT
value: "true"
- name: DATABASE_URL
value: "postgres://{{ first (pluck .Values.global.env .Values.postgresql.user) | default .Values.postgresql.user._default }}:{{ first (pluck .Values.global.env .Values.postgresql.password) | default .Values.postgresql.password._default }}@{{ first (pluck .Values.global.env .Values.postgresql.host) | default .Values.postgresql.host._default }}:5432"
- name: REDIS_URL
value: "redis://{{ first (pluck .Values.global.env .Values.redis.host )| default .Values.redis.host._default }}:6379/"
ports:
- containerPort: 3000
name: puma
protocol: TCP
# контейнер с nginx для отдачи статики
- name: assets
{{ tuple "assets" . | include "werf_container_image" | indent 8 }}
lifecycle:
preStop:
exec:
command: ["/usr/sbin/nginx", "-s", "quit"]
livenessProbe:
httpGet:
path: /healthz
port: 80
scheme: HTTP
readinessProbe:
httpGet:
path: /healthz
port: 80
scheme: HTTP
ports:
- containerPort: 80
name: http
protocol: TCP
env:
{{ tuple "assets" . | include "werf_container_env" | indent 8 }}
---
# описание сервиса для rails приложения
apiVersion: v1
kind: Service
metadata:
name: {{ .Chart.Name }}-rails
spec:
clusterIP: None
selector:
service: {{ .Chart.Name }}
ports:
- name: puma
port: 3000
protocol: TCP
---
# описание сервиса для отдачи assets через nginx
apiVersion: v1
kind: Service
metadata:
name: {{ .Chart.Name }}-assets
spec:
clusterIP: None
selector:
service: {{ .Chart.Name }}
ports:
- name: http
port: 80
protocol: TCP
43 changes: 43 additions & 0 deletions .helm/templates/redis.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Chart.Name }}-redis
spec:
strategy:
type: Recreate
selector:
matchLabels:
component: {{ .Chart.Name }}-redis
template:
metadata:
labels:
component: {{ .Chart.Name }}-redis
spec:
terminationGracePeriodSeconds: 10
containers:
- name: redis
image: redis:4-alpine
command:
- redis-server
ports:
- name: redis
containerPort: 6379
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
name: redis
spec:
selector:
component: {{ .Chart.Name }}-redis
ports:
- name: redis
port: 6379
targetPort: 6379
16 changes: 16 additions & 0 deletions .helm/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
rails:
master_key: 533269e346eb9fd9dc4adc9807749c03
postgresql:
host:
prod: evil-chat-pgsql
_default: postgresql
user:
prod: evil_chat
_default: evil_chat
password:
prod: evil_chat_password
_default: evil_chat_password
redis:
host:
prod: redis
_default: redis
45 changes: 45 additions & 0 deletions .werf/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
user nginx;
worker_processes 1;

error_log /dev/stderr;

events {
worker_connections 1024;
}

http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;

include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format combined_plus escape=json '{"time_local": "$time_local", '
'"remote_addr": "$remote_addr", '
'"remote_user": "$remote_user", '
'"request": "$request", '
'"status": "$status", '
'"body_bytes_sent": "$body_bytes_sent", '
'"request_time": "$request_time", '
'"http_referrer": "$http_referer", '
'"http_user_agent": "$http_user_agent"}';

server {
listen 80 default_server;
server_name _;
charset utf-8;
root /www;
access_log /dev/stdout combined_plus;

location /packs {
}

location /healthz {
return 200;
access_log off;
}
}
}

1 change: 1 addition & 0 deletions config/credentials.yml.enc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uhkreMzLXzxhKtP1vuzOjDc1q/s64gJ/8h7Kva8AKoriuZLAUvWdM71Mt5p0K+EWQZi7lyI6dwkguSL3q81CI1peFgI35qM50M6CFOMKPMRPyizs0lEfuL4gqfegYiLSbGBHX5onJS9jmFHRIxzG0cnLsl9bWjp58TvyCevelCB7GJ9SnsBmxMuCCTP1tPO17j1t6PU77Y7ZkjETTCmLUv6/eqGzVgP4R0vRrJ6Xv6nvrV/4epjvRl4V0kcXCqaT2yC3KHqrJVNvbvHuPHX7M6sYU5QBgI934uW5mvhGBCo/Vn4v+41v6kFV0JkBdJw2va9DrA5jGbJAfVAcoNy5rw7Mhs1wljs13xaNPlIkI5Iqngjy8naLMvxR1nGD8eJ4AbvD4s4jtZM/DpakpYcGPQhnM3mNclH8bAps--XC1ill0wJu77/OCh--5oAm/ESUg0c39ZqzMy6qlQ==
2 changes: 1 addition & 1 deletion config/webpacker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ development:
compile: true

# Verifies that correct packages and versions are installed by inspecting package.json, yarn.lock, and node_modules
check_yarn_integrity: true
# check_yarn_integrity: true

# Reference: https://webpack.js.org/configuration/dev-server/
dev_server:
Expand Down
Loading