Skip to content
Petkir Blog
XLinkedinBluesky

Getting Started with Azure Kubernetes Service (AKS) using Bicep

Code2 min read

This document outlines the initial steps for setting up an Azure Kubernetes Service (AKS) cluster using Bicep, a declarative language for deploying Azure resources.

Prerequisites

  1. An Azure subscription (free tier available)
  2. Azure CLI or Azure PowerShell installed locally
  3. Basic understanding of Kubernetes concepts

Benefits of using Bicep

  1. Readability: Bicep uses a syntax similar to human-readable code, improving comprehension compared to JSON templates.
  2. Maintainability: Bicep offers features like parameterization and modules, promoting code reusability and easier deployment management.
  3. Error handling: Bicep provides better error messages during deployment, helping you identify and fix issues faster.

Creating your AKS Bicep file

Open your preferred code editor and create a new file named main.bicep. (my favorite is Visual Studio Code)

Paste the following code into the file, replacing the placeholders with your desired values:

param (
location string,
resourceGroupName string,
clusterName string,
linuxUsername string,
nodeVmSize string = 'Standard_B1s'
)
resource group 'myAKSRG' = {
location: location
}
resource aksCluster 'myAKS' = {
name: clusterName
location: location
resourceGroup: resourceGroupName
dnsPrefix: clusterName
linuxProfile {
username: linuxUsername
sshKey {
publicKey: '// Enter your SSH public key here'
}
}
nodePoolProfile {
name: 'agentpool'
vmSize: nodeVmSize
count: 3
}
}

Explanation of the code:

  • The param block defines parameters that can be customized during deployment.
  • The resource group block creates a resource group to hold your AKS cluster resources.
  • The aksCluster block defines the AKS cluster configuration.
    • Properties like dnsPrefix, linuxProfile, and nodePoolProfile configure the cluster's name, SSH access, and virtual machine size for worker nodes.

Deploying the AKS Cluster

Open a terminal window and navigate to the directory containing your main.bicep file.

Using Azure CLI

az deployment group create --name myAKSDeployment --resource-group myAKSRG --template-file main.bicep --parameters location=yourLocation resourceGroupName=yourResourceGroup clusterName=yourClusterName linuxUsername=yourUsername

Using Azure PowerShell

New-AzResourceGroupDeployment -Name myAKSDeployment -ResourceGroupName myAKSRG -TemplateFile main.bicep -clusterName myCluster -location yourLocation -linuxUsername yourUsername

Connecting to your AKS cluster

Once the deployment finishes successfully, you can connect to your cluster using kubectl.

  • Install the Azure CLI extension for AKS:
az extension add collection --name kubectl-aks
  • Get the credentials for your cluster:
az aks get-credentials --resource-group myAKSRG --name myCluster
  • Verify the connection:
kubectl get nodes

This should list the worker nodes running in your AKS cluster.

Additional considerations

  • Remember to replace the placeholder values in the Bicep code according to your requirements.
  • Secure your SSH access with a strong key pair.
  • This is a basic example. AKS offers a wide range of configuration options for advanced scenarios.

Conclusion

Bicep provides a powerful and user-friendly way to deploy AKS clusters in Azure. This guide has given you a starting point for exploring the potential of Bicep for managing your cloud infrastructure. Refer to the official documentation for further details on Bicep syntax, AKS configuration options, and best practices for deploying containerized applications in AKS.