Custom BYO VNET/RT for Kubenet - Route Table Subnet Association
Hi,
When integrating a AKS cluster with a custom VNET we are seeing strange networking behaviour - if the cluster has 2 or more nodes then inter-node networking does not appear to work and pods cannot communicate with one another.
Test scenario:
- Using terraform (v.0.11.7)
- AzureRM provider (v1.6.0)
- Cluster provisioned using azurerm_kubernetes_cluster resource
- 2 worker nodes deployed into custom subnet (in our case a /24 within a /24 VNET)
The cluster builds fine but it appears that whilst both minions are present in the route table within the MC_* resource group, the custom subnet is not associated; if we manually make the association networking behaves as expected and stuff starts working.
The next problem is that when we re-execute Terraform - as the route_table_id is not exported from the azurerm_kubernetes_cluster resource - we overwrite this manual association of subnet to route table.
As a workaround we have automated the association of subnet to routetable and added this to our subnet resource:
lifecycle {
ignore_changes = ["route_table_id"]
}
+1, looks like we've ran into this too.
@moomzni ,
the custom subnet is not associated; if we manually make the association networking behaves as expected and stuff starts working.
Could you please share a workaround you found for this? What exactly you modify to let AKS with custom VNET work?
Quite a timely prompt - To work around this we needed to manually associate the routing table (inside the MC_* resource group) with the subnet.
For us this meant creating a null resource in Terraform like the below - this will only run on the initial creation of the cluster:
resource "null_resource" "fix_routetable" {
provisioner "local-exec" {
command = "az network vnet subnet update -n ${azurerm_subnet.aks_subnet.name} -g ${azurerm_resource_group.aks_resource_group.name} --vnet-name ${azurerm_virtual_network.aks_virtualnetwork.name} --route-table $(az resource list --resource-group MC_${azurerm_resource_group.aks_resource_group.name}_${azurerm_kubernetes_cluster.aks_cluster.name}_<YOUR LOCATION HERE> --resource-type Microsoft.Network/routeTables --query '[].{ID:id}' -o tsv)"
}
depends_on = ["azurerm_kubernetes_cluster.aks_cluster"]
}
Note you'll still need to add the lifecycle policy to ignore the route_table_id to where you are defining your subnet in Terraform - but this worked for us...
Great, thanks @moomzni ! I'll try this. :)
Note you'll still need to add the lifecycle policy to ignore the route_table_id to where you are defining your subnet in Terraform
What will happen or crash if I will omit that lifecycle policy? As far as I've understood, if my usecase of AKS is "deploy cluster, run tests, destroy cluster" - in this case I should not care about route table being overwritten by Terraform re-execution, but in all anothers - something will go wrong...
Not sure if manually attaching the route table is a sustainable solution. I created a cluster through the azure portal, no route table is created in the MC_* resource group.
This is also mentioned here: terraform-providers/terraform-provider-azurerm#1394 - so this might be a terraform problem.
AKS with custom vnet, created using
- Azure portal: all working as expected. No route table created.
- Terraform: a route table is create but not associated to the subnet. As a result, API calls fail with timeout for port-forward:
error: error upgrading connection: error dialing backend: dial tcp 172.21.0.5:10250: getsockopt: connection timed outAs workaround, manually associating the subnet fixes the problem.
Azure portal: all working as expected. No route table created.
If you have chosen an "advanced networking" in portal - Terraform currently does not support this. Terraform uses some old version of Azure API, as I've been told by Azure support, so we just need to wait a fix or implementation of new APIs from HashiCorp as I think.
I tried to read related issues but didn't get clear picture of status related to this issue. So does someone know is this fixed already in some version or if not does anyone any idea of eta?
I can confirm the issue is still there, we had internode network issues and they were gone when manually assigning the custom subnet to the routing table and NSG.
This is also a major problem for us. I already opened a support ticket for this: 119012225001304
This is also an issue when creating the cluster via the Azure CLI. Even when the node count is one a route table is created but it is never associated to the subnet. As soon as the cluster is scaled up the issue causes major problems. As far as I can see the problem cannot be traced using the deployment logs.
Thanks for the patience folks, we are working on resolving this within AKS in a few stages. In the initial stage we will ensure the RT and NSG are associated automatically IF there are no existing RTs/NSGs on the subnet. This is to avoid introducing new conflicts which can cause trouble in clusters. ETA for this first described fix should be released in the next 3-4 weeks at the latest.
@jluk How can we use AKS with a hub spoke model atm when you bring your own routetable with a vnet / subnet to redirect the traffic over to a azure firewall?
@ekarlso can you give more specific details on your network layout?
In general, we ask customer to bring in a subnet (reserved for AKS) which is not used by other components in the infrastructure. We also ask the various settings used in custom vnet, such as service Cidr, pod Cidr, etc are also reserved to avoid conflict. This whole should help the customer to fit in the AKS cluster into their infrastructure environment while not introducing network related issues.
If somehow you have a case that require to pre-define route table and nsg and associate them to the subnet used in AKS, can you share it? My guess is some merge resolve is needed, but it is really case by case, and might not resolve when some conflicts happens. For example, if a route is already defined to route traffic from 10.244.0.1 to 10.240.0.5 in the existing RT by customer, our K8S create logic may apply another Kubenet based routing from 10.244.0.1 to 10.240.0.1. I think this will end up with an un-resolvable conflict.
@JunSun17 We are routing all outgoing traffic via Azure Firewall basically. so 0.0.0.0 -> az firewall ip which requires us to modify the RT.
Expanding on @moomzni suggestion, here's what were doing to associate the AKS route table and network security group with the node subnet:
resource "null_resource" "assign_subnet_resources" {
provisioner "local-exec" {
command = "az network vnet subnet update --route-table $(az network route-table list --resource-group ${azurerm_kubernetes_cluster.k8s.node_resource_group} --query \"[].id | [0]\" -o tsv) --network-security-group $(az network nsg list -g ${azurerm_kubernetes_cluster.k8s.node_resource_group} --query \"[].id | [0]\" -o tsv) --ids ${azurerm_subnet.kubesubnet.id}"
}
depends_on = ["azurerm_kubernetes_cluster.main"]
}
Along with a lifecycle policy like this on the subnet resource:
resource "azurerm_subnet" "kubesubnet" {
...
lifecycle {
ignore_changes = [
"network_security_group_id",
"route_table_id",
]
}
}
Hi all,
I just ran in the same problem but in a slightly different constellation. However the root cause is, I think, very similar or the same. In my case I use ARM templates to deploy a vNet and to deploy an AKS to that previously created vNet respectively vSubnet. Now I noticed that when the templates are deployed for the first time the Routing Table is assigned to the subnet in which the AKS is deployed. So everything works as desired. But then when deploying the ARM templates a second time the Routing Table will be removed again from the subnet wich breaks of course the functionality of the AKS Cluster. Actually, I think, this is how the ARM templates actually work. Because the Routing Table is not specified as a property of the subnet in the template the deployment removes the Routing Table assignment. So actually the Routing Table should be defined in the ARM template which creates the vNet and vSubnet. However that is not possible because the Routing Table is automatically created by AKS after the vNet is already deployed.
So what is the current plan with this issue? Are there any plans for an actual fix beside the workarounds running some scripts after the deployment?
I think the main problem here is in the logic how the update of the vNet works when ARM or Terraform code is redeployed after the vNet already exists. Meaning it is more an problem with ARM/Azure REST APIs than an actual problem in AKS. Could that be?
Thx, Jonas
@J0F3 if your ARM templates re-deploy vnet/subnet, yes currently there will be no attempt to re-associate the AKS created route table to the new subnet. This association is only done once when the AKS cluster is created, and the association is to the subnet given in the AKS data model when the cluster is being created.
If the subnet is re-deployed, it might introduce downtime since AKS VMs will lose network during that period. If the subnet IP ranges are changed, it will introduce more issues since the subnet now is different from what the cluster thought it should be,
If the subnet is not changed during re-deployment, I think you can do a manual association of the route table to the new subnet. Hope it helps.
Thank you @JunSun17 Yes the manual re-association of the route table would work. However it is very hack and during the deployment the ASK is in a bad state until e.g. a script runs which does the association again. For me the logic of the vNet resource provider is also not really logical. When deploying something in "incremental" mode I would expect that anything existing is not changed. But for the vSubnet for example everything that is not specified in the template is obviously removed.
@J0F3 I think the gap is when re-deploy vnet/subnet, it does not first read the current state of the vnet/subnet and then apply the incremental changes. Therefore it is not aware of changes happened after the initial deployment. For your workflow, maybe some workaround can be done if you can integrate the current state bit into the new template you deploy.
+1 please guys, fix this.
Hi all As an intermediate "fix" you can deploy a linked ARM template to get the subnet association and then re-apply this every time you re-deploy the VNET via ARM template.
This is my linked template, which I use to get the current RouteTable.Id and make it ready via output:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"aksVNET": {
"defaultValue": {
"name": "aks-vnet",
"aksVNETSubnet": {
"name": "aks-vnet-nodes"
}
},
"type": "Object",
"metadata": {
"description": ""
}
}
},
"variables": {
"vnetname": "[parameters('vNetSettings')['name']]",
"subnetname": "[parameters('vNetSettings')['aksVNETSubnet']['name']]"
},
"resources": [],
"outputs": {
"subnetroutetableid": {
"type": "String",
"value": "[if(contains(reference(resourceId(resourceGroup().name, 'Microsoft.Network/virtualNetworks/subnets', variables('vnetname'), variables('subnetname')), '2018-03-01'), 'routeTable'), reference(resourceId(resourceGroup().name, 'Microsoft.Network/virtualNetworks/subnets', variables('vnetname'), variables('subnetname')), '2018-03-01').routeTable.Id, '')]"
}
}
}
And this template leverages the template above via linking and is meant to straight forward create a new VNET with a few subnets:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"vNetSettings": {
"defaultValue": {
"name": "aks-vnet",
"addressPrefixes": [
"10.0.0.0/8"
],
"subnetAksCluster": {
"name": "aksnodes",
"CIDR": "10.110.0.0/16"
},
"subnetIngressLb": {
"name": "ingresslb",
"CIDR": "10.120.0.0/24"
},
"subnetAppGw": {
"name": "appgw",
"CIDR": "10.130.0.0/24"
}
},
"type": "Object",
"metadata": {
"description": "Settings for the vNet. (See defaultValue for properties)"
}
}
},
"variables": {
"vnetResourceAPI": "2019-04-01",
"vnetname": "[parameters('vNetSettings')['name']]",
"subnetname": "[parameters('vNetSettings')['subnetAksCluster']['name']]"
},
"resources": [
{
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "[variables('vnetResourceAPI')]",
"name": "[parameters('vNetSettings')['name']]",
"location": "[resourceGroup().location]",
"dependsOn": [
"linkedTemplate"
],
"properties": {
"addressSpace": {
"addressPrefixes": "[parameters('vNetSettings')['addressPrefixes']]"
},
"subnets": [
{
"name": "[parameters('vNetSettings')['subnetAksCluster']['name']]",
"properties": {
"addressPrefix": "[parameters('vNetSettings')['subnetAksCluster']['CIDR']]",
"routeTable": {
"id": "[reference('linkedTemplate').outputs.subnetroutetableid.value]"
}
}
},
{
"name": "[parameters('vNetSettings')['subnetIngressLb']['name']]",
"properties": {
"addressPrefix": "[parameters('vNetSettings')['subnetIngressLb']['CIDR']]"
}
},
{
"name": "[parameters('vNetSettings')['subnetAppGw']['name']]",
"properties": {
"addressPrefix": "[parameters('vNetSettings')['subnetAppGw']['CIDR']]"
}
}
]
}
},
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2018-05-01",
"name": "linkedTemplate",
"properties": {
"mode": "Incremental",
"templateLink": {
"uri": "https://micchitransfer.blob.core.windows.net/mobi/subnetroutetableid.json",
"contentVersion": "1.0.0.0"
},
"parameters": {
"vNetSettings": {
"value": "[parameters('vNetSettings')]"
}
}
}
}
]
}
I hope this helps :-)