Unleash the Power of Kubernetes with Terraform: Deploy Sock Shop Microservice Like a Pro

Nwokolo Emmanuel
6 min readJun 7, 2023

Hey! Welcome once more!

in this section, we are going to be deploying the sock shop application with Kubernetes using Terraform

we are going to be deploying 10 services — carts, catalogue, frontend, orders, payment, queue-master, rabbitmq, session-db, shipping, and user

these 10 services are what make up the sock-shop microservice.

prerequisite:

- A cluster running

- An Ubuntu machine

- Terraform installed

- Kubectl installed

- AWS CLI installed

now you can go about the internet to find ways to install all these tools.

or you can just run a script I wrote that helps you install all these tools on your machine.

to get the script and how to use it then click HERE.

you can spin up a machine on DigitalOcean or any cloud provider.

there is a blog on how to do that HERE

if you have all this ready then let’s begin!

1. PROVIDER.TF

in this section, we are going to create a providers file for the sock shop microservice:

we will be creating a new namespace for it and we will be naming it sock-shop

We will also need to specify the location of the Kube config file for the providers listed.

We need to input the name of our cluster into the file, so anywhere you see ‘ClusterName’ replace it with the name of your cluster.

Where you see ClusterName_auth, replace the ‘ClusterName’ part as stated above, leaving the ‘_auth’ part as it is.

2. CARTS

in this section, we are going to be writing a terraform-Kubernetes script that deploys the carts application into the cluster:

the carts application is going to be running on port 80 and it also has its own database which is a Mongo DB

3. CATALOGUE

just like the carts section above. we are going to also be deploying the catalogue section of the sock-shop microservice:

this service is also running on port 80 and has a MySQL DB as you can see in the file above.

4. FRONTEND

the frontend service is going to be the front page of the sock-shop application. it is going to also be deployed in the sock-shop namespace with 1 replica.

the frontend service doesn’t have a database. it also runs on port 80.

5. ORDERS

the next service we will be deploying will be the order service. this is the service where customers can make orders.

we will just be pulling the already created app from docker.

the order service runs on port 27017 and has its own mongo db database

6. PAYMENTS

the payment service just like every other service will be deployed in the sock-shop namespace. with just 1 replica.

this is going to be a service where customers can make payments. and like every other service also we are pulling the payment service from docker.

the payment service is running on port 80

7. queue-master

the queue master has only one replica. it also runs on a port 80 with a CPU limit of 300m and memory limit of 500Mi

8. RABBITMQ

the RabbitMQ is for the order queue. and it has an exporter which runs on the port 9090 .

the management container runs on port 15672 while the service runs on port 5672

9. SESSION-DB

the session db is a Redis container that is used to store data. the redis container is running on port 6379

the service is also running on port 6379 as in the file above

10. SHIPPING

the shipping service is where shipping is made in the sock shop microservice. it has its own image which we will be pulling from docker as in the file below:

the service runs on port 80

11. USER

the user service of the sock shop microservice is where users can be created.

as you should expect we are using a database to store information about the user.

the database used here is mongo db. and the service is running on port 27017

there is also a volume mount in the DB.

when you are done your file structure should look like this:

to run the terraform files use the command below:

$ terraform init
$ terraform --auto-approve

to check if the sock shop has been deployed you can use this command:

$ kubectl get svc -n sock-shop

with the command above you should see all the services that were deployed in the sock-shop namespace.

VIEWING ON-BROWSER

Now that you have deployed the sock-shop you would want to also view that on your browser.

though this blog was not to cover that but to use an ingress rule to be able to access the microservice.

I will still show you how to just look at it on your browser.

first, we will need to run this command to display the services we have:

$ kubectl get svc -n sock-shop

then you should see a frontend service also present.

we will need to edit it with this command:

$ kubectl edit svc front-end -n sock-shop

after typing this a new page should pop up:

Scroll down to the point where you see this ‘type: ClusterIP’ and change it to these exact words:

LoadBalancer

NOTE: The text editor is Vim, so you will have to click on the letter ‘I’ on your keyboard to insert the values, and when you have finished editing, you will press the ESC key and type :wqto save it.

After saving the file, you will see a load balancer created for the front-end service.

something similar to this:

 NAME           TYPE           CLUSTER-IP       EXTERNAL-IP                                                               PORT(S)                      AGE
front-end LoadBalancer 172.20.128.125 a5ae5a761a6f243e2aa65692a14f2b79-1095997634.eu-west-2.elb.amazonaws.com 80:31801/TCP 24m

copy the external IP which in this case is a load balancer and put it in your browser.

and boom! you should see this website:

NOTE: you can use the same method to change it from load balancer to cluster IP.

because we are going to be using an ingress rule in the next section to view our microservices

congratulations!! you just deployed the sock shop application.

Resources

NOTICE

There is a platform called THE CLOUD TOP G MASTERMIND where you can learn more about becoming a Cloud Engineer, the tools you need to build your projects, and getting your dream job.

You can get more details by clicking this LINK.

see you soon!

NOTE: If you have any questions or want to add to this blog, you can send me an E-mail. And I reply faster to people that subscribe to my newsletter!!

Conclusion

If you loved this blog post, please, give it a like, comment, and don’t forget to click the follow button.

And if you would love to get an update on the two exciting blogs I will be posting this week, then you should sign up for my newsletter right here!!

--

--

Nwokolo Emmanuel

I am a Cloud Engineer, I love sharing easy solutions to problems that I found difficult. Interested in Open Source | twitter: twitter.com/CloudTopG