Scroll to top
© 2024, Global Digital Services LLC.

Deploy an Azure Kubernetes Services with Terraform


Fernando Noguera - April 19, 2023 - 0 comments

Azure Kubernetes Services (AKS) is a managed Kubernetes service provided by Microsoft that enables developers to easily deploy, scale, and manage containerized applications in the cloud. Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. By using AKS, developers can take advantage of the power of Kubernetes without having to manage the underlying infrastructure.

Terraform is an open-source tool for deploying and managing infrastructure as code. It enables developers to define and provision infrastructure in a declarative way, using a high-level configuration language. By using Terraform to deploy AKS resources, developers can automate the deployment process, manage their configuration, and ensure consistency across their environments.

In this article, we’ll walk through how to set up an AKS cluster, write Terraform code to deploy the resources and deploy a sample application to the cluster.

Understanding Kubernetes

Before we dive into AKS, let’s first understand what Kubernetes is and how it works. Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. Containers provide a lightweight and portable way to package and run applications, and Kubernetes provides a way to automate the management of those containers.

Kubernetes works by defining a set of resources that represent a containerized application, including pods, services, and deployments. Pods are the smallest deployable units in Kubernetes and contain one or more containers. Services provide a stable IP address and DNS name for a set of pods, and deployments manage the rollout and scaling of pods.

Setting up Azure Kubernetes Services

Before we can deploy an AKS cluster, we need to create an Azure Kubernetes Services account and configure the necessary resources, such as networking, storage, and authentication. Here’s how to do that:

  1. Go to the Azure portal and sign in with your Azure account.
  2. Click on “Create a resource” in the left-hand menu and search for “Kubernetes Service”.
  3. Select “Kubernetes Service” from the search results and click on “Create”.
  4. Choose a subscription, resource group, name, and location for your AKS cluster.
  5. Configure the networking and authentication options for your AKS cluster, such as the virtual network and the service principal.
  6. Review and create the cluster.

Once the cluster is created, you can use the Azure portal or the Kubernetes command-line tool kubectl to manage and deploy applications to the cluster.

Setting up Terraform

Next, we need to set up Terraform on our local machine and configure it to work with Azure. Here’s how to do that:

  1. Download and install Terraform from the official website.
  2. Open a terminal or command prompt and navigate to the directory where you want to store your Terraform code.
  3. Run the command terraform init to initialize the Terraform working directory and download the necessary plugins.
  4. Configure Terraform to work with Azure by setting the following environment variables:
export ARM_SUBSCRIPTION_ID="your_subscription_id"
export ARM_CLIENT_ID="your_client_id"
export ARM_CLIENT_SECRET="your_client_secret"
export ARM_TENANT_ID="your_tenant_id"
  1. Replace “your_subscription_id”, “your_client_id”, “your_client_secret”, and “your_tenant_id” with your actual Azure subscription ID, client ID, client secret, and tenant ID. You can obtain these values from the Azure portal by going to “Azure Active Directory” and selecting “App registrations”.

Now that we’ve set up AKS and Terraform, we can start writing the Terraform code to deploy the resources.

Writing the Terraform code

Here’s an example of how to write Terraform code to deploy an AKS cluster:

# Configure the Azure provider
provider "azurerm" {
features {}
}

# Create a resource group
resource "azurerm_resource_group" "example" {
name = "example-resource-group"
location = "eastus"
}

# Create a virtual network and subnet
resource "azurerm_virtual_network" "example" {
name = "example-vnet"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_subnet" "example" {
name = "example-subnet"
resource_group_name = azurerm_resource_group.example.name
virtual_network_name = azurerm_virtual_network.example.name
address_prefixes = ["10.0.1.0/24"]
}

# Create an AKS cluster
resource "azurerm_kubernetes_cluster" "example" {
name = "example-aks"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
dns_prefix = "example-aks"
kubernetes_version = "1.21.4"

default_node_pool {
name = "default"
node_count = 1
vm_size = "Standard_DS2_v2"
vnet_subnet_id = azurerm_subnet.example.id
}

service_principal {
client_id = "YOUR_CLIENT_ID_HERE"
client_secret = "YOUR_CLIENT_SECRET_HERE"
}

depends_on = [
azurerm_subnet.example,
]
}

# Output the AKS cluster configuration
output "kubeconfig" {
value = azurerm_kubernetes_cluster.example.kube_config_raw
}

This code defines a resource group, a virtual network, a subnet, and an AKS cluster. The azurerm_kubernetes_cluster resource is configured with a default node pool with one node, a specific Kubernetes version, and a service principal for authentication. You’ll need to replace “YOUR_CLIENT_ID_HERE” and “YOUR_CLIENT_SECRET_HERE” with your actual service principal client ID and client secret.

The output block at the end of the code prints out the AKS cluster configuration, which includes the kubeconfig file for accessing the cluster.

Deploying the resources

To deploy the AKS cluster using Terraform, follow these steps:

  1. Save the Terraform code to a file with a .tf extension (e.g., main.tf).
  2. Open a terminal or command prompt and navigate to the directory where you saved the Terraform code.
  3. Run the command terraform init to initialize the Terraform working directory and download the necessary plugins.
  4. Run the command terraform plan to generate an execution plan for the resources to be created.
  5. Review the plan and make sure it looks correct.
  6. Run the command terraform apply to create the resources in Azure.
  7. Review the output to make sure the deployment was successful.

Deploying an application

Once the AKS cluster is deployed, we can deploy a sample application to the cluster using Kubernetes manifests and kubectl. Here’s an example of how to do that:

  1. Create a Kubernetes manifest file for the sample application, such as example.yaml, with the following contents:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: example
spec:
  replicas: 1
  selector:
    matchLabels:
      app: example
  template:
      metadata:
       labels:
         app: example
  spec:
      containers:
       - name: example
         image: nginx
         ports:
          - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
   name: example
spec:
   type: LoadBalancer
ports:
   - port: 80
     targetPort: 80
selector:
     app: example

This manifest defines a deployment and a service for a sample application using  nginx image.

  1. Apply the manifest to the AKS cluster using kubectl:
kubectl apply -f example.yaml

This will create the necessary resources for the sample application in the AKS cluster.

  1. Verify that the application is running by accessing the external IP address of the load balancer service:
kubectl get service example

This will output the external IP address of the service, which can be used to access the sample application in a web browser.

In this article, we’ve walked through how to deploy Azure Kubernetes Services using Terraform and deploy a sample application to the cluster using Kubernetes manifests and kubectl. By using Terraform to automate the deployment process, we can manage our infrastructure as code and ensure consistency across our environments. We’ve also seen how Kubernetes provides a powerful platform for deploying and managing containerized applications. We encourage you to try out AKS and Terraform for yourself and see the benefits firsthand.

Contact us for more information or visit our blog.

Related posts