![]() |
VOOZH | about |
Before Node Affinity, we had two main tools for controlling Pod placement, each with significant limitations.
nodeSelector: This is the simplest form of constraint. You specify a map of key-value pairs in the Pod spec, and the Pod will only be scheduled on nodes that have all of those exact labels. The limitation is its rigidity; it only supports simple AND logic and cannot express preferences or more complex conditions like OR or NOT IN.Node Affinity was created to overcome these limitations by providing a Pod-centric way to express complex attraction rules.
Node Affinity allows you to constrain which nodes your Pod can be scheduled on, based on the labels of those nodes. It have two variations.
requiredDuringSchedulingIgnoredDuringExecutionnodeSelector. The scheduler must find a node that satisfies the specified rules. If no such node exists, the Pod will remain in the Pending state.requiredDuringScheduling: The rule is mandatory when the scheduler is first placing the Pod.IgnoredDuringExecution: If the labels on the node change after the Pod is already running, the Pod will not be evicted. It will continue running on that node.preferredDuringSchedulingIgnoredDuringExecutionweight field: Each preference is assigned a weight between 1 and 100. When the scheduler finds a node that matches a preference, it adds the weight to that node's score. A higher weight makes a rule more important.These rules use powerful matchExpressions with operators like In, NotIn, Exists, and DoesNotExist, making them far more expressive than nodeSelector.
First of all, you need to label your nodes to use Node Affinity or Anti-Affinity. Nodes contain labels, and key-value pairs, attached by which identification is useful when using this by certain criteria.
kubectl label nodes <node-name> <key>=<value>Output:
Next, Make a Pod manifest, containing the Node Affinity rules. The following example makes sure that the Pod is only scheduled on nodes that have the disktype=ssd label applied.
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: mycontainer
image: nginx
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values:
- ssd
Next, you va to apply the manifest, to apply the manifest and create the pod, using Kubectl.
kubectl apply -f mypod.yamlOutput:
Lastly, Verify if the intended node is where the Pod was scheduled.
kubectl get pod mypod -o wideOutput:
Node anti-affinity specifies restrictions that prevent kubernetes pods from being scheduled on the same or different nodes. It is especially beneficial in high-availability installations, as distributing pods over multiple nodes or zones reduces the chance of a single point of failure. However, in many circumstances, you may want to specify that pods run exclusively on specified nodes in a cluster, or prevent running on specific nodes.
First, make sure your web server pods are labeled correctly. Here's a sample deployment with labels for our web server pods.
apiVersion: apps/v1
kind: Deployment
metadata:
name: webserver-app-deployment
spec:
replicas: 3
selector:
matchLabels:
app: webserver
template:
metadata:
labels:
app: webserver
spec:
containers:
- name: nginx-container
image: nginx:latest
You must define Node Anti-Affinity in the affinity section of your deployment manifest or pod to use it. By using this example, you can make sure that node scheduling of pods with the app=frontend label occurs.
apiVersion: v1
kind: Pod
metadata:
name: frontend-pod
labels:
app: frontend
spec:
containers:
- name: nginx-container
image: nginx
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- frontend
topologyKey: "kubernetes.io/hostname"
To prevent two Pods with the app=frontend label from being scheduled on the same node, this command will construct the Pod and implement the Anti-Affinity rules.
kubectl apply -f frontend-pod.yamlOutput:
Lastly, After the Pods are operational, you may cross-check where they are located on each node to make sure the Anti-Affinity rules are operating as intended.
kubectl get pods -l app=frontend -o wideOutput:
In conclusion, Node Affinity and Anti-Affinity in Kubernetes are significant Kubernetes capabilities that provide fine-grained control over pod placement. Mastering these ideas will allow you to optimize your deployments for performance, reliability, and compliance.