AWS - Autoscaling with Karpenter#
Introduction#
The node groups of the AWS getting started guide keep the number of nodes they start with. Their minimum and maximum sizes take effect only with an autoscaler, which the guide does not install. Karpenter adds nodes when pods cannot be scheduled, and removes nodes that are empty or underused. This guide lets it manage the worker nodes, which run jobs, notebooks and model deployments.
Which nodes Karpenter manages#
- Karpenter manages the worker nodes only. This needs the head and database node groups of Optional: dedicated node groups, with the values of Dedicated node pools: without their labels and taints, any Hopsworks pod, the stateful services included, can land on a node that Karpenter creates.
- The head and database node groups stay managed node groups. Karpenter replaces nodes on its own: when their configuration no longer matches (drift), when they expire, and when it consolidates pods onto fewer nodes. The RonDB data nodes and the other stateful services cannot be moved that way without warning, so their nodes change only as described in AWS - Kubernetes upgrades.
- Keep the
workermanaged node group of that section with at least two nodes. It runs Karpenter itself, CoreDNS and the AWS Load Balancer Controller, which must run before Karpenter can create a node, and Karpenter's chart runs two controllers on different nodes.
Install Karpenter#
Install Karpenter as its guide for an existing cluster, Migrating from Cluster Autoscaler, describes, with these choices:
- A Karpenter version that supports the cluster's Kubernetes version, from the compatibility matrix.
- The controller on the
workermanaged node group: the guide's node affinity names the node group it runs on. - The policy POLICY_NAME of the getting started guide attached to the node role that Karpenter's guide creates,
KarpenterNodeRole-CLUSTER_NAME. Pods on Karpenter's nodes then reach the bucket and the ECR repository as pods on the managed node groups do. -
On a cluster whose authentication mode is
API, whichaws eks describe-cluster --name CLUSTER_NAME --query cluster.accessConfig.authenticationModeprints, an access entry for that role, because such a cluster ignores the guide'saws-authentry and Karpenter's nodes would not join: -
The guide's
karpenter.sh/discoverytag on the subnets and the security group of the node groups, so that Karpenter's nodes use the same ones.
Node class and node pool#
Apply a node class and a node pool for the worker nodes:
apiVersion: karpenter.k8s.aws/v1
kind: EC2NodeClass
metadata:
name: worker
spec:
role: KarpenterNodeRole-CLUSTER_NAME
amiSelectorTerms:
- alias: al2023@AMI_RELEASE
subnetSelectorTerms:
- tags:
karpenter.sh/discovery: CLUSTER_NAME
securityGroupSelectorTerms:
- tags:
karpenter.sh/discovery: CLUSTER_NAME
metadataOptions:
httpEndpoint: enabled
httpTokens: required
httpPutResponseHopLimit: 2
blockDeviceMappings:
- deviceName: /dev/xvda
ebs:
volumeSize: 100Gi
volumeType: gp3
deleteOnTermination: true
---
apiVersion: karpenter.sh/v1
kind: NodePool
metadata:
name: worker
spec:
template:
metadata:
labels:
node: Worker
spec:
nodeClassRef:
group: karpenter.k8s.aws
kind: EC2NodeClass
name: worker
requirements:
- key: node.kubernetes.io/instance-type
operator: In
values: ["m6i.2xlarge", "m6i.4xlarge"]
- key: karpenter.sh/capacity-type
operator: In
values: ["on-demand"]
expireAfter: 720h
terminationGracePeriod: 48h
disruption:
consolidationPolicy: WhenEmptyOrUnderutilized
consolidateAfter: 1m
limits:
cpu: "64"
memory: 256Gi
httpPutResponseHopLimit: 2lets pods use the node role, for the bucket and the ECR repository, as on the managed node groups. Karpenter's default of 1 keeps them from it.- The AMI alias names a release of the Amazon Linux 2023 EKS image, for example
al2023@v20261023, so the nodes change only when you change the alias or the Kubernetes version.al2023@latestreplaces them whenever AWS publishes a new image. - The
node: Workerlabel is the one of the worker nodes in Dedicated node pools, and the node pool has no taint, like theworkernode group. - With Kueue on,
global._hopsworks.kueue.enabled: true, the labels also need the node labels of its ResourceFlavors,cloud.provider.com/region: europefor the default one, as described in Kueue outside the Hopsworks namespace. Kueue sets them as node selectors on the workloads it admits, and Karpenter creates nodes only with labels its node pool defines. limitscaps the CPU and memory of all the nodes Karpenter creates together.
Disruption#
When Karpenter removes or replaces a node, it evicts the pods on it:
- Hopsworks marks the drivers of Spark jobs
karpenter.sh/do-not-disrupt, so Karpenter does not consolidate a node while a Spark driver runs on it. A node that has drifted is still replaced, and the driver is deleted before the node pool'sterminationGracePeriodhas passed. - Spark executors, Python jobs, notebooks and model deployments carry no such mark, so a running Python job or notebook session on a node that Karpenter removes is interrupted, and model deployments move to other nodes.
To keep nodes with running work, as described in Disruption:
consolidationPolicy: WhenEmptylets Karpenter consolidate only empty nodes, while drift and expiry still replace nodes with running pods.- A budget under
spec.disruption.budgetswithnodes: "0", ascheduleand adurationstops consolidation and drift for that window every day, for example during working hours. expireAfterreplaces a node at that age whatever the budgets allow, andNeverturns it off.
Kubernetes upgrades#
- Before the control plane, upgrade Karpenter to a version that supports the target Kubernetes version, and move the AMI alias to a release that has an image for the target version. A pinned alias selects that release's image for the control plane's version, as described in Managing AMIs, so an older release may have none for the new one.
- Once the control plane runs the new version, the AMI alias selects the image for that version, and Karpenter replaces its nodes as drifted, under the disruption rules above. The managed node groups follow the steps of AWS - Kubernetes upgrades.
Private clusters#
On a private EKS cluster, Karpenter also needs:
settings.isolatedVPC: truein its values, because the AWS pricing API it calls is not among the endpoints of that page.instanceProfilein the node class in place ofrole, with an instance profile of the node role that you create: Karpenter manages the instance profile of arolethrough the IAM API, which those endpoints do not cover either.- The controller's image from your registry,
controller.image.repositoryset toECR_AWS_ACCOUNT_ID.dkr.ecr.REGION.amazonaws.com/ecr-public/karpenter/controllerwith the pull-through cache of that page, and the Karpenter charts staged like the other charts.
Check#
A job or deployment that does not fit on the existing worker nodes gets a node claim, and its node joins the cluster with the label node: Worker.