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

solution #30

Open
wants to merge 6 commits into
base: main
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
9 changes: 9 additions & 0 deletions .infrastructure/helm-chart/todoapp/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: v2
name: todoapp
description: A Helm chart for Kubernetes
type: application
version: 0.1.0
appVersion: "1.16.0"
dependencies:
- name: mysql

25 changes: 25 additions & 0 deletions .infrastructure/helm-chart/todoapp/charts/mysql/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: v2
name: mysql
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: "1.16.0"

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Chart.Name }}
namespace: {{ .Values.mysql.namespace }}
labels:
app: {{ .Chart.Name }}
data:
init.sql: |
GRANT ALL PRIVILEGES ON app_db.* TO 'app_user'@'%';
USE app_db;
CREATE TABLE counter (
id INT AUTO_INCREMENT PRIMARY KEY,
value INT
);

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: {{ .Values.mysql.namespace }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
kind: Secret
metadata:
name: {{ .Chart.Name }}-secrets
namespace: {{ .Values.mysql.namespace }}
type: Opaque
data:
{{- range $k, $v := .Values.mysql.secrets.data }}
{{ $k }}: {{ $v | quote }}
{{- end }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Headless service for stable DNS entries of StatefulSet members.
apiVersion: v1
kind: Service
metadata:
name: {{ .Chart.Name }}
namespace: {{ .Values.mysql.namespace }}
spec:
selector:
app: {{ .Chart.Name }}
ports:
- name: {{ .Chart.Name }}
port: 3306
clusterIP: None

# pod-name.service-name.namespace.svc.cluster.local
# pod-name.service-name
# mysql-0.mysql.mysql.svc.cluster.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: {{ .Chart.Name }}
namespace: {{ .Values.mysql.namespace }}
spec:
selector:
matchLabels:
app: {{ .Chart.Name }}
replicas: {{ .Values.mysql.replicas }}
serviceName: {{ .Chart.Name }}
template:
metadata:
labels:
app: {{ .Chart.Name }}
spec:
containers:
- name: {{ .Values.mysql.image.name }}
image: {{ .Values.mysql.image.repository }}:{{ .Values.mysql.image.version }}
env:
{{- range $k, $v := .Values.mysql.secrets.data }}
- name: {{ $k }}
valueFrom:
secretKeyRef:
name: {{ $.Chart.Name }}-secret
key: {{ $k }}
{{- end }}
- name: MYSQL_DATABASE
value: app_db
ports:
- name: {{ .Chart.Name }}
containerPort: 3306
volumeMounts:
- name: data
mountPath: /var/lib/mysql
subPath: {{ .Chart.Name }}
- name: config-map
mountPath: /docker-entrypoint-initdb.d
resources:
requests:
cpu: {{ .Values.mysql.resources.requests.cpu }}
memory: {{ .Values.mysql.resources.requests.memory }}
livenessProbe:
exec:
command: ["mysqladmin", "ping"]
initialDelaySeconds: 30
periodSeconds: 10
timeoutSeconds: 5
readinessProbe:
exec:
command: ["mysqladmin", "ping"]
initialDelaySeconds: 5
periodSeconds: 2
timeoutSeconds: 1
volumes:
- name: config-map
configMap:
name: {{ .Chart.Name }}
tolerations:
- key: "app"
operator: "Equal"
value: "mysql"
effect: "NoSchedule"
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: {{ .Values.mysql.affinity.podAffinity.key }}
operator: In
values:
- {{ .Values.mysql.affinity.podAffinity.values }}
topologyKey: "kubernetes.io/hostname"
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: {{ .Values.mysql.affinity.nodeAffinity.key }}
operator: In
values:
- {{ .Values.mysql.affinity.nodeAffinity.values }}
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: {{ .Values.mysql.volumeClaimTemplates.resources.requests.storage }}

34 changes: 34 additions & 0 deletions .infrastructure/helm-chart/todoapp/charts/mysql/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
mysql:

namespace: mysql

secrets:
data:
MYSQL_ROOT_PASSWORD: "MTIzNA==" # Base64 encoding for "1234"
MYSQL_USER: "YXBwX3VzZXI=" # Base64 encoding for "app_user"
MYSQL_PASSWORD: "MTIzNA==" # Base64 encoding for "1234"

image:
name: mysql
repository: mysql
version: "8.0"

replicas: 2

resources:
requests:
cpu: 500m
memory: 1Gi

volumeClaimTemplates:
resources:
requests:
storage: 2Gi

affinity:
podAffinity:
key: "app"
value: "mysql"
nodeAffinity:
key: "app"
value: "mysql"
14 changes: 14 additions & 0 deletions .infrastructure/helm-chart/todoapp/templates/clusterIp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Service
metadata:
name: {{ .Chart.Name }}-service
namespace: {{ .Values.todoapp.namespace }}
spec:
type: ClusterIP
selector:
app: {{ .Chart.Name }}
ports:
- protocol: TCP
port: 80
targetPort: 8080

7 changes: 7 additions & 0 deletions .infrastructure/helm-chart/todoapp/templates/configMap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Chart.Name }}-config
namespace: {{ .Values.todoapp.namespace }}
data:
PYTHONUNBUFFERED: "1"
86 changes: 86 additions & 0 deletions .infrastructure/helm-chart/todoapp/templates/deployment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Release.Name }}-{{ .Chart.Name }}
namespace: {{ .Values.todoapp.namespace }}
spec:
strategy:
type: {{ .Values.todoapp.strategy.type }}
rollingUpdate:
maxSurge: {{ .Values.todoapp.strategy.rollingUpdate.maxSurge }}
maxUnavailable: {{ .Values.todoapp.strategy.rollingUpdate.maxUnavailable }}
selector:
matchLabels:
app: {{ .Chart.Name }}
template:
metadata:
labels:
app: {{ .Chart.Name }}
spec:
serviceAccountName: {{ .Values.todoapp.serviceAccount.name }}
containers:
- name: {{ .Values.todoapp.image.name }}
image: {{ .Values.todoapp.image.repository }}:{{ .Values.todoapp.image.version }}
volumeMounts:
- name: data
mountPath: /app/data
- name: app-secrets-volume
mountPath: "/app/secrets"
readOnly: true
- name: app-config-volume
mountPath: "/app/configs"
readOnly: true
resources:
requests:
memory: {{ .Values.todoapp.resources.requests.memory }}
cpu: {{ .Values.todoapp.resources.requests.cpu }}
limits:
memory: {{ .Values.todoapp.resources.limits.memory }}
cpu: {{ .Values.todoapp.resources.limits.cpu }}
env:
{{- range $k, $v := .Values.todoapp.secrets.data }}
- name: {{ $k }}
valueFrom:
secretKeyRef:
name: {{ $.Chart.Name }}-secret
key: {{ $k }}
{{- end }}
- name: PYTHONUNBUFFERED
valueFrom:
configMapKeyRef:
name: {{ .Chart.Name }}-config
key: PYTHONUNBUFFERED
ports:
- containerPort: 8080
livenessProbe:
httpGet:
path: api/health
port: 8080
initialDelaySeconds: 60
periodSeconds: 5
readinessProbe:
httpGet:
path: api/ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
volumes:
- name: data
persistentVolumeClaim:
claimName: pvc-data
- name: app-secrets-volume
secret:
secretName: {{ .Chart.Name }}-secret
- name: app-config-volume
configMap:
name: {{ .Chart.Name }}-config
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: {{ .Values.todoapp.affinity.nodeAffinity.key }}
operator: In
values:
- {{ .Values.todoapp.affinity.nodeAffinity.values | quote }}
26 changes: 26 additions & 0 deletions .infrastructure/helm-chart/todoapp/templates/hpa.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: {{ .Chart.Name }}
namespace: {{ .Values.todoapp.namespace }}
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: {{ .Release.Name }}-{{ .Chart.Name }}
minReplicas: {{ .Values.todoapp.hpa.minReplicas }}
maxReplicas: {{ .Values.todoapp.hpa.maxReplicas }}
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: {{ .Values.todoapp.hpa.cpu.averageUtilization }}
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: {{ .Values.todoapp.hpa.memory.averageUtilization }}

18 changes: 18 additions & 0 deletions .infrastructure/helm-chart/todoapp/templates/ingress.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ .Chart.Name }}-ingress
namespace: {{ .Values.todoapp.namespace }}
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
rules:
- http:
paths:
- pathType: Prefix
path: /(|$)(.*)
backend:
service:
name: {{ .Chart.Name }}-service
port:
number: 80
15 changes: 15 additions & 0 deletions .infrastructure/helm-chart/todoapp/templates/nodeport.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Service
metadata:
name: {{ .Chart.Name }}-nodeport
namespace: {{ .Values.todoapp.namespace }}
spec:
type: NodePort
selector:
app: {{ .Chart.Name }}
ports:
- protocol: TCP
port: 80
targetPort: 8080
nodePort: 30007

4 changes: 4 additions & 0 deletions .infrastructure/helm-chart/todoapp/templates/ns.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: {{ .Values.todoapp.namespace }}
Loading