diff --git a/docs/kubernetes/features.md b/docs/kubernetes/features.md index ea10b6dd9c..5844ad951a 100644 --- a/docs/kubernetes/features.md +++ b/docs/kubernetes/features.md @@ -166,29 +166,15 @@ Per default Calico still allows all communication within the cluster. Using Kube ## Custom VNET -ACS Engine supports deploying into an existing VNET. Operators must specify the ARM path/id of Subnets for the `masterProfile` and any `agentPoolProfiles`, as well as the first IP address to use for IP static IP allocation in `firstConsecutiveStaticIP`. Additionally, to prevent source address NAT'ing within the VNET, we assign to the `vnetCidr` property in `masterProfile` the CIDR block that represents the usable address space in the existing VNET. +ACS Engine supports deploying into an existing VNET. Operators must specify the ARM path/id of Subnets for the `masterProfile` and any `agentPoolProfiles`, as well as the first IP address to use for static IP allocation in `firstConsecutiveStaticIP`. Please note that in any azure subnet, the first four and the last ip address is reserved and can not be used. Additionally, each POD now gets the IP address from the Subnet. As a result, for the master nodes, enough IP addresses (equal to `ipAddressCount`) should be available beyond `firstConsecutiveStaticIP`. By default, the `ipAddressCount` has a value of 30, and can be changed if desired. Furthermore, to prevent source address NAT'ing within the VNET, we assign to the `vnetCidr` property in `masterProfile` the CIDR block that represents the usable address space in the existing VNET. -Depending upon the size of the VNET address space, during deployment, it is possible to experience IP address assignment collision between the required Kubernetes static IPs (one each per master and one for the API server load balancer, if more than one masters) and Azure CNI-assigned dynamic IPs (one for each NIC on the agent nodes). In practice, the larger the VNET the less likely this is to happen; some detail, and then a guideline. - -First, the detail: - -* Azure CNI assigns dynamic IP addresses from the "beginning" of the subnet IP address space (specifically, it looks for available addresses starting at ".4" ["10.0.0.4" in a "10.0.0.0/24" network]) -* acs-engine will require a range of up to 16 unused IP addresses in multi-master scenarios (1 per master for up to 5 masters, and then the next 10 IP addresses immediately following the "last" master for headroom reservation, and finally 1 more for the load balancer immediately adjacent to the afore-described _n_ masters+10 sequence) to successfully scaffold the network stack for your cluster - -A guideline that will remove the danger of IP address allocation collision during deployment: - -* If possible, assign to the `firstConsecutiveStaticIP` configuration property an IP address that is near the "end" of the available IP address space in the desired subnet. - * For example, if the desired subnet is a `/24`, choose the "239" address in that network space - -In larger subnets (e.g., `/16`) it's not as practically useful to push static IP assignment to the very "end" of large subnet, but as long as it's not in the "first" `/24` (for example) your deployment will be resilient to this edge case behavior. - -Before provisioning, modify the `masterProfile` and `agentPoolProfiles` to match the above requirements, with the below being a representative example: +See below profiles as an example: ```json "masterProfile": { ... "vnetSubnetId": "/subscriptions/SUB_ID/resourceGroups/RG_NAME/providers/Microsoft.Network/virtualNetworks/VNET_NAME/subnets/MASTER_SUBNET_NAME", - "firstConsecutiveStaticIP": "10.239.255.239", + "firstConsecutiveStaticIP": "10.239.0.5", "vnetCidr": "10.239.0.0/16", ... }, diff --git a/examples/e2e-tests/kubernetes/release/default/definition.json b/examples/e2e-tests/kubernetes/release/default/definition.json index c82deffc44..766df2f94a 100644 --- a/examples/e2e-tests/kubernetes/release/default/definition.json +++ b/examples/e2e-tests/kubernetes/release/default/definition.json @@ -13,7 +13,7 @@ "vmSize": "Standard_D2_v2", "OSDiskSizeGB": 200, "vnetSubnetId": "/subscriptions/SUB_ID/resourceGroups/RG_NAME/providers/Microsoft.Network/virtualNetworks/VNET_NAME/subnets/SUBNET_NAME", - "firstConsecutiveStaticIP": "10.239.255.239", + "firstConsecutiveStaticIP": "10.239.0.4", "vnetCidr": "10.239.0.0/16" }, "agentPoolProfiles": [ diff --git a/examples/vnet/kubernetesvnet-azure-cni.json b/examples/vnet/kubernetesvnet-azure-cni.json index f2cccee088..9450ef164e 100644 --- a/examples/vnet/kubernetesvnet-azure-cni.json +++ b/examples/vnet/kubernetesvnet-azure-cni.json @@ -9,7 +9,7 @@ "dnsPrefix": "test", "vmSize": "Standard_D2_v2", "vnetSubnetId": "/subscriptions/SUB_ID/resourceGroups/RG_NAME/providers/Microsoft.Network/virtualNetworks/VNET_NAME/subnets/SUBNET_NAME", - "firstConsecutiveStaticIP": "10.239.255.239", + "firstConsecutiveStaticIP": "10.239.255.10", "vnetCidr": "10.239.0.0/16" }, "agentPoolProfiles": [ diff --git a/parts/k8s/kubernetesagentresourcesvmas.t b/parts/k8s/kubernetesagentresourcesvmas.t index 74d39a79df..b81d379c83 100644 --- a/parts/k8s/kubernetesagentresourcesvmas.t +++ b/parts/k8s/kubernetesagentresourcesvmas.t @@ -9,6 +9,9 @@ "[variables('nsgID')]" {{else}} "[variables('vnetID')]" +{{end}} +{{range $masterOffset := loop 1 GetMasterCount}} + ,"[concat(variables('masterNICNamePrefix'), sub({{$masterOffset}}, 1))]" {{end}} ], "location": "[variables('location')]", diff --git a/parts/k8s/kubernetesagentvars.t b/parts/k8s/kubernetesagentvars.t index 4e3261df78..8e435826d8 100644 --- a/parts/k8s/kubernetesagentvars.t +++ b/parts/k8s/kubernetesagentvars.t @@ -24,4 +24,5 @@ "{{.Name}}osImageSKU": "[parameters('{{.Name}}osImageSKU')]", "{{.Name}}osImagePublisher": "[parameters('{{.Name}}osImagePublisher')]", "{{.Name}}osImageVersion": "[parameters('{{.Name}}osImageVersion')]", + "masterNICNamePrefix": "[concat(variables('orchestratorName'), '-master-', variables('nameSuffix'), '-', 'nic-')]", diff --git a/parts/k8s/kubernetesmasterresources.t b/parts/k8s/kubernetesmasterresources.t index 9f66ebdb0c..71984760f2 100644 --- a/parts/k8s/kubernetesmasterresources.t +++ b/parts/k8s/kubernetesmasterresources.t @@ -338,13 +338,14 @@ } } {{if IsAzureCNI}} - {{range $seq := loop 2 .MasterProfile.IPAddressCount}} + {{range $seq := loop 1 .MasterProfile.IPAddressCount}} , { - "name": "ipconfig{{$seq}}", + "name": "[concat('ipconfig', add({{$seq}}, 1))]", "properties": { + "privateIPAddress": "[variables('masterSecondaryAddrs')[add(mul(copyIndex(variables('masterOffset')), variables('ipAddressCount')), sub({{$seq}}, 1))]]", "primary": false, - "privateIPAllocationMethod": "Dynamic", + "privateIPAllocationMethod": "Static", "subnet": { "id": "[variables('vnetSubnetID')]" } diff --git a/parts/k8s/kubernetesmastervars.t b/parts/k8s/kubernetesmastervars.t index bed18bb84b..c96218ef0d 100644 --- a/parts/k8s/kubernetesmastervars.t +++ b/parts/k8s/kubernetesmastervars.t @@ -180,6 +180,7 @@ "orchestratorNameVersionTag": "{{.OrchestratorProfile.OrchestratorType}}:{{.OrchestratorProfile.OrchestratorVersion}}", {{if IsAzureCNI}} "allocateNodeCidrs": false, + "ipAddressCount": {{.MasterProfile.IPAddressCount}}, {{else}} "allocateNodeCidrs": true, {{end}} @@ -266,6 +267,9 @@ "[concat(variables('masterFirstAddrPrefix'), add(3, int(variables('masterFirstAddrOctet4'))))]", "[concat(variables('masterFirstAddrPrefix'), add(4, int(variables('masterFirstAddrOctet4'))))]" ], +{{if IsAzureCNI}} + "masterSecondaryAddrs": [{{GetMasterSecondaryIP}}], +{{end}} "masterEtcdServerPort": {{GetMasterEtcdServerPort}}, "masterEtcdClientPort": {{GetMasterEtcdClientPort}}, "masterEtcdPeerURLs":[ diff --git a/parts/k8s/kuberneteswinagentresourcesvmas.t b/parts/k8s/kuberneteswinagentresourcesvmas.t index 7f65df9e56..41426b31cb 100644 --- a/parts/k8s/kuberneteswinagentresourcesvmas.t +++ b/parts/k8s/kuberneteswinagentresourcesvmas.t @@ -9,6 +9,9 @@ "[variables('nsgID')]" {{else}} "[variables('vnetID')]" +{{end}} +{{range $masterOffset := loop 1 GetMasterCount}} + ,"[concat(variables('masterNICNamePrefix'), sub({{$masterOffset}}, 1))]" {{end}} ], "location": "[variables('location')]", diff --git a/pkg/acsengine/const.go b/pkg/acsengine/const.go index f9a7be62f6..c70c8c52e9 100644 --- a/pkg/acsengine/const.go +++ b/pkg/acsengine/const.go @@ -18,7 +18,7 @@ const ( // DefaultNonMasqueradeCidr specifies the subnet that should not be masqueraded on host DefaultNonMasqueradeCidr = "10.0.0.0/8" // DefaultFirstConsecutiveKubernetesStaticIP specifies the static IP address on Kubernetes master 0 - DefaultFirstConsecutiveKubernetesStaticIP = "10.240.255.5" + DefaultFirstConsecutiveKubernetesStaticIP = "10.240.0.4" // DefaultAgentSubnetTemplate specifies a default agent subnet DefaultAgentSubnetTemplate = "10.%d.0.0/16" // DefaultKubernetesSubnet specifies the default subnet used for all masters, agents and pods diff --git a/pkg/acsengine/defaults.go b/pkg/acsengine/defaults.go index 596590d6ad..a02f417a8c 100644 --- a/pkg/acsengine/defaults.go +++ b/pkg/acsengine/defaults.go @@ -1,6 +1,7 @@ package acsengine import ( + "encoding/binary" "fmt" "net" @@ -473,9 +474,6 @@ func setMasterNetworkDefaults(a *api.Properties) { // Set the default number of IP addresses allocated for masters. if a.MasterProfile.IPAddressCount == 0 { - // Allocate one IP address for the node. - a.MasterProfile.IPAddressCount = 1 - // Allocate IP addresses for pods if VNET integration is enabled. if a.OrchestratorProfile.IsAzureCNI() { if a.OrchestratorProfile.OrchestratorType == api.Kubernetes { @@ -673,27 +671,23 @@ func certsAlreadyPresent(c *api.CertificateProfile, m int) map[string]bool { // getFirstConsecutiveStaticIPAddress returns the first static IP address of the given subnet. func getFirstConsecutiveStaticIPAddress(subnetStr string) string { - _, subnet, err := net.ParseCIDR(subnetStr) + ip, _, err := net.ParseCIDR(subnetStr) if err != nil { return DefaultFirstConsecutiveKubernetesStaticIP } - // Find the first and last octet of the host bits. - ones, bits := subnet.Mask.Size() - firstOctet := ones / 8 - lastOctet := bits/8 - 1 - - // Set the remaining host bits in the first octet. - subnet.IP[firstOctet] |= (1 << byte((8 - (ones % 8)))) - 1 - - // Fill the intermediate octets with 1s and last octet with offset. This is done so to match - // the existing behavior of allocating static IP addresses from the last /24 of the subnet. - for i := firstOctet + 1; i < lastOctet; i++ { - subnet.IP[i] = 255 + ipv4 := ip.To4() + if ipv4 == nil { + return DefaultFirstConsecutiveKubernetesStaticIP } - subnet.IP[lastOctet] = DefaultKubernetesFirstConsecutiveStaticIPOffset - return subnet.IP.String() + ipint := binary.BigEndian.Uint32(ipv4) + + // First 4 IPs will be reserved in Azure and cannot use. + ipint += 4 + startIP := make(net.IP, 4) + binary.BigEndian.PutUint32(startIP, ipint) + return startIP.String() } func getAddonsIndexByName(addons []api.KubernetesAddon, name string) int { diff --git a/pkg/acsengine/engine.go b/pkg/acsengine/engine.go index 3c42bd8bb0..34c353cec2 100644 --- a/pkg/acsengine/engine.go +++ b/pkg/acsengine/engine.go @@ -4,6 +4,7 @@ import ( "bytes" "compress/gzip" "encoding/base64" + "encoding/binary" "encoding/json" "errors" "fmt" @@ -11,6 +12,7 @@ import ( "io/ioutil" "log" "math/rand" + "net" "net/http" "regexp" "runtime/debug" @@ -874,6 +876,62 @@ func (t *TemplateGenerator) getTemplateFuncMap(cs *api.ContainerService) templat } return strings.TrimSuffix(buf.String(), ", ") }, + "GetMasterCount": func() int { + masterProfile := cs.Properties.MasterProfile + if masterProfile == nil { + return 0 + } + return masterProfile.Count + }, + "GetMasterSecondaryIP": func() string { + var ips string + var ipint uint32 + + profile := cs.Properties.MasterProfile + if profile == nil { + return "" + } + + ip := net.ParseIP(profile.FirstConsecutiveStaticIP) + ipv4 := ip.To4() + if ipv4 != nil { + ipint = binary.BigEndian.Uint32(ipv4) + } else { + log.Fatalf("Net IP To4() function returns nil") + } + + // Make space for first few IPs. + ipint += uint32(profile.Count) + uint32(DefaultInternalLbStaticIPOffset) - 1 + startIP := make(net.IP, 4) + binary.BigEndian.PutUint32(startIP, ipint) + + totalIPCount := profile.Count * profile.IPAddressCount + ipint = 0 + + // Generate All secondary IPs that master will use. + for count := totalIPCount; count > 0; count-- { + ipv4 = startIP.To4() + if ipv4 != nil { + ipint = binary.BigEndian.Uint32(ipv4) + } else { + log.Fatalf("Net IP To4() function returns nil for startIP") + } + + ipint++ + newIP := make(net.IP, 4) + binary.BigEndian.PutUint32(newIP, ipint) + + if ips != "" { + ips = ips + "," + "\"" + newIP.String() + "\"" + } else { + ips = "\"" + newIP.String() + "\"" + } + + startIP = newIP + } + + return ips + }, "RequiresFakeAgentOutput": func() bool { return cs.Properties.OrchestratorProfile.OrchestratorType == api.Kubernetes },