From f4fdabc23daef70038c20bedb06d97d587aa33f6 Mon Sep 17 00:00:00 2001 From: mrfzy Date: Fri, 17 Nov 2023 21:27:51 +0700 Subject: [PATCH] added new configuration for helm deployment: added readme on the infrastructure --- .dockerignore | 1 - .gitignore | 1 + README.md | 0 api-laravel-crud/.dockerignore | 1 + laravel-minikube/.helmignore | 23 +++++++ laravel-minikube/Chart.yaml | 7 +++ laravel-minikube/templates/NOTES.txt | 22 +++++++ laravel-minikube/templates/_helpers.tpl | 62 +++++++++++++++++++ laravel-minikube/templates/deployment.yaml | 48 ++++++++++++++ laravel-minikube/templates/ingress.yaml | 61 ++++++++++++++++++ laravel-minikube/templates/service.yaml | 18 ++++++ .../templates/tests/test-connection.yaml | 15 +++++ laravel-minikube/values.yaml | 51 +++++++++++++++ 13 files changed, 309 insertions(+), 1 deletion(-) delete mode 100644 .dockerignore create mode 100644 README.md create mode 100644 api-laravel-crud/.dockerignore create mode 100644 laravel-minikube/.helmignore create mode 100644 laravel-minikube/Chart.yaml create mode 100644 laravel-minikube/templates/NOTES.txt create mode 100644 laravel-minikube/templates/_helpers.tpl create mode 100644 laravel-minikube/templates/deployment.yaml create mode 100644 laravel-minikube/templates/ingress.yaml create mode 100644 laravel-minikube/templates/service.yaml create mode 100644 laravel-minikube/templates/tests/test-connection.yaml create mode 100644 laravel-minikube/values.yaml diff --git a/.dockerignore b/.dockerignore deleted file mode 100644 index 9f4c740..0000000 --- a/.dockerignore +++ /dev/null @@ -1 +0,0 @@ -db/ \ No newline at end of file diff --git a/.gitignore b/.gitignore index 9f4c740..b143073 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +crud_laravel_api-sre-test-app-chart/templates/secrets.yaml db/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/api-laravel-crud/.dockerignore b/api-laravel-crud/.dockerignore new file mode 100644 index 0000000..48b8bf9 --- /dev/null +++ b/api-laravel-crud/.dockerignore @@ -0,0 +1 @@ +vendor/ diff --git a/laravel-minikube/.helmignore b/laravel-minikube/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/laravel-minikube/.helmignore @@ -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/ diff --git a/laravel-minikube/Chart.yaml b/laravel-minikube/Chart.yaml new file mode 100644 index 0000000..0c916bb --- /dev/null +++ b/laravel-minikube/Chart.yaml @@ -0,0 +1,7 @@ +apiVersion: v2 +name: laravel-minikube +description: A Helm chart for Kubernetes to deploy Laravel + +type: application +version: 0.1.0 +appVersion: "1.16.0" diff --git a/laravel-minikube/templates/NOTES.txt b/laravel-minikube/templates/NOTES.txt new file mode 100644 index 0000000..2b79b31 --- /dev/null +++ b/laravel-minikube/templates/NOTES.txt @@ -0,0 +1,22 @@ +1. Get the application URL by running these commands: +{{- if .Values.ingress.enabled }} +{{- range $host := .Values.ingress.hosts }} + {{- range .paths }} + http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }} + {{- end }} +{{- end }} +{{- else if contains "NodePort" .Values.service.type }} + export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "laravel-minikube.fullname" . }}) + export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}") + echo http://$NODE_IP:$NODE_PORT +{{- else if contains "LoadBalancer" .Values.service.type }} + NOTE: It may take a few minutes for the LoadBalancer IP to be available. + You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "laravel-minikube.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "laravel-minikube.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") + echo http://$SERVICE_IP:{{ .Values.service.port }} +{{- else if contains "ClusterIP" .Values.service.type }} + export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "laravel-minikube.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") + export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}") + echo "Visit http://127.0.0.1:8080 to use your application" + kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT +{{- end }} diff --git a/laravel-minikube/templates/_helpers.tpl b/laravel-minikube/templates/_helpers.tpl new file mode 100644 index 0000000..fc0652b --- /dev/null +++ b/laravel-minikube/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "laravel-minikube.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 "laravel-minikube.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 "laravel-minikube.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "laravel-minikube.labels" -}} +helm.sh/chart: {{ include "laravel-minikube.chart" . }} +{{ include "laravel-minikube.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "laravel-minikube.selectorLabels" -}} +app.kubernetes.io/name: {{ include "laravel-minikube.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "laravel-minikube.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "laravel-minikube.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/laravel-minikube/templates/deployment.yaml b/laravel-minikube/templates/deployment.yaml new file mode 100644 index 0000000..1ea2a5c --- /dev/null +++ b/laravel-minikube/templates/deployment.yaml @@ -0,0 +1,48 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "laravel-minikube.fullname" . }} + labels: + app.kubernetes.io/name: {{ include "laravel-minikube.name" . }} + helm.sh/chart: {{ include "laravel-minikube.chart" . }} + app.kubernetes.io/instance: {{ .Release.Name }} +spec: + replicas: {{ .Values.replicaCount }} + selector: + matchLabels: + app.kubernetes.io/name: {{ include "laravel-minikube.name" . }} + app.kubernetes.io/instance: {{ .Release.Name }} + template: + metadata: + labels: + app.kubernetes.io/name: {{ include "laravel-minikube.name" . }} + app.kubernetes.io/instance: {{ .Release.Name }} + spec: + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy | default "Always" }} + ports: + - name: http + containerPort: 80 + protocol: TCP + env: + - name: APP_KEY + valueFrom: + secretKeyRef: + name: laravel-minikube-app-key + key: APP_KEY + - name: DB_CONNECTION + value: mysql + - name: DB_HOST + value: laraveldb + - name: DB_PORT + value: "33060" + - name: DB_DATABASE + value: {{ .Values.database.name }} + - name: DB_USERNAME + value: {{ .Values.database.user }} + - name: DB_PASSWORD + value: {{ .Values.database.password }} diff --git a/laravel-minikube/templates/ingress.yaml b/laravel-minikube/templates/ingress.yaml new file mode 100644 index 0000000..c446ad2 --- /dev/null +++ b/laravel-minikube/templates/ingress.yaml @@ -0,0 +1,61 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "laravel-minikube.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 +{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1beta1 +{{- else -}} +apiVersion: extensions/v1beta1 +{{- end }} +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "laravel-minikube.labels" . | nindent 4 }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + {{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} + ingressClassName: {{ .Values.ingress.className }} + {{- end }} + {{- if .Values.ingress.tls }} + tls: + {{- range .Values.ingress.tls }} + - hosts: + {{- range .hosts }} + - {{ . | quote }} + {{- end }} + secretName: {{ .secretName }} + {{- end }} + {{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ .host | quote }} + http: + paths: + {{- range .paths }} + - path: {{ .path }} + {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }} + pathType: {{ .pathType }} + {{- end }} + backend: + {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }} + service: + name: {{ $fullName }} + port: + number: {{ $svcPort }} + {{- else }} + serviceName: {{ $fullName }} + servicePort: {{ $svcPort }} + {{- end }} + {{- end }} + {{- end }} +{{- end }} diff --git a/laravel-minikube/templates/service.yaml b/laravel-minikube/templates/service.yaml new file mode 100644 index 0000000..32b9ff9 --- /dev/null +++ b/laravel-minikube/templates/service.yaml @@ -0,0 +1,18 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "laravel-minikube.fullname" . }} + labels: + app.kubernetes.io/name: {{ include "laravel-minikube.name" . }} + helm.sh/chart: {{ include "laravel-minikube.chart" . }} + app.kubernetes.io/instance: {{ .Release.Name }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + app.kubernetes.io/name: {{ include "laravel-minikube.name" . }} + app.kubernetes.io/instance: {{ .Release.Name }} diff --git a/laravel-minikube/templates/tests/test-connection.yaml b/laravel-minikube/templates/tests/test-connection.yaml new file mode 100644 index 0000000..8c4fe5d --- /dev/null +++ b/laravel-minikube/templates/tests/test-connection.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: "{{ include "laravel-minikube.fullname" . }}-test-connection" + labels: + {{- include "laravel-minikube.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": test +spec: + containers: + - name: wget + image: busybox + command: ['wget'] + args: ['{{ include "laravel-minikube.fullname" . }}:{{ .Values.service.port }}'] + restartPolicy: Never diff --git a/laravel-minikube/values.yaml b/laravel-minikube/values.yaml new file mode 100644 index 0000000..1d659f4 --- /dev/null +++ b/laravel-minikube/values.yaml @@ -0,0 +1,51 @@ +replicaCount: 1 +image: + repository: mrfzy00/crud_laravel_api-sre-test-app + tag: latest + pullPolicy: IfNotPresent + +service: + name: laravel-app + type: ClusterIP + port: 80 + +ingress: + enabled: false + annotations: {} + hosts: + - host: chart-example.local + paths: ["/"] + tls: [] + +appKey: + secretName: laravel-minikube-app-key + secretKey: APP_KEY + +database: + enabled: true + type: mysql + name: laraveldb + user: root + rootPassword: "Ch4ng3me!@" + +prometheus: + enabled: true + alertmanager: + enabled: true + serverFiles: + prometheus.yml: + global: + scrape_interval: 15s + scrape_configs: + - job_name: 'laravel-app' + static_configs: + - targets: ['laravel-app:80'] + +grafana: + enabled: true + adminPassword: admin + service: + type: LoadBalancer + dashboards: + default: + - name: 'Laravel App Metrics'