Skip to content

Commit

Permalink
Add generic service chart to deploy on k8s
Browse files Browse the repository at this point in the history
  • Loading branch information
minottic committed Apr 28, 2022
1 parent 80e4b3f commit 4dfb534
Show file tree
Hide file tree
Showing 12 changed files with 452 additions and 0 deletions.
23 changes: 23 additions & 0 deletions generic_service_chart/.helmignore
Original file line number Diff line number Diff line change
@@ -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/
24 changes: 24 additions & 0 deletions generic_service_chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: v2
name: generic-service-chart
description: A Helm chart for Kubernetes to deploy a generic service. Highly configurable, app specific values can be set as files or as values

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 1.0.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "1.16.0"
120 changes: 120 additions & 0 deletions generic_service_chart/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# Generic-service-chart

The idea is to have a chart which is generic enough to be applied to many simple applications. Custom values, like config maps, volumes, env vars and others can be set as values or from file

## TL;DR

```bash
$ helm repo add internal http://melanie.gitpages.psi.ch/templates
$ helm install my-release internal/generic-service-chart
```

## Introduction

This chart bootstraps a generic service deployment on a [Kubernetes](http://kubernetes.io) cluster using the [Helm](https://helm.sh) package manager.

## Installing the Chart

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

```bash
$ helm install my-release internal/generic-service-chart
```

The command deploys a generic service on the Kubernetes cluster in the default configuration. The [Parameters](#parameters) section lists the parameters that can be configured during installation.

> **Tip**: List all releases using `helm list`
## Uninstalling the Chart

To uninstall/delete the `my-release` deployment:

```bash
$ helm delete my-release
```

The command removes all the Kubernetes components associated with the chart and deletes the release.

## Parameters

The following table lists the configurable parameters of the chart and their default values.

### Common parameters

| Parameter | Description | Default |
|---------------------|----------------------------------------------------------------------|--------------------------------|
| `nameOverride` | String to partially override fullname | `nil` |
| `fullnameOverride` | String to fully override fullname | `nil` |

### generic-service-chart parameters

| Parameter | Description | Default |
|------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------|
| `image.repository` | Image name | `busybox` |
| `image.tag` | Image tag | `latest` |
| `image.pullPolicy` | Image pull policy | `Always` |
| `run.command` | Command to be executed by the container | `nil` |
| `run.args` | Arguments to pass the the container command | `nil` |
| `env` | Envaironment variables to use in the deployment. It follows the same sintax as environment variabels in k8s | `[]` |
| `volumes` | Define what volumes to use in the deployment. It follows the same syntax as volumes in k8s | `nil` |
| `volumeMounts` | Define what volumes to mount in the deployment. It follows the same syntax as volumesMounts in k8s | `nil` |
| `configMaps` | Dictionary of `configmapName-> {key:value,}` used to define configmaps. An example `{cm1: {k1:v1,k2:v2}, cm2: {k3:v3}}` | `{}` |
| `test` | Test to run when using `helm test`. It follows the same syntax as containers in k8s | `nil` (evaluated as a template) |
| `initialDelaySeconds` | Number of seconds after the container has started before liveness or readiness probes are initiated | `nil` |

### Statefulset parameters

| Parameter | Description | Default |
|-----------------------------|-------------------------------------------------------------------------------------------|--------------------------------|
| `replicaCount` | Number of nodes | `1` |

### Exposure parameters

| Parameter | Description | Default |
|--------------------------------------|-----------------------------------------------------------------------------------|--------------------------------|
| `service.type` | Kubernetes Service type | `ClusterIP` |
| `service.externalPort` | Service external port | `3000` |
| `service.internalPort` | Service internal port name | `80` |
| `ingress.enabled` | Enable ingress resource for Management console | `false` |
| `ingress.hosts[0].host` | Host | `nil` |
| `ingress.hosts[0].paths[0].path` | Path for the default host | `/` |
| `ingress.hosts[0].tls[0].secretName` | Name of existing secret contiaining the tls certificate | `nil` |
| `ingress.hosts[0].tls[0].hosts[0]` | Host on which to apply the tls encription | `nil` |

Specify each parameter using the `--set key=value[,key=value]` or `--set-file key=value[,key=value]` argument to `helm install`. For example,

```bash
$ helm install my-release \
--set configMaps.cm.key=value \
--set-file configMaps.cm.key1=path_to_file \
internal/generic-service-chart
```

The above command sets and creates a configmap, named cm, with two values: one with key `key` and value `value` and the second with key `key1` and value equal to the content of the file in `path_to_file`.

> **Tip**: You can use the default [values.yaml](values.yaml)
## Configuration and installation details

### Define helm tests

To set the test to run, define this command:
```bash
$ TESTCASE=`cat << EOF
containers:
- name: wget
image: busybox
command: ['wget']
args: ['{{ include "helm_chart.fullname" . }}:{{ .Values.service.externalPort }}']
EOF`
$ helm install my-release \
--set test=$TESTCASE
$ helm test my-release
```
### Scale horizontally
To horizontally scale this chart once it has been deployed, two options are available:
- Use the `kubectl scale` command.
- Upgrade the chart modifying the `replicaCount` parameter.
22 changes: 22 additions & 0 deletions generic_service_chart/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -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_chart.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_chart.fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "helm_chart.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_chart.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 }}
62 changes: 62 additions & 0 deletions generic_service_chart/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "helm_chart.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_chart.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 }}
{{- .Release.Name }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "helm_chart.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "helm_chart.labels" -}}
helm.sh/chart: {{ include "helm_chart.chart" . }}
{{ include "helm_chart.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "helm_chart.selectorLabels" -}}
app.kubernetes.io/name: {{ include "helm_chart.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "helm_chart.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "helm_chart.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
13 changes: 13 additions & 0 deletions generic_service_chart/templates/configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{- range $name, $values := $.Values.configMaps }}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ tpl $name $ }}
labels:
{{- include "helm_chart.labels" $ | nindent 4 }}
data:
{{- range $k, $v := $values }}
{{ $k }}: |
{{ tpl $v $ | indent 4}}
{{- end }}
{{- end }}
63 changes: 63 additions & 0 deletions generic_service_chart/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "helm_chart.fullname" . }}
labels:
{{- include "helm_chart.labels" . | nindent 4 }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
{{- include "helm_chart.selectorLabels" . | nindent 6 }}
template:
metadata:
annotations:
checksum/config: {{ .Values.configMaps | toJson | sha256sum }}
checksum/secrets: {{ .Values.secrets | toJson | sha256sum }}
labels:
{{- include "helm_chart.selectorLabels" . | nindent 8 }}
spec:
{{- with .Values.image.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.initContainers }}
initContainers:
{{- toYaml . | nindent 8 }}
{{- end }}
containers:
- name: {{ .Chart.Name }}
image: "{{ tpl .Values.image.repository $ }}:{{ tpl .Values.image.tag $ | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- containerPort: {{ .Values.service.internalPort }}
protocol: TCP
livenessProbe:
{{- with .Values.initialDelaySeconds }}
initialDelaySeconds: {{ . }}
{{- end }}
httpGet:
path: /
port: {{ .Values.service.internalPort }}
readinessProbe:
{{- with .Values.initialDelaySeconds }}
initialDelaySeconds: {{ . }}
{{- end }}
httpGet:
path: /
port: {{ .Values.service.internalPort }}
{{- with .Values.run }}
{{- tpl (toYaml .) $ | nindent 10 }}
{{- end }}
{{- with .Values.env }}
env:
{{- tpl (toYaml .) $ | nindent 12 }}
{{- end }}
{{- with .Values.volumeMounts }}
volumeMounts:
{{- tpl (toYaml .) $ | nindent 12 }}
{{- end }}
{{- with .Values.volumes}}
volumes:
{{- tpl (toYaml .) $ | nindent 8 }}
{{- end }}
61 changes: 61 additions & 0 deletions generic_service_chart/templates/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{{- if .Values.ingress.enabled -}}
{{- $fullName := include "helm_chart.fullname" . -}}
{{- $svcPort := .Values.service.externalPort -}}
{{- 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 }}
{{- with .Values.ingress.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
labels:
{{- include "helm_chart.labels" . | nindent 4 }}
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 }}7
{{- end }}
{{- end }}
{{- end }}
{{- end }}
Loading

0 comments on commit 4dfb534

Please sign in to comment.