nodeName
调度到指定节点
nodeName是节点选择约束的最简单形式,但是由于其限制,通常不使用它。nodeName是PodSpec的一个字段。如果它是非空的,则调度程序将忽略容器,并且在指定节点上运行的kubelet会尝试运行容器。因此,如果
nodeNamePodSpec中指定nodeName,则它优先于上述用于节点选择的方法。
nodeName用于选择节点的一些限制是:
如果指定的节点不存在,则容器将不会运行,并且在某些情况下可能会自动删除。
如果命名节点没有足够的资源来容纳该Pod,则该Pod将失败,其原因将指示原因,例如OutOfmemory或OutOfcpu。
云环境中的节点名称并非总是可预测或稳定的
cat << EOF > nodename.yamlapiVersion: v1kind: Podmetadata:name: nginxspec:containers:- name: nginximage: nginximagePullPolicy: IfNotPresentnodeName: node01EOF
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
cat << EOF > pod-nodeSelector.yamlapiVersion: v1kind: Podmetadata:name: nginx-podlabels:env: testspec:containers:- name: nginximage: nginximagePullPolicy: IfNotPresentnodeSelector:disktype: ssdEOF
kubectl apply -f pod-nodeSelector.yaml
kubectl label nodes node01 app=nginx
kubectl get nodes node01 —show-labels
cat << EOF > nginx-app.yamlapiVersion: v1kind: Podmetadata:name: nginx-podlabels:env: testspec:containers:- name: nginximage: nginximagePullPolicy: IfNotPresentnodeSelector:app: nginxEOF
[liwm@rmaster01 liwm]$ kubectl label nodes node01 app-node/node01 labeled[liwm@rmaster01 liwm]$ kubectl get nodes node01 --show-labelsNAME STATUS ROLES AGE VERSION LABELSnode01 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=[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实现节点的反亲和行为
等于 不等于 已存在 不存在 大于 小于


Node affinity
# 硬性要求
cat << EOF > node-affinity.yamlapiVersion: v1kind: Podmetadata:name: with-node-affinityspec:affinity:nodeAffinity: # 使用node亲和进行判定requiredDuringSchedulingIgnoredDuringExecution: # 硬性要求nodeSelectorTerms: # 节点选择器的条件- matchExpressions: # 定义匹配的表达式- key: app # label的keyoperator: In # 定义判断方式多种:In,NotIn,Exists,DoesNotexist,Gt,Lt。values: # label的values- nginxcontainers:- name: with-node-affinityimage: nginximagePullPolicy: IfNotPresentEOF
kubectl create -f node-affinity.yaml
kubectl label nodes node01 app=nginx
删除label 落地后的pod不受影响
# 软性要求
cat << EOF > node-affinity.yamlapiVersion: v1kind: Podmetadata:name: with-node-affinityspec:affinity:nodeAffinity:preferredDuringSchedulingIgnoredDuringExecution: # 软性要求- weight: 1 # 设置权重,分数越高优先级越高preference:matchExpressions:- key: disktypeoperator: Invalues:- ssdcontainers:- name: with-node-affinityimage: nginximagePullPolicy: IfNotPresentEOF
kubectl create -f node-affinity.yaml
kubectl label nodes node02 disktype=ssd
kubectl label nodes node02 disktype-
#权重
cat << EOF > weight-affinity.yamlapiVersion: v1kind: Podmetadata:name: with-weightspec:affinity:nodeAffinity:preferredDuringSchedulingIgnoredDuringExecution: # 软性要求- weight: 1 # 设置权重,分数越高优先级越高preference:matchExpressions:- key: disktypeoperator: Invalues:- ssd2- weight: 10preference:matchExpressions:- key: disktypeoperator: Invalues:- ssd1containers:- name: with-weightimage: nginximagePullPolicy: IfNotPresentEOF
# 偏好要求
kubectl label nodes node02 disktype=ssd
kubectl label nodes node01 app=nginx1
kubectl label nodes node02 app=nginx2
cat << EOF > node-affinity.yamlapiVersion: v1kind: Podmetadata:name: with-node-affinityspec:affinity:nodeAffinity:requiredDuringSchedulingIgnoredDuringExecution:nodeSelectorTerms:- matchExpressions:- key: appoperator: Invalues:- nginx1- nginx2preferredDuringSchedulingIgnoredDuringExecution:- weight: 1preference:matchExpressions:- key: disktypeoperator: Invalues:- ssdcontainers:- name: with-node-affinityimage: nginximagePullPolicy: IfNotPresentEOF
**
kubectl create -f node-affinity.yaml
pod affinity
#podaffinity
创建pod-affinity
cat << EOF > pod-affinity.yamlapiVersion: apps/v1kind: Deploymentmetadata:name: redisspec:selector:matchLabels:app: storereplicas: 3 #node节点不够有pod无法调度一直pending状态template:metadata:labels:app: storespec:affinity:podAntiAffinity: #反亲和、一个节点只运行1个podrequiredDuringSchedulingIgnoredDuringExecution:- labelSelector:matchExpressions:- key: appoperator: Invalues:- storetopologyKey: "kubernetes.io/hostname" # 拓扑域,用来判定拥有哪些标签的节点是同一个位置containers:- name: redis-serverimage: redis:3.2-alpineimagePullPolicy: IfNotPresentEOF
#podantiaffinity
创建web-server
cat << EOF > web-server.yamlapiVersion: apps/v1kind: Deploymentmetadata:name: web-serverspec:selector:matchLabels:app: web-storereplicas: 3template:metadata:labels:app: web-storespec:affinity:podAntiAffinity:requiredDuringSchedulingIgnoredDuringExecution:- labelSelector:matchExpressions:- key: appoperator: Invalues:- web-storetopologyKey: "kubernetes.io/hostname"podAffinity:requiredDuringSchedulingIgnoredDuringExecution:- labelSelector:matchExpressions:- key: appoperator: Invalues:- storetopologyKey: "kubernetes.io/hostname"containers:- name: web-appimage: nginximagePullPolicy: IfNotPresentEOF
