一、K8s节点特殊资源保留

当 Kubernetes 中存储特殊节点时,应该尽量保持不要特殊资源的 Pod 不要调度到这些节点上,此时可以通过污点进行控制。

假如包含了GPU的节点不能被任意调度

kubectl taint node k8s-node02 gpu=true:NoSchedule

具有其它特殊资源,尽量不要调度

kubectl taint node k8s-node02 ssd=true:PreferNoSchedule

二、使用容忍调度到具有污点的节点

在生产环境中,经常根据实际情况给节点打上污点,比如特殊资源节点不能随意调度、主节点不能随意调度,但是需要特殊资源的服务还是需要调度到该节点,一些监控和收集的服务还是需要调度到主节点,此时需要给这些服务添加合适的容忍才能部署到这些节点。

2.1 环境准备

将GPU机器k8s-node02设置污点,不能被调度

[root@k8s-master01 ~]# kubectl taint node k8s-node02 gpu=true:NoSchedule

将GPU机器k8s-node02打上标签

[root@k8s-master01 ~]# kubectl label node k8s-node02 gpu="true"

2.2 开始容忍调度

1、现在模拟有个服务需要GPU资源,需要添加容忍部署到GPU机器

      tolerations:
      - key: "gpu"
        operator: "Exists"
        effect: "NoSchedule"

完整配置

[root@k8s-master01 ~]# vim gpu-example.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: gpu-example
  name: gpu-example
spec:
  replicas: 1
  selector:
    matchLabels:
      app: gpu-example
  template:
    metadata:
      labels:
        app: gpu-example
    spec:
      nodeSelector:
        gpu: "true"
      tolerations:
      - key: "gpu"
        operator: "Exists"
        effect: "NoSchedule"
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/zq-demo/nginx:1.14.2
        imagePullPolicy: IfNotPresent
        name: gpu-example

说明:在真实生产环境中,不会使用nodeSelector来指定具体的GPU机器,一般通过resources字段中的limits部分指定需要的GPU资源数量

2、部署应用

[root@k8s-master01 ~]# kaf gpu-example.yaml

验证,观察到该应用成功部署到GPU机器上

[root@k8s-master01 ~]# kgp -owide | grep gpu-example
gpu-example-57fc4f765d-kmn4x   1/1     Running   0          33s   192.168.58.231   k8s-node02   <none>           <none>

3、环境复原

删除应用

[root@k8s-master01 ~]# k delete -f gpu-example.yaml 

删除污点

[root@k8s-master01 ~]# k taint node k8s-node02 gpu=true:NoSchedule-

删除标签

[root@k8s-master01 ~]# kubectl label node k8s-node02 gpu-