使用Graviton机器

本节我们介绍如何用Karpenter开出Graviton机器来

先删除上节的资源:

kubectl delete deployment inflate
kubectl delete provisioners.karpenter.sh default
kubectl delete awsnodetemplates.karpenter.k8s.aws default

部署NodePool

kubernetes.io/arch中,加入arm64, Karpenter会检查每个Pod的 nodeAffinity / nodeSelector,如果有CPU架构的要求,就开出来对应的机器:

mkdir -p ~/environment/karpenter
cd ~/environment/karpenter
cat <<EoF | kubectl apply -f -
apiVersion: karpenter.sh/v1beta1
kind: NodePool
metadata:
  name: default
spec:
  disruption:
    consolidationPolicy: WhenUnderutilized
    expireAfter: 720h0m0s
  limits:
    cpu: 1k
    memory: 1000Gi
  template:
    metadata:
      labels:
        intent: apps
    spec:
      nodeClassRef:
        name: default
      requirements:
      - key: karpenter.sh/capacity-type
        operator: In
        values:
        - on-demand
      - key: kubernetes.io/arch
        operator: In
        values:
        - amd64
        - arm64
  weight: 100

---
apiVersion: karpenter.k8s.aws/v1beta1
kind: EC2NodeClass
metadata:
  name: default
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

部署应用

创建应用,它的nodeSelector中声明了kubernetes.io/arch: arm64

cd ~/environment/karpenter
cat <<EOF > inflate.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: inflate
spec:
  replicas: 2
  selector:
    matchLabels:
      app: inflate
  template:
    metadata:
      labels:
        app: inflate
    spec:
      nodeSelector:
        intent: apps
        kubernetes.io/arch: arm64
      containers:
      - image: public.ecr.aws/eks-distro/kubernetes/pause:3.7
        name: inflate
        resources:
          requests:
            cpu: "1"
            memory: 256M 
EOF
kubectl apply -f inflate.yaml

等待一段时间后,Karpenter开出来c6g.xlarge机器:

image-20231029102728555

查看日志:

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

image-20240803215116848