k8s之 pod调度

案例:确保Pod分配到具有SSD硬盘的节点上
第一步:给节点添加标签
格式:kubectl label nodes <node-name> <label-key>=<label-value>
例如:kubectl label nodes k8s-node1 disktype=ssd
验证:kubectl get nodes --show-labels
第二步:写yaml 文件
vim pod-ssd.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx-kusc00401
spec:
  containers:
  - name: nginx
    image: nginx
  nodeSelector:
    disk: ssd

第三步:利用yaml 部署pod

kubectl apply -f pod-ssd.yaml

验证:

kubectl get pods -o wide
--------
删除节点标签:
kubectl label nodes node1.lab.example.com disktype-
**************
nodeSelector & nodeAffinity的区别:
nodeAffinity:节点亲和类似于nodeSelector,可以根据节点上
的标签来约束Pod可以调度到哪些节点。
相比nodeSelector:
• 匹配有更多的逻辑组合,不只是字符串的完全相等,支持的操作
符有:In、NotIn、Exists、DoesNotExist、Gt、Lt
• 调度分为软策略和硬策略,而不是硬性要求
• 硬(required):必须满足
• 软(preferred):尝试满足,但不保证
********************
Taint(污点)与Tolerations(污点容忍)
基于节点标签分配是站在Pod的角度上,通过在Pod上添加属性,来确定Pod是否要调度到指定的Node上,其实我们也可以站在
Node的角度上,通过在Node上添加污点属性,来避免Pod被分配到不合适的节点上。
Taints:避免Pod调度到特定Node上
Tolerations:允许Pod调度到持有Taints的Node上
案例:pod调度之污点
第一步:给节点添加污点
格式:kubectl taint node [node] key=value:[effect]
例如:kubectl taint node k8s-node1 gpu=yes:NoSchedule
验证:kubectl describe node k8s-node1 |grep Taint
其中[effect] 可取值:
• NoSchedule :一定不能被调度
• PreferNoSchedule:尽量不要调度,非必须配置容忍
• NoExecute:不仅不会调度,还会驱逐Node上已有的Pod
第二步: 编写yaml
vim pod-taints.yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: test
    image: busybox
  tolerations:
  - key: "gpu"
      operator: "Equal"
      value: "yes"
      effect: "NoSchedule"
注:如果希望Pod可以被分配到带有污点的节点上,要在Pod配置
中添加污点容忍(tolrations)字段
---
删除污点:kubectl taint node [node] key:[effect]-
*****
nodeName:指定节点名称,用于将Pod调度到指定的Node上,不经过调度器
vim pod-nodename.yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  nodeName: k8s-node2
  containers:
  - name: web
    image: nginx
 
 
posted @ 2022-08-29 12:27  cloud-qing0212  阅读(113)  评论(0编辑  收藏  举报