Need help on Azure LoadBalancer to extract frontend to backend servers mapping

Givary-MSFT 34,546 Reputation points Microsoft Employee
2024-12-30T10:36:42.9966667+00:00

I have an external azure loadbalancer where i have a lot of loadbalancing rules with multiple public frontend ip address and multiple backend pool. I need to patch some servers on load balancer for that purpose i need help as how can i find which backend servers is mapped with which front end ip address. Is there a simpler way to get this mapping. Any script which can perform this task ?  

Azure Virtual Network
Azure Virtual Network
An Azure networking service that is used to provision private networks and optionally to connect to on-premises datacenters.
2,580 questions
Azure Load Balancer
Azure Load Balancer
An Azure service that delivers high availability and network performance to applications.
467 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,713 questions
0 comments No comments
{count} votes

Accepted answer
  1. UJTyagi-MSFT 390 Reputation points Microsoft Employee
    2024-12-30T11:47:23.1433333+00:00

    Hi @Givary-MSFT ,

    I understand that extracting this information from the portal could be challenging, however we may extract this information from the Json configuration of the Azure load balancer and using Powershell on the JSON output.

    • First to load the configuration of the Azure Load balancer go to Loadbalancer on Azure Portal and load the JSON View as shown below

    User's image

    • Once JSON configuration is loaded, save the configuration and assign it to a JSON Variable $jsonConfig.

    Below the sample LB configuration

    # Sample JSON configuration of the load balancer
    This configuration includes:
    - Four frontend IP configurations, each with a different public IP address.
    - Four backend address pools, each with different VM NICs.
    - Four load balancing rules, each associated with a different frontend IP and backend pool.
    - A health probe to check the health of the backend instances.
    
    # Sample JSON configuration of the load balancer
    $jsonConfig = @'
    {
      "name": "myLoadBalancer",
      "location": "eastus",
      "properties": {
        "frontendIPConfigurations": [
          {
            "name": "LoadBalancerFrontEnd1",
            "properties": {
              "publicIPAddress": {
                "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/publicIPAddresses/{public-ip1-name}"
              }
            }
          },
          {
            "name": "LoadBalancerFrontEnd2",
            "properties": {
              "publicIPAddress": {
                "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/publicIPAddresses/{public-ip2-name}"
              }
            }
          },
          {
            "name": "LoadBalancerFrontEnd3",
            "properties": {
              "publicIPAddress": {
                "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/publicIPAddresses/{public-ip3-name}"
              }
            }
          },
          {
            "name": "LoadBalancerFrontEnd4",
            "properties": {
              "publicIPAddress": {
                "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/publicIPAddresses/{public-ip4-name}"
              }
            }
          }
        ],
        "backendAddressPools": [
          {
            "name": "myBackendPool1",
            "properties": {
              "backendIPConfigurations": [
                {
                  "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/networkInterfaces/{nic1-name}/ipConfigurations/ipconfig1"
                },
                {
                  "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/networkInterfaces/{nic2-name}/ipConfigurations/ipconfig1"
                }
              ]
            }
          },
          {
            "name": "myBackendPool2",
            "properties": {
              "backendIPConfigurations": [
                {
                  "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/networkInterfaces/{nic3-name}/ipConfigurations/ipconfig1"
                },
                {
                  "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/networkInterfaces/{nic4-name}/ipConfigurations/ipconfig1"
                }
              ]
            }
          },
          {
            "name": "myBackendPool3",
            "properties": {
              "backendIPConfigurations": [
                {
                  "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/networkInterfaces/{nic5-name}/ipConfigurations/ipconfig1"
                },
                {
                  "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/networkInterfaces/{nic6-name}/ipConfigurations/ipconfig1"
                }
              ]
            }
          },
          {
            "name": "myBackendPool4",
            "properties": {
              "backendIPConfigurations": [
                {
                  "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/networkInterfaces/{nic7-name}/ipConfigurations/ipconfig1"
                },
                {
                  "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/networkInterfaces/{nic8-name}/ipConfigurations/ipconfig1"
                }
              ]
            }
          }
        ],
        "loadBalancingRules": [
          {
            "name": "rule1",
            "properties": {
              "frontendIPConfiguration": {
                "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/loadBalancers/myLoadBalancer/frontendIPConfigurations/LoadBalancerFrontEnd1"
              },
              "backendAddressPool": {
                "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/loadBalancers/myLoadBalancer/backendAddressPools/myBackendPool1"
              },
              "protocol": "Tcp",
              "frontendPort": 80,
              "backendPort": 80,
              "enableFloatingIP": false,
              "idleTimeoutInMinutes": 4,
              "loadDistribution": "Default",
              "probe": {
                "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/loadBalancers/myLoadBalancer/probes/probe1"
              }
            }
          },
          {
            "name": "rule2",
            "properties": {
              "frontendIPConfiguration": {
                "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/loadBalancers/myLoadBalancer/frontendIPConfigurations/LoadBalancerFrontEnd2"
              },
              "backendAddressPool": {
                "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/loadBalancers/myLoadBalancer/backendAddressPools/myBackendPool2"
              },
              "protocol": "Tcp",
              "frontendPort": 443,
              "backendPort": 443,
              "enableFloatingIP": false,
              "idleTimeoutInMinutes": 4,
              "loadDistribution": "Default",
              "probe": {
                "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/loadBalancers/myLoadBalancer/probes/probe2"
              }
            }
          },
          {
            "name": "rule3",
            "properties": {
              "frontendIPConfiguration": {
                "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/loadBalancers/myLoadBalancer/frontendIPConfigurations/LoadBalancerFrontEnd3"
              },
              "backendAddressPool": {
                "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/loadBalancers/myLoadBalancer/backendAddressPools/myBackendPool3"
              },
              "protocol": "Tcp",
              "frontendPort": 8080,
              "backendPort": 8080,
              "enableFloatingIP": false,
              "idleTimeoutInMinutes": 4,
              "loadDistribution": "Default",
              "probe": {
                "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/loadBalancers/myLoadBalancer/probes/probe3"
              }
            }
          },
          {
            "name": "rule4",
            "properties": {
              "frontendIPConfiguration": {
                "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/loadBalancers/myLoadBalancer/frontendIPConfigurations/LoadBalancerFrontEnd4"
              },
              "backendAddressPool": {
                "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/loadBalancers/myLoadBalancer/backendAddressPools/myBackendPool4"
              },
              "protocol": "Tcp",
              "frontendPort": 8443,
              "backendPort": 8443,
              "enableFloatingIP": false,
              "idleTimeoutInMinutes": 4,
              "loadDistribution": "Default",
              "probe": {
                "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/loadBalancers/myLoadBalancer/probes/probe4"
              }
            }
          }
        ],
        "probes": [
          {
            "name": "probe1",
            "properties": {
              "protocol": "Http",
              "port": 80,
              "requestPath": "/healthcheck1",
              "intervalInSeconds": 5,
              "numberOfProbes": 2
            }
          },
          {
            "name": "probe2",
            "properties": {
              "protocol": "Http",
              "port": 443,
              "requestPath": "/healthcheck2",
              "intervalInSeconds": 5,
              "numberOfProbes": 2
            }
          },
          {
            "name": "probe3",
            "properties": {
              "protocol": "Http",
              "port": 8080,
              "requestPath": "/healthcheck3",
              "intervalInSeconds": 5,
              "numberOfProbes": 2
            }
          },
          {
            "name": "probe4",
            "properties": {
              "protocol": "Http",
              "port": 8443,
              "requestPath": "/healthcheck4",
              "intervalInSeconds": 5,
              "numberOfProbes": 2
            }
          }
        ]
      }
    }
    '@
    
    • Open PowerShell in your machine and run below command
    # Convert JSON string to PowerShell object
    $config = $jsonConfig | ConvertFrom-Json
    # Create a map of frontend public IP to backend pool
    $ipToBackendPool = @{}
    # Extract frontend IP configurations
    $frontendIPs = @{}
    foreach ($item in $config.properties.frontendIPConfigurations) {
        Write-Output "Processing frontend IP configuration: $($item.name)"
        $frontendIPs[$item.name] = $item.properties.publicIPAddress.id
    }
    # Debugging output for frontend IPs
    Write-Output "Frontend IPs:"
    $frontendIPs
    # Extract backend pools
    $backendPools = @{}
    foreach ($item in $config.properties.backendAddressPools) {
        $backendPools[$item.name] = $item.properties.backendIPConfigurations
    }
    # Debugging output for backend pools
    Write-Output "Backend Pools:"
    $backendPools
    # Map frontend IP to backend pool using load balancing rules
    foreach ($rule in $config.properties.loadBalancingRules) {
        $frontendIPName = $rule.properties.frontendIPConfiguration.id.Split('/')[-1]
        $backendPoolName = $rule.properties.backendAddressPool.id.Split('/')[-1]
        
        $frontendIP = $frontendIPs[$frontendIPName]
        $backendPool = $backendPools[$backendPoolName]
        
        if ($frontendIP -and $backendPool) {
            if (-not $ipToBackendPool.ContainsKey($frontendIP)) {
                $ipToBackendPool[$frontendIP] = @()
            }
            $ipToBackendPool[$frontendIP] += $backendPool
        }
    }
    # Debugging output for IP to Backend Pool map
    Write-Output "IP to Backend Pool Map:"
    $ipToBackendPool
    # Convert the map to JSON and print it
    $ipToBackendPoolJson = $ipToBackendPool | ConvertTo-Json -Depth 4
    Write-Output "IP to Backend Pool JSON:"
    Write-Output $ipToBackendPoolJson
    
    • Sample Output
    {
        "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/publicIPAddresses/{public-ip2-name}":  [
                 {
                     "id":  "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/networkInterfaces/{nic3-name}/ipConfigurations/ipconfig1"
                 },
                 {
                     "id":  "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/networkInterfaces/{nic4-name}/ipConfigurations/ipconfig1"
                 }
             ],
        "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/publicIPAddresses/{public-ip3-name}":  [
                 {
                     "id":  "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/networkInterfaces/{nic5-name}/ipConfigurations/ipconfig1"
                 },
                 {
                     "id":  "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/networkInterfaces/{nic6-name}/ipConfigurations/ipconfig1"
                 }
             ],
        "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/publicIPAddresses/{public-ip4-name}":  [
                 {
                     "id":  "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/networkInterfaces/{nic7-name}/ipConfigurations/ipconfig1"
                 },
                 {
                     "id":  "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/networkInterfaces/{nic8-name}/ipConfigurations/ipconfig1"
                 }
             ],
        "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/publicIPAddresses/{public-ip1-name}":  [
                 {
                     "id":  "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/networkInterfaces/{nic1-name}/ipConfigurations/ipconfig1"
                 },
                 {
                     "id":  "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Network/networkInterfaces/{nic2-name}/ipConfigurations/ipconfig1"
                 }
             ]
    }
    

    If the below answer addressed your query, please don’t forget to click "Accept the answer" and Up-Vote for the same, which might be beneficial to other community members reading this thread. And, if you have any further query do let us know.

    Thanks,

    Ujjawal Tyagi

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.