nodeName
调度到指定节点
nodeName是节点选择约束的最简单形式,但是由于其限制,通常不使用它。nodeName是PodSpec的一个字段。如果它是非空的,则调度程序将忽略容器,并且在指定节点上运行的kubelet会尝试运行容器。因此,如果
nodeNamePodSpec中指定nodeName,则它优先于上述用于节点选择的方法。
nodeName用于选择节点的一些限制是:
如果指定的节点不存在,则容器将不会运行,并且在某些情况下可能会自动删除。
如果命名节点没有足够的资源来容纳该Pod,则该Pod将失败,其原因将指示原因,例如OutOfmemory或OutOfcpu。
云环境中的节点名称并非总是可预测或稳定的
cat << EOF > nodename.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
nodeName: node01
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
cat << EOF > pod-nodeSelector.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
env: test
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
nodeSelector:
disktype: ssd
EOF
kubectl apply -f pod-nodeSelector.yaml
kubectl label nodes node01 app=nginx
kubectl get nodes node01 —show-labels
cat << EOF > nginx-app.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
env: test
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
nodeSelector:
app: nginx
EOF
[liwm@rmaster01 liwm]$ kubectl label nodes node01 app-
node/node01 labeled
[liwm@rmaster01 liwm]$ kubectl get nodes node01 --show-labels
NAME STATUS ROLES AGE VERSION LABELS
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=
[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.yaml
apiVersion: v1
kind: Pod
metadata:
name: with-node-affinity
spec:
affinity:
nodeAffinity: # 使用node亲和进行判定
requiredDuringSchedulingIgnoredDuringExecution: # 硬性要求
nodeSelectorTerms: # 节点选择器的条件
- matchExpressions: # 定义匹配的表达式
- key: app # label的key
operator: In # 定义判断方式多种:In,NotIn,Exists,DoesNotexist,Gt,Lt。
values: # label的values
- nginx
containers:
- name: with-node-affinity
image: nginx
imagePullPolicy: IfNotPresent
EOF
kubectl create -f node-affinity.yaml
kubectl label nodes node01 app=nginx
删除label 落地后的pod不受影响
# 软性要求
cat << EOF > node-affinity.yaml
apiVersion: v1
kind: Pod
metadata:
name: with-node-affinity
spec:
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution: # 软性要求
- weight: 1 # 设置权重,分数越高优先级越高
preference:
matchExpressions:
- key: disktype
operator: In
values:
- ssd
containers:
- name: with-node-affinity
image: nginx
imagePullPolicy: IfNotPresent
EOF
kubectl create -f node-affinity.yaml
kubectl label nodes node02 disktype=ssd
kubectl label nodes node02 disktype-
#权重
cat << EOF > weight-affinity.yaml
apiVersion: v1
kind: Pod
metadata:
name: with-weight
spec:
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution: # 软性要求
- weight: 1 # 设置权重,分数越高优先级越高
preference:
matchExpressions:
- key: disktype
operator: In
values:
- ssd2
- weight: 10
preference:
matchExpressions:
- key: disktype
operator: In
values:
- ssd1
containers:
- name: with-weight
image: nginx
imagePullPolicy: IfNotPresent
EOF
# 偏好要求
kubectl label nodes node02 disktype=ssd
kubectl label nodes node01 app=nginx1
kubectl label nodes node02 app=nginx2
cat << EOF > node-affinity.yaml
apiVersion: v1
kind: Pod
metadata:
name: with-node-affinity
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: app
operator: In
values:
- nginx1
- nginx2
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: disktype
operator: In
values:
- ssd
containers:
- name: with-node-affinity
image: nginx
imagePullPolicy: IfNotPresent
EOF
**
kubectl create -f node-affinity.yaml
pod affinity
#podaffinity
创建pod-affinity
cat << EOF > pod-affinity.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
spec:
selector:
matchLabels:
app: store
replicas: 3 #node节点不够有pod无法调度一直pending状态
template:
metadata:
labels:
app: store
spec:
affinity:
podAntiAffinity: #反亲和、一个节点只运行1个pod
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- store
topologyKey: "kubernetes.io/hostname" # 拓扑域,用来判定拥有哪些标签的节点是同一个位置
containers:
- name: redis-server
image: redis:3.2-alpine
imagePullPolicy: IfNotPresent
EOF
#podantiaffinity
创建web-server
cat << EOF > web-server.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-server
spec:
selector:
matchLabels:
app: web-store
replicas: 3
template:
metadata:
labels:
app: web-store
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- web-store
topologyKey: "kubernetes.io/hostname"
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- store
topologyKey: "kubernetes.io/hostname"
containers:
- name: web-app
image: nginx
imagePullPolicy: IfNotPresent
EOF