Weighted NodePool

Karpenter 允许我们使用 .spec.weight 字段对节点池进行排序,以便节点调度程序在尝试调度另一个节点池之前确定性地尝试调度一个节点池。以下是现在使用Weighted Nodepool的一些示例。

部署Weighted NodePool

现在,我们将创建一个名为"weight-nodepool"的自定义 Karpenter 节点池,它将与默认节点池并存。请注意,我们已将自定义节点池的权重设置为 2,高于默认的 1。此外,我们已指示自定义节点池仅使用"t"类别的实例。

mkdir -p ~/environment/karpenter
cd ~/environment/karpenter
cat <<EoF> weight-nodepool.yaml
apiVersion: karpenter.sh/v1beta1
kind: NodePool
metadata:
  name: weight-nodepool
spec:
  disruption:
    consolidateAfter: 30s
    consolidationPolicy: WhenEmpty
    expireAfter: Never
  limits:
    cpu: "50"
  template:
    metadata:
      labels:
        eks-immersion-team: weight-nodepool
    spec:
      nodeClassRef:
        name: weight-nodepool
      requirements:
      - key: karpenter.k8s.aws/instance-category
        operator: In
        values:
        - t
      - key: kubernetes.io/arch
        operator: In
        values:
        - amd64
      - key: kubernetes.io/os
        operator: In
        values:
        - linux
      - key: karpenter.sh/capacity-type
        operator: In
        values:
        - on-demand
  weight: 2

---
apiVersion: karpenter.k8s.aws/v1beta1
kind: EC2NodeClass
metadata:
  name: weight-nodepool
spec:
  amiFamily: AL2
  role: "KarpenterNodeRole-${CLUSTER_NAME}"
  securityGroupSelectorTerms:
  - tags:
      alpha.eksctl.io/cluster-name: $CLUSTER_NAME
  subnetSelectorTerms:
  - tags:
      alpha.eksctl.io/cluster-name: $CLUSTER_NAME
  tags:
    intent: apps
    managed-by: karpenter
EoF

kubectl apply -f weight-nodepool.yaml

输出:

nodepool.karpenter.sh/weight-nodepool created
ec2nodeclass.karpenter.k8s.aws/weight-nodepool created

现在,让我们检查集群中有多少个节点池。我们将看到另一个名为"weight-nodepool"的节点池已被创建:

kubectl get nodepools.karpenter.sh -owide

输出:

kongpingfan:~/environment/karpenter $ kubectl get nodepools.karpenter.sh -owide
NAME              NODECLASS         WEIGHT
default           default           
weight-nodepool   weight-nodepool   2

测试Weighted NodePool

我们现在准备测试我们的加权节点池。我们将创建一个新的deployment,有 8 个replica,所以我们的 EKS 集群需要新的节点才能调度该部署,因为该部署需要更多的 CPU/内存资源:

cd ~/environment/karpenter
cat <<EoF> weight-workload.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: inflate
spec:
  replicas: 8
  selector:
    matchLabels:
      app: inflate
  template:
    metadata:
      labels:
        app: inflate
    spec:
      containers:
        - name: inflate
          image: public.ecr.aws/eks-distro/kubernetes/pause:3.7
          resources:
            requests:
              memory: 1Gi
              cpu: 1
EoF
kubectl apply -f weight-workload.yaml

输出

deployment.apps/inflate created

新扩容出来一个节点,它是t类型:

image-20240803173256403

此外,我们可以检查 karpenter 日志

kubectl -n karpenter logs -l app.kubernetes.io/name=karpenter | grep provisioner

输出:

image-20240803202512293

清理本节创建的资源:

kubectl apply -f weight-workload.yaml
kubectl delete nodepool.karpenter.sh weight-nodepool
kubectl delete ec2nodeclass.karpenter.k8s.aws weight-nodepool

结论

我们创建了"weight-nodepool”,其权重为2,高于默认节点池。当需要调度新 pod 时,Karpenter 将选择"weight-nodepool”。

参考: https://karpenter.sh/docs/concepts/nodepools/#specweight