Skip to content

Commit

Permalink
feat: add helm-chart-hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
seifrajhi committed Feb 10, 2024
1 parent e235542 commit 10caf27
Show file tree
Hide file tree
Showing 21 changed files with 660 additions and 3 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ GitOps is an operating model for Kubernetes and other cloud native technologies.
- [Elastic Cloud on Kubernetes](https://github.com/elastic/cloud-on-k8s): Elastic Cloud on Kubernetes automates the deployment, provisioning, management, and orchestration of Elasticsearch, Kibana, APM Server, Enterprise Search, Beats, Elastic Agent, Elastic Maps Server, and Logstash on Kubernetes based on the operator pattern.
=======
## CNCF certifications:
- [Kubernetes CKS Full Course](https://www.youtube.com/watch?v=d9xfB5qaOfg) Theory + Practice + Browser Scenarios by Kim Wuestkamp
Expand All @@ -924,7 +924,6 @@ GitOps is an operating model for Kubernetes and other cloud native technologies.
- [CKA Exercises](https://github.com/chadmcrowell/CKA-Exercises): Practice for the Certified Kubernetes Administrator (CKA) Exam.
## Kubernetes IAC:
Certainly! Here's a list of some popular tools for managing Kubernetes Infrastructure as Code (IAC):
Expand Down Expand Up @@ -971,7 +970,6 @@ Certainly! Here's a list of some popular tools for managing Kubernetes Infrastru
This is not an exhaustive list, and the choice of tools depends on your specific use case and preferences. Always check the official documentation and community support for each tool for the most accurate and up-to-date information.
### Coming next
* Cluster Operation and maintanance
Expand Down
279 changes: 279 additions & 0 deletions helm-chart-hooks-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
# Example of How Helm Chart Hooks works

## Helm Chart Hooks?

1. Using hooks chart developers can either execute logic or create Kubernetes objects at a certain point in the release life cycle.
2. For e.g., Creating a secret pre-install or Taking a backup of database pre-upgrade.
3. Hooks are like any other template like Pod, Deployment, etc. Helm identifies between a Hook and any other template using `helm.sh/hook` annotation.

## Types of Hooks:

![Types of Hooks](./images/types_of_hooks.png)

## Hooks Deletion Policies:

![Hooks Deletion Policies](./images/hook_deletion_policies.png)

## Hooks Execution Order:

![Hooks Execution Order](./images/hooks_execution_order.png)

## How are Hooks considered to be ready?

1. If hooks are Pods or Jobs, then hooks become ready once Pods or Jobs are completed.
2. If hooks are any other Kubernetes objects other than Pods and Jobs, then hooks become ready as soon as those objects are loaded or updated.

## Example:

![Example](./images/demo.png)

### Prerequisite

1. Clone `helm-chart-hooks-example` GitHub repository by running the following command:

```
git clone https://github.com/sagar-jadhav/helm-chart-hooks-example.git
```

2. Move to `helm-chart-hooks-example` directory by running the following command:

```
cd ./helm-chart-hooks-example
```

3. log in to [Docker Hub](https://hub.docker.com/) by running the following command:

```
docker login -u <Username>
```

Where:
- `<Username>` is the username to log in to the docker hub.

4. Create the repositories in [Docker Hub](https://hub.docker.com/) with the following names:

- post-install-db-init
- post-upgrade-add-data

5. Move to `post-install-init-db` directory by running the following command:

```
cd ./post-install-init-db
```

6. Build `post-install` job docker image by running the following command:

```
docker build -t post-install-db-init:1.0.0 .
```
7. Tag `post-install` job docker image by running the following command:
```
docker tag post-install-db-init:1.0.0 <Username>/post-install-db-init:1.0.0
```
Where:
- `<Username>` is the username to log in to the docker hub.
8. Push `post-install` job docker image by running the following command:
```
docker push <Username>/post-install-db-init:1.0.0
```
Where:
- `<Username>` is the username to log in to the docker hub.
9. Move out of `post-install-init-db` directory by running the following command:
```
cd ..
```
10. Move to `post-upgrade-add-data` by running the following command:
```
cd ./post-upgrade-add-data
```
11. Build `post-upgrade` job docker image by running the following command:
```
docker build -t post-upgrade-add-data:1.0.0 .
```
12. Tag `post-upgrade` job docker image by running the following command:
```
docker tag post-upgrade-add-data:1.0.0 <Username>/post-upgrade-add-data:1.0.0
```
Where:
- `<Username>` is the username to log in to the docker hub.
13. Push `post-upgrade` job docker image by running the following command:
```
docker push <Username>/post-upgrade-add-data:1.0.0
```
Where:
- `<Username>` is the username to log in to the docker hub.
14. Move out of `post-upgrade-add-data` directory by running the following command:
```
cd ..
```
15. Provision a single-node Kubernetes cluster.
16. Create the following directories in the Kubernetes node:
- /root/db-data
- /root/share
- /root/scripts
17. Replace `<DockerHub Username>` with the username to log in to the docker hub in `values.yaml` file located at `<Path to helm-chart-hooks-example>/mysql/values.yaml` location.
18. Create a file with name `rename-backup.sh` at `/root/share/post-backup` location and add the following content into it:
```
#!/bin/bash
# Rename backup file.
if [[ -n "$DB_DUMP_DEBUG" ]]; then
set -x
fi
if [ -e ${DUMPFILE} ];
then
new_name=mysql-backup.gz
old_name=$(basename ${DUMPFILE})
echo "Renaming backup file from ${old_name} to ${new_name}"
cp ${DUMPFILE} /db/${new_name}
else
echo "ERROR: Backup file ${DUMPFILE} does not exist!"
fi
```
### Demo
1. Install the `MySQL` application by running the following command:
```
helm install mysql ./mysql/
```
Wait for some time for the command to get executed successfully.
2. Verify that data got populated in the database by running the following command:
```
kubectl exec -it <MySQL Pod Name> -n mysql -- /bin/bash
```
```
mysql -hmysql -uroot -padmin
```
```
Use Universe;
```
```
Select * From Heroes;
```
```
exit
```
```
exit
```
Where:
- `<MySQL Pod Name>` is the MySQL Pod name, You can get the MySQL Pod name using the `kubectl get po -n mysql` command.
3. Upgrade the MySQL application by running the following command:
```
helm upgrade mysql ./mysql/
```
Wait for some time for the command to get executed successfully.
4. Verify that data got populated in database post upgrade by running the following command:
```
kubectl exec -it <MySQL Pod Name> -n mysql -- /bin/bash
```
```
mysql -hmysql -uroot -padmin
```
```
Use Universe;
```
```
Select * From Heroes;
```
```
exit
```
```
exit
```
Where:
- `<MySQL Pod Name>` is the MySQL Pod name, You can get the MySQL Pod name using `kubectl get po -n mysql` command.
5. Rollback the MySQL application by running the following command:
```
helm rollback mysql 1
```
Wait for some time for the command to get executed successfully.
6. Verify that data got corrected in the database post rollback by running the following commands:
```
kubectl exec -it <MySQL Pod Name> -n mysql -- /bin/bash
```
```
mysql -hmysql -uroot -padmin
```
```
Use Universe;
```
```
Select * From Heroes;
```
```
exit
```
```
exit
```
Where:
- `<MySQL Pod Name>` is the MySQL Pod name, You can get the MySQL Pod name using `kubectl get po -n mysql` command.
7. Delete the MySQL application by running the following command:
```
helm uninstall mysql
```
Wait for some time for the command to get executed successfully.
Binary file added helm-chart-hooks-example/images/demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions helm-chart-hooks-example/mysql/.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/
23 changes: 23 additions & 0 deletions helm-chart-hooks-example/mysql/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: v2
name: mysql
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.
appVersion: 1.16.0
37 changes: 37 additions & 0 deletions helm-chart-hooks-example/mysql/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: {{ .Values.mysql.label }}
name: {{ .Values.mysql.name }}
namespace: {{ .Values.mysql.namespace }}
spec:
replicas: {{ .Values.mysql.replicaCount }}
selector:
matchLabels:
app: {{ .Values.mysql.label }}
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
app: {{ .Values.mysql.label }}
spec:
containers:
- image: {{ .Values.mysql.image.name }}:{{ .Values.mysql.image.tag }}
imagePullPolicy: {{ .Values.mysql.image.pullPolicy }}
name: {{ .Values.mysql.name }}
resources: {}
env:
- name: MYSQL_ROOT_PASSWORD
value: {{ .Values.mysql.password }}
volumeMounts:
- mountPath: /var/lib/mysql
name: data
volumes:
- name: data
hostPath:
path: {{ .Values.mysql.hostPath.db.path }}
6 changes: 6 additions & 0 deletions helm-chart-hooks-example/mysql/templates/ns.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v1
kind: Namespace
metadata:
name: {{ .Values.mysql.namespace }}
labels:
app: {{ .Values.mysql.label }}
Loading

0 comments on commit 10caf27

Please sign in to comment.