Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
249 changes: 249 additions & 0 deletions k8s/HELM.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@

# Lab 10 — Helm Package Manager

![difficulty](https://img.shields.io/badge/difficulty-intermediate-yellow)
![topic](https://img.shields.io/badge/topic-Helm-blue)
![points](https://img.shields.io/badge/points-12%2B2.5-orange)
![tech](https://img.shields.io/badge/tech-Helm-informational)

> Package your Kubernetes applications with Helm for reusable, configurable deployments across environments.

---

## Chart Overview

This Helm chart (`myapp`) converts Kubernetes manifests from Lab 9 into a reusable, configurable deployment system.

### Structure

```

myapp/
├── Chart.yaml
├── values.yaml
├── values-dev.yaml
├── values-prod.yaml
└── templates/
├── deployment.yaml
├── service.yaml
└── hooks/
├── pre-install-job.yaml
└── post-install-job.yaml

````

### Key Components

- **Deployment template** — runs the Python application with configurable replicas, image, and resources
- **Service template** — exposes the application inside cluster (NodePort / ClusterIP)
- **Values files** — environment-based configuration (dev/prod)
- **Hooks** — lifecycle jobs executed before and after deployment

---

## Configuration Guide

### Main values (`values.yaml`)

- `replicaCount` — number of pods
- `image.repository` — Docker image name
- `image.tag` — image version
- `service.port` — exposed port
- `service.targetPort` — container port (5000)
- `resources` — CPU/memory limits
- `livenessProbe` — health check configuration
- `readinessProbe` — readiness check configuration

### Dev environment (`values-dev.yaml`)

- 1 replica
- lightweight CPU/memory limits
- NodePort service
- relaxed probes

### Prod environment (`values-prod.yaml`)

- 3–5 replicas
- higher resource limits
- LoadBalancer-ready configuration
- stricter probes

### Example usage

```bash
helm install myapp-dev ./myapp -f values-dev.yaml
helm install myapp-prod ./myapp -f values-prod.yaml
````

---

## Hook Implementation

### Pre-install hook

Executes before deployment starts.

* Used for initialization / validation
* Runs a Kubernetes Job

### Post-install hook

Executes after deployment completes.

* Used for smoke tests or verification
* Confirms application readiness

### Hook execution order

```
Pre-install → Deploy resources → Post-install
```

### Hook weights

* `-5` → pre-install (runs first)
* `5` → post-install (runs last)

### Deletion policy

Hooks are configured with:

```
hook-delete-policy: hook-succeeded
```

This ensures:

* cleanup after successful execution
* no leftover Jobs in cluster

---

## Installation Evidence

### Helm release list

```bash
helm list
```

📸 Screenshot: `lab10-set.png`

---

### Kubernetes resources

```bash
kubectl get all
```

📸 Screenshot: `lab10-set.png`

---

### Application running

```bash
kubectl get pods
kubectl get svc
```

📸 Screenshot: `lab10-app.png`

---

### Hooks execution

```bash
kubectl get jobs
kubectl logs job/<pre-install-job>
kubectl logs job/<post-install-job>
```

📸 Screenshot: `lab10-hooks.png`

---

## Operations

### Install

```bash
helm install myapp-release ./myapp
```

### Upgrade

```bash
helm upgrade myapp-release ./myapp
```

### Rollback

```bash
helm rollback myapp-release 1
```

### Uninstall

```bash
helm uninstall myapp-release
```

---

## Testing & Validation

### Chart validation

```bash
helm lint ./myapp
```

### Template rendering

```bash
helm template myapp ./myapp
```

### Dry-run install

```bash
helm install test ./myapp --dry-run --debug
```

### Result

* Deployment successfully created
* Service exposed via NodePort
* Application accessible in browser via `http://localhost:8080`

---

## Summary

This Helm chart demonstrates:

* templating Kubernetes manifests
* environment-based configuration
* lifecycle hooks (pre/post install)
* production-ready structure
* reusable deployment patterns

Helm significantly improves maintainability, scalability, and repeatability of Kubernetes deployments.

---

## Screenshots

### Cluster setup & Helm state

![lab10-set](lab10-set.png)

### Application running

![lab10-app](lab10-app.png)

### Hooks execution

![lab10-hooks](lab10-hooks.png)
23 changes: 23 additions & 0 deletions k8s/myapp/.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 k8s/myapp/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: v2
name: myapp
description: A Helm chart for Kubernetes

# 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: 0.1.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"
62 changes: 62 additions & 0 deletions k8s/myapp/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "myapp.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 "myapp.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

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

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

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

{{/*
Create the name of the service account to use
*/}}
{{- define "myapp.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "myapp.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
33 changes: 33 additions & 0 deletions k8s/myapp/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "myapp.fullname" . }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: {{ include "myapp.name" . }}
template:
metadata:
labels:
app: {{ include "myapp.name" . }}
spec:
containers:
- name: app
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
ports:
- containerPort: {{ .Values.service.targetPort }}
resources:
{{- toYaml .Values.resources | nindent 12 }}
livenessProbe:
httpGet:
path: {{ .Values.livenessProbe.path }}
port: {{ .Values.service.targetPort }}
initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
readinessProbe:
httpGet:
path: {{ .Values.readinessProbe.path }}
port: {{ .Values.service.targetPort }}
initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
Loading
Loading