pod调度因素
一,标签
nodeSelector:用于将pod调度到Label的Node上,如果没有匹配的标签会调度失败。
作用:约束pod到特定节点运行,完全匹配节点标签
应用场景:
专用节点:根据业务线将node分组管理
配备特殊硬件:部分node配有ssd硬盘,gpu
1,添加标签
格式:kubectl label nodes node名称 <label-key>=<label-value>
例:kubectl label nodes k8s-node2 gpu=ok
2,查看标签
kubectl get nodes --show-labels
kubectl get nodes k8s-node1 --show-labels
3,删除标签
格式:kubectl label nodes 节点名称 <label-key>-
kubectl label nodes k8s-node2 gpu-
4,修改标签
kubectl label nodes k8s-master01 ssd=true
kubectl label nodes k8s-master01 ssd=false --overwrite
yaml中pod标签应用:
spec:
nodeSelector:
gpu: 'ok'
containers:
...
spec:
nodeSelector:
kubernetes.io/hostname: node1
containers:
二,nodeAffinity节点亲和性
标签表达式的类型 = != in notin exists
- requiredDuringSchedulingIgnoredDuringExecution除非满足规则,否则调度程序无法调度 Pod。此函数类似于nodeSelector,但具有更具表现力的语法。
- preferredDuringSchedulingIgnoredDuringExecution 调度器尝试寻找符合规则的节点。如果匹配的节点不可用,调度程序仍会调度 Pod。
- IgnoredDuringExecution表示如果 Kubernetes 调度 Pod 后节点标签发生变化,则 Pod 继续运行。
1,节点打测试标签
kubectl label nodes k8s-node1 disktype=ssd
2,requiredDuringSchedulingIgnoredDuringExecution 硬性规则
调度pod到带有disktype=ssd标签的节点,使用硬性规则
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values:
- ssd
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
3,preferredDuringSchedulingIgnoredDuringExecution 软性规则
调度pod到带有标签disktype=ssd的节点上
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: disktype
operator: In
values:
- ssd
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
三,污点和污点容忍
目的:保留特殊节点资源,如高性能节点
污点:避免pod调度到污点节点上
污点容忍:
effect可取值
NoSchedule:一定不能被调度(新来的不要来,在这的就别动了)
PreferNoSchedule:尽量不要调度(尽量不要来,除非没办法)
NoExecute:不仅不会调度,还会驱逐Node节点上已有pod(新来的不要来,在这的赶紧走)
1,添加污点
格式:kubectl taint node node名称 key=value:[effect]
例:kubectl taint node k8s-node1 gpu=yes:NoExecute
2,查看污点
kubectl describe nodes k8s-node1 | grep Taint
3,删除污点
格式:kubectl taint node node名称 key:[effect]-
例:kubectl taint node k8s-node1 gpu:NoExecute-
4,配置污点容忍
可以分配pod到带有污点gpu=yes的节点上
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
env: test
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
tolerations:
- key: "gpu"
value: "yes"
operator: "Equal"
effect: "NoSchedule"
# tolerations:
#- key: "gpu"
# operator: "Exists"
# effect: "NoSchedule"
#operator 的默认值是 Equal ,是=的意思
一个容忍度和一个污点相“匹配”是指它们有一样的键名和效果,并且:
如果 operator 是 Exists (此时容忍度不能指定 value),如果 operator 是 Equal ,则它们的 value 应该相等
四,nodename
指定节点名称,用于将pod调度到指定节点上,不经过调度器
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
nodeName: kube-01
五,资源限制
- request 资源请求量(下限):容器运行时,向节点申请的最少保障资源,实际并不一定完全占用
- limits 资源上限:容器在node节点上允许消耗的最大资源上限
- requests一般是小于limits的20%-30%
- limits一般最高设置低于宿主机20%
- requests不能大于limits
- CPU的单位是milicpu,500mcpu=0.5cpu;而内存的单位则包括E, P, T, G, M, K, Ei, Pi, Ti, Gi, Mi, Ki等
- ephemeral-storage 临时存储资源,磁盘使用限制
apiVersion: v1
kind: Pod
metadata:
name: frontend
spec:
containers:
- name: app
image: images.my-company.example/app:v4
resources:
requests:
memory: "64Mi"
cpu: "250m"
ephemeral-storage: "2Gi"
limits:
memory: "128Mi"
cpu: "500m"
ephemeral-storage: "2Gi"
六,命名空间资源限制
1,资源总量限制
default命名空间创建pod默认资源限制,超出限制创建失败
apiVersion: v1
kind: LimitRange
metadata:
name: limit
spec:
limits:
- default:
memory: 600Mi
cpu: 1
defaultRequest:
memory: 512Mi
cpu: 0.5
type: Container
2,pod数量限额
hard限制pod数量最多为2
apiVersion: v1
kind: ResourceQuota
metadata:
name: pod-demo
spec:
hard:
pods: "2"
七,pod驱逐
软驱逐是指K8S节点上的Pod被驱逐到其他节点,而硬驱逐则是将Pod从节点上完全删除。软驱逐在节点需要维护或升级时非常有用,可以确保应用程序持续运行,而硬驱逐则用于终止节点上的不必要的或故障的Pod。
软驱逐
kubectl drain 节点名称 --ignore-daemonsets
硬驱逐
kubectl delete pod --grace-period=0 --force