From 6576ecbbe53c88b461444ef075cf06731bf69d6a Mon Sep 17 00:00:00 2001 From: Ashwani Singh Date: Fri, 5 Jul 2024 10:20:47 +0530 Subject: [PATCH 01/15] Implement application health check and hpa Signed-off-by: Ashwani Singh --- charts/microservice/Chart.yaml | 20 +++++++- charts/microservice/README.md | 4 +- charts/microservice/templates/_helpers.tpl | 39 +++++++++------- charts/microservice/templates/deployment.yaml | 40 ++++++++++++---- charts/microservice/templates/hpa.yaml | 27 +++++++++++ charts/microservice/templates/service.yaml | 6 +-- charts/microservice/values.yaml | 46 +++++++++++++++---- 7 files changed, 144 insertions(+), 38 deletions(-) create mode 100644 charts/microservice/templates/hpa.yaml diff --git a/charts/microservice/Chart.yaml b/charts/microservice/Chart.yaml index 7be8a3b0..e6410f85 100644 --- a/charts/microservice/Chart.yaml +++ b/charts/microservice/Chart.yaml @@ -1,9 +1,27 @@ ---- apiVersion: v2 name: microservice description: A Helm chart for a microservice in Kubernetes + +# A chart can be either an 'application' or a 'library' chart +# +# Application chart deploys a complete application, includes all necessary Kubernetes resources, +# used for installing and managing applications. +# +# Library chart provides reusable templates and components, does not deploy an application by itself, +# used to promote consistency and reuse across multiple charts. type: application + +# This is the chart version. This chart version should be incremented with each change to the chart +# or its templates, including the app version. +# Follow Semantic Versioning (https://semver.org/). version: 0.1.2 + +# This is the application version number, which should be incremented with each change to the application. +# It doesn't need to follow Semantic Versioning but should reflect the application's version. +# Quotes are recommended. appVersion: "0.1.2" + +# This is maintainers files. this field lists the individuals or teams responsible for maintaining the Helm chart. +# It typically includes their names and contact information, such as email addresses. maintainers: - name: Opstree Solutions diff --git a/charts/microservice/README.md b/charts/microservice/README.md index cfe8f6e0..4bb7b668 100644 --- a/charts/microservice/README.md +++ b/charts/microservice/README.md @@ -25,9 +25,9 @@ helm install my-release microservice/ ## TODO -- [ ] Application Health Probes +- [x] Application Health Probes - [ ] Service -- [ ] Horizontal Pod Autoscaler +- [x] Horizontal Pod Autoscaler - [ ] ConfigMap - [ ] RBAC - [ ] Service Account diff --git a/charts/microservice/templates/_helpers.tpl b/charts/microservice/templates/_helpers.tpl index f825ae67..7f8af161 100644 --- a/charts/microservice/templates/_helpers.tpl +++ b/charts/microservice/templates/_helpers.tpl @@ -5,8 +5,8 @@ Create a defautl fully qualified app name It will use the release name to give the app name */}} -{{- define "app.name" -}} -{{- default .Chart.Name .Values.nameOverride | trunc 60 | trimSuffix "-" }} +{{- define "microservice.name" -}} +{{- default .Values.global.nameOverride | trunc 60 | trimSuffix "-" }} {{- end }} {{/* @@ -14,11 +14,11 @@ 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 "app.tempname" -}} -{{- if .Values.fullnameOverride }} -{{- .Values.fullnameOverride | trunc 60 | trimSuffix "-" }} +{{- define "microservice.tempname" -}} +{{- if .Values.global.fullnameOverride }} +{{- .Values.global.fullnameOverride | trunc 60 | trimSuffix "-" }} {{- else }} -{{- $name := default .Chart.Name .Values.nameOverride }} +{{- $name := default .Values.global.nameOverride }} {{- if contains $name .Release.Name }} {{- .Release.Name | trunc 63 | trimSuffix "-" }} {{- else }} @@ -27,34 +27,41 @@ If release name contains chart name it will be used as a full name. {{- end }} {{- end }} -{{- define "app.fullname" -}} -{{- printf "%s-%s" ( include "app.tempname" . ) "app" | trunc 63 | trimSuffix "-" }} +{{- define "microservice.fullname" -}} +{{- printf "%s-%s" ( include "microservice.tempname" . ) "app" | trunc 63 | trimSuffix "-" }} {{- end }} {{/* Common labels */}} -{{- define "app.labels" -}} -app: {{ include "app.tempname" . }} +{{- define "microservice.labels" -}} +app: {{ include "microservice.tempname" . }} {{- end }} {{/* Selector labels */}} -{{- define "app.selectorLabels" -}} -app: {{ include "app.tempname" . }} +{{- define "microservice.selectorLabels" -}} +app: {{ include "microservice.tempname" . }} {{- end }}% {{/* service name */}} -{{- define "app.servicename" -}} -{{- printf "%s-%s" ( include "app.tempname" . ) "svc" | trunc 63 | trimSuffix "-" }} +{{- define "microservice.servicename" -}} +{{- printf "%s-%s" ( include "microservice.tempname" . ) "svc" | trunc 63 | trimSuffix "-" }} {{- end }}% {{/* configmap name */}} -{{- define "app.configmapname" -}} -{{- printf "%s-%s" ( include "app.tempname" . ) "cm" | trunc 63 | trimSuffix "-" }} +{{- define "microservice.configmapname" -}} +{{- printf "%s-%s" ( include "microservice.tempname" . ) "cm" | trunc 63 | trimSuffix "-" }} +{{- end }}% + +{{/* +Create name of the HPA to use +*/}} +{{- define "microservice.HorizontalScalingName" -}} +{{- printf "%s-%s" ( include "microservice.tempname" . ) "hpa" | trunc 63 | trimSuffix "-" }} {{- end }}% diff --git a/charts/microservice/templates/deployment.yaml b/charts/microservice/templates/deployment.yaml index 4292f925..4d7651f6 100644 --- a/charts/microservice/templates/deployment.yaml +++ b/charts/microservice/templates/deployment.yaml @@ -1,24 +1,48 @@ apiVersion: apps/v1 kind: Deployment metadata: - name: {{ include "app.fullname" . }} + name: {{ include "microservice.fullname" . }} labels: - {{- include "app.labels" . | nindent 4 }} + {{- include "microservice.labels" . | nindent 4 }} spec: - replicas: {{ .Values.replicaCount }} + replicas: {{ .Values.global.replicaCount }} selector: matchLabels: - {{- include "app.selectorLabels" . | nindent 6 }} + {{- include "microservice.selectorLabels" . | nindent 6 }} template: metadata: labels: - {{- include "app.selectorLabels" . | nindent 8 }} + {{- include "microservice.selectorLabels" . | nindent 8 }} spec: containers: - - name: {{ .Chart.Name }} - image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" - imagePullPolicy: {{ .Values.image.pullPolicy }} + - name: {{ include "microservice.fullname" . }} + image: "{{ .Values.deployment.image.repository }}:{{ .Values.deployment.image.tag }}" + imagePullPolicy: {{ .Values.deployment.image.pullPolicy | default "IfNotPresent" }} ports: - name: http containerPort: 80 protocol: TCP + {{- if .Values.deployment.livenessProbe }} + livenessProbe: + httpGet: + path: {{ default "/health" .Values.deployment.livenessProbe.httpPath }} + port: http + initialDelaySeconds: {{ default 25 .Values.deployment.livenessProbe.initialDelaySeconds }} + periodSeconds: {{ default 10 .Values.deployment.livenessProbe.periodSeconds }} + timeoutSeconds: {{ default 5 .Values.deployment.livenessProbe.timeoutSeconds }} + successThreshold: {{ default 1 .Values.deployment.livenessProbe.successThreshold }} + failureThreshold: {{ default 5 .Values.deployment.livenessProbe.failureThreshold }} + {{- end }} + {{- if .Values.deployment.readinessProbe }} + readinessProbe: + httpGet: + path: {{ default "/health" .Values.deployment.readinessProbe.httpPath }} + port: http + initialDelaySeconds: {{ default 25 .Values.deployment.readinessProbe.initialDelaySeconds }} + periodSeconds: {{ default 10 .Values.deployment.readinessProbe.periodSeconds }} + timeoutSeconds: {{ default 5 .Values.deployment.readinessProbe.timeoutSeconds }} + successThreshold: {{ default 1 .Values.deployment.readinessProbe.successThreshold }} + failureThreshold: {{ default 5 .Values.deployment.readinessProbe.failureThreshold }} + {{- end }} + resources: + {{- toYaml .Values.deployment.resources | nindent 12 }} \ No newline at end of file diff --git a/charts/microservice/templates/hpa.yaml b/charts/microservice/templates/hpa.yaml new file mode 100644 index 00000000..8b021f8c --- /dev/null +++ b/charts/microservice/templates/hpa.yaml @@ -0,0 +1,27 @@ +{{- if .Values.hpa }} +apiVersion: autoscaling/v2beta1 +kind: HorizontalPodAutoscaler +metadata: + name: {{ include "microservice.HorizontalScalingName" . }} + labels: + {{- include "microservice.labels" . | nindent 4 }} +spec: + scaleTargetRef: + kind: Deployment + name: {{ include "microservice.fullname" . }} + minReplicas: {{ default 1 .Values.hpa.minReplicas }} + maxReplicas: {{ default 1 .Values.hpa.maxReplicas }} + metrics: + {{- if .Values.hpa.targetAverageCPUUtilization }} + - type: Resource + resource: + name: cpu + targetAverageUtilization: {{ default 50 .Values.hpa.targetAverageCPUUtilization }} + {{- end }} + {{- if .Values.hpa.targetAverageMemoryUtilization }} + - type: Resource + resource: + name: memory + targetAverageUtilization: {{ default 50 .Values.hpa.targetAverageMemoryUtilization }} + {{- end }} +{{- end }} \ No newline at end of file diff --git a/charts/microservice/templates/service.yaml b/charts/microservice/templates/service.yaml index 78d58947..7bec37d2 100644 --- a/charts/microservice/templates/service.yaml +++ b/charts/microservice/templates/service.yaml @@ -1,9 +1,9 @@ apiVersion: v1 kind: Service metadata: - name: {{ include "app.servicename" . }} + name: {{ include "microservice.servicename" . }} labels: - {{- include "app.labels" . | nindent 4 }} + {{- include "microservice.labels" . | nindent 4 }} spec: type: {{ .Values.service.type }} ports: @@ -12,4 +12,4 @@ spec: protocol: TCP name: http selector: - {{- include "app.selectorLabels" . | nindent 4 }} + {{- include "microservice.selectorLabels" . | nindent 4 }} diff --git a/charts/microservice/values.yaml b/charts/microservice/values.yaml index 70374108..e9deaa77 100644 --- a/charts/microservice/values.yaml +++ b/charts/microservice/values.yaml @@ -1,12 +1,42 @@ -replicaCount: 1 +# -- global variables +global: + replicaCount: 1 + fullnameOverride: "nginx" + nameOverride: "" -fullnameOverride: "nginx" -nameOverride: "" -image: - repository: nginx - pullPolicy: IfNotPresent - tag: "latest" +# -- Object that configures Deployment instance +deployment: + # -- Override default container image format + image: + # -- Image repository + repository: "nginx" + # -- Image tag + tag: "latest" + # -- Variable used to control when the image should be fetched + pullPolicy: always + # -- Definition of the liveness probe + livenessProbe: + httpPath: '/' + # -- Definition of the readiness probe + readinessProbe: + httpPath: '/' + # -- Configure container's resource request and limits + resources: + # -- Resources that the container is guaranteed to get + requests: + # -- Memory that the container is guaranteed to get + memory: + # -- CPU that the container is guaranteed to get + cpu: + # -- Defines a hard ceiling on resource use + limits: + # -- Defines a hard ceiling on how much Memory time that the container can use + memory: + # -- Defines a hard ceiling on how much CPU time that the container can use + cpu: + +hpa: {} service: type: ClusterIP - port: 80 + port: 80 \ No newline at end of file From 898d793b96d43effb891eb975cfde387b428514a Mon Sep 17 00:00:00 2001 From: Ashwani Singh Date: Mon, 8 Jul 2024 15:32:32 +0530 Subject: [PATCH 02/15] Update according to recommendation Signed-off-by: Ashwani Singh --- .../microservice/examples/deploy-nginx.yaml | 5 + charts/microservice/templates/NOTES.txt | 5 + .../microservice/templates/_capabilities.tpl | 25 +++ charts/microservice/templates/_helpers.tpl | 54 ++----- charts/microservice/templates/configmap.yaml | 30 ++++ charts/microservice/templates/deployment.yaml | 86 +++++++--- charts/microservice/templates/hpa.yaml | 21 +-- charts/microservice/templates/service.yaml | 31 +++- charts/microservice/values.yaml | 152 ++++++++++++++---- 9 files changed, 300 insertions(+), 109 deletions(-) create mode 100644 charts/microservice/examples/deploy-nginx.yaml create mode 100644 charts/microservice/templates/NOTES.txt create mode 100644 charts/microservice/templates/_capabilities.tpl create mode 100644 charts/microservice/templates/configmap.yaml diff --git a/charts/microservice/examples/deploy-nginx.yaml b/charts/microservice/examples/deploy-nginx.yaml new file mode 100644 index 00000000..03e4b923 --- /dev/null +++ b/charts/microservice/examples/deploy-nginx.yaml @@ -0,0 +1,5 @@ +deployment: + image: + name: "nginx" + tag: "latest" + pullPolicy: IfNotPresent \ No newline at end of file diff --git a/charts/microservice/templates/NOTES.txt b/charts/microservice/templates/NOTES.txt new file mode 100644 index 00000000..c12295b8 --- /dev/null +++ b/charts/microservice/templates/NOTES.txt @@ -0,0 +1,5 @@ +You have deployed the following release: {{ .Release.Name }}. +To get further information, you can run the commands: + $ helm status {{ .Release.Name }} + $ helm get all {{ .Release.Name }} + diff --git a/charts/microservice/templates/_capabilities.tpl b/charts/microservice/templates/_capabilities.tpl new file mode 100644 index 00000000..5c7814bd --- /dev/null +++ b/charts/microservice/templates/_capabilities.tpl @@ -0,0 +1,25 @@ +{{/* vim: set filetype=mustache: */}} + +{{/* +Return the target Kubernetes version +*/}} +{{- define "microservice.capabilities.kubeVersion" -}} +{{- default (default .Capabilities.KubeVersion.Version .Values.kubeVersion) ((.Values.global).kubeVersion) -}} +{{- end -}} + +{{/* +Return the appropriate apiVersion for Horizontal Pod Autoscaler. +*/}} +{{- define "microservice.capabilities.hpa.apiVersion" -}} +{{- $kubeVersion := include "microservice.capabilities.kubeVersion" .context -}} +{{- if and (not (empty $kubeVersion)) (semverCompare "<1.23-0" $kubeVersion) -}} +{{- if .beta2 -}} +{{- print "autoscaling/v2beta2" -}} +{{- else -}} +{{- print "autoscaling/v2beta1" -}} +{{- end -}} +{{- else -}} +{{- print "autoscaling/v2" -}} +{{- end -}} +{{- end -}} + diff --git a/charts/microservice/templates/_helpers.tpl b/charts/microservice/templates/_helpers.tpl index 7f8af161..c6684172 100644 --- a/charts/microservice/templates/_helpers.tpl +++ b/charts/microservice/templates/_helpers.tpl @@ -6,7 +6,7 @@ It will use the release name to give the app name */}} {{- define "microservice.name" -}} -{{- default .Values.global.nameOverride | trunc 60 | trimSuffix "-" }} +{{- default .Chart.Name .Values.global.nameOverride | trunc 63 | trimSuffix "-" }} {{- end }} {{/* @@ -14,54 +14,30 @@ 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 "microservice.tempname" -}} -{{- if .Values.global.fullnameOverride }} -{{- .Values.global.fullnameOverride | trunc 60 | trimSuffix "-" }} -{{- else }} -{{- $name := default .Values.global.nameOverride }} -{{- if contains $name .Release.Name }} -{{- .Release.Name | trunc 63 | trimSuffix "-" }} -{{- else }} -{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} -{{- end }} -{{- end }} -{{- end }} {{- define "microservice.fullname" -}} -{{- printf "%s-%s" ( include "microservice.tempname" . ) "app" | trunc 63 | trimSuffix "-" }} -{{- end }} +{{- if .Values.global.fullnameOverride -}} +{{- .Values.global.fullnameOverride | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- $name := default .Chart.Name .Values.global.nameOverride -}} +{{- if contains $name .Release.Name -}} +{{- .Release.Name | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}} +{{- end -}} +{{- end -}} +{{- end -}} {{/* Common labels */}} {{- define "microservice.labels" -}} -app: {{ include "microservice.tempname" . }} +app: {{ include "microservice.fullname" . }} {{- end }} {{/* Selector labels */}} {{- define "microservice.selectorLabels" -}} -app: {{ include "microservice.tempname" . }} -{{- end }}% - -{{/* -service name -*/}} -{{- define "microservice.servicename" -}} -{{- printf "%s-%s" ( include "microservice.tempname" . ) "svc" | trunc 63 | trimSuffix "-" }} -{{- end }}% - -{{/* -configmap name -*/}} -{{- define "microservice.configmapname" -}} -{{- printf "%s-%s" ( include "microservice.tempname" . ) "cm" | trunc 63 | trimSuffix "-" }} -{{- end }}% - -{{/* -Create name of the HPA to use -*/}} -{{- define "microservice.HorizontalScalingName" -}} -{{- printf "%s-%s" ( include "microservice.tempname" . ) "hpa" | trunc 63 | trimSuffix "-" }} -{{- end }}% +app: {{ include "microservice.fullname" . }} +{{- end }} diff --git a/charts/microservice/templates/configmap.yaml b/charts/microservice/templates/configmap.yaml new file mode 100644 index 00000000..ce28fa0f --- /dev/null +++ b/charts/microservice/templates/configmap.yaml @@ -0,0 +1,30 @@ +#ConfigMap mounted as volumes +{{- if .Values.deployment.volumes.configMaps }} +{{- if .Values.deployment.volumes.enabled }} +{{ $header := .Values.deployment.volumes.configFileCommonHeader | default "" }} +{{ $root := . }} +{{ range $cm := .Values.deployment.volumes.configMaps}} +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ include "microservice.fullname" $root }}-{{ $cm.name }}-cm + namespace: {{ .Values.global.namespace }} +data: + {{- if $cm.data }} + {{- range $filename, $content := $cm.data }} + # property-like keys; each key maps to a simple value + {{ $filename }}: |- +{{ $content | toString | indent 4}} + {{- end }} + {{- end }} + {{- if $cm.files }} + {{- range $file := $cm.files }} + {{ $file.destination }}: | +{{ $header | toString | indent 4 }} + {{ $root.Files.Get $file.source }} + {{- end}} + {{- end }} +{{- end }} +{{- end }} +{{- end }} \ No newline at end of file diff --git a/charts/microservice/templates/deployment.yaml b/charts/microservice/templates/deployment.yaml index 4d7651f6..adbdfb10 100644 --- a/charts/microservice/templates/deployment.yaml +++ b/charts/microservice/templates/deployment.yaml @@ -1,7 +1,16 @@ +{{ $root := . }} +--- apiVersion: apps/v1 kind: Deployment metadata: - name: {{ include "microservice.fullname" . }} + name: {{ include "microservice.fullname" . }}-app + namespace: {{ .Values.global.namespace }} + {{- if .Values.annotations }} + annotations: + {{- range $key, $value := .Values.annotations }} + {{ $key }}: {{ $value }} + {{- end }} + {{- end }} labels: {{- include "microservice.labels" . | nindent 4 }} spec: @@ -16,33 +25,64 @@ spec: spec: containers: - name: {{ include "microservice.fullname" . }} - image: "{{ .Values.deployment.image.repository }}:{{ .Values.deployment.image.tag }}" - imagePullPolicy: {{ .Values.deployment.image.pullPolicy | default "IfNotPresent" }} + image: "{{ .Values.deployment.image.name }}:{{ .Values.deployment.image.tag }}" + imagePullPolicy: {{ .Values.deployment.image.pullPolicy }} ports: - - name: http - containerPort: 80 - protocol: TCP + {{- range .Values.service.specs}} + - name: {{ .name }} + containerPort: {{ .targetPort | default .port}} + protocol: {{ .protocol | default "TCP" }} + {{- end }} + {{- if (merge .Values.global.environment .Values.deployment.environment) }} + env: + {{- range $name, $value := merge .Values.global.environment .Values.deployment.environment}} + - name: {{ $name | quote}} + value: {{ $value | quote }} + {{- end }} + {{- end }} {{- if .Values.deployment.livenessProbe }} livenessProbe: - httpGet: - path: {{ default "/health" .Values.deployment.livenessProbe.httpPath }} - port: http - initialDelaySeconds: {{ default 25 .Values.deployment.livenessProbe.initialDelaySeconds }} - periodSeconds: {{ default 10 .Values.deployment.livenessProbe.periodSeconds }} - timeoutSeconds: {{ default 5 .Values.deployment.livenessProbe.timeoutSeconds }} - successThreshold: {{ default 1 .Values.deployment.livenessProbe.successThreshold }} - failureThreshold: {{ default 5 .Values.deployment.livenessProbe.failureThreshold }} + {{- toYaml .Values.deployment.livenessProbe | nindent 12 }} {{- end }} {{- if .Values.deployment.readinessProbe }} readinessProbe: - httpGet: - path: {{ default "/health" .Values.deployment.readinessProbe.httpPath }} - port: http - initialDelaySeconds: {{ default 25 .Values.deployment.readinessProbe.initialDelaySeconds }} - periodSeconds: {{ default 10 .Values.deployment.readinessProbe.periodSeconds }} - timeoutSeconds: {{ default 5 .Values.deployment.readinessProbe.timeoutSeconds }} - successThreshold: {{ default 1 .Values.deployment.readinessProbe.successThreshold }} - failureThreshold: {{ default 5 .Values.deployment.readinessProbe.failureThreshold }} + {{- toYaml .Values.deployment.readinessProbe | nindent 12 }} {{- end }} resources: - {{- toYaml .Values.deployment.resources | nindent 12 }} \ No newline at end of file + {{- toYaml .Values.deployment.resources | nindent 12 }} + {{- if .Values.deployment.volumes.enabled }} + volumeMounts: + {{- range $conf := .Values.deployment.volumes.configMaps }} + - mountPath: {{ $conf.mountPath }} + name: {{ include "microservice.fullname" $root }}-{{ $conf.name }}-cm + {{- end }} + {{- if .Values.deployment.volumes.pvc.enabled }} + - mountPath: {{ .Values.volumes.pvc.mountPath }} + name: {{ .Values.volumes.pvc.existing_claim | default .Values.volumes.pvc.name }}-volume + {{- end }} + {{- end }} + {{- if .Values.deployment.volumes.enabled }} + volumes: + {{- range $conf := .Values.deployment.volumes.configMaps }} + - name: {{ include "microservice.fullname" $root }}-{{ $conf.name }}-cm + configMap: + name: {{ $conf.name }} + {{- end }} + {{- if .Values.deployment.volumes.pvc.enabled}} + - name: {{ .Values.deployment.volumes.pvc.existing_claim | default .Values.volumes.pvc.name }}-volume + persistentVolumeClaim: + claimName: {{ .Values.deployment.volumes.pvc.existing_claim | default .Values.volumes.pvc.name }} + {{- end}} + {{- end }} + {{- with .Values.deployment.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.deployment.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.deployment.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} \ No newline at end of file diff --git a/charts/microservice/templates/hpa.yaml b/charts/microservice/templates/hpa.yaml index 8b021f8c..b2a4e86d 100644 --- a/charts/microservice/templates/hpa.yaml +++ b/charts/microservice/templates/hpa.yaml @@ -1,27 +1,28 @@ -{{- if .Values.hpa }} -apiVersion: autoscaling/v2beta1 +{{- if .Values.hpa.enabled }} +apiVersion: {{ include "microservice.capabilities.hpa.apiVersion" ( dict "context" $ ) }} kind: HorizontalPodAutoscaler metadata: - name: {{ include "microservice.HorizontalScalingName" . }} + name: {{ include "microservice.fullname" . }}-hpa + namespace: {{ .Values.global.namespace }} labels: {{- include "microservice.labels" . | nindent 4 }} spec: scaleTargetRef: kind: Deployment name: {{ include "microservice.fullname" . }} - minReplicas: {{ default 1 .Values.hpa.minReplicas }} - maxReplicas: {{ default 1 .Values.hpa.maxReplicas }} + minReplicas: {{ .Values.hpa.minReplicas }} + maxReplicas: {{ .Values.hpa.maxReplicas }} metrics: - {{- if .Values.hpa.targetAverageCPUUtilization }} + {{- if .Values.hpa.targetCPUUtilizationPercentage }} - type: Resource resource: name: cpu - targetAverageUtilization: {{ default 50 .Values.hpa.targetAverageCPUUtilization }} + targetAverageUtilization: {{ .Values.hpa.targetCPUUtilizationPercentage }} {{- end }} - {{- if .Values.hpa.targetAverageMemoryUtilization }} + {{- if .Values.hpa.targetMemoryUtilizationPercentage }} - type: Resource resource: name: memory - targetAverageUtilization: {{ default 50 .Values.hpa.targetAverageMemoryUtilization }} + targetAverageUtilization: {{ .Values.hpa.targetMemoryUtilizationPercentage }} {{- end }} -{{- end }} \ No newline at end of file +{{- end }} diff --git a/charts/microservice/templates/service.yaml b/charts/microservice/templates/service.yaml index 7bec37d2..8d052686 100644 --- a/charts/microservice/templates/service.yaml +++ b/charts/microservice/templates/service.yaml @@ -1,15 +1,34 @@ +{{- $root:= . }} +--- apiVersion: v1 kind: Service metadata: - name: {{ include "microservice.servicename" . }} + name: {{ include "microservice.fullname" . }}-svc + namespace: {{ .Values.global.namespace }} + {{- if .Values.service.annotations }} + annotations: + {{- range $key, $value := .Values.service.annotations }} + {{ $key }}: {{ $value }} + {{- end }} + {{- end }} labels: {{- include "microservice.labels" . | nindent 4 }} spec: type: {{ .Values.service.type }} - ports: - - port: {{ .Values.service.port }} - targetPort: http - protocol: TCP - name: http selector: {{- include "microservice.selectorLabels" . | nindent 4 }} + ports: + {{- range $spec := .Values.service.specs }} + - name: {{ $spec.name }} + port: {{ $spec.port }} + protocol: {{ $spec.protocol | default "TCP" }} + {{- if $spec.targetPort }} + targetPort: {{ $spec.targetPort }} + {{- else }} + targetPort: {{ $spec.name }} + {{- end}} + {{- if $spec.nodePort }} + nodePort: {{ $spec.nodePort }} + {{- end }} + {{- end -}} + diff --git a/charts/microservice/values.yaml b/charts/microservice/values.yaml index e9deaa77..fd10418f 100644 --- a/charts/microservice/values.yaml +++ b/charts/microservice/values.yaml @@ -1,42 +1,132 @@ # -- global variables global: + namespace: "" replicaCount: 1 - fullnameOverride: "nginx" - nameOverride: "" + nameOverride: "ashwani" + fullnameOverride: "ashwani" + environment: {} + # list of key: value + # GLOBAL1: value + +## @param kubeVersion Override Kubernetes version +## +kubeVersion: "" # -- Object that configures Deployment instance deployment: # -- Override default container image format image: - # -- Image repository - repository: "nginx" - # -- Image tag - tag: "latest" - # -- Variable used to control when the image should be fetched - pullPolicy: always - # -- Definition of the liveness probe - livenessProbe: - httpPath: '/' - # -- Definition of the readiness probe - readinessProbe: - httpPath: '/' - # -- Configure container's resource request and limits - resources: - # -- Resources that the container is guaranteed to get - requests: - # -- Memory that the container is guaranteed to get - memory: - # -- CPU that the container is guaranteed to get - cpu: - # -- Defines a hard ceiling on resource use - limits: - # -- Defines a hard ceiling on how much Memory time that the container can use - memory: - # -- Defines a hard ceiling on how much CPU time that the container can use - cpu: - -hpa: {} + name: "" + tag: "" + pullPolicy: IfNotPresent + + # Annotation for the Deployment + annotations: {} + + livenessProbe: {} + # livenessProbe: + # httpGet: + # path: "/" + # port: http + # initialDelaySeconds: 25 + # periodSeconds: 10 + # timeoutSeconds: 5 + # successThreshold: 1 + # failureThreshold: 5 + + readinessProbe: {} + # readinessProbe: + # httpGet: + # path: "/" + # port: http + # initialDelaySeconds: 25 + # periodSeconds: 10 + # timeoutSeconds: 5 + # successThreshold: 1 + # failureThreshold: 5 + + environment: + ashwani: singh + + # environment: {} + # VAR1: value1 + + resources: {} + + # resources: + # requests: + # memory: 100m + # cpu: 100m + # limits: + # memory: 100m + # cpu: 100m + + # Additional volumes on the output Deployment definition. + volumes: + enabled: true + pvc: + enabled: false + existing_claim: + name: pvc + mountPath: /pv + size: 1G + class: + accessModes: + - ReadWriteOnce + + # configFileCommonHeader: | + # line1 + # line2 + + configMaps: + - name: test + mountPath: /test + data: + test.conf: | + hello + # hello2 + + # - name: test-from-file + # mountPath: /test2 + # files: + # - source: config.conf + # destination: application.conf + # - name: test-mixed + # mountPath: /test3 + # data: + # test2.conf: | + # another hello + # files: + # - source: config.conf + # destination: application2.conf + + # - name: foo + # secret: + # secretName: mysecret + # optional: false + + # Additional volumeMounts on the output Deployment definition. + volumeMounts: [] + # - name: foo + # mountPath: "/etc/foo" + # readOnly: true + + nodeSelector: {} + + tolerations: [] + + affinity: {} +hpa: + enabled: true + minReplicas: 1 + maxReplicas: 1 + targetCPUUtilizationPercentage: 80 + targetMemoryUtilizationPercentage: 80 + service: type: ClusterIP - port: 80 \ No newline at end of file + annotations: {} + specs: + - port: 80 + name: http From 6b9a43f6eb8a287cc8c4b2793b3feb6485fb197f Mon Sep 17 00:00:00 2001 From: Ashwani Singh Date: Mon, 8 Jul 2024 16:34:00 +0530 Subject: [PATCH 03/15] create pvc and configmap Signed-off-by: Ashwani Singh --- charts/microservice/templates/_helpers.tpl | 11 +++ charts/microservice/templates/configmap.yaml | 2 +- charts/microservice/templates/deployment.yaml | 7 +- charts/microservice/templates/pvc.yaml | 21 +++++ .../templates/serviceaccount.yaml | 13 +++ charts/microservice/values.yaml | 81 ++++++++++--------- 6 files changed, 96 insertions(+), 39 deletions(-) create mode 100644 charts/microservice/templates/pvc.yaml create mode 100644 charts/microservice/templates/serviceaccount.yaml diff --git a/charts/microservice/templates/_helpers.tpl b/charts/microservice/templates/_helpers.tpl index c6684172..5556c262 100644 --- a/charts/microservice/templates/_helpers.tpl +++ b/charts/microservice/templates/_helpers.tpl @@ -41,3 +41,14 @@ Selector labels {{- define "microservice.selectorLabels" -}} app: {{ include "microservice.fullname" . }} {{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "microservice.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "microservice.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} \ No newline at end of file diff --git a/charts/microservice/templates/configmap.yaml b/charts/microservice/templates/configmap.yaml index ce28fa0f..8e73d584 100644 --- a/charts/microservice/templates/configmap.yaml +++ b/charts/microservice/templates/configmap.yaml @@ -9,7 +9,7 @@ apiVersion: v1 kind: ConfigMap metadata: name: {{ include "microservice.fullname" $root }}-{{ $cm.name }}-cm - namespace: {{ .Values.global.namespace }} + namespace: {{ $root.Values.global.namespace }} data: {{- if $cm.data }} {{- range $filename, $content := $cm.data }} diff --git a/charts/microservice/templates/deployment.yaml b/charts/microservice/templates/deployment.yaml index adbdfb10..67da2a1c 100644 --- a/charts/microservice/templates/deployment.yaml +++ b/charts/microservice/templates/deployment.yaml @@ -23,6 +23,11 @@ spec: labels: {{- include "microservice.selectorLabels" . | nindent 8 }} spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "microservice.serviceAccountName" . }} containers: - name: {{ include "microservice.fullname" . }} image: "{{ .Values.deployment.image.name }}:{{ .Values.deployment.image.tag }}" @@ -66,7 +71,7 @@ spec: {{- range $conf := .Values.deployment.volumes.configMaps }} - name: {{ include "microservice.fullname" $root }}-{{ $conf.name }}-cm configMap: - name: {{ $conf.name }} + name: {{ include "microservice.fullname" $root }}-{{ $conf.name }}-cm {{- end }} {{- if .Values.deployment.volumes.pvc.enabled}} - name: {{ .Values.deployment.volumes.pvc.existing_claim | default .Values.volumes.pvc.name }}-volume diff --git a/charts/microservice/templates/pvc.yaml b/charts/microservice/templates/pvc.yaml new file mode 100644 index 00000000..53a36951 --- /dev/null +++ b/charts/microservice/templates/pvc.yaml @@ -0,0 +1,21 @@ +{{- if .Values.deployment.volumes.pvc.enabled }} +{{- if .Values.deployment.volumes.pvc.existing_claim -}} +{{- else -}} +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: {{ .Values.deployment.volumes.pvc.name }} + namespace: {{ .Values.global.namespace }} +spec: + {{- if .Values.deployment.volumes.pvc.class }} + storageClassName: {{ .Values.deployment.volumes.pvc.class }} + {{- end }} + accessModes: + {{- range $accessMode := .Values.deployment.volumes.pvc.accessModes }} + - {{ $accessMode }} + {{- end }} + resources: + requests: + storage: {{ .Values.deployment.volumes.pvc.size }} +{{- end }} +{{- end }} diff --git a/charts/microservice/templates/serviceaccount.yaml b/charts/microservice/templates/serviceaccount.yaml new file mode 100644 index 00000000..8ad50af3 --- /dev/null +++ b/charts/microservice/templates/serviceaccount.yaml @@ -0,0 +1,13 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "microservice.serviceAccountName" . }}-sa + labels: + {{- include "microservice.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +automountServiceAccountToken: {{ .Values.serviceAccount.automount }} +{{- end }} diff --git a/charts/microservice/values.yaml b/charts/microservice/values.yaml index fd10418f..e12a2556 100644 --- a/charts/microservice/values.yaml +++ b/charts/microservice/values.yaml @@ -1,9 +1,10 @@ # -- global variables global: - namespace: "" + namespace: "default" replicaCount: 1 nameOverride: "ashwani" fullnameOverride: "ashwani" + imagePullSecrets: [] environment: {} # list of key: value # GLOBAL1: value @@ -63,49 +64,45 @@ deployment: # Additional volumes on the output Deployment definition. volumes: - enabled: true - pvc: - enabled: false - existing_claim: - name: pvc - mountPath: /pv - size: 1G - class: - accessModes: - - ReadWriteOnce + enabled: false + # pvc: + # enabled: false + # existing_claim: false + # name: pvc + # mountPath: /pv + # size: 1G + # class: "default" + # accessModes: + # - ReadWriteOnce # configFileCommonHeader: | # line1 # line2 - configMaps: - - name: test - mountPath: /test - data: - test.conf: | - hello - # hello2 - - # - name: test-from-file - # mountPath: /test2 - # files: - # - source: config.conf - # destination: application.conf - # - name: test-mixed - # mountPath: /test3 - # data: - # test2.conf: | - # another hello - # files: - # - source: config.conf - # destination: application2.conf - - # - name: foo - # secret: - # secretName: mysecret - # optional: false + # configMaps: + # - name: test + # mountPath: /test + # data: + # test.conf: | + # hello + # hello2 + + # - name: test-from-file + # mountPath: /test2 + # files: + # - source: config.conf + # destination: application.conf + # - name: test-mixed + # mountPath: /test3 + # data: + # test2.conf: | + # another hello + # files: + # - source: config.conf + # destination: application2.conf # Additional volumeMounts on the output Deployment definition. + volumeMounts: [] # - name: foo # mountPath: "/etc/foo" @@ -130,3 +127,13 @@ service: specs: - port: 80 name: http + +serviceAccount: + create: true + # Automatically mount a ServiceAccount's API credentials? + automount: true + # Annotations to add to the service account + annotations: {} + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: "" \ No newline at end of file From 688f9a55d49165ce09924e2c24f0de20cf971f0d Mon Sep 17 00:00:00 2001 From: tripathishikha1 <156056746+tripathishikha1@users.noreply.github.com> Date: Mon, 8 Jul 2024 19:08:20 +0530 Subject: [PATCH 04/15] Update deploy-nginx.yaml Signed-off-by: Ashwani Singh --- .../microservice/examples/deploy-nginx.yaml | 72 ++++++++++++++++++- 1 file changed, 69 insertions(+), 3 deletions(-) diff --git a/charts/microservice/examples/deploy-nginx.yaml b/charts/microservice/examples/deploy-nginx.yaml index 03e4b923..e5e0c0cd 100644 --- a/charts/microservice/examples/deploy-nginx.yaml +++ b/charts/microservice/examples/deploy-nginx.yaml @@ -1,5 +1,71 @@ deployment: image: - name: "nginx" - tag: "latest" - pullPolicy: IfNotPresent \ No newline at end of file + name: nginx + tag: latest + pullPolicy: IfNotPresent + + livenessProbe: + httpGet: + path: / + port: http + initialDelaySeconds: 25 + periodSeconds: 10 + timeoutSeconds: 5 + successThreshold: 1 + failureThreshold: 5 + + readinessProbe: + httpGet: + path: / + port: http + initialDelaySeconds: 25 + periodSeconds: 10 + timeoutSeconds: 5 + successThreshold: 1 + failureThreshold: 5 + + resources: + requests: + memory: 100Mi + cpu: 100m + limits: + memory: 100Mi + cpu: 100m + + pvc: + enabled: false # Adjust based on your requirements + existing_claim: false # Adjust based on your requirements + name: pvc # Adjust based on your requirements + mountPath: /pv # Adjust based on your requirements + size: 1Gi # Adjust based on your requirements + class: default # Adjust based on your requirements + accessModes: + - ReadWriteOnce + + configMaps: + - name: test + mountPath: /test + data: + test.conf: | + hello + hello2 + + - name: test-from-file + mountPath: /test2 + files: + - source: config.conf + destination: application.conf + + - name: test-mixed + mountPath: /test3 + data: + test2.conf: | + another hello + files: + - source: config.conf + destination: application2.conf + + volumeMounts: + - name: foo + mountPath: /etc/foo + readOnly: true From 4434b007fd6aeff0fe69ee919d6cffa1e79ae250 Mon Sep 17 00:00:00 2001 From: tripathishikha1 <156056746+tripathishikha1@users.noreply.github.com> Date: Mon, 8 Jul 2024 19:15:01 +0530 Subject: [PATCH 05/15] Update deploy-nginx.yaml Signed-off-by: Ashwani Singh --- .../microservice/examples/deploy-nginx.yaml | 66 +------------------ 1 file changed, 2 insertions(+), 64 deletions(-) diff --git a/charts/microservice/examples/deploy-nginx.yaml b/charts/microservice/examples/deploy-nginx.yaml index e5e0c0cd..e4cd49b8 100644 --- a/charts/microservice/examples/deploy-nginx.yaml +++ b/charts/microservice/examples/deploy-nginx.yaml @@ -4,68 +4,6 @@ deployment: tag: latest pullPolicy: IfNotPresent - livenessProbe: - httpGet: - path: / - port: http - initialDelaySeconds: 25 - periodSeconds: 10 - timeoutSeconds: 5 - successThreshold: 1 - failureThreshold: 5 + - readinessProbe: - httpGet: - path: / - port: http - initialDelaySeconds: 25 - periodSeconds: 10 - timeoutSeconds: 5 - successThreshold: 1 - failureThreshold: 5 - - resources: - requests: - memory: 100Mi - cpu: 100m - limits: - memory: 100Mi - cpu: 100m - - pvc: - enabled: false # Adjust based on your requirements - existing_claim: false # Adjust based on your requirements - name: pvc # Adjust based on your requirements - mountPath: /pv # Adjust based on your requirements - size: 1Gi # Adjust based on your requirements - class: default # Adjust based on your requirements - accessModes: - - ReadWriteOnce - - configMaps: - - name: test - mountPath: /test - data: - test.conf: | - hello - hello2 - - - name: test-from-file - mountPath: /test2 - files: - - source: config.conf - destination: application.conf - - - name: test-mixed - mountPath: /test3 - data: - test2.conf: | - another hello - files: - - source: config.conf - destination: application2.conf - - volumeMounts: - - name: foo - mountPath: /etc/foo - readOnly: true + From 0b4593f389ea3c34a881a846c727c626e6ddda76 Mon Sep 17 00:00:00 2001 From: Ashwani Singh Date: Mon, 8 Jul 2024 22:57:57 +0530 Subject: [PATCH 06/15] create notes Signed-off-by: Ashwani Singh --- charts/microservice/templates/NOTES.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/charts/microservice/templates/NOTES.txt b/charts/microservice/templates/NOTES.txt index c12295b8..b80c1dea 100644 --- a/charts/microservice/templates/NOTES.txt +++ b/charts/microservice/templates/NOTES.txt @@ -1,5 +1,5 @@ -You have deployed the following release: {{ .Release.Name }}. +You have deployed the following release: {{ include "microservice.fullname" . }}. To get further information, you can run the commands: - $ helm status {{ .Release.Name }} - $ helm get all {{ .Release.Name }} + $ helm status {{ include "microservice.fullname" . }} + $ helm get all {{ include "microservice.fullname" . }} From 7e2f84d8da475b11c2916e84f47376e1a86990e2 Mon Sep 17 00:00:00 2001 From: Ashwani Singh Date: Tue, 9 Jul 2024 01:14:15 +0530 Subject: [PATCH 07/15] example Signed-off-by: Ashwani Singh --- charts/microservice/examples/config.conf | 2 + .../microservice/examples/deploy-nginx.yaml | 31 +++++++-- .../microservice/templates/_capabilities.tpl | 11 +++ charts/microservice/templates/configmap.yaml | 2 +- charts/microservice/templates/hpa.yaml | 39 +++++++---- charts/microservice/values.yaml | 69 +++++++++---------- 6 files changed, 101 insertions(+), 53 deletions(-) create mode 100644 charts/microservice/examples/config.conf diff --git a/charts/microservice/examples/config.conf b/charts/microservice/examples/config.conf new file mode 100644 index 00000000..8fa9e87f --- /dev/null +++ b/charts/microservice/examples/config.conf @@ -0,0 +1,2 @@ +name=opstree +address=opstreesolution \ No newline at end of file diff --git a/charts/microservice/examples/deploy-nginx.yaml b/charts/microservice/examples/deploy-nginx.yaml index e4cd49b8..3245c113 100644 --- a/charts/microservice/examples/deploy-nginx.yaml +++ b/charts/microservice/examples/deploy-nginx.yaml @@ -1,9 +1,32 @@ +global: + namespace: "nginx" + fullnameOverride: "webapp" + deployment: image: name: nginx tag: latest pullPolicy: IfNotPresent - - - - + livenessProbe: + httpGet: + path: "/" + port: http + readinessProbe: + httpGet: + path: "/" + port: http + resources: + requests: + memory: 100Mi + cpu: 100m + limits: + memory: 500Mi + cpu: 500m + volumes: + enabled: true + configMaps: + - name: webapp + mountPath: /webapp + data: + webapp.conf: | + name="opstree" \ No newline at end of file diff --git a/charts/microservice/templates/_capabilities.tpl b/charts/microservice/templates/_capabilities.tpl index 5c7814bd..1998e762 100644 --- a/charts/microservice/templates/_capabilities.tpl +++ b/charts/microservice/templates/_capabilities.tpl @@ -23,3 +23,14 @@ Return the appropriate apiVersion for Horizontal Pod Autoscaler. {{- end -}} {{- end -}} +{{/* +Return the appropriate apiVersion for deployment. +*/}} +{{- define "microservice.capabilities.deployment.apiVersion" -}} +{{- $kubeVersion := include "microservice.capabilities.kubeVersion" . -}} +{{- if and (not (empty $kubeVersion)) (semverCompare "<1.14-0" $kubeVersion) -}} +{{- print "extensions/v1beta1" -}} +{{- else -}} +{{- print "apps/v1" -}} +{{- end -}} +{{- end -}} diff --git a/charts/microservice/templates/configmap.yaml b/charts/microservice/templates/configmap.yaml index 8e73d584..0bc15025 100644 --- a/charts/microservice/templates/configmap.yaml +++ b/charts/microservice/templates/configmap.yaml @@ -22,7 +22,7 @@ data: {{- range $file := $cm.files }} {{ $file.destination }}: | {{ $header | toString | indent 4 }} - {{ $root.Files.Get $file.source }} + {{ $root.Files.Get "$file.source" }} {{- end}} {{- end }} {{- end }} diff --git a/charts/microservice/templates/hpa.yaml b/charts/microservice/templates/hpa.yaml index b2a4e86d..a0a5f805 100644 --- a/charts/microservice/templates/hpa.yaml +++ b/charts/microservice/templates/hpa.yaml @@ -8,21 +8,34 @@ metadata: {{- include "microservice.labels" . | nindent 4 }} spec: scaleTargetRef: + apiVersion: {{ include "microservice.capabilities.deployment.apiVersion" . }} kind: Deployment - name: {{ include "microservice.fullname" . }} + name: {{ include "microservice.fullname" . }}-app minReplicas: {{ .Values.hpa.minReplicas }} maxReplicas: {{ .Values.hpa.maxReplicas }} metrics: - {{- if .Values.hpa.targetCPUUtilizationPercentage }} - - type: Resource - resource: - name: cpu - targetAverageUtilization: {{ .Values.hpa.targetCPUUtilizationPercentage }} - {{- end }} - {{- if .Values.hpa.targetMemoryUtilizationPercentage }} - - type: Resource - resource: - name: memory - targetAverageUtilization: {{ .Values.hpa.targetMemoryUtilizationPercentage }} - {{- end }} + {{- if .Values.hpa.targetMemory }} + - type: Resource + resource: + name: memory + {{- if semverCompare "<1.23-0" (include "microservice.capabilities.kubeVersion" .) }} + targetAverageUtilization: {{ .Values.hpa.targetMemory }} + {{- else }} + target: + type: Utilization + averageUtilization: {{ .Values.hpa.targetMemory }} + {{- end }} + {{- end }} + {{- if .Values.hpa.targetCPU }} + - type: Resource + resource: + name: cpu + {{- if semverCompare "<1.23-0" (include "microservice.capabilities.kubeVersion" .) }} + targetAverageUtilization: {{ .Values.hpa.targetCPU }} + {{- else }} + target: + type: Utilization + averageUtilization: {{ .Values.hpa.targetCPU }} + {{- end }} + {{- end }} {{- end }} diff --git a/charts/microservice/values.yaml b/charts/microservice/values.yaml index e12a2556..f13cad1b 100644 --- a/charts/microservice/values.yaml +++ b/charts/microservice/values.yaml @@ -2,8 +2,8 @@ global: namespace: "default" replicaCount: 1 - nameOverride: "ashwani" - fullnameOverride: "ashwani" + nameOverride: "" + fullnameOverride: "" imagePullSecrets: [] environment: {} # list of key: value @@ -24,47 +24,46 @@ deployment: # Annotation for the Deployment annotations: {} - livenessProbe: {} - # livenessProbe: - # httpGet: - # path: "/" - # port: http - # initialDelaySeconds: 25 - # periodSeconds: 10 - # timeoutSeconds: 5 - # successThreshold: 1 - # failureThreshold: 5 - - readinessProbe: {} - # readinessProbe: - # httpGet: - # path: "/" - # port: http - # initialDelaySeconds: 25 - # periodSeconds: 10 - # timeoutSeconds: 5 - # successThreshold: 1 - # failureThreshold: 5 - - environment: - ashwani: singh - - # environment: {} + # livenessProbe: {} + livenessProbe: + # httpGet: + # path: "/" + # port: http + initialDelaySeconds: 250 + periodSeconds: 10 + timeoutSeconds: 5 + successThreshold: 1 + failureThreshold: 5 + + # readinessProbe: {} + readinessProbe: + # httpGet: + # path: "/" + # port: http + initialDelaySeconds: 30 + periodSeconds: 10 + timeoutSeconds: 5 + successThreshold: 1 + failureThreshold: 5 + + environment: {} # VAR1: value1 resources: {} # resources: # requests: - # memory: 100m + # memory: 100Mi # cpu: 100m # limits: - # memory: 100m + # memory: 100Mi # cpu: 100m # Additional volumes on the output Deployment definition. volumes: - enabled: false + enabled: true + pvc: + enabled: false # pvc: # enabled: false # existing_claim: false @@ -79,7 +78,7 @@ deployment: # line1 # line2 - # configMaps: + configMaps: # - name: test # mountPath: /test # data: @@ -118,8 +117,8 @@ hpa: enabled: true minReplicas: 1 maxReplicas: 1 - targetCPUUtilizationPercentage: 80 - targetMemoryUtilizationPercentage: 80 + targetCPU: 80 + targetMemory: 80 service: type: ClusterIP @@ -129,7 +128,7 @@ service: name: http serviceAccount: - create: true + create: false # Automatically mount a ServiceAccount's API credentials? automount: true # Annotations to add to the service account From d4a1e8f53f1c9dcd7f0c9b3f94d5029942943a8a Mon Sep 17 00:00:00 2001 From: Ashwani Singh Date: Tue, 9 Jul 2024 09:51:19 +0530 Subject: [PATCH 08/15] validate k8s api version Signed-off-by: Ashwani Singh --- charts/microservice/Chart.yaml | 23 +++---------------- .../microservice/examples/deploy-nginx.yaml | 9 ++++---- charts/microservice/templates/configmap.yaml | 2 +- charts/microservice/templates/deployment.yaml | 10 ++++++-- charts/microservice/templates/hpa.yaml | 2 +- charts/microservice/templates/pvc.yaml | 2 +- charts/microservice/templates/service.yaml | 2 +- .../templates/serviceaccount.yaml | 1 + charts/microservice/values.yaml | 21 +++++++++-------- 9 files changed, 32 insertions(+), 40 deletions(-) diff --git a/charts/microservice/Chart.yaml b/charts/microservice/Chart.yaml index e6410f85..cf6e966c 100644 --- a/charts/microservice/Chart.yaml +++ b/charts/microservice/Chart.yaml @@ -1,27 +1,10 @@ apiVersion: v2 name: microservice description: A Helm chart for a microservice in Kubernetes - -# A chart can be either an 'application' or a 'library' chart -# -# Application chart deploys a complete application, includes all necessary Kubernetes resources, -# used for installing and managing applications. -# -# Library chart provides reusable templates and components, does not deploy an application by itself, -# used to promote consistency and reuse across multiple charts. type: application - -# This is the chart version. This chart version should be incremented with each change to the chart -# or its templates, including the app version. -# Follow Semantic Versioning (https://semver.org/). version: 0.1.2 - -# This is the application version number, which should be incremented with each change to the application. -# It doesn't need to follow Semantic Versioning but should reflect the application's version. -# Quotes are recommended. appVersion: "0.1.2" - -# This is maintainers files. this field lists the individuals or teams responsible for maintaining the Helm chart. -# It typically includes their names and contact information, such as email addresses. maintainers: - - name: Opstree Solutions + - name: Ashwani Singh + email: ashwani.singh@opstree.com + - name: Shikha Tripathi diff --git a/charts/microservice/examples/deploy-nginx.yaml b/charts/microservice/examples/deploy-nginx.yaml index 3245c113..d0b81b8e 100644 --- a/charts/microservice/examples/deploy-nginx.yaml +++ b/charts/microservice/examples/deploy-nginx.yaml @@ -25,8 +25,9 @@ deployment: volumes: enabled: true configMaps: - - name: webapp - mountPath: /webapp + - name: index + mountPath: /usr/share/nginx/html data: - webapp.conf: | - name="opstree" \ No newline at end of file + index.html: | + Hello! Opstree + \ No newline at end of file diff --git a/charts/microservice/templates/configmap.yaml b/charts/microservice/templates/configmap.yaml index 0bc15025..e420ac01 100644 --- a/charts/microservice/templates/configmap.yaml +++ b/charts/microservice/templates/configmap.yaml @@ -9,7 +9,7 @@ apiVersion: v1 kind: ConfigMap metadata: name: {{ include "microservice.fullname" $root }}-{{ $cm.name }}-cm - namespace: {{ $root.Values.global.namespace }} + namespace: {{ $root.Values.global.namespace | quote }} data: {{- if $cm.data }} {{- range $filename, $content := $cm.data }} diff --git a/charts/microservice/templates/deployment.yaml b/charts/microservice/templates/deployment.yaml index 67da2a1c..9bf469a8 100644 --- a/charts/microservice/templates/deployment.yaml +++ b/charts/microservice/templates/deployment.yaml @@ -1,10 +1,10 @@ {{ $root := . }} --- -apiVersion: apps/v1 +apiVersion: {{ include "microservice.capabilities.deployment.apiVersion" . }} kind: Deployment metadata: name: {{ include "microservice.fullname" . }}-app - namespace: {{ .Values.global.namespace }} + namespace: {{ .Values.global.namespace | quote }} {{- if .Values.annotations }} annotations: {{- range $key, $value := .Values.annotations }} @@ -32,6 +32,12 @@ spec: - name: {{ include "microservice.fullname" . }} image: "{{ .Values.deployment.image.name }}:{{ .Values.deployment.image.tag }}" imagePullPolicy: {{ .Values.deployment.image.pullPolicy }} + {{- if .Values.deployment.command }} + command: {{ .Values.deployment.command }} + {{- end }} + {{- if .Values.deployment.args }} + args: {{ .Values.deployment.args }} + {{- end }} ports: {{- range .Values.service.specs}} - name: {{ .name }} diff --git a/charts/microservice/templates/hpa.yaml b/charts/microservice/templates/hpa.yaml index a0a5f805..02db2f41 100644 --- a/charts/microservice/templates/hpa.yaml +++ b/charts/microservice/templates/hpa.yaml @@ -3,7 +3,7 @@ apiVersion: {{ include "microservice.capabilities.hpa.apiVersion" ( dict "contex kind: HorizontalPodAutoscaler metadata: name: {{ include "microservice.fullname" . }}-hpa - namespace: {{ .Values.global.namespace }} + namespace: {{ .Values.global.namespace | quote }} labels: {{- include "microservice.labels" . | nindent 4 }} spec: diff --git a/charts/microservice/templates/pvc.yaml b/charts/microservice/templates/pvc.yaml index 53a36951..ce149caa 100644 --- a/charts/microservice/templates/pvc.yaml +++ b/charts/microservice/templates/pvc.yaml @@ -5,7 +5,7 @@ apiVersion: v1 kind: PersistentVolumeClaim metadata: name: {{ .Values.deployment.volumes.pvc.name }} - namespace: {{ .Values.global.namespace }} + namespace: {{ .Values.global.namespace | quote }} spec: {{- if .Values.deployment.volumes.pvc.class }} storageClassName: {{ .Values.deployment.volumes.pvc.class }} diff --git a/charts/microservice/templates/service.yaml b/charts/microservice/templates/service.yaml index 8d052686..08d9749f 100644 --- a/charts/microservice/templates/service.yaml +++ b/charts/microservice/templates/service.yaml @@ -4,7 +4,7 @@ apiVersion: v1 kind: Service metadata: name: {{ include "microservice.fullname" . }}-svc - namespace: {{ .Values.global.namespace }} + namespace: {{ .Values.global.namespace | quote }} {{- if .Values.service.annotations }} annotations: {{- range $key, $value := .Values.service.annotations }} diff --git a/charts/microservice/templates/serviceaccount.yaml b/charts/microservice/templates/serviceaccount.yaml index 8ad50af3..d69c1171 100644 --- a/charts/microservice/templates/serviceaccount.yaml +++ b/charts/microservice/templates/serviceaccount.yaml @@ -3,6 +3,7 @@ apiVersion: v1 kind: ServiceAccount metadata: name: {{ include "microservice.serviceAccountName" . }}-sa + namespace: {{ .Values.global.namespace | quote }} labels: {{- include "microservice.labels" . | nindent 4 }} {{- with .Values.serviceAccount.annotations }} diff --git a/charts/microservice/values.yaml b/charts/microservice/values.yaml index f13cad1b..4dec45b8 100644 --- a/charts/microservice/values.yaml +++ b/charts/microservice/values.yaml @@ -46,6 +46,9 @@ deployment: successThreshold: 1 failureThreshold: 5 + # command: ["/bin/sh","-c"] + # args: ["echo 'consuming a message'; sleep 5"] + environment: {} # VAR1: value1 @@ -62,17 +65,15 @@ deployment: # Additional volumes on the output Deployment definition. volumes: enabled: true - pvc: + pvc: enabled: false - # pvc: - # enabled: false - # existing_claim: false - # name: pvc - # mountPath: /pv - # size: 1G - # class: "default" - # accessModes: - # - ReadWriteOnce + existing_claim: false + name: pvc + mountPath: /pv + size: 1G + class: "default" + accessModes: + - ReadWriteOnce # configFileCommonHeader: | # line1 From d6045e87163849cd007254d4fa2c6a92aa397359 Mon Sep 17 00:00:00 2001 From: Ashwani Singh Date: Tue, 9 Jul 2024 10:22:55 +0530 Subject: [PATCH 09/15] Generate helm docs Signed-off-by: Ashwani Singh --- charts/microservice/Chart.yaml | 4 +- charts/microservice/README.md | 68 +++++++++++++++------------- charts/microservice/README.md.gotmpl | 22 +++++++++ 3 files changed, 61 insertions(+), 33 deletions(-) create mode 100644 charts/microservice/README.md.gotmpl diff --git a/charts/microservice/Chart.yaml b/charts/microservice/Chart.yaml index cf6e966c..381b6b8d 100644 --- a/charts/microservice/Chart.yaml +++ b/charts/microservice/Chart.yaml @@ -1,8 +1,8 @@ apiVersion: v2 name: microservice -description: A Helm chart for a microservice in Kubernetes +description: Basic helm chart for deploying microservices on kubernetes with best practices type: application -version: 0.1.2 +version: 0.1.3 appVersion: "0.1.2" maintainers: - name: Ashwani Singh diff --git a/charts/microservice/README.md b/charts/microservice/README.md index 4bb7b668..d078e33f 100644 --- a/charts/microservice/README.md +++ b/charts/microservice/README.md @@ -1,39 +1,45 @@ -# Basic Helm Template for Deploying Microservice on K8s +# microservice -## Chart Structure -``` -microservice/ -├── Chart.yaml -├── values.yaml -├── charts/ -└── templates/ - ├── deployment.yaml - └── service.yaml -``` +Basic helm chart for deploying microservices on kubernetes with best practices -## Usage +![Version: 0.1.2](https://img.shields.io/badge/Version-0.1.2-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 0.1.2](https://img.shields.io/badge/AppVersion-0.1.2-informational?style=flat-square) -You can change the Docker image by modifying the values.yaml file. For example, to use a different image, update the `image.repository` and `image.tag` fields. +## Maintainers -The Helm chart is specifically for deploying a microservice with a Kubernetes service and deployment. You can further customize and expand this chart as needed for your application. +| Name | Email | Url | +| ---- | ------ | --- | +| Ashwani Singh | | | +| Shikha Tripathi | | | -## Installing the Chart on K8s +## Installing the Chart -```bash -helm install my-release microservice/ +To install the chart with the release name `my-release`: + +```console +$ helm install my-release microservice/ ``` -## TODO - -- [x] Application Health Probes -- [ ] Service -- [x] Horizontal Pod Autoscaler -- [ ] ConfigMap -- [ ] RBAC - - [ ] Service Account - - [ ] Role Binding - - [ ] Role -- [ ] Rolling Deployment Manipulation -- [ ] Environment Parmaters -- [ ] Container Port Mapping -- [ ] Application Secrets \ No newline at end of file +## Values + +| Key | Type | Default | Description | +|-----|------|---------|-------------| +| deployment | object | `{"affinity":{},"annotations":{},"environment":{},"image":{"name":"","pullPolicy":"IfNotPresent","tag":""},"livenessProbe":{"failureThreshold":5,"initialDelaySeconds":250,"periodSeconds":10,"successThreshold":1,"timeoutSeconds":5},"nodeSelector":{},"readinessProbe":{"failureThreshold":5,"initialDelaySeconds":30,"periodSeconds":10,"successThreshold":1,"timeoutSeconds":5},"resources":{},"tolerations":[],"volumeMounts":[],"volumes":{"configMaps":null,"enabled":true,"pvc":{"accessModes":["ReadWriteOnce"],"class":"default","enabled":false,"existing_claim":false,"mountPath":"/pv","name":"pvc","size":"1G"}}}` | Object that configures Deployment instance | +| deployment.image | object | `{"name":"","pullPolicy":"IfNotPresent","tag":""}` | Override default container image format | +| global | object | `{"environment":{},"fullnameOverride":"","imagePullSecrets":[],"nameOverride":"","namespace":"default","replicaCount":1}` | global variables | +| hpa.enabled | bool | `true` | | +| hpa.maxReplicas | int | `1` | | +| hpa.minReplicas | int | `1` | | +| hpa.targetCPU | int | `80` | | +| hpa.targetMemory | int | `80` | | +| kubeVersion | string | `""` | | +| service.annotations | object | `{}` | | +| service.specs[0].name | string | `"http"` | | +| service.specs[0].port | int | `80` | | +| service.type | string | `"ClusterIP"` | | +| serviceAccount.annotations | object | `{}` | | +| serviceAccount.automount | bool | `true` | | +| serviceAccount.create | bool | `false` | | +| serviceAccount.name | string | `""` | | + +> **_NOTE:_** Please find the sample helm values yaml in example repository. + diff --git a/charts/microservice/README.md.gotmpl b/charts/microservice/README.md.gotmpl new file mode 100644 index 00000000..b1d9f5ad --- /dev/null +++ b/charts/microservice/README.md.gotmpl @@ -0,0 +1,22 @@ +{{ template "chart.header" . }} +{{ template "chart.description" . }} + +{{ template "chart.versionBadge" . }}{{ template "chart.typeBadge" . }}{{ template "chart.appVersionBadge" . }} + +{{ template "chart.maintainersSection" . }} + +## Installing the Chart + +To install the chart with the release name `my-release`: + +```console +$ helm install my-release microservice/ +``` + +{{/* {{ template "chart.requirementsSection" . }} */}} + +{{ template "chart.valuesSection" . }} + +> **_NOTE:_** Please find the sample helm values yaml in example repository. + +{{/* {{ template "helm-docs.versionFooter" . }} */}} \ No newline at end of file From d7b80fa6d255512970b0ed6779951ef645758f66 Mon Sep 17 00:00:00 2001 From: Ashwani Singh Date: Tue, 9 Jul 2024 11:49:20 +0530 Subject: [PATCH 10/15] Change namespace Signed-off-by: Ashwani Singh --- charts/microservice/examples/deploy-nginx.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/charts/microservice/examples/deploy-nginx.yaml b/charts/microservice/examples/deploy-nginx.yaml index d0b81b8e..e2efc3e4 100644 --- a/charts/microservice/examples/deploy-nginx.yaml +++ b/charts/microservice/examples/deploy-nginx.yaml @@ -1,5 +1,5 @@ global: - namespace: "nginx" + namespace: "default" fullnameOverride: "webapp" deployment: From 58be3fe1896de6fb0871de0f35f92491dabb60cd Mon Sep 17 00:00:00 2001 From: Ashwani Singh Date: Tue, 9 Jul 2024 12:02:53 +0530 Subject: [PATCH 11/15] demo-dev namespace Signed-off-by: Ashwani Singh --- charts/microservice/examples/deploy-nginx.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/charts/microservice/examples/deploy-nginx.yaml b/charts/microservice/examples/deploy-nginx.yaml index e2efc3e4..20311c45 100644 --- a/charts/microservice/examples/deploy-nginx.yaml +++ b/charts/microservice/examples/deploy-nginx.yaml @@ -1,5 +1,5 @@ global: - namespace: "default" + namespace: "demo-dev" fullnameOverride: "webapp" deployment: From da0962b89b6382a936503be3220954c81d19ef3b Mon Sep 17 00:00:00 2001 From: Ashwani Singh Date: Tue, 9 Jul 2024 12:13:35 +0530 Subject: [PATCH 12/15] remove trailing spaces Signed-off-by: Ashwani Singh --- charts/microservice/values.yaml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/charts/microservice/values.yaml b/charts/microservice/values.yaml index 4dec45b8..e3da4f73 100644 --- a/charts/microservice/values.yaml +++ b/charts/microservice/values.yaml @@ -1,4 +1,4 @@ -# -- global variables +# -- global variables global: namespace: "default" replicaCount: 1 @@ -25,7 +25,7 @@ deployment: annotations: {} # livenessProbe: {} - livenessProbe: + livenessProbe: # httpGet: # path: "/" # port: http @@ -48,7 +48,7 @@ deployment: # command: ["/bin/sh","-c"] # args: ["echo 'consuming a message'; sleep 5"] - + environment: {} # VAR1: value1 @@ -78,7 +78,7 @@ deployment: # configFileCommonHeader: | # line1 # line2 - + configMaps: # - name: test # mountPath: /test @@ -114,13 +114,13 @@ deployment: affinity: {} -hpa: +hpa: enabled: true minReplicas: 1 maxReplicas: 1 targetCPU: 80 targetMemory: 80 - + service: type: ClusterIP annotations: {} @@ -136,4 +136,4 @@ serviceAccount: annotations: {} # The name of the service account to use. # If not set and create is true, a name is generated using the fullname template - name: "" \ No newline at end of file + name: "" From 7b2e60128490996eeed5c58cb6aec0c8eca87721 Mon Sep 17 00:00:00 2001 From: Ashwani Singh Date: Tue, 9 Jul 2024 12:29:33 +0530 Subject: [PATCH 13/15] Fix readme Signed-off-by: Ashwani Singh --- charts/microservice/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/charts/microservice/README.md b/charts/microservice/README.md index d078e33f..8e38be25 100644 --- a/charts/microservice/README.md +++ b/charts/microservice/README.md @@ -16,7 +16,7 @@ Basic helm chart for deploying microservices on kubernetes with best practices To install the chart with the release name `my-release`: ```console -$ helm install my-release microservice/ +helm install my-release microservice/ ``` ## Values From 5593407ee6b7e1e81dc2cfdb52e471128f7699c8 Mon Sep 17 00:00:00 2001 From: Ashwani Singh Date: Tue, 9 Jul 2024 12:39:20 +0530 Subject: [PATCH 14/15] Update maintainers list Signed-off-by: Ashwani Singh --- charts/microservice/Chart.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/charts/microservice/Chart.yaml b/charts/microservice/Chart.yaml index 381b6b8d..e5627444 100644 --- a/charts/microservice/Chart.yaml +++ b/charts/microservice/Chart.yaml @@ -7,4 +7,7 @@ appVersion: "0.1.2" maintainers: - name: Ashwani Singh email: ashwani.singh@opstree.com + url: " " - name: Shikha Tripathi + email: "shikha@opstree.com" + url: " " \ No newline at end of file From 334cd43dd622913c627c7e0413f9e6dbfbfc65b2 Mon Sep 17 00:00:00 2001 From: Ashwani Singh Date: Tue, 9 Jul 2024 12:42:22 +0530 Subject: [PATCH 15/15] Fix maintainers list Signed-off-by: Ashwani Singh --- charts/microservice/Chart.yaml | 3 --- 1 file changed, 3 deletions(-) diff --git a/charts/microservice/Chart.yaml b/charts/microservice/Chart.yaml index e5627444..381b6b8d 100644 --- a/charts/microservice/Chart.yaml +++ b/charts/microservice/Chart.yaml @@ -7,7 +7,4 @@ appVersion: "0.1.2" maintainers: - name: Ashwani Singh email: ashwani.singh@opstree.com - url: " " - name: Shikha Tripathi - email: "shikha@opstree.com" - url: " " \ No newline at end of file