Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement application health check and hpa #209

Merged
merged 15 commits into from
Jul 9, 2024
9 changes: 5 additions & 4 deletions charts/microservice/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
---
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: Opstree Solutions
- name: Ashwani Singh
email: ashwani.singh@opstree.com
- name: Shikha Tripathi
66 changes: 36 additions & 30 deletions charts/microservice/README.md
Original file line number Diff line number Diff line change
@@ -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 | <ashwani.singh@opstree.com> | |
| Shikha Tripathi | | |

## Installing the Chart on K8s
## Installing the Chart

```bash
To install the chart with the release name `my-release`:

```console
helm install my-release microservice/
```

## TODO

- [ ] Application Health Probes
- [ ] Service
- [ ] Horizontal Pod Autoscaler
- [ ] ConfigMap
- [ ] RBAC
- [ ] Service Account
- [ ] Role Binding
- [ ] Role
- [ ] Rolling Deployment Manipulation
- [ ] Environment Parmaters
- [ ] Container Port Mapping
- [ ] Application Secrets
## 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.

22 changes: 22 additions & 0 deletions charts/microservice/README.md.gotmpl
Original file line number Diff line number Diff line change
@@ -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" . }} */}}
2 changes: 2 additions & 0 deletions charts/microservice/examples/config.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name=opstree
address=opstreesolution
33 changes: 33 additions & 0 deletions charts/microservice/examples/deploy-nginx.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
global:
namespace: "demo-dev"
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: index
mountPath: /usr/share/nginx/html
data:
index.html: |
Hello! Opstree

5 changes: 5 additions & 0 deletions charts/microservice/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
You have deployed the following release: {{ include "microservice.fullname" . }}.
To get further information, you can run the commands:
$ helm status {{ include "microservice.fullname" . }}
$ helm get all {{ include "microservice.fullname" . }}

36 changes: 36 additions & 0 deletions charts/microservice/templates/_capabilities.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{{/* 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 -}}

{{/*
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 -}}
60 changes: 27 additions & 33 deletions charts/microservice/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,50 @@ 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 .Chart.Name .Values.global.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 "app.tempname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 60 | 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 }}

{{- define "app.fullname" -}}
{{- printf "%s-%s" ( include "app.tempname" . ) "app" | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- define "microservice.fullname" -}}
{{- 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 "app.labels" -}}
app: {{ include "app.tempname" . }}
{{- define "microservice.labels" -}}
app: {{ include "microservice.fullname" . }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "app.selectorLabels" -}}
app: {{ include "app.tempname" . }}
{{- end }}%

{{/*
service name
*/}}
{{- define "app.servicename" -}}
{{- printf "%s-%s" ( include "app.tempname" . ) "svc" | trunc 63 | trimSuffix "-" }}
{{- end }}%
{{- define "microservice.selectorLabels" -}}
app: {{ include "microservice.fullname" . }}
{{- end }}

{{/*
configmap name
Create the name of the service account to use
*/}}
{{- define "app.configmapname" -}}
{{- printf "%s-%s" ( include "app.tempname" . ) "cm" | trunc 63 | trimSuffix "-" }}
{{- end }}%
{{- define "microservice.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "microservice.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
30 changes: 30 additions & 0 deletions charts/microservice/templates/configmap.yaml
Original file line number Diff line number Diff line change
@@ -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: {{ $root.Values.global.namespace | quote }}
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 }}
Loading
Loading