By default, if a pod is unable to be scheduled because of a node affinity constraint and the specified node is not available, the pod will remain in a "Pending" state until the node becomes available or the node affinity constraint is removed.
One option to address this issue is to use a pod disruption budget (PDB) to specify the minimum number of available nodes that must be present in the cluster at all times. This can help ensure that there are always enough available nodes to satisfy the node affinity constraints of your pods.
For example, you can use a PDB to specify that at least one node must always be available in the cluster by setting the minAvailable field to 1:
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
name: my-pdb
spec:
minAvailable: 1
selector:
matchLabels:
app: my-app
This PDB would prevent any nodes from being deleted if there are pods with node affinity constraints that are scheduled on those nodes.
Alternatively, you can use a deployment or replica set to manage the lifecycle of your pods and specify a node affinity constraint for the pods in the deployment or replica set. If a node becomes unavailable, the deployment or replica set will automatically reschedule the pods on another available node. This can help ensure that your pods are always running and avoid the "Pending" state.
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- node1
containers:
- name: my-container
image: my-image
I hope this helps!