Overview
Hey Geeks, welcome to another blog where we are utilizing Terrateam
(Terrateam is Terraform automation for GitHub) to deploy AKS
(Azure Kubernetes Services) on the Azure cloud platform. We have made use of Terrateam for deploying EKS
(Elastic Kubernetes Service) for AWS, learn more.
Steps we are following:
We will create
Static Credentials
forAzure
.We will create a GitHub repo and clone it to our local.
We will write a Terraform configuration file for
AKS
(Azure Kubernetes Services).We will push our code to the main branch of the repo.
We will create a new branch named
“terrateam-setups”
make some changes to the configuration and push the branch to the repo to raise a pull request(PR).We will view our PR and on the successful plan, we will comment
“terrateam apply”
to apply all the changes to the Azure cloud.We will create
deployment.yml
,mkdocs-ns.yml
, andservice.yml
to run our MKDocs application on the AKS, this part will be done manually on your local.And done, our MKDocs application is running on an External IP.
For whom this guide is for
- Our main audience for this current blog is Developers who work on DevOps and Infrastructure management using Terraform and GitHub on an almost daily basis and find a tool that automates their burden and can give comprehensive information for each PR(Pull Requests) requested on the GitHub repo. You can build multiple infrastructures with Terrateam on your
Azure
cloud, we are deploying ourAKS
(Azure Kubernetes Services) in this blog post.
Pre-requisites
Terraform: install terraform on your local.
Azure account: You need an
Azure
account set up for the blog.Azure-CLI: You need Azure CLI to run Kubectl commands on the Azure cloud.
Kubectl: You need Kubectl installed to run Kubectl commands that are required to run deployment files for the MKDocs application.
GitHub account: Create or use a GitHub account for the blog.
Terrateam is installed on your GitHub account, click here to install it.
Let’s get our hands on the tutorial.
Follow the same steps as we are doing to get the best out of the blog
- First, install Terraform on your local
brew install terraform
“brew” will only work on Linux, Mac, and Shell you can follow the official Terraform download page for complete download and setting up steps.
- Next, install Azure-CLI on your local.
- I’m using a Windows environment so I have installed it in this particular way, you can install it for your respective OS.
- After clicking on “
Install - Windows
” click for your Windows bit type, mine is a 64-bit, simple click, download, and install it to your local system.
- Next, install Kubectl from here.
- On the web page, scroll down and download it for your respective OS by following the steps mentioned on the website.
We have fast-forwarded these installation processes since our audiences as a developer are very skilled in installing simple software.
Let’s move forward.
- Next, clone our GitHub repo for
main.tf
file.
- This
main.tf
file contains Terraform configuration for theazure resource group
, andazure kubernetes cluster
withdefault node pool
.
For complete code visit here.
- Setting up the Static Credentials in Azure for Terrateam.
We are following Terrateam’s official website which is available here.
First, log in to your Azure account after installing Azure CLI, from your terminal write the below command.
az login
Get your Subscription ID
az account list
Example output
Export your Subscription ID
export SUBSCRIPTION_ID="<subscription-id>"
Set the Subscription ID
az account set --subscription "$SUBSCRIPTION_ID"
Create a terrateam service principal
az ad sp create-for-rbac --role="Contributor" \ --scopes="/subscriptions/$SUBSCRIPTION_ID"
Example output:
Record the following to use below:
appID
maps toARM_CLIENT_ID
password
maps toARM_CLIENT_SECRET
tenant maps
toARM_TENANT_ID
Export your Terraform organization/repo combination as an environment variable.
export REPO="<OWNER/REPO>", in our case it was ScaleupInfra/terrateam-azure
Save your Azure Subscription ID credentials in your GitHub Secret
gh secret --repo "$REPO" set ARM_SUBSCRIPTION_ID --body "$SUBSCRIPTION_ID"
Create the Azure Client ID (appID) GitHub Secret
gh secret --repo "$REPO" set ARM_CLIENT_ID
Create the Azure Client Secret (password) GitHub Secret
gh secret --repo "$REPO" set ARM_CLIENT_SECRET
Create the Azure Tenant ID (tenant) GitHub Secret
gh secret --repo "$REPO" set ARM_TENANT_ID
Setup done!
- Add GitHub workflow files to run CI/CD with
Terrateam
.
Create a .github/workflows directory
mkdir -p .github/workflows
Store the Terrateam GitHub Actions workflow file in the
.github/workflows
directorycurl -L -o .github/workflows/terrateam.yml \
https://terrateam.io/.github/workflows/terrateam.yml
- Now push these files to the main branch of your GitHub repository.
- Create a branch and make changes.
Now to run Terrateam on PR, create a branch from the main branch make minor changes, and push it to the GitHub repo.
To make another branch write the following commands in your terminal:
git checkout -b terrateam-setups
we are creating a new branch named terrateam-setups
Make changes and push it to GitHub.
Use the following commands to push the branch to GitHub.
git add .
git commit -m “terrateam start”
git push origin terrateam-setups
Now to create a pull request you can use GitHub UI or run the following command after installing GitHub CLI in your local.
gh pr create --fill
Now check your GitHub PR for the same.
- PR approval
- Go to your GitHub account and click on Pull Request from the top bar.
- You will find a PR check running on your branch with the Terrateam plan running on it as well.
Let it run and wait for a few moments to let it go green.
In the checks, you may see
pre-hooks
andpost-hooks
, hooks can be used to run commands or set environment variables pre and post Workflows. Pre and post-hooks are only executed a single time during a Terrateam Operation.
Now we are ready to apply the changes on our Azure Cloud.
To apply changes comment
terrateam apply
on the PR.
- Wait for a couple of minutes for Terrateam to apply changes on your Azure cloud.
- It’s done now!
- This is the output of apply that you can see by clicking on “details” of
apply post hooks.
- Now let’s check the connectivity and deploy the MKDocs application from the local system.
Run the following command in your local to connect to your AKS
az aks get-credentials --resource-group example-resources --name example-aks1
Now run the
mkdocs-ns.yml
,deployment.yml
,service.yml
.mkdocs-ns.yml
file contains namespace manifest.
For complete code visit here.
deployment.yml
file contains the deployment manifest.
For complete code visit here.
service.yml
file contains the service manifest.
For complete code visit here.
Now run the following command to apply these manifests.
kubectl apply -f mkdocs-ns.yml
kubectl apply -f deployment.yml
kubectl apply -f service.yml
Now, wait for a few minutes and let the service apply.
To check the service External IP run the following command.
kubectl get svc -n mkdocs
Copy this External-IP and paste it on your browser in the following format
<EXTERNAL-IP>:8000
- Congratulations! You have deployed the MKDocs application.
Conclusion
We have installed
Terraform
,Kubectl
, andAzure CLI
on our local system.We have written all the Terraform configuration files to spin up
AKS
(Azure Kubernetes Services).We have set up the
Terrateam
for Azure cloud using Static Credentials and generated Azure Credentials forTerrateam
authentication.Pushed our code to GitHub.
Created a branch named
terrateam-setups,
made changes, and pushed it to GitHub.Connected our
AKS
(Azure Kubernetes Services) cluster fromAzure CLI
locally.Deployed our
MKDocs
application.