In previous article, we learnt how to deploy a WordPress using Kubernetes. Even though in that article I have prepared some scripts to deploy and it is fast enough, there is another way to deploy much more faster. Yes, we can use Terraform. If you have experience on AWS, you may hear about CloudFormation. Terraform is similar to CloudFormation but it can be applied on different cloud providers. In this article, we will deploy two applications using Terraform step by step.
Why use Terraform?
In fact, CloudShell is very convenient to deploy application on GCP. Apart from GCP, AWS and Azure also have their command line tools. Then you may ask why we have to learn and use Terraform? There are 4 main reasons:
- Terraform is able to show the difference between your current state and desired state. That means when you slightly edit your Terraform config files, then you are able to see what changes you are going to make.
- Terraform does not only take care of the initial deployment, but also the maintenance. We can easily create, update and delete tracked resources using the provided command.
- It is extremely easy to clean up all the things which are created by Terraform. If we use scripting ,we have to write a clean up script as well. But for Terraform we can simply do this by “terraform destroy” command.
- Terraform is able to check the orders of the actions stated in config file. That means if you want to run services or deployments of Kubernetes, Terraform will create the cluster first even if you wrongly state the sequence of orders.
We are going to deploy two applications: Guest Book and WordPress. We assume the Kubernetes yaml files work fine, then what we do is to integrate our Terraform with those yaml files.
In console, we choose Cloud Shell and then click the editor mode such that we could easily review our code.
The we have to setup our environment e.g. define project, zone and region:
gcloud config set project <you-project-id>
gcloud config set compute/zone us-west1-a
gcloud config set compute/region us-west1
Please download my prepared file from github or
git clone https://github.com/manbobo2002/terraform-k8s-demo.git
In fact, you can have your own structure. But here I want to share my thoughts about my structure.
As the figure shown above, we have main.tf, k8s.tf, outputs.tf, variables.tf, versions.tf.
For main.tf, we mainly use it to define the infrastructure of GCP. It can be the IP range of nodes or instances, size of instances, or number of nodes.
For k8s.tf, we define the details of Kubernetes. It can be the version of cluster or the path of Kubernetes files.
For outputs.tf, we define all the parameters we want to print it out. It can be the cluster region or cluster name.
For variables.tf, we define all the variables here. It can be location or the name of cluster, all the variables can be used in other tf files.
For versions.tf, we define which version of Terraform we are using. Also, we may use some third-party providers, it can be defined here as well.
Feel free to treat this structure as a template. We will know how to deploy different applications by just editing a little bit of config soon.
Deploy the First Application — Guest Book
Actually, this application is a sample application provided by Kubernetes. We can easily find the tutorial on the official guideline. But here we will not go through what is going on of the Kubernetes file, if your are not familiar with Kubernetes, please check my previous article.
We just have to make sure the pattern points to our application folder. The magic of this line is to easily edit and apply to all different Kubernetes yaml file. You may see the power on the second application.
First of all, we initialize the terraform by typing:
Then we see what we are going to build by typing:
Finally, if all the things are okay, then we apply the config by typing:
and type “yes”.
When we are creating the resources, we can easily see the progress. If we use scripting, we sometimes have to echo something manually.
In case you see some error about “… failed to create kubernetes rest client for update of resource… ”, just run below script:
And then run “terraform apply” one more time.
Once the Terraform is done, we can see the outputs like above figure.
To see which services we deployed, we can type:
kubectl get service --all-namespace
Of course, if you clearly know the namespace, you can just specify it instead of using all-namespace.
Then we browse the External IP of the frontend.
Yes, the application is done.
Deploy the Second Application — Wordpress
In order to see the power of Terraform, we are going to deploy the second application. Actually, we just have to do 2 steps only. All the yaml files are exactly the same as my previous article.
- Go to k8s.tf and edit the Kubernetes path.
- Run the cleanup script and apply it.
Yes, it is done. Just that simple.
Don’t trust me, just test it by yourself by typing:
kubectl get service --all-namespaces
We are able to see all the previous services are replaced by WordPress services now. We can try the external IP of the LoadBalancer as well.
Welcome to WordPress again.
Edit Instance Size and Number of Nodes
In main.tf, we can edit the instance size and number of nodes as above figure shown.
As I mentioned early, it is super easy to cleanup.
We just type:
Then type “yes”.
All the things are gone now.
Terraform is a extremely useful tools when we talk about Infrastructure as a Code. Of course, it takes time to learn Terraform. You can take my git repo as a template. But in case you want to have some adjustments, you have to learn more by yourself. If you are deploying a small application, Cloud Shell script can help you a lot. However, if you have to deploy many different types of application quickly, then please consider Terraform.