/terraform-aks-rbac-azure-ad

Build a Role Based Access Controlled (RBAC) Azure Kubernetes Service (AKS) cluster using terraform

Primary LanguageHCL

Create a RBAC Azure Kubernetes Services (AKS) cluster with Azure Active Directory using Terraform

In this article I am going to show you how to build a Role Based Access Controlled (RBAC) Azure Kubernetes Services (AKS) cluster using Terraform and Azure Active Directory. At the time of writing this article, when you create an AKS cluster using the portal or terraform RBAC is disabled by default. Luckily since version 1.19.0 of the AzureRM Terraform provider RBAC is supported.
 
You can find all the files used at the following GitHub repository.
 

Prerequisites

 
Before you can set up your new AKS cluster you need to make sure you have terraform installed on your local machine and it set up correctly. You can find out how to do that using this guide. https://learn.hashicorp.com/terraform/getting-started/install.html
 
You will also need a Service Principal. You can read my article, First look at terraform (https://pixelrobots.co.uk/2018/11/first-look-at-terraform-and-the-azure-cloud-shell/) to get this. Make sure you take note of the App ID (Client ID) and Password (Client Secret) , we will need them for the variables.tf file later.
 
An Azure Storage account with a container and an Access Key to store your Terraform state file. How to do this using this guide from Microsoft. https://pixelrobots.co.uk/2019/01/how-to-store-your-terraform-state-file-in-azure-storage/
 
An SSH certificate for the Linux VMs for your AKS cluster. You can read more about creating them here. https://docs.microsoft.com/en-us/azure/virtual-machines/linux/ssh-from-windows. Just make sure you have it saved in the same path that's stated in the variables terraform file.
 
 
 

Creating the Azure Active Directory applications

 
AKS with RBAC needs two applications created in Azure AD. The first one is a Server application, the second is a client application. We will use the Azure portal to create them.
 

Note:
You can use the same Server application for multiple AKS clusters, but it is recommended to use one Client application per cluster.
 

Create the Server application

 
This application is used to get a users Azure AD group membership.
 
In the Azure Portal navigate to Azure Active Directory and then click on App registrations and click New application registration .
 
clip_image001.png
 
In here we need to enter a Name and make sure the Application type is Web app / API. In the Sign-on URL enter any web address. I am using my domain name. Then click Create .
 
clip_image002.png
 
 
In the new blade click on Manifest .
 
clip_image003.png
 
 
In here we need to edit the groupMembershipClaims value to "All" . Make sure to include the ". Then click Save .
 
clip_image004.png
 
Now Click on Settings and then click on Keys .
 
clip_image005.png
 
Now enter a Description for the key and select when you would like it to Expire . Then click Save .
 
clip_image006.png
 
Take a copy of the Value . We will need it later when we create the AKS cluster. The value is referred to as the Server application secret .
 

Warning:
You will not be able to get this value again if you leave this blade. Make sure you copy it.

 
Now click on Required permissions In this blade click on + add .
 
clip_image007.png
 
Click Select an API then Microsoft Graph, then click Select .
 
clip_image008.png
 
Under Application permissions put a tick next to Read Directory Data .
 
clip_image009.png
 
Scroll down further to Delegated permissions . Under here put a tick next to Sign in and read user profile . Then click Select . In the next blade click Done.
 
clip_image010.png
 
Now we have to Grant admin consent . All we a have to do is click the button.
 
clip_image011.png
 
Click Yes.
 
clip_image012.png
 
 
Take a note of the Application ID we will need it for later.
 
clip_image013.png
 

Create the Client application

 
This application is used when logging in using the Kubectl the Kubernetes CLI.
 
Navigate back to the Azure Active Directory blade again and click on App registrations . Create a new one again.
 
Enter a Name and then under Application type select Native. Add a Redirect URI again I have used my domain. Then click Create.
 
clip_image014.png
 
Now click on Settings and click Required permissions . In here click on Add.
 
clip_image015.png
 
Click on Select an API. In the search box enter the name of the Server application we just created. Click it and then click Select .
 
clip_image016.png
 
Put a tick next to Access AKSRBAC. (the AKSRBAC is your server application name.) Then click Select . IN the next blade click Done.
 
clip_image017.png
 
Now we have to Grant admin consent . All we a have to do is click the button and then click Yes.
 
clip_image018.png
 
Now take a note of the Application ID. This will be the Client application ID.
 
clip_image019.png
  Get the Tenant ID
 
Now we need to get the Tenant ID. This is easy. Just go back to Azure Active Directory in the Azure portal and click on Properties . In here you will see the Tenant ID .
 
clip_image020.png
 
You should now have a set of IDs like the ones I do below.
 

Server application secret: rfHXIJmz6d9/sTHQk4ekyvescN7PcogFyIVmYytmxBs=   
Server Application ID: c59c8bf4-c1be-46a5-992a-18efdd9b08ac   
Client Application ID: 9418f3aa-7845-4de8-90bf-0231ad06450b   
Tenant ID: d8171bb5-a0de-40a6-afdf-8b569cf6dbb8  

 
 

Deploying the Cluster with Terraform.

 
Now its time to deploy the AKS cluster using terraform.
 
First we need to edit the variables.tf file from the GitHub repo with the right names and values for your environment. We will need to also add our IDs we have from above along with our Service Principal details.
 
Now its time to initialize Terraform. First, we need to update the backend.tfvars file with our storage account details for the tfstate file. You should have all this information if you followed the guide in the prerequisites. To actually initialize terraform in your VS Code Bash terminal or Windows subsystem for Linux terminal type the following. Just make sure you're in the directory with the terraform files.
 
You will need to login to your Azure subscription first use:
 
az login
 
 
terraform init -backend-config=backend.tfvars
 
clip_image022.png
 
Lets test our Terraform files to see what will happen. We use the plan option for this.
 
terraform plan -out "out.plan"
 
clip_image023.png
 
Everything looks good. 4 items are going to be created. Now its time to actually apply the configuration. To do that just run:
 
terraform apply "out.plan"
 
Its going to take some time to build everything. Maybe 20 minutes or more. You might want to go get a cup of tea.
 
clip_image024.png
 

Configuring Kubernetes RBAC

 
That's the cluster deployed! Now its time for us to configure RBAC. To do this we need to create Cluster Role Binding and a Cluster Role using a yaml file. But first we need to connect to Kubernetes cluster as an admin. Use the following command to do that. Just change the resource group and name to match yours.
 
az aks get-credentials --resource-group pixelrobots-tst-aks --name pixelrobots-tst-aks --admin
 
clip_image025.png
 
In the Git repo under the k8s folder you will find two yaml files one to add a user the other for a group. The user one is easy. You just change the email address at the bottom. For the group one you will need to go into Azure AD and get the Group Object ID . Once you have the .yaml file you want to use ready. Make sure your in the directory with the files and then type the following to apply it.
 
kubectl apply -f rbac-aad-group.yaml

 
clip_image026.png
 

Connect to the cluster using RBAC

 
Now that we have configured the cluster for RBAC its time to connect to it. Lets get some non admin credentials first.
 
az aks get-credentials --resource-group pixelrobots-tst-aks --name pixelrobots-tst-aks

 
Lets use the kubectl to see what nodes we have.
 
kubectl get nodes

 
You will notice it is asking us to sign in to the azure portal. Go ahead and do it.
 
clip_image027.png
 
As you can see you can see the two nodes in the cluster. And that's it. You now have an RBAC AKS cluster. If you have any questions please reach out.