Skip to content

Commit

Permalink
Implement application health check and hpa
Browse files Browse the repository at this point in the history
Signed-off-by: Ashwani Singh <ashwani.singh@opstree.com>
  • Loading branch information
ashwani-opstree committed Jul 5, 2024
1 parent 1a5113d commit b962755
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 38 deletions.
20 changes: 19 additions & 1 deletion charts/microservice/Chart.yaml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions charts/microservice/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 23 additions & 16 deletions charts/microservice/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ 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 }}

{{/*
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 }}
Expand All @@ -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 }}%
40 changes: 32 additions & 8 deletions charts/microservice/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
27 changes: 27 additions & 0 deletions charts/microservice/templates/hpa.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
6 changes: 3 additions & 3 deletions charts/microservice/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -12,4 +12,4 @@ spec:
protocol: TCP
name: http
selector:
{{- include "app.selectorLabels" . | nindent 4 }}
{{- include "microservice.selectorLabels" . | nindent 4 }}
46 changes: 38 additions & 8 deletions charts/microservice/values.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b962755

Please sign in to comment.