Creating the VNet and NSG – Configuring Load Balancing
Creating the VNet and NSG
This sectionwill deal with the creation of your VNet and NSG; run the following:
- We now need to create the VNet to deploy the VMs too, with the following code:
#Create a virtual network
az network vnet create --resource-group AZ104-PublicLoadBalancer
–location westeurope --name PacktVnet
–subnet-name PacktSubnet
- Next, we will create an NSG. It is a requirement for a standard load balancer that the VMs in the backend have NICs that are placed in an NSG. For standard IPs or resource SKUs in Azure, an NSG is what defines the traffic that is allowed or not. The default configuration for NSGs is to disallow all traffic (that is, the NSG is configured for whitelisting). We need to create the NSG to define the inbound connections to the VNet as follows:
az network nsg create `
–resource-group AZ104-PublicLoadBalancer ` –name PacktNetworkSecurityGroup
- To allow inbound connections through port 80, create an NSG rule as follows:
#Create a Network Security Group rule az network nsg rule create `
–resource-group AZ104-PublicLoadBalancer
–nsg-name PacktNetworkSecurityGroup --name PacktNetworkSecurityGroupRuleHTTP
–protocol tcp --direction inbound
–source-address-prefix ‘‘ --source-port-range '*'
–destination-address-prefix ‘‘ --destination-port-range 80
–access allow `
–priority 200
- We need to create two network interfaces and associate them with the NSG and the public IP address. To create the NICs, run the code as follows:
#Create NICs
for ($i = 1; $i -le 2; $i++){
az network nic create `
–resource-group AZ104-PublicLoadBalancer --name PacktNic$i
–vnet-name PacktVnet `
–subnet PacktSubnet `
–network-security-group PacktNetworkSecurityGroup --lb-name PacktPublicLoadBalancer
–lb-address-pools PacktBackEndPool
}
You now have your public load balancer deployed and you need to configure your backend servers. We will do this next.