From c58a98c8ce9493719cf951d223f7e6f8fd10e609 Mon Sep 17 00:00:00 2001 From: Andreas Eberle Date: Fri, 16 Feb 2024 00:01:30 +0100 Subject: [PATCH] Add helm chart to deploy to k8s --- k8s/helm/.helmignore | 23 +++++ k8s/helm/Chart.yaml | 7 ++ k8s/helm/templates/NOTES.txt | 22 +++++ k8s/helm/templates/_helpers.tpl | 61 +++++++++++++ k8s/helm/templates/deployment.yaml | 83 ++++++++++++++++++ k8s/helm/templates/ingress.yaml | 61 +++++++++++++ k8s/helm/templates/service.yaml | 15 ++++ k8s/helm/values.yaml | 63 ++++++++++++++ test.yaml | 134 +++++++++++++++++++++++++++++ 9 files changed, 469 insertions(+) create mode 100644 k8s/helm/.helmignore create mode 100644 k8s/helm/Chart.yaml create mode 100644 k8s/helm/templates/NOTES.txt create mode 100644 k8s/helm/templates/_helpers.tpl create mode 100644 k8s/helm/templates/deployment.yaml create mode 100644 k8s/helm/templates/ingress.yaml create mode 100644 k8s/helm/templates/service.yaml create mode 100644 k8s/helm/values.yaml create mode 100644 test.yaml diff --git a/k8s/helm/.helmignore b/k8s/helm/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/k8s/helm/.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/k8s/helm/Chart.yaml b/k8s/helm/Chart.yaml new file mode 100644 index 0000000..279246a --- /dev/null +++ b/k8s/helm/Chart.yaml @@ -0,0 +1,7 @@ +apiVersion: v2 +name: bookstore +description: A Helm chart for Kubernetes + +type: application +version: 0.1.0 +appVersion: "1.0.0" diff --git a/k8s/helm/templates/NOTES.txt b/k8s/helm/templates/NOTES.txt new file mode 100644 index 0000000..f3b73f1 --- /dev/null +++ b/k8s/helm/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 }}://{{ tpl $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 "helm.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 "helm.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "helm.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 "helm.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/k8s/helm/templates/_helpers.tpl b/k8s/helm/templates/_helpers.tpl new file mode 100644 index 0000000..de4f19b --- /dev/null +++ b/k8s/helm/templates/_helpers.tpl @@ -0,0 +1,61 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "helm.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 "helm.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 "helm.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "helm.labels" -}} +helm.sh/chart: {{ include "helm.chart" . }} +{{ include "helm.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "helm.selectorLabels" -}} +app.kubernetes.io/name: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "helm.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "helm.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/k8s/helm/templates/deployment.yaml b/k8s/helm/templates/deployment.yaml new file mode 100644 index 0000000..5a4440c --- /dev/null +++ b/k8s/helm/templates/deployment.yaml @@ -0,0 +1,83 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "helm.fullname" . }} + labels: + {{- include "helm.labels" . | nindent 4 }} +spec: + replicas: {{ .Values.replicaCount }} + selector: + matchLabels: + {{- include "helm.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "helm.labels" . | nindent 8 }} + {{- with .Values.podLabels }} + {{- toYaml . | nindent 8 }} + {{- end }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}/{{ .Values.image.name }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - name: http + containerPort: {{ .Values.service.port }} + protocol: TCP +{{/* livenessProbe:*/}} +{{/* httpGet:*/}} +{{/* path: /*/}} +{{/* port: http*/}} +{{/* readinessProbe:*/}} +{{/* httpGet:*/}} +{{/* path: /*/}} +{{/* port: http*/}} + resources: + {{- toYaml .Values.resources | nindent 12 }} + env: + - name: DB_PASSWORD + valueFrom: + secretKeyRef: + name: {{ .Release.Name }}-postgresql + key: postgres-password + - name: DB_HOST + value: {{ tpl .Values.db.host $ }} + - name: DB_USERNAME + value: {{ tpl .Values.db.username $ }} + - name: DB_NAME + value: {{ tpl .Values.db.name $ }} + - name: DATABASE_URL + value: postgres://postgres:postgres@db/bookstore + {{- with .Values.volumeMounts }} + volumeMounts: + {{- toYaml . | nindent 12 }} + {{- end }} + {{- with .Values.volumes }} + volumes: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/k8s/helm/templates/ingress.yaml b/k8s/helm/templates/ingress.yaml new file mode 100644 index 0000000..e299f80 --- /dev/null +++ b/k8s/helm/templates/ingress.yaml @@ -0,0 +1,61 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "helm.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 "helm.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 }} + - {{ tpl . $ | quote }} + {{- end }} + secretName: {{ tpl .secretName $ }} + {{- end }} + {{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ tpl .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/k8s/helm/templates/service.yaml b/k8s/helm/templates/service.yaml new file mode 100644 index 0000000..de450fc --- /dev/null +++ b/k8s/helm/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "helm.fullname" . }} + labels: + {{- include "helm.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "helm.selectorLabels" . | nindent 4 }} diff --git a/k8s/helm/values.yaml b/k8s/helm/values.yaml new file mode 100644 index 0000000..5351940 --- /dev/null +++ b/k8s/helm/values.yaml @@ -0,0 +1,63 @@ +replicaCount: 1 + +image: + repository: ghcr.io/arconsis/server_benchmarks + name: + pullPolicy: IfNotPresent + tag: + + +db: + host: "{{ .Release.Name }}-postgresql" + name: books-db + username: postgres + + +nameOverride: "" +fullnameOverride: "" + +imagePullSecrets: [] + +podAnnotations: {} +podLabels: {} + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +service: + type: ClusterIP + port: 80 + +ingress: + enabled: true + className: "" + annotations: + kubernetes.io/tls-acme: "true" + cert-manager.io/cluster-issuer: letsencrypt-prod + kubernetes.io/ingress.class: nginx + hosts: + - host: "{{ .Release.Name }}.benchmarks.k8s.dev.arconsis.com" + paths: + - path: / + pathType: ImplementationSpecific + tls: + - secretName: "{{ .Release.Name }}-tls" + hosts: + - "{{ .Release.Name }}.benchmarks.k8s.dev.arconsis.com" + +resources: + limits: + cpu: 1000m + memory: 256Mi + requests: + cpu: 10m + memory: 32Mi + diff --git a/test.yaml b/test.yaml new file mode 100644 index 0000000..60f729f --- /dev/null +++ b/test.yaml @@ -0,0 +1,134 @@ +--- +# Source: bookstore/templates/service.yaml +apiVersion: v1 +kind: Service +metadata: + name: bookstore-rocketrs + labels: + helm.sh/chart: bookstore-0.1.0 + app.kubernetes.io/name: bookstore-rocketrs + app.kubernetes.io/version: "1.0.0" + app.kubernetes.io/managed-by: Helm +spec: + type: ClusterIP + ports: + - port: 80 + targetPort: http + protocol: TCP + name: http + selector: + app.kubernetes.io/name: bookstore-rocketrs +--- +# Source: bookstore/templates/deployment.yaml +apiVersion: apps/v1 +kind: Deployment +metadata: + name: bookstore-rocketrs + labels: + helm.sh/chart: bookstore-0.1.0 + app.kubernetes.io/name: bookstore-rocketrs + app.kubernetes.io/version: "1.0.0" + app.kubernetes.io/managed-by: Helm +spec: + replicas: 1 + selector: + matchLabels: + app.kubernetes.io/name: bookstore-rocketrs + template: + metadata: + labels: + helm.sh/chart: bookstore-0.1.0 + app.kubernetes.io/name: bookstore-rocketrs + app.kubernetes.io/version: "1.0.0" + app.kubernetes.io/managed-by: Helm + spec: + securityContext: + {} + containers: + - name: bookstore + securityContext: + {} + image: "ghcr.io/arconsis/server_benchmarks/bookstore-rocketrs:fb23d8d23b56491b49cf51dd9ede111c456a270c" + imagePullPolicy: IfNotPresent + ports: + - name: http + containerPort: 80 + protocol: TCP + + + + + + + + + resources: + limits: + cpu: 1000m + memory: 256Mi + requests: + cpu: 10m + memory: 32Mi + env: + - name: DB_PASSWORD + valueFrom: + secretKeyRef: + name: bookstore-rocketrs-postgresql + key: postgres-password + - name: DB_HOST + value: bookstore-rocketrs-postgresql + - name: DB_USERNAME + value: postgres + - name: DB_NAME + value: books-db +--- +# Source: bookstore/templates/ingress.yaml +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: bookstore-rocketrs + labels: + helm.sh/chart: bookstore-0.1.0 + app.kubernetes.io/name: bookstore-rocketrs + app.kubernetes.io/version: "1.0.0" + app.kubernetes.io/managed-by: Helm + annotations: + cert-manager.io/cluster-issuer: letsencrypt-prod + kubernetes.io/ingress.class: nginx + kubernetes.io/tls-acme: "true" +spec: + tls: + - hosts: + - "bookstore-rocketrs.benchmarks.k8s.dev.arconsis.com" + secretName: bookstore-rocketrs-tls + rules: + - host: "bookstore-rocketrs.benchmarks.k8s.dev.arconsis.com" + http: + paths: + - path: / + pathType: ImplementationSpecific + backend: + service: + name: bookstore-rocketrs + port: + number: 80 +--- +# Source: bookstore/templates/tests/test-connection.yaml +apiVersion: v1 +kind: Pod +metadata: + name: "bookstore-rocketrs-test-connection" + labels: + helm.sh/chart: bookstore-0.1.0 + app.kubernetes.io/name: bookstore-rocketrs + app.kubernetes.io/version: "1.0.0" + app.kubernetes.io/managed-by: Helm + annotations: + "helm.sh/hook": test +spec: + containers: + - name: wget + image: busybox + command: ['wget'] + args: ['bookstore-rocketrs:80'] + restartPolicy: Never