💡
EC2ノードインスタンスで EKS on Fargete のように振る舞うためのPod配置戦略
前提
- AWS
- EKS
- Kubernetes 1.30
- NodeManagement : Cluster Autoscaler
やりたいこと
EKSでEC2ノードインスタンスに特定のPodを1台だけ起動させたい。
※DeamonSetを除く
実現方法
ec2-node-group
を追加する
1. ManagedNodeGroupsにapiVersion: eksctl.io/v1alpha5
kind: ClusterConfig
...
managedNodeGroups:
# Start
- name: ec2-node-group
# End
Taints
を設定する
2. NodeにTolerationsを設定したPod以外スケジュールを許可しない。
apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig
...
managedNodeGroups:
- name: ec2-node-group
# Start
taints:
- key: runOnEc2Node
value: "true"
effect: NoSchedule
# End
3. Nodeに任意のLabelを設定する
PodのnodeSelector
で指定するためのLabelを設定する。
apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig
...
managedNodeGroups:
- name: ec2-node-group
taints:
- key: runOnEc2Node
value: "true"
effect: NoSchedule
# Start
labels:
nodeGroupName: ec2-node-group
# End
4. PodにTolerationsを設定する
このPodのみStep1でTaintsを設定したNodeにスケジュール可能になる。
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
# Start
tolerations:
- key: "runOnEc2Node"
operator: "Equal"
value: "true"
effect: "NoSchedule"
# End
5. PodにNodeSelectorを設定する
Step2で設定したラベルを指定する。
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
tolerations:
- key: "runOnEc2Node"
operator: "Equal"
value: "true"
effect: "NoSchedule"
# Start
nodeSelector:
nodeGroupName: ec2-node-group
# End
6. PodにpodAntiAffinityを設定する
Labelapp: PodA
が設定されているPodがいるNodeにはスケジューリングされなくなる。つまり同一NodeにPodAが複数台起動しなくなる。
apiVersion: apps/v1
kind: Deployment
spec:
template:
metadata:
labels:
app: PodA
spec:
tolerations:
- key: "runOnEc2Node"
operator: "Equal"
value: "true"
effect: "NoSchedule"
nodeSelector:
nodeGroupName: ec2-node-group
# Start
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- PodA
topologyKey: kubernetes.io/hostname
# End
Tolerations
を設定する
7. DeamonSetにStep1で設定したTailtsの影響で、Tolerationsが設定されていないDeamonSetがec2-node-group
のNodeに起動しなくなります。Tolerationsを設定しましょう。
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: daemonSetA
spec:
selector:
matchLabels:
app: daemonSetA
template:
spec:
tolerations:
- operator: Exists
Step1~Step7までの設定により、Node(ec2-node-group) : PodA = 1 : 1
でスケジューリングされるようになりました。
Discussion