GKE多区域测试集群nodeAffinity配置技术咨询
Hey there! Based on your GKE 1.9.4 setup, here's a practical nodeAffinity configuration to make sure your frontend and backend pods land exactly on their designated node pools.
Frontend Pods (Targeting frontend Labeled Nodes)
For your frontend workloads, you’ll want to enforce scheduling only on nodes in the frontend-pool (labeled frontend). Add this section under the spec.template.spec block of your frontend Deployment YAML:
affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: frontend operator: In values: - "true" # Adjust this value to match your actual node label value
What this does:
requiredDuringSchedulingIgnoredDuringExecution: Guarantees the pod only gets scheduled on nodes matching the label rule. Once the pod is running, if the node’s label changes later, the pod won’t be evicted (this is ideal for stable frontend workloads).matchExpressions: Matches nodes where thefrontendkey exists and has the value you specified—double-check this aligns with how you labeled yourfrontend-poolnodes.
Backend Pods (Targeting backend Labeled Nodes)
For backend workloads, use this configuration to pin them exclusively to nodes in the backend-pool (labeled backend):
affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: backend operator: In values: - "true" # Match your actual node label value here
Quick Validation Steps
Before applying these configs, confirm your node labels are correctly set:
- List all nodes and their labels with this command:
kubectl get nodes --show-labels - Verify that nodes in
frontend-poolhave thefrontend=true(or your custom value) label, andbackend-poolnodes havebackend=true.
Optional: Flexible Preferred Scheduling
If you ever want to prioritize the target node pool but allow fallback scheduling elsewhere (e.g., during node pool maintenance), use preferredDuringSchedulingIgnoredDuringExecution instead:
affinity: nodeAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 100 preference: matchExpressions: - key: frontend operator: In values: - "true"
The weight (1-100) sets how strong this preference is relative to other scheduling rules.
内容的提问来源于stack exchange,提问作者d.ansimov

