Azure Cli Create VMS With Loadbalancer

Azure

Azure CLI Commands to Create Resources

Step 1: Create a Resource Group

# Create a new resource group in the specified location
az group create --name az-uat-poc-hubo --location uaenorth

Step 2: Create a Virtual Network and Subnet

# Create a virtual network with a subnet
az network vnet create \
    --resource-group az-uat-poc-hubo \
    --name nl-uat-vnet \
    --address-prefix 10.0.0.0/16 \
    --subnet-name nl-uat-subnet \
    --subnet-prefix 10.0.1.0/24

Step 3: Create Network Interfaces (NICs)

# Loop through and create 4 NICs, each associated with the subnet
for i in {1..4}; do
    az network nic create \
        --resource-group az-uat-poc-hubo \
        --name nl-uat-nic-$i \
        --vnet-name nl-uat-vnet \
        --subnet nl-uat-subnet
done

Step 4: Create a Public IP Address for the Load Balancer

# Create a static public IP for the load balancer
az network public-ip create \
    --resource-group az-uat-poc-hubo \
    --name nl-uat-pip \
    --sku Standard \
    --allocation-method Static

Step 5: Create a Load Balancer

# Create a load balancer with the public IP created earlier
az network lb create \
    --resource-group az-uat-poc-hubo \
    --name nl-uat-lb \
    --sku Standard \
    --frontend-ip-name PublicIPAddress \
    --public-ip-address nl-uat-pip

Step 6: Create a Backend Address Pool for the Load Balancer

# Define a backend pool for the load balancer
az network lb address-pool create \
    --resource-group az-uat-poc-hubo \
    --lb-name nl-uat-lb \
    --name nl-uat-backend-lb-test

Step 7: Create Inbound NAT Rules for the Load Balancer

# Create NAT rules to allow SSH access to each VM through the load balancer
for i in {1..4}; do
    az network lb inbound-nat-rule create \
        --resource-group az-uat-poc-hubo \
        --lb-name nl-uat-lb \
        --name nl-uat-nat-rule-$i \
        --protocol Tcp \
        --frontend-port $((5000 + i)) \
        --backend-port 22 \
        --frontend-ip-name PublicIPAddress
done

Step 8: Associate NICs with the Load Balancer Backend Pool

# Link each NIC to the load balancer's backend pool
for i in {1..4}; do
    az network nic ip-config address-pool add \
        --resource-group az-uat-poc-hubo \
        --nic-name nl-uat-nic-$i \
        --ip-config-name ipconfig1 \
        --lb-name nl-uat-lb \
        --address-pool nl-uat-testlb
done

Step 9: Create Virtual Machines and Associate NICs

# Create 4 VMs, each associated with a NIC
for i in {1..4}; do
    az vm create \
        --resource-group az-uat-poc-hubo \
        --name nl-uat-vm-$i \
        --size Standard_B2s \
        --nics nl-uat-nic-$i \
        --image Debian:debian-11:11-backports-gen2:latest \
        --admin-username azureuser \
        --ssh-key-values ~/.ssh/id_rsa.pub
done

Step 10: List Resources in the Resource Group

# Display all resources in the resource group
az resource list --resource-group az-uat-poc-hubo --output table

Step 11: Retrieve the Public IP Address of the Load Balancer

# Get the public IP of the load balancer
az network public-ip show --resource-group az-uat-poc-hubo --name nl-uat-pip --query "ipAddress" --output table

Step 12: Retrieve Private IP Addresses of NICs

# Fetch the private IPs of all NICs
for i in {1..4}; do
    az network nic show \
        --resource-group az-uat-poc-hubo \
        --name nl-uat-nic-$i \
        --query "ipConfigurations[0].privateIpAddress" \
        --output tsv
done

Step 13: Create a Network Security Group (NSG) for Each VM

# Create NSGs for each VM
for i in {1..4}; do
    az network nsg create \
        --resource-group az-uat-poc-hubo \
        --name nl-uat-vm-$i-nsg
done

Step 14: Add NSG Rules to Allow SSH and HTTP

# Add rules to allow SSH and HTTP traffic
for i in {1..4}; do
    az network nsg rule create \
        --resource-group az-uat-poc-hubo \
        --nsg-name nl-uat-vm-$i-nsg \
        --name Allow-SSH \
        --priority 1000 \
        --direction Inbound \
        --access Allow \
        --protocol Tcp \
        --destination-port-ranges 22

    az network nsg rule create \
        --resource-group az-uat-poc-hubo \
        --nsg-name nl-uat-vm-$i-nsg \
        --name Allow-HTTP \
        --priority 1001 \
        --direction Inbound \
        --access Allow \
        --protocol Tcp \
        --destination-port-ranges 80
done

Step 15: Associate NSG with each NIC

for i in {1..4}; do
    az network nic update \
        --resource-group az-uat-poc-hubo \
        --name nl-uat-nic-$i \
        --network-security-group nl-uat-vm-$i-nsg
done

Step 16: Configure Load Balancer Health Probe and Rules

# Configure health probe for the load balancer
az network lb probe create \
    --resource-group az-uat-poc-hubo \
    --lb-name nl-uat-lb \
    --name nginx-health-probe \
    --protocol Tcp \
    --port 80 \
    --interval 5 \
    --threshold 2

az network lb rule create \
    --resource-group az-uat-poc-hubo \
    --lb-name nl-uat-lb \
    --name nginx-lb-rule \
    --protocol Tcp \
    --frontend-port 80 \
    --backend-port 80 \
    --frontend-ip-name PublicIPAddress \
    --backend-pool-name nl-uat-testlb \
    --probe-name nginx-health-probe

Test Load Balancer

# Check the LB Public IP
az network public-ip show \
    --resource-group az-uat-poc-hubo \
    --name nl-uat-pip \
    --query "ipAddress" \
    --output table
# Verify Load Balancer Configuration
az network lb show --resource-group az-uat-poc-hubo --name nl-uat-lb --output table
# for i in {1..20}; do curl -sk https://ngx.azureapp.org || echo "Request $i failed";done
<h1>Hostname: nl-uat-vm-3</h1>
<h1>Hostname: nl-uat-vm-2</h1>
<h1>Hostname: nl-uat-vm-4</h1>
<h1>Hostname: nl-uat-vm-3</h1>
<h1>Hostname: nl-uat-vm-3</h1>
<h1>Hostname: nl-uat-vm-3</h1>
<h1>Hostname: nl-uat-vm-3</h1>
<h1>Hostname: nl-uat-vm-2</h1>
<h1>Hostname: nl-uat-vm-2</h1>
<h1>Hostname: nl-uat-vm-4</h1>
<h1>Hostname: nl-uat-vm-1</h1>
<h1>Hostname: nl-uat-vm-3</h1>
<h1>Hostname: nl-uat-vm-2</h1>
<h1>Hostname: nl-uat-vm-4</h1>
<h1>Hostname: nl-uat-vm-3</h1>
<h1>Hostname: nl-uat-vm-3</h1>
<h1>Hostname: nl-uat-vm-1</h1>
<h1>Hostname: nl-uat-vm-2</h1>
<h1>Hostname: nl-uat-vm-4</h1>
<h1>Hostname: nl-uat-vm-1</h1>