Automatic DNS scaling by cluster size
Managed Service for Kubernetes supports automatic DNS scaling. The Managed Service for Kubernetes cluster runs the kube-dns-autoscaler
app that tunes the number of CoreDNS replicas depending on:
- The number of cluster nodes.
- The number of vCPUs in the cluster.
The number of replicas is calculated by the formulas.
To automate DNS scaling:
If you no longer need automatic scaling, disable it.
If you no longer need these resources, delete them.
Before you begin
-
Create Kubernetes resources:
-
Create a Managed Service for Kubernetes cluster with the Public address setting :
Auto
. -
Create a node group in any suitable configuration.
-
-
Install kubectl
and configure it to work with the created cluster.
Configure kube-dns-autoscaler
Make sure that the app is up and running
Check the Deploymentkube-system
namespace:
kubectl get deployment --namespace=kube-system
Result:
NAME READY UP-TO-DATE AVAILABLE AGE
...
kube-dns-autoscaler 1/1 1 1 52m
Define the scaling parameters
The kube-dns-autoscaler
pod regularly polls the Kubernetes server for the number of cluster nodes and cores. Based on this data, the number of CoreDNS replicas is calculated.
Two types of calculation are possible:
- Linear mode.
- Ladder mode (a step function).
For more information about calculating, see the cluster-proportional-autoscaler
In this example, we use the linear
mode where calculations follow this formula:
replicas = max( ceil( cores * 1/coresPerReplica ) , ceil( nodes * 1/nodesPerReplica ) )
Where:
coresPerReplica
: Configuration parameter indicating the number of CoreDNS replicas per vCPU of the cluster.nodesPerReplica
: Configuration parameter indicating the number of CoreDNS replicas per cluster node.cores
: Actual number of vCPUs in the cluster.nodes
: Actual number of nodes in the cluster.ceil
: Ceiling function that rounds up a decimal number to an integer.max
: Max function that returns the largest of the two values.
The preventSinglePointFailure
additional parameter is relevant for multi-node clusters. If true
, the minimum number of DNS replicas is two.
You can also define the min
and max
configuration parameters that set the minimum and maximum number of CoreDNS replicas in the cluster:
replicas = min(replicas, max)
replicas = max(replicas, min)
For more information about calculating, see the cluster-proportional-autoscaler
Change the configuration
-
Check the current settings.
In this example, we are creating
node-group-1
with the following parameters:- Number of nodes:
3
. vCPU cores
: 256.
By default,
linear
mode and the following scaling parameters are used:coresPerReplica
:256
.nodesPerReplica
:16
.preventSinglePointFailure
:true
.
replicas = max( ceil( 12 * 1/256 ), ceil( 3 * 1/16 ) ) = 1
The
preventSinglePointFailure
parameter istrue
, meaning the number of CoreDNS replicas is two.To get the
coredns
pod data, run this command:kubectl get pods -n kube-system
Result:
NAME READY STATUS RESTARTS AGE ... coredns-7c646474c9-4dmjl 1/1 Running 0 128m coredns-7c646474c9-n7qsv 1/1 Running 0 134m
- Number of nodes:
-
Set new parameters.
Change the configuration as follows:
coresPerReplica
:4
.nodesPerReplica
:2
.preventSinglePointFailure
:true
.
replicas = max( ceil( 12 * 1/4 ), ceil( 3 * 1/2 ) ) = 3
To deliver the parameters to the
kube-dns-autoscaler
application, edit the appropriate ConfigMap using this command:kubectl edit configmap kube-dns-autoscaler --namespace=kube-system
Once a text editor with the
kube-dns-autoscaler
configuration opens, change the line with the following parameters:linear: '{"coresPerReplica":4,"nodesPerReplica":2,"preventSinglePointFailure":true}'
Save your changes to see the operation output:
configmap/kube-dns-autoscaler edited
The
kube-dns-autoscaler
application will upload the configuration and scale the DNS service with the new parameters.
Test scaling
Resize the cluster
Create a second node group using this command:
ncp managed-kubernetes node-group create \
--name node-group-2 \
--cluster-name dns-autoscaler \
--location zone=eu-north1-c \
--public-ip \
--fixed-size 2 \
--cores 4 \
--core-fraction 5
Result:
done (2m43s)
...
Now the cluster has 5 nodes with 20 vCPUs. Calculate the number of replicas:
replicas = max( ceil( 20 * 1/4 ), ceil( 5 * 1/2 ) ) = 5
Check the changes in the number of CoreDNS replicas
Run the following command:
kubectl get pods -n kube-system
Result:
NAME READY STATUS RESTARTS AGE
...
coredns-7c646474c9-7l8mc 1/1 Running 0 3m30s
coredns-7c646474c9-n7qsv 1/1 Running 0 3h20m
coredns-7c646474c9-pv9cv 1/1 Running 0 3m40s
coredns-7c646474c9-r2lss 1/1 Running 0 49m
coredns-7c646474c9-s5jgz 1/1 Running 0 57m
Set up reducing the number of nodes
By default, Cluster Autoscaler does not reduce the number of nodes in a node group with auto scaling if these nodes contain pods from the kube-system
namespace managed by the Deployment
To allow reducing the number of nodes, configure the PodDisruptionBudget
kubectl create poddisruptionbudget <pdb name> \
--namespace=kube-system \
--selector k8s-app=kube-dns \
--min-available=2
Result:
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: <pdb name>
spec:
minAvailable: 2
selector:
matchLabels:
k8s-app: kube-dns
Disable scaling
Reset the number of replicas in the kube-dns-autoscaler
application Deployment
kubectl scale deployment --replicas=0 kube-dns-autoscaler --namespace=kube-system
Result:
deployment.apps/kube-dns-autoscaler scaled
Check the result with this command:
kubectl get rs --namespace=kube-system
Result:
NAME READY UP-TO-DATE AVAILABLE AGE
...
kube-dns-autoscaler 0/0 0 0 3h53m
Delete the resources you created
Delete the Managed Service for Kubernetes cluster you no longer need.