Deploy Azure Kubernetes Services with Terraform
Welcome to a comprehensive guide on developing Terraform code to deploy necessary resources, and successfully deploying a sample application to the cluster.
Microsoft's Azure Kubernetes Services (AKS) provides developers with a managed Kubernetes service, enabling the effortless deployment, scaling, and management of containerized applications in the cloud. Kubernetes, an open-source container orchestration platform, automates crucial aspects of containerized applications, including deployment, scaling, and management.
With AKS, developers can harness the capabilities of Kubernetes without the burden of managing underlying infrastructure.Terraform, an open-source solution for deploying and managing infrastructure via code, allows developers to define and provision infrastructure using a declarative approach and a high-level configuration language. Utilizing Terraform for deploying AKS resources empowers developers to automate the deployment process, manage configurations, and maintain consistency across different environments.
Understanding Kubernetes
Before delving into the specifics of AKS, it's essential first to comprehend what Kubernetes is and its operational mechanics. Kubernetes is a platform that's open-source, leading in container orchestration, and automates the deployment, scaling, and management of containerized applications. Containers offer a lightweight, transportable option to package and execute applications, with Kubernetes providing the automation required for managing these containers.Kubernetes operates by establishing a set of resources embodying a containerized application, which includes pods, services, and deployments.
Pods represent the smallest deployable entities within Kubernetes and house one or multiple containers. Services contribute a stable IP address and DNS name to an assembled set of pods, while deployments handle the rollout and scaling of these pods.
Setting up Azure Kubernetes Services
Before executing the deployment of an AKS cluster, it is necessary to establish an Azure Kubernetes Services account and set up the required resources like networking, storage, and authentication.
Here are the steps to achieve this:
- Navigate to the Azure portal and authenticate using your Azure account credentials.
- On the left-hand side menu, select "Create a resource", and then search for "Kubernetes Service".
- From the search outcomes, choose "Kubernetes Service" and then click on "Create".
- Allocate an appropriate subscription, resource group, name, and location for your designed AKS cluster.
- Set up networking and authentication parameters for your AKS cluster. This may include aspects such as the virtual network and the service principal.
- Review the cluster details and proceed to creating the cluster.
Once the creation of the cluster is complete, you can employ the Azure portal or use the Kubernetes command-line tool, kubectl, to manage and deploy applications to the newly created cluster.
Setting up Terraform
Following that, we must install Terraform on our local machine and configure its compatibility with Azure.
Here's the step-by-step process:
- Download and install Terraform from the official website.
- Launch a terminal or command prompt and navigate to the directory where you plan to save your Terraform code.
- Execute the terraform init command to initialize the Terraform working directory as well as download the required plugins.
- Configure Terraform to function in conjunction with Azure by establishing the following environment variable:
<pre class="codeWrap"><code>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"</code></pre>
- Substitute “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 respectively. These values can be obtained from the Azure portal by navigating to “Azure Active Directory” and choosing “App registrations”.
With AKS and Terraform now successfully set up, we can commence the process of writing the Terraform code that will deploy the necessary resources.
Writing the Terraform code
Here's a sample illustrating how to draft Terraform code for deploying an AKS cluster:
<pre class="codeWrap"><code>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
}
</code></pre>
This code delineates a resource group, a virtual network, a subnet, and an AKS cluster. The azurerm_kubernetes_cluster resource comes set up with a default node pool containing one node, a specific Kubernetes version, and a service principal for authentication. It is necessary 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 script displays the AKS cluster configuration, incorporating the kubeconfig file necessary to access the cluster.
Deploying the resources
To operationalize the AKS cluster employing Terraform, adhere to the following steps:
- Save the Terraform code into a file with a .tf suffix (for example, main.tf).
- Launch a terminal or command prompt and transition into the directory where your Terraform code is stored.
- Initiate the Terraform working directory and download the required plugins by executing the command terraform init.
- To generate an execution blueprint for the impending resource creation, run the command terraform plan.
- Review the plan to ensure its correctness.
- Execute the command terraform apply to instantiate the resources in Azure.
- Inspect the output to confirm the success of the deployment.
Deploying an application
Once the AKS cluster is successfully deployed, we can proceed to launch a sample application to the cluster utilizing Kubernetes manifests and kubectl.
Here's a demonstration of how this can be done:
Construct a Kubernetes manifest file for the sample application, for instance, example.yaml, with the contents as follows:
<pre class="codeWrap"><code>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
</code></pre>
This manifest establishes a deployment and a service for a sample application utilizing the nginx image.
Administer the manifest to the AKS cluster by implementing kubectl:
<pre class="codeWrap"><code>kubectl apply -f example.yaml</code></pre>
This would lead to the creation of the requisite resources for the sample application within the AKS cluster.
Verify that the application is functioning by accessing the external IP address of the load balancer service:
<pre class="codeWrap"><code>kubectl get service example</code></pre>
This will provide the external IP address of the service, which can be utilized to access the sample application via a web browser.In this article, we've navigated the process of deploying Azure Kubernetes Services with the use of Terraform and launching a sample application to the cluster with Kubernetes manifests and kubectl. The application of Terraform to automate the deployment operation allows us to handle our infrastructure as code and ascertain uniformity across our environments.
Facing Challenges in Cloud, DevOps, or Security?
Let’s tackle them together!
get free consultation sessionsWe will contact you shortly.