From 3989ec66eba9a9da9ae85fb981ee776e4579ec41 Mon Sep 17 00:00:00 2001 From: yankaiz Date: Tue, 24 Apr 2018 17:50:07 -0700 Subject: [PATCH 1/2] Add MAX_PODS_PER_NODE env allowing kubelet to be max-pods aware. --- cluster/gce/config-default.sh | 2 ++ cluster/gce/config-test.sh | 2 ++ cluster/gce/util.sh | 8 ++++++++ 3 files changed, 12 insertions(+) diff --git a/cluster/gce/config-default.sh b/cluster/gce/config-default.sh index cefbdb7fe96..69516c1857e 100755 --- a/cluster/gce/config-default.sh +++ b/cluster/gce/config-default.sh @@ -305,6 +305,8 @@ if [ ${ENABLE_IP_ALIASES} = true ]; then PROVIDER_VARS="${PROVIDER_VARS:-} ENABLE_IP_ALIASES" PROVIDER_VARS="${PROVIDER_VARS:-} NODE_IPAM_MODE" PROVIDER_VARS="${PROVIDER_VARS:-} SECONDARY_RANGE_NAME" + # Number of Pods that can run on this node. + MAX_PODS_PER_NODE=${MAX_PODS_PER_NODE:-110} fi # Enable GCE Alpha features. diff --git a/cluster/gce/config-test.sh b/cluster/gce/config-test.sh index bad9917cd15..d11512ac32b 100755 --- a/cluster/gce/config-test.sh +++ b/cluster/gce/config-test.sh @@ -312,6 +312,8 @@ if [ ${ENABLE_IP_ALIASES} = true ]; then PROVIDER_VARS="${PROVIDER_VARS:-} ENABLE_IP_ALIASES" PROVIDER_VARS="${PROVIDER_VARS:-} NODE_IPAM_MODE" PROVIDER_VARS="${PROVIDER_VARS:-} SECONDARY_RANGE_NAME" + # Number of Pods that can run on this node. + MAX_PODS_PER_NODE=${MAX_PODS_PER_NODE:-110} fi # Enable GCE Alpha features. diff --git a/cluster/gce/util.sh b/cluster/gce/util.sh index 9d411206c27..87b1cbf196d 100755 --- a/cluster/gce/util.sh +++ b/cluster/gce/util.sh @@ -677,6 +677,9 @@ function construct-kubelet-flags { if [[ -n "${CONTAINER_RUNTIME_ENDPOINT:-}" ]]; then flags+=" --container-runtime-endpoint=${CONTAINER_RUNTIME_ENDPOINT}" fi + if [[ -n "${MAX_PODS_PER_NODE:-}" ]]; then + flags+=" --max-pods=${MAX_PODS_PER_NODE}" + fi KUBELET_ARGS="${flags}" } @@ -1136,6 +1139,11 @@ EOF if [ -n "${SCHEDULING_ALGORITHM_PROVIDER:-}" ]; then cat >>$file <>$file < Date: Wed, 25 Apr 2018 17:47:41 -0700 Subject: [PATCH 2/2] Use IP_ALIAS_SIZE to calculate and update IP_ALIAS_SIZE. Error added when ip-alias is not enabled when IP_ALIAS_SIZE is not empty. --- cluster/gce/config-common.sh | 13 +++++++++++++ cluster/gce/config-default.sh | 12 ++++++++---- cluster/gce/config-test.sh | 12 ++++++++---- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/cluster/gce/config-common.sh b/cluster/gce/config-common.sh index 628ad582719..112c63a637c 100644 --- a/cluster/gce/config-common.sh +++ b/cluster/gce/config-common.sh @@ -98,6 +98,19 @@ function get-cluster-ip-range { echo "${suggested_range}" } +# Calculate ip alias range based on max number of pods. +# Let pow be the smallest integer which is bigger than log2($1 * 2). +# (32 - pow) will be returned. +# +# $1: The number of max pods limitation. +function get-alias-range-size() { + for pow in {0..31}; do + if (( 1 << $pow > $1 * 2 )); then + echo $((32 - pow)) + return 0 + fi + done +} # NOTE: Avoid giving nodes empty scopes, because kubelet needs a service account # in order to initialize properly. NODE_SCOPES="${NODE_SCOPES:-monitoring,logging-write,storage-ro}" diff --git a/cluster/gce/config-default.sh b/cluster/gce/config-default.sh index 69516c1857e..eb2bd973d7b 100755 --- a/cluster/gce/config-default.sh +++ b/cluster/gce/config-default.sh @@ -289,8 +289,10 @@ ENABLE_RESCHEDULER="${KUBE_ENABLE_RESCHEDULER:-true}" ENABLE_IP_ALIASES=${KUBE_GCE_ENABLE_IP_ALIASES:-false} NODE_IPAM_MODE=${KUBE_GCE_NODE_IPAM_MODE:-RangeAllocator} if [ ${ENABLE_IP_ALIASES} = true ]; then - # Size of ranges allocated to each node. Currently supports only /32 and /24. - IP_ALIAS_SIZE=${KUBE_GCE_IP_ALIAS_SIZE:-/24} + # Number of Pods that can run on this node. + MAX_PODS_PER_NODE=${MAX_PODS_PER_NODE:-110} + # Size of ranges allocated to each node. + IP_ALIAS_SIZE="/$(get-alias-range-size ${MAX_PODS_PER_NODE})" IP_ALIAS_SUBNETWORK=${KUBE_GCE_IP_ALIAS_SUBNETWORK:-${INSTANCE_PREFIX}-subnet-default} # If we're using custom network, use the subnet we already create for it as the one for ip-alias. # Note that this means SUBNETWORK would override KUBE_GCE_IP_ALIAS_SUBNETWORK in case of custom network. @@ -305,8 +307,10 @@ if [ ${ENABLE_IP_ALIASES} = true ]; then PROVIDER_VARS="${PROVIDER_VARS:-} ENABLE_IP_ALIASES" PROVIDER_VARS="${PROVIDER_VARS:-} NODE_IPAM_MODE" PROVIDER_VARS="${PROVIDER_VARS:-} SECONDARY_RANGE_NAME" - # Number of Pods that can run on this node. - MAX_PODS_PER_NODE=${MAX_PODS_PER_NODE:-110} +elif [[ -n "${MAX_PODS_PER_NODE:-}" ]]; then + # Should not have MAX_PODS_PER_NODE set for route-based clusters. + echo -e "${color_red}Cannot set MAX_PODS_PER_NODE for route-based projects for ${PROJECT}." >&2 + exit 1 fi # Enable GCE Alpha features. diff --git a/cluster/gce/config-test.sh b/cluster/gce/config-test.sh index d11512ac32b..b800e855f82 100755 --- a/cluster/gce/config-test.sh +++ b/cluster/gce/config-test.sh @@ -296,8 +296,10 @@ ENABLE_RESCHEDULER="${KUBE_ENABLE_RESCHEDULER:-true}" ENABLE_IP_ALIASES=${KUBE_GCE_ENABLE_IP_ALIASES:-false} NODE_IPAM_MODE=${KUBE_GCE_NODE_IPAM_MODE:-RangeAllocator} if [ ${ENABLE_IP_ALIASES} = true ]; then - # Size of ranges allocated to each node. gcloud current supports only /32 and /24. - IP_ALIAS_SIZE=${KUBE_GCE_IP_ALIAS_SIZE:-/24} + # Number of Pods that can run on this node. + MAX_PODS_PER_NODE=${MAX_PODS_PER_NODE:-110} + # Size of ranges allocated to each node. + IP_ALIAS_SIZE="/$(get-alias-range-size ${MAX_PODS_PER_NODE})" IP_ALIAS_SUBNETWORK=${KUBE_GCE_IP_ALIAS_SUBNETWORK:-${INSTANCE_PREFIX}-subnet-default} # If we're using custom network, use the subnet we already create for it as the one for ip-alias. # Note that this means SUBNETWORK would override KUBE_GCE_IP_ALIAS_SUBNETWORK in case of custom network. @@ -312,8 +314,10 @@ if [ ${ENABLE_IP_ALIASES} = true ]; then PROVIDER_VARS="${PROVIDER_VARS:-} ENABLE_IP_ALIASES" PROVIDER_VARS="${PROVIDER_VARS:-} NODE_IPAM_MODE" PROVIDER_VARS="${PROVIDER_VARS:-} SECONDARY_RANGE_NAME" - # Number of Pods that can run on this node. - MAX_PODS_PER_NODE=${MAX_PODS_PER_NODE:-110} +elif [[ -n "${MAX_PODS_PER_NODE:-}" ]]; then + # Should not have MAX_PODS_PER_NODE set for route-based clusters. + echo -e "${color_red}Cannot set MAX_PODS_PER_NODE for route-based projects for ${PROJECT}." >&2 + exit 1 fi # Enable GCE Alpha features.