Node affinity is a generalization of the node selector method, which allows the necessary rules and preference rules to be specified
The following example is an nginx, which is required by affinity rules
Only nodes with more than 3 cores (excluding 3) are considered
Try to select nodes that are not master nodes
There is also a weight with a value of 1. If this node matches the name and is not master, it will get a weight score of 1
Finally, the scheduler will select a node with the highest weight score, and the weight is a list
Look at the following example
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-hello-deployment
namespace: lizhe
labels:
app: nginx-hello
spec:
replicas: 2
selector:
matchLabels:
app: nginx-hello
template:
metadata:
labels:
app: nginx-hello
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: numberCores
operator: Gt
values: ["3"]
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchFields:
- key: metadata.name
operator: NotIn
values: ["master"]
containers:
- name: nginx-hello
image: nginx
ports:
- containerPort: 80
In this example, numbercores is used. Please don’t mistakenly think that this is a native field. In fact, this is a label on node, which we need to create manually
If it is not created, the scheduling will not succeed
add label
now it can be scheduled