Create AKS Cluster Using Terraform
Create AKS Cluster Using Terraform
Created By
Linkedin Youtube
Contents
1. Prerequisites............................................................................................................................................................ 2
2. Create Directory and login to Azure CLI ................................................................................................................... 2
3. Implement the Terraform code ............................................................................................................................... 3
4. Create Resources using Terraform ..........................................................................................................................10
5. Verify the results ....................................................................................................................................................11
6. Deploy Application .................................................................................................................................................13
7. Cleanup ..................................................................................................................................................................18
1
1. Prerequisites
1.1 Install Terraform in Windows
https://youtu.be/ERM6UKCh3Hg
1.2 Install Azure CLI
https://learn.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest
https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-windows?tabs=azure-cli
1.3 Download kubectl
https://kubernetes.io/releases/download/
1.4 Download and install VS Code
https://code.visualstudio.com/download
https://learn.microsoft.com/en-us/cli/azure/aks?view=azure-cli-latest#az-aks-install-cli
1.5 Install the Azure Terraform Visual Studio Code extension
https://learn.microsoft.com/en-us/azure/developer/terraform/configure-vs-code-
extension-for-terraform?tabs=azure-cli
2
3. Implement the Terraform code
3.1 Create file provider.tf under folder “Terraform Code” and provide below content to file,
save the file.
terraform {
required_version = ">=1.0"
required_providers {
azapi = {
source = "azure/azapi"
version = "~>1.5"
}
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"
}
random = {
source = "hashicorp/random"
version = "~>3.0"
}
time = {
source = "hashicorp/time"
version = "0.9.1"
}
}
}
provider "azurerm" {
features {}
}
3
3.2 Create a file named ssh.tf and insert the following code, save the file.
output "key_data" {
value = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey
}
4
3.3 Create the main.tf, insert following script and save the file.
5
identity {
type = "SystemAssigned"
}
default_node_pool {
name = "agentpool"
vm_size = "Standard_D2_v2"
node_count = var.node_count
}
linux_profile {
admin_username = var.username
ssh_key {
key_data = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey
}
}
network_profile {
network_plugin = "kubenet"
load_balancer_sku = "standard"
}
}
6
3.4 Create variables.tf, insert following code and save the file.
variable "resource_group_location" {
type = string
default = "eastus"
description = "Location of the resource group."
}
variable "resource_group_name_prefix" {
type = string
default = "rg"
description = "Prefix of the resource group name that's combined with a random ID so
name is unique in your Azure subscription."
}
variable "node_count" {
type = number
description = "The initial quantity of nodes for the node pool."
default = 2
}
variable "msi_id" {
type = string
description = "The Managed Service Identity ID. Set this value if you're running this
example using Managed Identity as the authentication method."
default = null
}
variable "username" {
type = string
description = "The admin username for the new cluster."
default = "azureadmin"
}
7
3.5 Create a file named outputs.tf, insert the following code and save the file.
output "resource_group_name" {
value = azurerm_resource_group.rg.name
}
output "kubernetes_cluster_name" {
value = azurerm_kubernetes_cluster.k8s.name
}
output "client_certificate" {
value = azurerm_kubernetes_cluster.k8s.kube_config[0].client_certificate
sensitive = true
}
output "client_key" {
value = azurerm_kubernetes_cluster.k8s.kube_config[0].client_key
sensitive = true
}
output "cluster_ca_certificate" {
value = azurerm_kubernetes_cluster.k8s.kube_config[0].cluster_ca_certificate
sensitive = true
}
8
output "cluster_password" {
value = azurerm_kubernetes_cluster.k8s.kube_config[0].password
sensitive = true
}
output "cluster_username" {
value = azurerm_kubernetes_cluster.k8s.kube_config[0].username
sensitive = true
}
output "host" {
value = azurerm_kubernetes_cluster.k8s.kube_config[0].host
sensitive = true
}
output "kube_config" {
value = azurerm_kubernetes_cluster.k8s.kube_config_raw
sensitive = true
}
9
4. Create Resources using Terraform
4.1 Initialize Terraform
terraform init –upgrade
10
5. Verify the results
5.1 Get the Kubernetes configuration from the Terraform state and store it in a file
that kubectl can read using the following command.
5.2 Verify the previous command didn't add an ASCII EOT character using the following
command.
5.3 cat ./azurek8s
If you see << EOT at the beginning and EOT at the end, remove these characters from the file.
Otherwise, you may receive the following error message: error: error loading config file
"./azurek8s": yaml: line 2: mapping values are not allowed in this context
Now you will get the version of kubectl with below command
11
5.5 On Azure portal you can see your AKS cluster under Kubernetes services. If you click on
connect, it will show you the commands to connect to your k8s cluster.
az login
Note: Replace these commands with your details or copy these commands directly from
portal.
5.6 Now with command kubectl get nodes you will get your node details.
12
6. Deploy Application
6.1 Create deployment.yml manifest file.
apiVersion: apps/v1
kind: Deployment
metadata:
name: swiggy-app
labels:
app: swiggy-app
spec:
replicas: 2
selector:
matchLabels:
app: swiggy-app
template:
metadata:
labels:
app: swiggy-app
spec:
terminationGracePeriodSeconds: 30
containers:
- name: swiggy-app
image: ashfaque9x/swiggy-clone:latest
imagePullPolicy: "Always"
ports:
- containerPort: 3000
13
Created by: www.linkedin.com/in/ashfaque-ahmed-shaikh
14
6.2 Create Service.yml file
apiVersion: v1
kind: Service
metadata:
name: swiggy-app
labels:
app: swiggy-app
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 3000
selector:
app: swiggy-app
15
6.2 Create pod for application with below command
Kubectl apply –f deployment.yml
16
6.5 Browse the application with external IP of the application swiggy-app.
17
7. Cleanup
7.1 Run command “kubectl get all” to get deployment and service details.
18
7.4 Delete AKS resources with below command
terraform plan -destroy -out main.destroy.tfplan
19
Connect to me,
20