Helm - flask app deployment
Prerequisite:-
Refer to my last blog for Kubernetes cluster setup and manual application deployment.
https://yes-we-can-devops.hashnode.dev/deploying-flask-applications-on-kubernetes
What is Helm & Helm charts
https://hashnode.com/edit/clnvy0m87000009ig2bcwfu5l
To Install helm and add helm repository in Ubuntu.
curl https://baltocdn.com/helm/signing.asc | gpg --dearmor | sudo tee /usr/share/keyrings/helm.gpg > /dev/null
sudo apt-get install apt-transport-https --yes
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/helm.gpg] https://baltocdn.com/helm/stable/debian/ all main" | sudo tee /etc/apt/sources.list.d/helm-stable-debian.list
sudo apt-get update
sudo apt-get install helm
Commands:
sudo apt install bash-completion
alias k='kubectl'
vim ~/.bashrc
To add aliases (shortcuts for commands) in ~/.bashrc file. To enable auto-completion.
Add below lines:-
alias k='kubectl'
alias kgp='kubectl get pods'
alias kga='kubectl get all'
alias a='kubectl get all -A'
source <(kubectl completion bash)
source <(helm completion bash)
Save the file and exit. (Shift+zz)
mkdir two-tier-app
cd two-tier-app/
helm create mysql-chart
cd mysql-chart/
ls
vim values.yaml
image: repository: mysql pullPolicy: IfNotPresent tag: "latest"Add:
service: type: ClusterIP port: 3306
env:
mysqlrootpw: admin
mysqldb: mydb
mysqluser: admin
mysqlpass: admin
- vim templates/deployment.yaml
Add the below contents:
env:
- name: MYSQL_ROOT_PASSWORD
value: "{{ .Values.env.mysqlrootpw }}"
- name: MYSQL_DATABASE
value: "{{ .Values.env.mysqldb }}"
- name: MYSQL_USER
value: "{{ .Values.env.mysqluser }}"
- name: MYSQL_PASSWORD
value: "{{ .Values.env.mysqlpass }}"
cd ..
ls
helm package mysql-chart/
helm install mysql-chart ./mysql-chart
k get all
- k get all
vim mysql-chart/templates/deployment.yaml
delete or # the 1) livenessProbe: and 2) livenessProbe: sections.
helm list
helm uninstall mysql-chart
helm package mysql-chart
helm install mysql-chart ./mysql-chart
- k get all
Go to the worker node (ssh into node) and run the below commands:
docker ps
docker exec -it 1e0b78b2f125 bash (Change container ID of yours)
mysql -u admin -p
admin
use mydb;
CREATE TABLE messages ( id INT AUTO_INCREMENT PRIMARY KEY, message TEXT );
SELECT * FROM messages;
The database is empty.
Go to the master node (ssh into node) and run the below commands:
helm create flask-app-chart
ls
cd flask-app-chart/
ls
k get svc =Copy the MySQL service IP
vim values.yaml
Add the below contents:
image: repository: yesicanvinayak/projects pullPolicy: IfNotPresent # Overrides the image tag whose default is the chart appVersion. tag: "flaskapp" env: mysqlhost: 10.99.113.204 # This should be IP of your mysql service. mysqldb: mydb mysqluser: admin mysqlpass: admin
service: type: NodePort port: 80 targetPort: 5000 nodePort: 30007
Check the below screenshots for your reference.
Save and exit.
- vim templates/deployment.yaml
Add the below contents:
env:
- name: MYSQL_HOST
value: "{{ .Values.env.mysqlhost }}"
- name: MYSQL_DB
value: "{{ .Values.env.mysqldb }}"
- name: MYSQL_USER
value: "{{ .Values.env.mysqluser }}"
- name: MYSQL_PASSWORD
value: "{{ .Values.env.mysqlpass }}"
ports:
- name: http
containerPort: {{ .Values.service.targetPort }}
protocol: TCP
# livenessProbe:
# httpGet:
# path: /
# port: http
# readinessProbe:
# httpGet:
# path: /
# port: http
Check the below screenshots for your reference.
vim templates/service.yaml
Add the below contents:
ports: - port: {{ .Values.service.port }} targetPort: {{ .Values.service.targetPort }} nodePort: {{ .Values.service.nodePort }} protocol: TCP name: http
Check the below screenshots for your reference.
cd ..
helm template flask-app-chart
Check all template values and make sure all are correct.
helm package flask-app-chart/
helm install flask-app-chart-release-1 ./flask-app-chart
- k get all
Open the AWS console and copy the worker node's public IP
Open the 30007 port in the security group > edit inbond rule.
Open the browser and enter the worker node's public IP:30007
Go to the worker node (ssh into node) and run the below commands:
docker ps
docker exec -it 1e0b78b2f125 bash (Change container ID of yours)
mysql -u admin -p
admin
use mydb;
SELECT * FROM messages;
π Congratulation! π Your data has been successfully injected into the database. πΎπ
To uninstall the charts:
helm list
helm uninstall flask-app-chart-release-1 mysql-chart
helm list
If you find the blog helpful, please do like it and share it with your friends. If you have any doubts or questions related to the topics covered in the blog, feel free to ask them in the comments section. I'll be more than happy to help! πππ₯