nodeName

调度到指定节点
nodeName是节点选择约束的最简单形式,但是由于其限制,通常不使用它。nodeName是PodSpec的一个字段。如果它是非空的,则调度程序将忽略容器,并且在指定节点上运行的kubelet会尝试运行容器。因此,如果 nodeNamePodSpec中指定nodeName,则它优先于上述用于节点选择的方法。

nodeName用于选择节点的一些限制是:
如果指定的节点不存在,则容器将不会运行,并且在某些情况下可能会自动删除。
如果命名节点没有足够的资源来容纳该Pod,则该Pod将失败,其原因将指示原因,例如OutOfmemory或OutOfcpu。
云环境中的节点名称并非总是可预测或稳定的

  1. cat << EOF > nodename.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. name: nginx
  6. spec:
  7. containers:
  8. - name: nginx
  9. image: nginx
  10. imagePullPolicy: IfNotPresent
  11. nodeName: node01
  12. EOF

node节点添加label
[root@master01 ~]# kubectl label nodes node01 disktype=ssd
[root@master01 ~]# kubectl get nodes node01 —show-labels
NAME STATUS ROLES AGE VERSION LABELS
删除标签
[liwm@rmaster01 liwm]$ kubectl label nodes node01 disktype-
node/node01 labeled
[liwm@rmaster01 liwm]$

无指定的标签时 pod会一直处于 Pending状态
Warning FailedScheduling default-scheduler 0/5 nodes are available: 5 node(s) didn’t match node selector.
**

nodeSelector

使用yaml创建pod,指定nodeSelector

  1. cat << EOF > pod-nodeSelector.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. name: nginx-pod
  6. labels:
  7. env: test
  8. spec:
  9. containers:
  10. - name: nginx
  11. image: nginx
  12. imagePullPolicy: IfNotPresent
  13. nodeSelector:
  14. disktype: ssd
  15. EOF

kubectl apply -f pod-nodeSelector.yaml

kubectl label nodes node01 app=nginx
kubectl get nodes node01 —show-labels

  1. cat << EOF > nginx-app.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. name: nginx-pod
  6. labels:
  7. env: test
  8. spec:
  9. containers:
  10. - name: nginx
  11. image: nginx
  12. imagePullPolicy: IfNotPresent
  13. nodeSelector:
  14. app: nginx
  15. EOF
  1. [liwm@rmaster01 liwm]$ kubectl label nodes node01 app-
  2. node/node01 labeled
  3. [liwm@rmaster01 liwm]$ kubectl get nodes node01 --show-labels
  4. NAME STATUS ROLES AGE VERSION LABELS
  5. node01 Ready worker 21d v1.17.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node01,kubernetes.io/os=linux,node-role.kubernetes.io/worker=
  6. [liwm@rmaster01 liwm]$

Affinity and anti-affinity

亲和与反亲和

affinity/anti-affinity功能有丰富的约束类型。
比如:
语言更具表现力(不仅是“and或完全匹配”);
规则除了硬性要求(完全匹配)还可以使用“软性要求”/“偏好要求”;(当硬性要求和软性要求同时出现就出现偏好要求)偏好有权重打分
可以使用Node节点的Label与Pod的Label实现亲和与反亲和;

affinity/anti-affinity的两种类型:
requiredDuringSchedulinglgnoredDuringExecution(硬性要求)
preferredDuringSchedulinglgnoredDuringExecution(软性要求)
运算符(operator):
In,Notln,Exists,DoesNotExist,Gt,Lt 可以使用NotIn和DoesNotExist实现节点的反亲和行为
等于 不等于 已存在 不存在 大于 小于

image.png

image.png

Node affinity

# 硬性要求

  1. cat << EOF > node-affinity.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. name: with-node-affinity
  6. spec:
  7. affinity:
  8. nodeAffinity: # 使用node亲和进行判定
  9. requiredDuringSchedulingIgnoredDuringExecution: # 硬性要求
  10. nodeSelectorTerms: # 节点选择器的条件
  11. - matchExpressions: # 定义匹配的表达式
  12. - key: app # label的key
  13. operator: In # 定义判断方式多种:In,NotIn,Exists,DoesNotexist,Gt,Lt。
  14. values: # label的values
  15. - nginx
  16. containers:
  17. - name: with-node-affinity
  18. image: nginx
  19. imagePullPolicy: IfNotPresent
  20. EOF

kubectl create -f node-affinity.yaml
kubectl label nodes node01 app=nginx

删除label 落地后的pod不受影响

# 软性要求

  1. cat << EOF > node-affinity.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. name: with-node-affinity
  6. spec:
  7. affinity:
  8. nodeAffinity:
  9. preferredDuringSchedulingIgnoredDuringExecution: # 软性要求
  10. - weight: 1 # 设置权重,分数越高优先级越高
  11. preference:
  12. matchExpressions:
  13. - key: disktype
  14. operator: In
  15. values:
  16. - ssd
  17. containers:
  18. - name: with-node-affinity
  19. image: nginx
  20. imagePullPolicy: IfNotPresent
  21. EOF

kubectl create -f node-affinity.yaml
kubectl label nodes node02 disktype=ssd

kubectl label nodes node02 disktype-

#权重

  1. cat << EOF > weight-affinity.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. name: with-weight
  6. spec:
  7. affinity:
  8. nodeAffinity:
  9. preferredDuringSchedulingIgnoredDuringExecution: # 软性要求
  10. - weight: 1 # 设置权重,分数越高优先级越高
  11. preference:
  12. matchExpressions:
  13. - key: disktype
  14. operator: In
  15. values:
  16. - ssd2
  17. - weight: 10
  18. preference:
  19. matchExpressions:
  20. - key: disktype
  21. operator: In
  22. values:
  23. - ssd1
  24. containers:
  25. - name: with-weight
  26. image: nginx
  27. imagePullPolicy: IfNotPresent
  28. EOF

# 偏好要求

kubectl label nodes node02 disktype=ssd
kubectl label nodes node01 app=nginx1
kubectl label nodes node02 app=nginx2

  1. cat << EOF > node-affinity.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. name: with-node-affinity
  6. spec:
  7. affinity:
  8. nodeAffinity:
  9. requiredDuringSchedulingIgnoredDuringExecution:
  10. nodeSelectorTerms:
  11. - matchExpressions:
  12. - key: app
  13. operator: In
  14. values:
  15. - nginx1
  16. - nginx2
  17. preferredDuringSchedulingIgnoredDuringExecution:
  18. - weight: 1
  19. preference:
  20. matchExpressions:
  21. - key: disktype
  22. operator: In
  23. values:
  24. - ssd
  25. containers:
  26. - name: with-node-affinity
  27. image: nginx
  28. imagePullPolicy: IfNotPresent
  29. EOF

**
kubectl create -f node-affinity.yaml

pod affinity

#podaffinity

创建pod-affinity

  1. cat << EOF > pod-affinity.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: redis
  6. spec:
  7. selector:
  8. matchLabels:
  9. app: store
  10. replicas: 3 #node节点不够有pod无法调度一直pending状态
  11. template:
  12. metadata:
  13. labels:
  14. app: store
  15. spec:
  16. affinity:
  17. podAntiAffinity: #反亲和、一个节点只运行1个pod
  18. requiredDuringSchedulingIgnoredDuringExecution:
  19. - labelSelector:
  20. matchExpressions:
  21. - key: app
  22. operator: In
  23. values:
  24. - store
  25. topologyKey: "kubernetes.io/hostname" # 拓扑域,用来判定拥有哪些标签的节点是同一个位置
  26. containers:
  27. - name: redis-server
  28. image: redis:3.2-alpine
  29. imagePullPolicy: IfNotPresent
  30. EOF

#podantiaffinity

创建web-server

  1. cat << EOF > web-server.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: web-server
  6. spec:
  7. selector:
  8. matchLabels:
  9. app: web-store
  10. replicas: 3
  11. template:
  12. metadata:
  13. labels:
  14. app: web-store
  15. spec:
  16. affinity:
  17. podAntiAffinity:
  18. requiredDuringSchedulingIgnoredDuringExecution:
  19. - labelSelector:
  20. matchExpressions:
  21. - key: app
  22. operator: In
  23. values:
  24. - web-store
  25. topologyKey: "kubernetes.io/hostname"
  26. podAffinity:
  27. requiredDuringSchedulingIgnoredDuringExecution:
  28. - labelSelector:
  29. matchExpressions:
  30. - key: app
  31. operator: In
  32. values:
  33. - store
  34. topologyKey: "kubernetes.io/hostname"
  35. containers:
  36. - name: web-app
  37. image: nginx
  38. imagePullPolicy: IfNotPresent
  39. EOF