Chapter 5. Manage compute nodes using Red Hat build of Karpenter
Red Hat build of Karpenter provisions compute nodes automatically based on workload demand. By automatically provisioning your nodes, Red Hat build of Karpenter helps improve cluster efficiency and reduce costs.
5.1. Prerequisites Copy linkLink copied to clipboard!
-
You have installed the ROSA command-line interface (CLI) (
rosa) version 1.2.61 or later. - You have an OpenShift cluster version 4.22.0 or later.
-
You have installed the
jqCLI tool. -
You have installed the
curlCLI tool. - You have the required AWS Identity and Access Management (IAM) permissions to create policies and roles.
5.2. About Red Hat build of Karpenter Copy linkLink copied to clipboard!
Red Hat build of Karpenter builds on the open source Karpenter project and provides automatic node provisioning for Red Hat OpenShift Service on AWS clusters. Karpenter watches for pods that the Kubernetes scheduler marks as unschedulable and evaluates their scheduling constraints, including resource requests, node selectors, affinities, tolerations, and topology spread constraints. Karpenter then provisions nodes that meet the specific requirements of those waiting pods.
Karpenter improves cluster efficiency by provisioning nodes that match workload requirements instead of requiring pre-configured node pools. When nodes are no longer needed, Karpenter removes them to reduce costs. For more information about Karpenter capabilities and architecture, see Karpenter project documentation in the Additional resources.
5.3. Prepare an AWS IAM role for Red Hat build of Karpenter Copy linkLink copied to clipboard!
Create the IAM policy and role that Red Hat build of Karpenter requires to provision Amazon Elastic Compute Cloud (Amazon EC2) instances.
This policy grants Red Hat build of Karpenter the following permissions:
- Create and terminate EC2 instances and fleets
- Create and manage launch templates
- Tag resources with Karpenter-specific tags
- Describe EC2 resources such as instances, instance types, and subnets
- Access Systems Manager (SSM) parameters for Amazon Machine Image (AMI) information
- Query EC2 pricing information
- Manage Simple Queue Service (SQS) interruption queues for Spot instance handling
Create and manage IAM instance profiles
NoteAll permissions are restricted to resources tagged with Karpenter-specific tags to ensure Red Hat build of Karpenter manages only nodes that it provisions.
Procedure
Download the latest Red Hat build of Karpenter IAM policy:
$ curl -o autonode-policy.json https://raw.githubusercontent.com/openshift/managed-cluster-config/refs/heads/master/resources/sts/hypershift/openshift_hcp_karpenter_controller_credentials_policy.jsonExport your cluster name and ID to environment variables:
$ export CLUSTER_NAME=<cluster_name> $ export CLUSTER_ID=$(rosa describe cluster -c "$CLUSTER_NAME" -o json | jq -r '.id') $ echo $CLUSTER_NAME $CLUSTER_IDCreate the IAM policy:
$ POLICY_ARN=$(aws iam create-policy \ --policy-name rosa-karpenter-controller-role-${CLUSTER_NAME} \ --policy-document file://autonode-policy.json \ --query 'Policy.Arn' \ --output text)Create the trust policy for the Karpenter service account:
$ cat > trust-policy.json <<EOF { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Federated": "arn:aws:iam::$(aws sts get-caller-identity --query Account --output text):oidc-provider/$(rosa describe cluster -c $CLUSTER_NAME -o json | jq -r .aws.sts.oidc_endpoint_url | sed 's|https://||')" }, "Action": "sts:AssumeRoleWithWebIdentity", "Condition": { "StringEquals": { "$(rosa describe cluster -c $CLUSTER_NAME -o json | jq -r .aws.sts.oidc_endpoint_url | sed 's|https://||'):sub": "system:serviceaccount:kube-system:karpenter" } } } ] } EOFCreate the IAM role and attach the policy:
$ ROLE_ARN=$(aws iam create-role \ --role-name rosa-karpenter-controller-role-${CLUSTER_NAME} \ --assume-role-policy-document file://trust-policy.json \ --query 'Role.Arn' \ --output text) $ aws iam attach-role-policy \ --role-name rosa-karpenter-controller-role-${CLUSTER_NAME} \ --policy-arn $POLICY_ARNVerification
Verify that policies are present by running:
$ aws iam list-attached-role-policies --role-name rosa-karpenter-controller-role-${CLUSTER_NAME}
5.4. Tag your AWS resources for Red Hat build of Karpenter Copy linkLink copied to clipboard!
Create the necessary AWS tags so that Red Hat build of Karpenter discovers the security group to use when provisioning nodes. Tag the security group for the cluster that was created during cluster installation.
Procedure
Export your cluster name and ID to environment variables:
$ export CLUSTER_NAME=<cluster_name> $ export AWS_REGION=us-east-2 $ export CLUSTER_ID=$(rosa describe cluster -c "$CLUSTER_NAME" -o json | jq -r '.id') $ echo $CLUSTER_NAME $CLUSTER_IDImportantEnsure that your AWS client is using the region where your cluster is deployed. In this example,
us-east-2is used.Find your cluster’s security group by running the following command:
$ SECURITY_GROUP_ID=$(aws ec2 describe-security-groups --filters "Name=tag:Name,Values=$CLUSTER_ID-default-sg" | jq -r .SecurityGroups[0].GroupId)Tag the security group with the Red Hat build of Karpenter auto-discovery tags by running the following command:
$ aws ec2 create-tags --resources "$SECURITY_GROUP_ID" --tags Key="karpenter.sh/discovery",Value="$CLUSTER_ID"
5.5. Enable Red Hat build of Karpenter using ROSA CLI Copy linkLink copied to clipboard!
Enable Red Hat build of Karpenter on your cluster by using ROSA command-line interface (CLI) (rosa) after it finishes installing.
Procedure
Export your cluster name and ID to environment variables:
$ export CLUSTER_NAME=<cluster_name> $ export CLUSTER_ID=$(rosa describe cluster -c "$CLUSTER_NAME" -o json | jq -r '.id') $ echo $CLUSTER_NAME $CLUSTER_IDWait for the cluster to become ready:
$ rosa describe cluster -c $CLUSTER_ID | grep -i StateExample output
State: readyEnsure that your Red Hat build of Karpenter IAM role is correctly set:
$ ROLE_ARN=$(aws iam get-role --role-name rosa-karpenter-controller-role-${CLUSTER_NAME} --query 'Role.Arn' --output text)Enable Red Hat build of Karpenter:
$ rosa edit cluster -c $CLUSTER_ID \ --autonode=enabled \ --autonode-iam-role-arn=$ROLE_ARNIf you do not already have cluster admin access, create a cluster admin user:
$ rosa create admin -c $CLUSTER_IDLog in to the cluster using the credentials from the previous command:
$ oc login <api_url> --username cluster-admin --password <password>Verify that the Red Hat build of Karpenter custom resource definitions (CRDs) are present:
$ oc get ec2nodeclassNoteThe node pool manifest uses the
EC2NodeClassresource.Example output
NAME READY AGE default True 5m$ oc get openshiftec2nodeclassNoteThe
OpenShiftEC2NodeClassresource is Red Hat’s wrapper to communicate with theEC2NodeClassresource.Example output
NAME READY default True
5.6. Enable Red Hat build of Karpenter using OpenShift Cluster Manager Copy linkLink copied to clipboard!
Enable Red Hat build of Karpenter on your cluster by using OpenShift Cluster Manager after it finishes installing.
Prerequisites
- You have created a Red Hat OpenShift Service on AWS cluster, version 4.22.0 or later.
- You have created an AWS Identity and Access Management (IAM) role to be configured for Red Hat build of Karpenter.
You have your cluster’s Open ID Connect (OIDC) Endpoint URL.
NoteRun
rosa describe cluster -c $CLUSTER_NAME | grep "OIDC Endpoint URL"to see this URL. Do not include thehttps://prefix from the OIDC Endpoint URL. For example, useexample-oidc-endpoint.cloudfront.net/abcd1234examplehash5678instead ofhttps://example-oidc-endpoint.cloudfront.net/abcd1234examplehash5678.- You have the proper credentials to access the AWS console.
Procedure
Export your AWS ID:
$ export AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)- Log in to the AWS console.
- In the AWS console, navigate to IAM > Roles.
On your Red Hat build of Karpenter Amazon Resource Name (ARN), update the trust policy to include the following policy specifications:
NoteTo access this ARN, run:
$ echo $ROLE_ARN{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Federated": "arn:aws:iam::<aws_account_id>:oidc-provider/<oidc-endpoint-url>" }, "Action": "sts:AssumeRoleWithWebIdentity", "Condition": { "StringEquals": { "<oidc-endpoint-url>:sub": "system:serviceaccount:kube-system:karpenter" } } } ] }where:
- <aws_account_id>
- Specifies your AWS Account ID.
- <oidc-endpoint-url>
- Specifies the OIDC endpoint URL that you acquired.
- In OpenShift Cluster Manager, select your cluster from the cluster list.
- On the cluster details screen, select the Edit button next to the status for Red Hat build of Karpenter.
- On the Edit Autonode settings dialog box, toggle Enable Autonode.
- Add your Red Hat build of Karpenter IAM role ARN to the field in this dialog box.
- Select Save to save your configurations and close the Edit Autonode settings box.
5.7. Create a node pool Copy linkLink copied to clipboard!
Create a node pool to define the compute capacity that Red Hat build of Karpenter can provision.
Procedure
Create a node pool manifest:
$ cat > nodepool.yaml <<'EOF' apiVersion: karpenter.sh/v1 kind: NodePool metadata: name: default-np spec: template: metadata: labels: autonode: "true" spec: requirements: - key: node.kubernetes.io/instance-type operator: In values: - c5.xlarge - key: karpenter.sh/capacity-type operator: In values: ["on-demand"] nodeClassRef: group: karpenter.k8s.aws kind: EC2NodeClass name: default EOFwhere:
nodeClassRef.kind-
Required field that must use the
EC2NodeClasstype. spec.labelsOptional field that you can use to place pods by using labels.
NoteFor a list of all requirements available under
spec.requirements, see the Additional resources.
Apply the node pool:
$ oc apply -f nodepool.yamlVerify the node pool is ready:
$ oc get nodepoolExample output
NAME NODECLASS NODES READY AGE default-np default 0 True 3s