This tutorial will show you on how to create a Azure Kubernetes Cluster (AKS) by using the Terraform tool.
Prerequisite
- Azure Account( https://azure.microsoft.com/en-us/features/azure-portal/)
- Install Azure CLI ( https://docs.microsoft.com/en-us/cli/azure/install-azure-cli)
- Azure Login ( az login)
- Terraform
Set up and initialize your Terraform Folder Workspace
You Folder structure become like below;
Theaks-cluster.tf
provisions a resource group and an AKS cluster. The default_node_pool
defines the number of VMs and the VM type the cluster uses. The addon_profile
enables the Kubernetes dashboard.
variables.tf
declares the appID
and password
so Terraform can use reference its configuration:
terraform.tfvars
defines the appId
and password
variables to authenticate to Azure
outputs.tf
declares values that can be useful to interact with your AKS cluster
providers.tf
sets the Terraform version to at least 0.13 and defines the required_provider
block
Create an Active Directory service principal account
We need to configure the provider with the subscription_id
, client_id
, client_secret
and tenant_id
. Set the following environment variables with the values which correspond to your account.
export ARM_SUBSCRIPTION_ID=xxxxxxxx
export ARM_CLIENT_ID=xxxxxxx
export ARM_CLIENT_SECRET=xxxxxxx
export ARM_TENANT_ID=xxxxxxxx
We can create a new Service Account by execute command below;
Initialize Terraform
After you have saved your customized variables file, initialize your Terraform workspace, which will download the provider and initialize it with the values provided in your terraform.tfvars
file.
Run / Execute Terraform and Provision the AKS cluster
It will execute below;
This process should take approximately 10 minutes. Upon successful application, your terminal prints the outputs defined in aks-cluster.tf
.
Configure kubectl
Create and Access Kubernetes Dashboard
To verify that your cluster is configured correctly and running, you will navigate to it in your local browser. We need to create a ClusterRoleBinding
to use the Kubernetes dashboard. This gives the cluster-admin
permission to access the kubernetes-dashboard
. you don't need to configure your Terraform Kubernetes Provider since you create this using Terraform, kubectl
.
Finally, to access the Kubernetes dashboard, run the following command, customized with your cluster name instead of sample-aks-
. This will continue running until you stop the process by pressing CTRL + C
.
You should be able to access the Kubernetes dashboard at http://127.0.0.1:8001/.
If want to destroy any resources you create once you are done with this tutorial. Run the destroy
command and confirm with yes
in your terminal.
Recap / Summary
We had learned how to use Terraform to provision Azure Kubernetest Cluster by following steps;
- Set up and initialize your Terraform workspace
- Create an Active Directory service principal account
- Initialize Terraform
- Run / Execute Terraform and Provision the AKS cluster
- Configure kubectl locally
- Access Kubernetes Dashboard
- Clean up your tutorial workspace