Blog Post

Azure Arc Blog
3 MIN READ

Deploy a Kubernetes Application Programmatically Using Terraform and CLI

maanasagovi's avatar
maanasagovi
Former Employee
Dec 13, 2024

In our previous blog post, we explored Kubernetes apps benefits along with an introduction into how to programmatically deploy Kubernetes Apps. Today we will cover deploying a Kubernetes application programmatically using Terraform and CLI. These deployment methods can streamline your workflow and automate repetitive tasks.  

Deploying your Kubernetes Application using Terraform 

This walkthrough assumes you have previous knowledge of Terraform. For additional information and guidance on using Terraform to provision a cluster, please refer here. 

Prerequisites 

Before we begin, ensure you have the following: 

Sample Location 

You can find the Terraform sample we will be using at this location: Terraform Sample 

Prepare the Environment 

First, initialize Terraform in the current directory where you have copied the k8s-extension-install sample by running the following command: 

terraform init

In the directory, you will find two example tfvars files. These files can be used to deploy the application with different configurations: 

  • azure-vote-without-config.tfvars - Deploy the application with the default configuration for azure-vote. 
  • azure-vote-with-config.tfvars - Deploy/update the application with a custom configuration for azure-vote. 

Before you test run the sample tfvars files, update the following in the tfvars files: 

  • cluster_name - The name of the AKS cluster. 
  • resource_group_name - The name of the resource group where the AKS cluster is located. 
  • subscription_id - The subscription ID where the AKS cluster is located. 
Deploy the Application 

To deploy the application with the default configuration for azure-vote, run: 

terraform apply -var-file="azure-vote-without-config.tfvars"

To deploy or update the application with a custom configuration for azure-vote, use: 

terraform apply -var-file="azure-vote-with-config.tfvars"
Conclusion 

And that's it! You've successfully deployed your Kubernetes application programmatically using Terraform. This process can drastically reduce the time and effort involved in managing and scaling your applications. By using Terraform, you can ensure that your deployment is consistent and repeatable, making it easier to maintain your infrastructure as code. 

Deploying a Kubernetes Application from Azure CLI 

Deploying a Kubernetes application using Azure CLI can seem daunting, but we’re here to make it simple and accessible. Follow these steps, and you’ll have your azure-vote application up and running in no time! 

Prerequisites 

Before we get started, ensure you have the following: 

  • Azure CLI installed on your machine 
Deploying the Sample Azure-Vote Application from the Marketplace 
Step 1: Log in to Azure 

Open your terminal and log in to your Azure account by running: 

az login
Step 2: Set Your Subscription 

Specify the subscription you want to use with: 

az account set --subscription
Step 3: Deploy the Azure-Vote Application 

Now, deploy the azure-vote application to your Kubernetes cluster with the following command: 

az k8s-extension create --name azure-vote --scope cluster ` --cluster-name <clusterName> --resource-group <resourceGroupName> --cluster-type managedClusters ` --extension-type commercialMarketplaceServices.AzureVote ` --plan-name azure-vote-paid ` --plan-product azure-vote-final-1 ` --plan-publisher microsoft_commercial_marketplace_services ` --configuration-settings title=VoteAnimal value1=Cats value2=Dogs
Updating Configuration Settings 

If you want to update the configuration settings of the azure-vote application, you can do so easily. Use the following command to change the configuration settings: 

az k8s-extension update --name azure-vote ` --cluster-name <clusterName> --resource-group <resourceGroupName> --cluster-type managedClusters ` --configuration-settings value1=Elephant value2=Horse

And there you have it! By following these steps, you can deploy and update the azure-vote application on your Kubernetes cluster using Azure CLI. 

Conclusion 

Deploying Kubernetes applications using Azure CLI is a powerful way to manage and scale your applications. The process described above helps ensure your deployments are consistent and repeatable, simplifying the maintenance of your infrastructure as code.😄

Updated Jan 16, 2025
Version 2.0

1 Comment

  • While it is great to see so many options are provided, I'd dare challenging on the usability of either beyond the inner-loop for developers. No enterprise environment would ever allow for such autonomy (nor a healthy practice for that matter) to engineers. In my humble opinion it would have been more effective to express how these can work in a pipeline, or part of a GitOps approach - which is more likely to resonate with enterprise customers.