In this tutorial, you will learn how to implement a multi-tier application using Azure Kubernetes Service (AKS) and Azure Arc. We will be using the Azure CLI to create resources and deploy the application.
Before you begin, you will need the following:
- Azure subscription
- Azure CLI installed on your machine
- Basic knowledge of Kubernetes and containerization
Step 1: Create an Azure Kubernetes Service (AKS) cluster
To create an AKS cluster, you will use the az aks create
command. This command creates an AKS cluster with a single node. Replace the placeholder values with your own unique values for the resource group, cluster name, and service principal:
az group create --name myResourceGroup --location eastus
az aks create --resource-group myResourceGroup --name myAKSCluster --node-count 1 --generate-ssh-keys
Step 2: Deploy a multi-tier application
We will use a sample multi-tier application called “guestbook” which is a simple web-based guestbook application that allows users to post messages. You can deploy this application using the kubectl apply
command.
kubectl apply -f https://k8s.io/examples/application/guestbook/all-in-one-redis.yaml
This will create a Deployment, ReplicaSet, and Service for the frontend, and another Deployment, ReplicaSet, and Service for the Redis master.
Step 3: Enable Azure Arc for your AKS cluster
Azure Arc enables you to manage your Kubernetes clusters across on-premises, multi-cloud, and edge. To enable Azure Arc, you will use the az aks enable-addons
command.
az aks enable-addons --resource-group myResourceGroup --name myAKSCluster --addons azure-arc
Step 4: Check the application
To check the application, you will use the kubectl get
command to get the external IP address of the frontend service.
kubectl get service frontend
This will return the external IP address, which you can use to access the application in a web browser.
This tutorial provides a basic example of how to deploy a multi-tier application using Azure Kubernetes Service (AKS) and Azure Arc. You can use this as a starting point for deploying your own multi-tier applications in AKS and managing them with Azure Arc.
Using Terraform
Here is an example of how you could use Terraform to create an AKS cluster and deploy a multi-tier application using Terraform:
# Create resource group
resource "azurerm_resource_group" "example" {
name = "myResourceGroup"
location = "eastus"
}
# Create AKS cluster
resource "azurerm_kubernetes_cluster" "example" {
name = "myAKSCluster"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
dns_prefix = "myakscluster"
linux_profile {
admin_username = "azureuser"
ssh_key {
key_data = file("~/.ssh/id_rsa.pub")
}
}
agent_pool_profile {
name = "default"
count = 1
vm_size = "Standard_DS2_v2"
}
}
# Deploy multi-tier application
resource "kubernetes_manifest" "guestbook" {
manifests = templatefile("path/to/guestbook.yaml", {
frontend_image = "k8s.gcr.io/guestbook:v3"
redis_image = "k8s.gcr.io/redis:e2e"
})
depends_on = [azurerm_kubernetes_cluster.example]
}
In this example, the multi-tier application is defined in the guestbook.yaml file. Replace the placeholder values with your own unique values for the frontend and redis images.
For Azure Arc, you can use the azurerm_kubernetes_cluster_config resource to configure the kubernetes cluster after creating it, This resource can be used to configure the AKS cluster to enable Azure Arc.
resource "azurerm_kubernetes_cluster_config" "example" {
kubernetes_cluster_id = azurerm_kubernetes_cluster.example.id
addons = ["azure-arc"]
}
Please note that you may need to install the latest version of azurerm provider for Terraform, and configure the correct authentication method for your Azure subscription in order to run this code.
Azure DevOps Pipeline
Here is an example of a pipeline that uses Azure CLI and Terraform tasks to deploy an AKS cluster and a multi-tier application using Terraform:
# Azure DevOps pipeline
# Trigger pipeline on push to master branch
trigger:
- master
# Define variables for Terraform and Azure CLI tasks
variables:
azureSubscription: 'your-subscription-name'
resourceGroupName: 'your-resource-group-name'
clusterName: 'your-cluster-name'
location: 'your-location'
tenantId: 'your-tenant-id'
clientId: 'your-client-id'
clientSecret: 'your-client-secret'
# Authenticate to Azure
- task: AzureCLI@2
inputs:
azureSubscription: $(azureSubscription)
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
az login --service-principal -u $(clientId) -p $(clientSecret) --tenant $(tenantId)
# Initialize Terraform
- task: Terraform@0
inputs:
command: 'init'
workingDirectory: './terraform'
# Run Terraform
- task: Terraform@0
inputs:
command: 'apply'
workingDirectory: './terraform'
environmentServiceNameAzure: $(azureSubscription)
commandOptions: '-auto-approve'
# Configure the kubernetes cluster to enable Azure Arc
- task: AzureCLI@2
inputs:
azureSubscription: $(azureSubscription)
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
az aks enable-addons -a azure-arc -g $(resourceGroupName) -n $(clusterName)
# Deploy the multi-tier application
- task: AzureCLI@2
inputs:
azureSubscription: $(azureSubscription)
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
kubectl apply -f path/to/guestbook.yaml
You can customize this pipeline to suit your specific requirements, such as adding additional tasks to configure the AKS cluster or deploy the multi-tier application, etc.