DEPLOYING A WORDPRESS APPLICATION WITH MYSQL DATABASE IN K8S Cluster  @ HELM
Published: 2021-07-19 18:52:21

####Talking About HELM
Helm is a tool to manage applications within Kubernetes. You can easily deploy charts with your application information, allowing them to be up and preconfigured in minutes within your Kubernetes environment.

####Advantage of Helm -->
In kubernetes to deploy an application we need to write lots of code as well to run as many commands. Suppose if we have requirement to create an multi-tier application then we need to deploy them differently and manually i.e we need to deploy them one by one. here helms comes into picture. as we discuss helm is tool to manage application within k8s. actually what is meant & how they performs?

In helm , to manage kind of resources , helms tell you to create a chart. Helm uses a packaging format called charts. A chart is a collection of files that describe a related set of Kubernetes resources.

Main usecase of helm is , In k8s suppose if we want to rollout the previous version then we are changing in main deployment file and then we rebuild it , it means when we rollout k8s first terminate all old pods and launch new pods which look like complicated , but in helm , helm provides a values files in which we can create or manage our variables as we required so no need to change main file again & again.

Lets start to create Helm Chart -->

create one directory where we want to create an helm chart.
-->mkdir dir_name

 


2. Now Lets create an Chart.yaml file which consists of the version and resource type of the kubernetes you wanted to use. The directory in which chart lies is known as package. in our case wpapp is a package . Ensure C is capital for Chart and its yaml.

 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xf1wjlfyd2lfkhoczh5g.png)


3. Basically the structure of any helm package is somthing in which Chart.yaml file consist and templates directory is there which consist of all resources which will be needed to create an application  

 
As you can see that i have created a directory named “CHART” and inside that we need to create respective files like “Chart.yaml”,  and a template folder

4. Now lets create resources for our wordpress mysql application . For that we need to create two deployments i.e one for wordpress and another for mysql and one service to expose our wordpress application .

WordPress Code:
You can use this command to create a code of the wordpress file.

kubectl create deploy mywp1 --image=wordpress:5.1.1-php7.3-apache    --dry-run  -o yaml  > wordpress.yml

 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wybwltaz2dqeq0j2bb5d.png)

This above code will create a file named wordpress.yml and it will launch a wordpress application.
 
MySQL Code:
kubectl deploy mydb1 --image= mysql:5.7  --dry-run   -o  yaml  > mysql.yml

 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ossdnn4ujx7wffkzwq10.png)

Expose Wordpress:
Now to see our application to the clients exposing wordpress deployment.


kubectl  expose  deploy  mywp1   --type=NodePort  --port=80  --dry-run -o yaml  >  service.yml

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6jmc2u32mwiz6bxvv29o.png)

 
5. Launching Application using Helm:
So, now all is set we can now proceed further. Now we have to create a app. So helm will launch all the resources.

helm install myapp  ./chart

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gcgqocojinsiklf1u57z.png)

As you can see that the application has been deployed. Now we can check the wordpress application. you can see wordpress application with master ip with exposed port.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0qc7yexco7b7znhcm938.png)
 
You can check the list of helm you have using helm list command. Now we can check the the pods in which slave are running in the kubernetes cluster as shown in above image/photo.

 

6. Packaging resources inside the Helm chart
We have our helm chart ready inside the wpapp/ directory, but we can’t publish it as it is. Firstly, we have to create a package for this helm chart.

Create one directory named charts.

$ mkdir charts/

Now, run the following command to packages the chart and store it inside the charts/ directory.

$ helm package wpmysql -d charts/

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jpevxvhzlvro1nfya9lc.png)

You can see on tarball package is created inside charts/ directory.

7. Creating an index.yaml file
For every Helm repository, we must require an index.yaml file. The index.yaml file contains the information about the chart that is present inside the current repository/directory.

For generating index.yaml file inside charts/ directory, run following command.

$ helm repo index charts/

You can see an index.yaml file generated with the details of the chart.

 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jpevxvhzlvro1nfya9lc.png)
8. Push the chart to the GitHub public repository
Here, I will push the packaged chart as well as the chart source code to the Github repository, but you can push only the chart/ folder to the Github as well.

 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j3b0drelx0zicy6h9iuy.png)
 


[my repo](https://github.com/sonukushwaha403/wordpress_mysql_helm_chart)

9. Hosting the chart with GitHub pages
For publishing the Helm chart on artifacthub.io/, we must require a URL where we had hosted our helm chart. One of the free and easy ways to host the charts is using GitHub pages. So, we will first host our charts using GitHub pages.

Go to the setting of the repository and go all the way down to the Github pages section.

Select the branch and directory for the charts and save it.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f7dh6aa8qv7k4mxah5w9.png)
 
This will provides us one URL where our charts are hosted.

As we had placed our charts inside charts/ directory, the actual URL for the charts is look like this — >

https://sonukushwaha403.github.io/wordpress_mysql_helm_chart/charts

10. Publishing the Helm chart to ArtifactHub
Go to the [artifactoryhub.io](https://artifacthub.io/) and login to your account.

Click on add repository.

Select the kind as Helm charts, provide a name to the repository and enter the URL to the charts as we discussed

 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9veuq8pa3bfqvnak6e7k.png)
Click on add.

If the URL you had provided to the charts is correct then it will create a Helm repository.

Click on add.

If the URL you had provided to the charts is correct then it will create a Helm repository.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pvkx52btdspid9053eky.png)
So, we had successfully published our own Helm chart to the artifacthub.io

11. Using our Helm chart from ArtifactHub
First of all, run following command to check if we have any helm repositories added.

$ helm repo list

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m0rtqxj8zx0vc3qjyo33.png)
For installing any helm chart, we first required to add the repository in which that chart is present. As our chart is present in

 https://sonukushwaha403.github.io/wordpress_mysql_helm_chart/charts

$ helm repo add myaapp https://sonukushwaha403.github.io/wordpress_mysql_helm_chart/charts
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pis287vnpxb8pnvf73sg.png)
 
$ helm repo list

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d6lf0lch9bdiyq9e15ou.png)
 
 
Now, we can install our chart from this repository. As you known, the name of our chart is myapp

$ helm install myapp wpmysql
https://media-exp1.licdn.com/dms/image/C5612AQGWNo1NKmUIZg/article-inline_image-shrink_1000_1488/0/1617193770514?e=1632355200&v=beta&t=bxZ9VeaywL1hOl_ZO-DVjZCM0hgRLo07UeHEPX_7Hok

.$ helm list
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4n7bgwjdydgdg557t9xc.png)
 
Finally, we had successfully published our helm chart.

 
!! Thanking you all for visiting my article !!