- Using hooks chart developers can either execute logic or create Kubernetes objects at a certain point in the release life cycle.
- For e.g., Creating a secret pre-install or Taking a backup of database pre-upgrade.
- Hooks are like any other template like Pod, Deployment, etc. Helm identifies between a Hook and any other template using
helm.sh/hook
annotation.
- If hooks are Pods or Jobs, then hooks become ready once Pods or Jobs are completed.
- 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.
-
Clone
helm-chart-hooks-example
GitHub repository by running the following command:git clone https://github.com/sagar-jadhav/helm-chart-hooks-example.git
-
Move to
helm-chart-hooks-example
directory by running the following command:cd ./helm-chart-hooks-example
-
log in to Docker Hub by running the following command:
docker login -u <Username>
Where:
<Username>
is the username to log in to the docker hub.
-
Create the repositories in Docker Hub with the following names:
- post-install-db-init
- post-upgrade-add-data
-
Move to
post-install-init-db
directory by running the following command:cd ./post-install-init-db
-
Build
post-install
job docker image by running the following command:docker build -t post-install-db-init:1.0.0 .
-
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.
-
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.
-
Move out of
post-install-init-db
directory by running the following command:cd ..
-
Move to
post-upgrade-add-data
by running the following command:
cd ./post-upgrade-add-data
-
Build
post-upgrade
job docker image by running the following command:docker build -t post-upgrade-add-data:1.0.0 .
-
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.
-
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.
-
Move out of
post-upgrade-add-data
directory by running the following command:cd ..
-
Provision a single-node Kubernetes cluster.
-
Create the following directories in the Kubernetes node:
- /root/db-data
- /root/share
- /root/scripts
-
Replace
<DockerHub Username>
with the username to log in to the docker hub invalues.yaml
file located at<Path to helm-chart-hooks-example>/mysql/values.yaml
location. -
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
-
Install the
MySQL
application by running the following command:helm install mysql ./mysql/
Wait for some time for the command to get executed successfully.
-
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 thekubectl get po -n mysql
command.
-
Upgrade the MySQL application by running the following command:
helm upgrade mysql ./mysql/
Wait for some time for the command to get executed successfully.
-
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 usingkubectl get po -n mysql
command.
-
Rollback the MySQL application by running the following command:
helm rollback mysql 1
Wait for some time for the command to get executed successfully.
-
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 usingkubectl get po -n mysql
command.
-
Delete the MySQL application by running the following command:
helm uninstall mysql
Wait for some time for the command to get executed successfully.