Skip to content

Instantly share code, notes, and snippets.

@vfarcic
Last active August 15, 2025 08:32
Show Gist options
  • Save vfarcic/baaf4adb25e9efaba886c17a2ad722a5 to your computer and use it in GitHub Desktop.
Save vfarcic/baaf4adb25e9efaba886c17a2ad722a5 to your computer and use it in GitHub Desktop.

Revisions

  1. vfarcic revised this gist Sep 14, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 137-karpenter.sh
    Original file line number Diff line number Diff line change
    @@ -65,7 +65,7 @@ aws ec2 create-tags \
    --resources $(echo $SUBNET_IDS | tr ',' '\n') \
    --tags Key="kubernetes.io/cluster/$CLUSTER_NAME",Value=

    curl -fsSL https://karpenter.sh/v0.16.2/getting-started/getting-started-with-eksctl/cloudformation.yaml \
    curl -fsSL https://raw.githubusercontent.com/aws/karpenter/v0.30.0/website/content/en/preview/getting-started/getting-started-with-karpenter/cloudformation.yaml \
    | tee karpenter.yaml

    aws cloudformation deploy \
  2. vfarcic revised this gist Sep 23, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 137-karpenter.sh
    Original file line number Diff line number Diff line change
    @@ -65,7 +65,7 @@ aws ec2 create-tags \
    --resources $(echo $SUBNET_IDS | tr ',' '\n') \
    --tags Key="kubernetes.io/cluster/$CLUSTER_NAME",Value=

    curl -fsSL https://karpenter.sh/docs/getting-started/cloudformation.yaml \
    curl -fsSL https://karpenter.sh/v0.16.2/getting-started/getting-started-with-eksctl/cloudformation.yaml \
    | tee karpenter.yaml

    aws cloudformation deploy \
  3. vfarcic revised this gist Sep 23, 2022. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions 137-karpenter.sh
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,12 @@
    # Source: https://gist.github.com/baaf4adb25e9efaba886c17a2ad722a5

    ########################################################
    # How To Auto-Scale Kubernetes Clusters With Karpenter #
    # https://youtu.be/C-2v7HT-uSA #
    ########################################################

    # Referenced videos:
    # - Karpenter: https://karpenter.sh
    # - GKE Autopilot - Fully Managed Kubernetes Service From Google: https://youtu.be/Zztufl4mFQ4

    #########
  4. vfarcic created this gist Dec 19, 2021.
    185 changes: 185 additions & 0 deletions 137-karpenter.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,185 @@
    ########################################################
    # How To Auto-Scale Kubernetes Clusters With Karpenter #
    # https://youtu.be/C-2v7HT-uSA #
    ########################################################

    # Referenced videos:
    # - GKE Autopilot - Fully Managed Kubernetes Service From Google: https://youtu.be/Zztufl4mFQ4

    #########
    # Setup #
    #########

    git clone https://github.com/vfarcic/karpenter-demo

    cd karpenter-demo

    export CLUSTER_NAME=devops-toolkit

    # Replace `[...]` with your access key ID
    export AWS_ACCESS_KEY_ID=[...]

    # Replace `[...]` with your secret access key
    export AWS_SECRET_ACCESS_KEY=[...]

    export AWS_DEFAULT_REGION=us-east-1

    cat cluster.yaml \
    | sed -e "s@name: .*@name: $CLUSTER_NAME@g" \
    | sed -e "s@region: .*@region: $AWS_DEFAULT_REGION@g" \
    | tee cluster.yaml

    cat provisioner.yaml \
    | sed -e "s@instanceProfile: .*@instanceProfile: KarpenterNodeInstanceProfile-$CLUSTER_NAME@g" \
    | sed -e "s@.* # Zones@ values: [${AWS_DEFAULT_REGION}a, ${AWS_DEFAULT_REGION}b, ${AWS_DEFAULT_REGION}c] # Zones@g" \
    | tee provisioner.yaml

    cat app.yaml \
    | sed -e "[email protected]/zone: .*@topology.kubernetes.io/zone: ${AWS_DEFAULT_REGION}a@g" \
    | tee app.yaml

    eksctl create cluster \
    --config-file cluster.yaml

    export CLUSTER_ENDPOINT=$(aws eks describe-cluster \
    --name $CLUSTER_NAME \
    --query "cluster.endpoint" \
    --output json)

    echo $CLUSTER_ENDPOINT

    ###########################
    # Karpenter Prerequisites #
    ###########################

    export SUBNET_IDS=$(\
    aws cloudformation describe-stacks \
    --stack-name eksctl-$CLUSTER_NAME-cluster \
    --query 'Stacks[].Outputs[?OutputKey==`SubnetsPrivate`].OutputValue' \
    --output text)

    aws ec2 create-tags \
    --resources $(echo $SUBNET_IDS | tr ',' '\n') \
    --tags Key="kubernetes.io/cluster/$CLUSTER_NAME",Value=

    curl -fsSL https://karpenter.sh/docs/getting-started/cloudformation.yaml \
    | tee karpenter.yaml

    aws cloudformation deploy \
    --stack-name Karpenter-$CLUSTER_NAME \
    --template-file karpenter.yaml \
    --capabilities CAPABILITY_NAMED_IAM \
    --parameter-overrides ClusterName=$CLUSTER_NAME

    export AWS_ACCOUNT_ID=$(\
    aws sts get-caller-identity \
    --query Account \
    --output text)

    eksctl create iamidentitymapping \
    --username system:node:{{EC2PrivateDNSName}} \
    --cluster $CLUSTER_NAME \
    --arn arn:aws:iam::${AWS_ACCOUNT_ID}:role/KarpenterNodeRole-$CLUSTER_NAME \
    --group system:bootstrappers \
    --group system:nodes

    eksctl create iamserviceaccount \
    --cluster $CLUSTER_NAME \
    --name karpenter \
    --namespace karpenter \
    --attach-policy-arn arn:aws:iam::$AWS_ACCOUNT_ID:policy/KarpenterControllerPolicy-$CLUSTER_NAME \
    --approve

    # Execute only if this is the first time using spot instances in this account
    aws iam create-service-linked-role \
    --aws-service-name spot.amazonaws.com

    ###########################################
    # Applications Without Cluster Autoscaler #
    ###########################################

    kubectl get nodes

    cat app.yaml

    kubectl apply --filename app.yaml

    kubectl get pods,nodes

    #####################
    # Install Karpenter #
    #####################

    helm repo add karpenter \
    https://charts.karpenter.sh

    helm repo update

    helm upgrade --install \
    karpenter karpenter/karpenter \
    --namespace karpenter \
    --create-namespace \
    --set serviceAccount.create=false \
    --version 0.5.0 \
    --set controller.clusterName=$CLUSTER_NAME \
    --set controller.clusterEndpoint=$CLUSTER_ENDPOINT \
    --wait

    kubectl --namespace karpenter get all

    ##############################################
    # Scale Up Kubernetes Cluster With Karpenter #
    ##############################################

    cat provisioner.yaml

    # Open https://kubernetes.io/docs/reference/labels-annotations-taints/

    kubectl apply \
    --filename provisioner.yaml

    kubectl get pods,nodes

    kubectl get pods,nodes

    kubectl --namespace karpenter logs \
    --selector karpenter=controller

    ####################################################
    # Scale Down The Kubernetes Cluster With Karpenter #
    ####################################################

    kubectl delete --filename app.yaml

    kubectl --namespace karpenter logs \
    --selector karpenter=controller

    kubectl get nodes

    ###########
    # Destroy #
    ###########

    helm --namespace karpenter \
    uninstall karpenter

    eksctl delete iamserviceaccount \
    --cluster $CLUSTER_NAME \
    --name karpenter \
    --namespace karpenter

    aws cloudformation delete-stack \
    --stack-name Karpenter-$CLUSTER_NAME

    # Install jq from https://stedolan.github.io/jq if you do not have it already

    aws ec2 describe-launch-templates \
    | jq -r ".LaunchTemplates[].LaunchTemplateName" \
    | grep -i Karpenter-$CLUSTER_NAME \
    | xargs -I{} \
    aws ec2 delete-launch-template \
    --launch-template-name {}

    eksctl delete cluster \
    --name $CLUSTER_NAME \
    --region $AWS_DEFAULT_REGION