Creating a basic AKS(Kubernetes) cluster on Azure with Terraform

Creating an AKS cluster on azure is really simple and you don’t need to know anything about it, well maybe some Azure but nothing complex

Installing tools

1. Azure cli

This terraform code uses the cli authentication so you will need to have installed the azure cli https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest

you will need it to get some data nevertheless

2. Terraform

If you still doesn’t have Terraform installed on your computer you can follow this link https://www.terraform.io/downloads.html

3. Helm

Helm is used to deploy an example app on AKS you can skip this if you don’t pretend to do it https://helm.sh/docs/intro/install/

4. kubectl

This tool is used to contact and manage the Kubernetes cluster, you will need it in order to check the cluster status https://kubernetes.io/docs/tasks/tools/install-kubectl/

Create AKS cluster

1. Login with Azure cli

Using azure cli to Login is not the best way to do it in a pipeline, but works for this small tuto

We will write another tuto working with pipelines and maybe Azure DevOps

You will be prompted with your default browser and asked to login on the Azure portal

az login

Please save the subscription id on a notepad you will need it later

2. Git clone

git clone https://gitlab.com/Giondo/aks-apps-cloudflare.git

3. Before running the terraform code

This code is made to work with a pre-existing resource group if none exists and you want the code to create a new one please modify the resourcegroup.tf file and the modules calling to the group

├── aks
│   ├── main.tf
│   ├── outputs.tf
│   ├── provider.tf
│   ├── resourcegroup.tf <-- ResourceGroup file (example creating a new RG inside)
│   └── variables.tf <-- Variables file

Also on the variables.tf file you can declare the default value for the resourcegroup if you pretend to use a existing one

4. Running terraform code

cd aks-apps-cloudflare\aks
terraform init
terraform plan

terraform apply -auto-approve
var.prefix
  A prefix used for all resources in this example

  Enter a value: virtualinfra

data.azurerm_resource_group.main: Refreshing state...
azurerm_kubernetes_cluster.example: Creating...
azurerm_kubernetes_cluster.example: Still creating... [10s elapsed]
azurerm_kubernetes_cluster.example: Still creating... [20s elapsed]
azurerm_kubernetes_cluster.example: Still creating... [30s elapsed]
azurerm_kubernetes_cluster.example: Still creating... [40s elapsed]
azurerm_kubernetes_cluster.example: Still creating... [50s elapsed]
azurerm_kubernetes_cluster.example: Still creating... [1m0s elapsed]
azurerm_kubernetes_cluster.example: Still creating... [1m10s elapsed]
azurerm_kubernetes_cluster.example: Still creating... [1m20s elapsed]
azurerm_kubernetes_cluster.example: Still creating... [1m30s elapsed]
azurerm_kubernetes_cluster.example: Still creating... [1m40s elapsed]
azurerm_kubernetes_cluster.example: Still creating... [1m50s elapsed]
azurerm_kubernetes_cluster.example: Still creating... [2m0s elapsed]
azurerm_kubernetes_cluster.example: Still creating... [2m10s elapsed]
azurerm_kubernetes_cluster.example: Still creating... [2m20s elapsed]
azurerm_kubernetes_cluster.example: Still creating... [2m30s elapsed]
azurerm_kubernetes_cluster.example: Still creating... [2m40s elapsed]
azurerm_kubernetes_cluster.example: Still creating... [2m50s elapsed]
azurerm_kubernetes_cluster.example: Still creating... [3m0s elapsed]
azurerm_kubernetes_cluster.example: Still creating... [3m10s elapsed]
azurerm_kubernetes_cluster.example: Still creating... [3m20s elapsed]
azurerm_kubernetes_cluster.example: Creation complete after 3m22s [id=/subscriptions/0f39574d-d756-48cf-b622-0e27a6943bd2/resourcegroups/1-a84cef1f-playground-sandbox/providers/Microsoft.ContainerService/managedClusters/virtualinfra-k8s]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

Checking cluster status

1. Get credentials

You will need to connect to the cluster and for that you need credentials: azure cli make it really easy for you

set subscription to work with

az account set --subscription SubscriptionID

Credentials

ClusterName is in the format {PREFIX}-k8s

az aks get-credentials --resource-group ResourceGroupID --name clusterName

az aks get-credentials --resource-group ResourceGroupID --name virtualinfra-k8s
Merged "virtualinfra-k8s" as current context in /Users/.kube/config

2. Check status

This cluster is launched with only one worker node that once deployed should be in Ready Status

kubectl get nodes

NAME                              STATUS   ROLES   AGE    VERSION
aks-default-20141758-vmss000000   Ready    agent   4m1s   v1.16.13

Installing an app on AKS with helm

1. Installing example app

Once the cluster is Ready to use you can deploy an example app with helm really easy

There is a folder on the git repo called “helm” ignore it for now we will use it in the next tutos

helm repo add azure-marketplace https://marketplace.azurecr.io/helm/v1/repo
helm install my-release azure-marketplace/wordpress

2. Checking App deploy

Checking the status of the application pods

kubectl get pods -w
NAME                                    READY   STATUS              RESTARTS   AGE
my-release-mariadb-0                    0/1     ContainerCreating   0          81s
my-release-wordpress-64657ff5d4-zx9dn   0/1     ContainerCreating   0          81s
my-release-mariadb-0                    0/1     Running             0          103s
my-release-mariadb-0                    1/1     Running             0          2m16s
my-release-wordpress-64657ff5d4-zx9dn   0/1     Running             0          2m38s
my-release-wordpress-64657ff5d4-zx9dn   1/1     Running             0          3m14s

3. Getting the external IP to connect to the application

You need a way to connect to the application just deployed that Helm chart will also create a LoadBalancer with an external IP, so you can connect to the application

kubectl get svc
NAME                   TYPE           CLUSTER-IP     EXTERNAL-IP     PORT(S)                      AGE
kubernetes             ClusterIP      10.0.0.1       <none>          443/TCP                      9m27s
my-release-mariadb     ClusterIP      10.0.242.124   <none>          3306/TCP                     3m38s
my-release-wordpress   LoadBalancer   10.0.16.242    168.61.159.91   80:30561/TCP,443:30094/TCP   3m38s

The LoadBalancer External IP is the one that you need to use in order to access the application

Application Working on External IP

openshift-installed

And That’s it, You’ve created an AKS cluster and deployed an application on it

Pre-requisites

  • Azure cli
  • Terraform
  • Kubectl
  • Helm

https://gitlab.com/Giondo/aks-apps-cloudflare.git

https://docs.bitnami.com/azure/get-started-charts-marketplace/

https://github.com/terraform-providers/terraform-provider-azurerm.git