需求:后端一个ubuntu host, 在它上面起了多个containers,通过一个网卡分配多个IPs的情况下提供服务
比如:一个NIC有两个IP (10.0.0.5, 10.0.0.6, ....)
step 1: 创建vnet and subnet 10.0.0.0/24
step 2: 创建azure nlb:
$vnet = Get-AzVirtualNetwork -Name "yifei1" -ResourceGroupName "Branch"
$PrivateNetwork = $vnet.Subnets[0]
$frontend = New-AzLoadBalancerFrontendIpConfig -Name "lb_fec_cluster" -SubnetId $PrivateNetwork.Id
$backendAddressPool = New-AzLoadBalancerBackendAddressPoolConfig -Name "lb_bep_cluster"
$probe = New-AzLoadBalancerProbeConfig -Name "lb_hp_c" -Protocol "TCP" -Port 443 -IntervalInSeconds 15 -ProbeCount 2
$lbhttpsrule = New-AzLoadBalancerRuleConfig -Name "lbr_https_c" -FrontendIPConfiguration $frontend -BackendAddressPool $backendAddressPool -Probe $probe -Protocol "TCP" -FrontendPort 443 -BackendPort 443 -IdleTimeoutInMinutes 4 -LoadDistribution Default
$lb = New-AzLoadBalancer -Name "lb_cMR_c" -ResourceGroupName "Branch" -Location eastus -FrontendIpConfiguration $frontend -BackendAddressPool $backendAddressPool -Probe $probe -Sku Standard -LoadBalancingRule $lbhttpsrule
这是IPs还没有加到backendpool里. The nlb frontend IP is 10.0.0.4
Step3: 创建ubuntu 20.04 VM. 挂个公网IP,为了远程连接操作;一个NIC配置成两个IPs (in networking -> IP Configurations).
Step 4: 登陆到ubuntu:
a). python 3.8够新了
b). install docker (refer to Install Docker Engine on Ubuntu | Docker Documentation )Instructions for installing Docker Engine on Ubuntuhttps://docs.docker.com/engine/install/ubuntu/
c). 创建 image, and containers (refer to 使用 Docker 创建简单的 Web 应用 - 简书)
注意:起container用下面命令:
docker run -d -p 10.0.0.5:443:5000 --name simple-flask1 avatar
docker run -d -p 10.0.0.6:443:5000 --name simple-flask2 avatar
这样两个Container使用不同的IP,同样的端口就起来了
通过curl 10.0.0.5:443 curl 10.0.0.6:443 可以返回html文本
Step 5: Add the 2 IPs to LB backend pool
you need disassociate public IP of the ubuntu1 first, otherwise it will raise error message about public IP.
Add IPs to LB:
$Nic = Get-AzNetworkInterface -ResourceGroupName "Branch" -Name "ubuntu1999"
$Lb = Get-AzLoadBalancer -ResourceGroupName "Branch" -Name "lb_cMR_c"
$NIC.IpConfigurations[0].LoadBalancerBackendAddressPools = $LB.BackendAddressPools[0]
$NIC.IpConfigurations[1].LoadBalancerBackendAddressPools = $LB.BackendAddressPools[0]
Set-AzNetworkInterface -NetworkInterface $NIC
Step 6:
Create 2 ubuntu machines with new public Ips
log in the 2 machines, via curl 10.0.0.4:443 for many times,
you can see returned results are from different containers.