Skip to content

Commit

Permalink
Merge pull request #9 from TohaVoice/feature/hw03-crud
Browse files Browse the repository at this point in the history
Added HealthController, helm templates, Makefile, postman collection,…
  • Loading branch information
TohaVoice authored Apr 13, 2023
2 parents 2a9f56d + 2015977 commit fbc40a2
Show file tree
Hide file tree
Showing 22 changed files with 507 additions and 32 deletions.
23 changes: 23 additions & 0 deletions hw03-crud/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
docker-build:
docker build -f docker/Dockerfile . -t avshatokhin/msa:hw03-crud

docker-push:
docker push avshatokhin/msa:hw03-crud

docker-start:
cd docker && docker-compose up -d

docker-stop:
cd docker && docker-compose down

k8s-deploy-sh:
helm/deploy.sh

k8s-remove-sh:
helm/remove.sh

k8s-deploy:
kubectl create ns otus && helm install nginx --namespace otus nginx-stable/nginx-ingress --set controller.service.httpPort.port=80 --set controller.enableSnippets=True && helm upgrade --install -n otus hw03-crud helm/chart

k8s-remove:
helm uninstall -n otus nginx && kubectl delete ns otus
20 changes: 20 additions & 0 deletions hw03-crud/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
### Установка

Все деплоится в namespace otus, если данный namespace был в системе, то сначала запустить <b> make k8s-remove-sh </b> команду. </br>
Если никаких рудиментов нет, запускаем как на примере ниже
```
1. make k8s-deploy-sh
2. make k8s-remove-sh
```
В случае когда sh файлы не отрабатывают (Windows или что то не настроено), запускаем пример как указано ниже. </br>
Однако в данном случае нужно будет следить чтобы вся цепочка команд выполнилась без ошибок
```
1. make k8s-deploy
2. make k8s-remove
```

Шаблонизация в папке
```
helm/chart
```

### Домашнее задание
Инфраструктурные паттерны

Expand Down
18 changes: 7 additions & 11 deletions hw03-crud/docker/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ services:
container_name: postgres-sql
image: postgres:15.2-alpine
restart: always
# ports:
# - 5434:5434
networks:
- hw03-crud
environment:
POSTGRES_INITDB_ARGS: --auth-host=scram-sha-256
POSTGRES_USER: sa
POSTGRES_DB: test
POSTGRES_PASSWORD: example
POSTGRES_USER: ashatokhin
POSTGRES_DB: msaDb
POSTGRES_PASSWORD: root
TZ: Europe/Samara
app:
container_name: crud-app
Expand All @@ -25,13 +23,11 @@ services:
environment:
HTTP_PORT: 8000
APP_LOG_LEVEL: ERROR
DB_HOST: jdbc:postgresql://db/test
DB_USER: sa
DB_PASSWORD: example
DB_HOST: jdbc:postgresql://db/msaDb
DB_USER: ashatokhin
DB_PASSWORD: root
depends_on:
- db

networks:
hw03-crud:

# jdbc:postgresql://postgres:5432/postgresdb
hw03-crud:
6 changes: 6 additions & 0 deletions hw03-crud/helm/chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v2
name: app-chart
description: A Helm chart for Kubernetes
type: application
version: 0.1.0
appVersion: v1
62 changes: 62 additions & 0 deletions hw03-crud/helm/chart/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "hw03-crud.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 "hw03-crud.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 "hw03-crud.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

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

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

{{/*
Create the name of the service account to use
*/}}
{{- define "hw03-crud.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "hw03-crud.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
9 changes: 9 additions & 0 deletions hw03-crud/helm/chart/templates/app_configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
DB_HOST: "jdbc:postgresql://postgres-db/msaDb"
HTTP_PORT: "8000"
APP_LOG_LEVEL: "ERROR"
DB_USER: "ashatokhin"
29 changes: 29 additions & 0 deletions hw03-crud/helm/chart/templates/app_deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "hw03-crud.fullname" . }}
labels:
{{- include "hw03-crud.labels" . | nindent 4 }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
{{- include "hw03-crud.selectorLabels" . | nindent 6 }}
template:
metadata:
labels:
{{- include "hw03-crud.selectorLabels" . | nindent 8 }}
spec:
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: http
containerPort: {{ .Values.service.port }}
protocol: TCP
envFrom:
- configMapRef:
name: app-config
- secretRef:
name: app-secrets
38 changes: 38 additions & 0 deletions hw03-crud/helm/chart/templates/app_ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{{- if .Values.ingress.enabled -}}
{{- $fullName := include "hw03-crud.fullname" . -}}
{{- $svcPort := .Values.service.port -}}
{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }}
{{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }}
{{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}}
{{- end }}
{{- end }}
{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ $fullName }}
labels:
{{- include "hw03-crud.labels" . | nindent 4 }}
{{- with .Values.ingress.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
ingressClassName: {{ .Values.ingress.className }}
rules:
{{- range .Values.ingress.hosts }}
- host: {{ .host | quote }}
http:
paths:
{{- range .paths }}
- path: {{ .path }}
pathType: {{ .pathType }}
{{- end }}
backend:
service:
name: {{ $fullName }}
port:
number: {{ $svcPort }}
{{- end }}
{{- end }}
{{- end }}
9 changes: 9 additions & 0 deletions hw03-crud/helm/chart/templates/app_secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: v1
kind: Secret
type: Opaque
metadata:
name: app-secrets
labels:
name: hw03-crud
data:
DB_PASSWORD: cm9vdA==
15 changes: 15 additions & 0 deletions hw03-crud/helm/chart/templates/app_service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Service
metadata:
name: {{ include "hw03-crud.fullname" . }}
labels:
{{- include "hw03-crud.labels" . | nindent 4 }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: {{ .Values.service.targetPort }}
protocol: TCP
name: http
selector:
{{- include "hw03-crud.selectorLabels" . | nindent 4 }}
8 changes: 8 additions & 0 deletions hw03-crud/helm/chart/templates/postgres_configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: postgres-config
data:
POSTGRES_USER: "ashatokhin"
POSTGRES_DB: "msaDb"
PGDATA: "/data/pgdata"
9 changes: 9 additions & 0 deletions hw03-crud/helm/chart/templates/postgres_secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: v1
kind: Secret
type: Opaque
metadata:
name: postgres-secrets
labels:
name: hw03-crud
data:
POSTGRES_PASSWORD: cm9vdA==
11 changes: 11 additions & 0 deletions hw03-crud/helm/chart/templates/postgres_service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: Service
metadata:
name: postgres-db
spec:
selector:
app: postgresql-db
type: LoadBalancer
ports:
- port: 5432
targetPort: 5432
36 changes: 36 additions & 0 deletions hw03-crud/helm/chart/templates/postgres_statefulset.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
apiVersion: apps/v1
kind: StatefulSet

metadata:
name: postgresql-db

spec:
serviceName: postgresql-db-service
selector:
matchLabels:
app: postgresql-db
replicas: 1
template:
metadata:
labels:
app: postgresql-db
spec:
containers:
- name: postgresql-db
image: postgres:15.2-alpine
volumeMounts:
- name: postgresql-db-disk
mountPath: /data
envFrom:
- configMapRef:
name: postgres-config
- secretRef:
name: postgres-secrets
volumeClaimTemplates:
- metadata:
name: postgresql-db-disk
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 1Gi
21 changes: 21 additions & 0 deletions hw03-crud/helm/chart/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
replicaCount: 1

image:
repository: avshatokhin/msa
pullPolicy: IfNotPresent
tag: hw03-crud

service:
type: NodePort
port: 80
targetPort: 8000

ingress:
enabled: true
className: nginx
annotations: {}
hosts:
- host: arch.homework
paths:
- path: /
pathType: Prefix
5 changes: 5 additions & 0 deletions hw03-crud/helm/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env sh

kubectl create ns otus
helm install nginx --namespace otus nginx-stable/nginx-ingress --set controller.service.httpPort.port=80 --set controller.enableSnippets=True
helm upgrade --install -n otus hw03-crud chart
4 changes: 4 additions & 0 deletions hw03-crud/helm/remove.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh

helm uninstall -n otus nginx
kubectl delete ns otus
Loading

0 comments on commit fbc40a2

Please sign in to comment.