mountainss Cloud and Datacenter Management Blog

Microsoft Hybrid Cloud blogsite about Management


Leave a comment

Creating VM Cluster on Azure #Cloud with Terraform #IaC #Azure #Terraform #Linux #Winserv

Type az and you should see this Azure CLI

Type Terraform and you should see the terraform commands

 

Install and configure Terraform to provision VMs and other infrastructure into Azure

Before you begin with Terraform and deploying your solution to Microsoft Azure you have to install Azure CLI and Terraform for your OS.

In the following step-by-step guide we will deploy a VM Cluster with Terraform into Microsoft Azure Cloud Services.

First we open Powershell in Administrator mode :

You should have your Terraform script ready.

It’s great to edit your Terraform script in Visual Studio Code

Create a Terraform configuration file
In this section, you create a file that contains resource definitions for your infrastructure.
Create a new file named main.tf.
Copy following sample resource definitions into the newly created main.tf file:


resource “azurerm_resource_group” “test” {
name = “acctestrg”
location = “West US 2”
}

resource “azurerm_virtual_network” “test” {
name = “acctvn”
address_space = [“10.0.0.0/16”]
location = “${azurerm_resource_group.test.location}”
resource_group_name = “${azurerm_resource_group.test.name}”
}

resource “azurerm_subnet” “test” {
name = “acctsub”
resource_group_name = “${azurerm_resource_group.test.name}”
virtual_network_name = “${azurerm_virtual_network.test.name}”
address_prefix = “10.0.2.0/24”
}

resource “azurerm_public_ip” “test” {
name = “publicIPForLB”
location = “${azurerm_resource_group.test.location}”
resource_group_name = “${azurerm_resource_group.test.name}”
public_ip_address_allocation = “static”
}

resource “azurerm_lb” “test” {
name = “loadBalancer”
location = “${azurerm_resource_group.test.location}”
resource_group_name = “${azurerm_resource_group.test.name}”

frontend_ip_configuration {
name = “publicIPAddress”
public_ip_address_id = “${azurerm_public_ip.test.id}”
}
}

resource “azurerm_lb_backend_address_pool” “test” {
resource_group_name = “${azurerm_resource_group.test.name}”
loadbalancer_id = “${azurerm_lb.test.id}”
name = “BackEndAddressPool”
}

resource “azurerm_network_interface” “test” {
count = 2
name = “acctni${count.index}”
location = “${azurerm_resource_group.test.location}”
resource_group_name = “${azurerm_resource_group.test.name}”

ip_configuration {
name = “testConfiguration”
subnet_id = “${azurerm_subnet.test.id}”
private_ip_address_allocation = “dynamic”
load_balancer_backend_address_pools_ids = [“${azurerm_lb_backend_address_pool.test.id}”]
}
}

resource “azurerm_managed_disk” “test” {
count = 2
name = “datadisk_existing_${count.index}”
location = “${azurerm_resource_group.test.location}”
resource_group_name = “${azurerm_resource_group.test.name}”
storage_account_type = “Standard_LRS”
create_option = “Empty”
disk_size_gb = “1023”
}

resource “azurerm_availability_set” “avset” {
name = “avset”
location = “${azurerm_resource_group.test.location}”
resource_group_name = “${azurerm_resource_group.test.name}”
platform_fault_domain_count = 2
platform_update_domain_count = 2
managed = true
}

resource “azurerm_virtual_machine” “test” {
count = 2
name = “acctvm${count.index}”
location = “${azurerm_resource_group.test.location}”
availability_set_id = “${azurerm_availability_set.avset.id}”
resource_group_name = “${azurerm_resource_group.test.name}”
network_interface_ids = [“${element(azurerm_network_interface.test.*.id, count.index)}”]
vm_size = “Standard_DS1_v2”

# Uncomment this line to delete the OS disk automatically when deleting the VM
# delete_os_disk_on_termination = true

# Uncomment this line to delete the data disks automatically when deleting the VM
# delete_data_disks_on_termination = true

storage_image_reference {
publisher = “Canonical”
offer = “UbuntuServer”
sku = “16.04-LTS”
version = “latest”
}

storage_os_disk {
name = “myosdisk${count.index}”
caching = “ReadWrite”
create_option = “FromImage”
managed_disk_type = “Standard_LRS”
}

# Optional data disks
storage_data_disk {
name = “datadisk_new_${count.index}”
managed_disk_type = “Standard_LRS”
create_option = “Empty”
lun = 0
disk_size_gb = “1023”
}

storage_data_disk {
name = “${element(azurerm_managed_disk.test.*.name, count.index)}”
managed_disk_id = “${element(azurerm_managed_disk.test.*.id, count.index)}”
create_option = “Attach”
lun = 1
disk_size_gb = “${element(azurerm_managed_disk.test.*.disk_size_gb, count.index)}”
}

os_profile {
computer_name = “hostname”
admin_username = “testadmin”
admin_password = “Password1234!”
}

os_profile_linux_config {
disable_password_authentication = false
}

tags {
environment = “staging”
}
}


Type : terraform init

You should see this screen.

Type : az login

We now logging into Microsoft Azure subscription.

https://microsoft.com/devicelogin

Insert the code from your Powershell screen.

Now we have the Terraform INIT running and we are connected to our Azure Subscription 😉

Type : terraform plan

It will refreshing the state and getting ready for deployment.

Type : terraform apply

and then type : yes <enter>

Terraform is now creating the azure resources

Azure resource group acctestrg is made

Terraform deployment VM Cluster on Azure is Ready 😉

Azure VM Cluster is running.

When you want to remove the complete Azure VM Cluster with terraform, it’s really easy :

Type : terraform destroy

and then type : yes <enter>

Azure resources are being deleted via terraform script

Terraform destroyed the Azure VM Cluster


All Azure Resources of the VM Cluster are removed.

Hope this step-by-step guide deploying infrastructure as Code with terraform will help you with your own Cloud solutions in Microsoft azure.

Ps. don’t forget to install Visual Studio Code Azure Terraform extension and play !

#MVPbuzz


Advertisements


Leave a comment

Microsoft Azure #CloudShell Overview with #Bash CLI 2.0 and #Powershell #Azure #DevOps

Azure Powershell in the Portal

Azure Cloud Shell is an interactive, browser-accessible shell for managing Azure resources. It gives you the flexibility of choosing the shell experience that best suits the way you work. Linux users can opt for a Bash experience, while Windows users can opt for PowerShell.

At the left corner you can change from Powershell Cmd to Bash

Bash with Azure CLI 2.0 

Features
Browser-based shell experience
Cloud Shell enables access to a browser-based command-line experience built with Azure management tasks in mind. Leverage Cloud Shell to work untethered from a local machine in a way only the cloud can provide.

Choice of preferred shell experience
Azure Cloud Shell gives you the flexibility of choosing the shell experience that best suits the way you work. Linux users can opt for a Bash experience, while Windows users can opt for PowerShell.

Pre-configured Azure workstation
Cloud Shell comes pre-installed with popular command-line tools and language support so you can work faster.

View the full tooling list for Bash experience and PowerShell experience.

Automatic authentication
Cloud Shell securely authenticates automatically on each session for instant access to your resources through the Azure CLI 2.0 or Azure PowerShell cmdlets.

Connect your Azure File storage
Cloud Shell machines are temporary and as a result require an Azure Files share to be mounted as clouddrive to persist your $Home directory. On first launch Cloud Shell prompts to create a resource group, storage account, and file share on your behalf. This is a one-time step and will be automatically attached for all sessions. A single file share can be mapped and will be used by both Bash and PowerShell in Cloud Shell.

I like Microsoft Azure CLI 2.0 in the Cloud Shell and here you find a CLI 2.0 command line reference guide.

In the following step-by-step examples ( in Bash and Powershell ) you will see how easy it is to make an Azure Container Instance in the Cloud.
we begin with starting Bash Shell from the Azure Portal.

When you run Cloud Shell for the first time it will create a Cloud Drive of 5GB.

Cloud Shell machines are temporary and as a result require an Azure Files share to be mounted as clouddrive to persist your $Home directory. On first launch Cloud Shell prompts to create a resource group, storage account, and file share on your behalf. This is a one-time step and will be automatically attached for all sessions. A single file share can be mapped and will be used by both Bash and PowerShell in Cloud Shell.

Create Storage for your CloudDrive

A locally-redundant storage (LRS) account and Azure Files share can be created on your behalf. The Azure Files share will be used for both Bash and PowerShell environments if you choose to use both. Regular storage costs apply.

  • Cloud Shell runs on a temporary machine provided on a per-session, per-user basis
  • Cloud Shell times out after 20 minutes without interactive activity
  • Cloud Shell can only be accessed with a file share attached
  • Cloud Shell uses a the same file share for both Bash and PowerShell
  • Cloud Shell is assigned one machine per user account
  • Permissions are set as a regular Linux user (Bash)

az container create -h

With this command you see the options to create a Container Instance in Microsoft Azure Cloud.

You see also some examples to learn from

az group create –name MyResourceGroup –location eastus

We now created a resource group in the East US location of Azure for our Container.

az container create –name mycontainer –image microsoft/aci-helloworld –resource-group MyResourceGroup –ip-address public

We now Created an Azure Container Instance.

ProvisioningState Succeeded

az container list –output table

The result of your Azure Container Instance

To see how your Azure Container Instance is doing, you can read the logs.

az container logs –name mycontainer –resource-group MyResourceGroup

When your Azure Container Instance was for testing, you can delete the instance by :

az container delete –name mycontainer  –resource-group MyResourceGroup

az container list –output table

I got two Azure Container Instances running, and now you can see that mycontainer instance is deleted.
This was just an simple example by using Bash in the Azure Portal with CLI 2.0 commands. Of course there are a lot of Azure Solutions to play with:

In the following step-by-step example we will use Azure Powershell from the portal instead of Bash :

$PSVersionTable.PSVersion

Microsoft Azure Powershell via the Portal is using version 5.1 Build 14393 Revision 1480 in my example.
Of course there is also an Azure Powershell reference guide online

New-AzureRmResourceGroup -Name MyResourceGroup -Location EastUS

We now have created the Resource Group with Azure Powershell for the Container Instance.

New-AzureRmContainerGroup -ResourceGroupName MyResourceGroup -name mycontainer -image microsoft/iis:nanoserver -OsType Windows -IpAddressType Public

Get-AzureRmContainerGroup -ResourceGroupName MyResourceGroup -Name mycontainer

You will see that the New Azure Container Instance is provisioned.

Get-AzureRmContainerGroup

And the IIS is running in the Azure Container Instance.

Of course you don’t have your laptop always with you, but Microsoft Azure has an Awesome Mobile App to work with.

 

Mobile Azure Powershell via the App

Mobile Azure Bash via the App

Here you can get the Microsoft Azure Mobile App

Here are some handy links to use with Microsoft Azure Cloud Shell :

Deploy resources with Resource Manager templates and Azure CLI

Deploy resources with Resource Manager templates and Azure PowerShell

Overview of Azure Cloud Shell (Preview)

Microsoft Azure Cloud Roadmap