Overview
In the post, Azure/PowerShell: Resource Groups, Resource Clean Up (Docker, ACR, Kubernetes, AKS), a demonstration on how to clean up Azure resources was given using PowerShell. The current post will present the same strategy (using the removal of an Azure resource group to clean up resources) but instead will use the Azure CLI. Bash accesses Azure using the Azure CLI. The concepts introduced in the previous post are as follows:
All source code (Azure CLI) is provided in text form in Appendix A: Source Code at the end of this post.
Azure CLI
Like Azure PowerShell, the Azure CL can be accessed using Cloud Shell which can be launched using the url, https://shell.azure.com.
Using Cloud Shell and Azure CLI a resource group can be created as follows (in location West US 2 with name rgdockerkubernetes00) using Azure CLI's az group create:
The code to remove the resource group is as follows using Azure CLI's az group delete:
It should be noted in the previous examples that the resource group was created at line 7 and the resource group removed at line 33. Any Azure objects (such as am ACR or AKS) created after line 7 and before line 33 for the same resource are cleaned up when the resource group is removed.
The code to create an ACR associated with resource group, rgdockerkubernetes00, is as follows using Azure CLI's az acr create:
The ACR will be removed when the resource group is removed.
The code to create an AKS associated with resource group, rgdockerkubernetes00, is as follows using Azure CLI's az aks create (line 17 and line 23):
The AKS will be removed when the resource group is removed. After line 29 code could be added in order experiment with Docker/Kubernetes. The ultimate invocation of Azure CLI's az group delete would insure that all Azure resources are cleaned up for the resource group.
A bit of explanation is needed of the above code. A Kubernetes cluster internally uses Linux virtual machines. These virtual machines require an SSH key in order to be accessed. Line 16 above detects if the key exists. If the key does not exist then Azure CLI's az aks create is invoked with the generate-ssh-keys parameter. When this parameter is specified no user response is required as the SSH keys are automatically created. The following text is generated when az aks create is invoked with the generate-ssh-keys parameter:
The generate-ssh-keys parameter was useful given this was a sample script. A more real world approach would be to create the SSH key backup the SSH key before creating the AKS. The following link from Microsoft, Quick steps: Create and use an SSH public-private key pair for Linux VMs in Azure., demonstrates how to create an SSH key.
Since the Azure CLI's az group delete cleans up the ACR and AKS resources, there is no need to explicitly invoke az acr delete or az aks delete.
Appendix A: Source Code
The Azure CLI source code for this post is as follows:
#!/bin/bash -x
resource_group_name='rgdockerkubernetes00'
acr_name='crdockerkubernetes00'
aks_name='ksdockerkubernetes00'
node_count=2
az group create \
--name $resource_group_name \
--location 'West US 2'
az acr create \
--resource-group $resource_group_name \
--name $acr_name \
--sku Basic
if [ -f ~/.ssh/id_rsa ]; then
az aks create \
--resource-group $resource_group_name \
--name $aks_name \
--node-count $node_count \
--attach-acr $acr_name
else
az aks create \
--resource-group $resource_group_name \
--name $aks_name \
--node-count $node_count \
--attach-acr $acr_name \
--generate-ssh-keys
fi
# Manipulate Azure resources here
az group delete \
--name $resource_group_name \
--yes
No comments :
Post a Comment