标签(空格分隔): kubernetes 系列
一: kubernetes 的调度
二: kubernetes 的节点的亲和性
三: kubernetes 的污点与容忍
四: kubernetes 的固定节点
一: kubernetes 的调度
1.1 scheduler 的介绍
Scheduler 是 kubernetes 的调度器, 主要的任务是把定义的 pod 分配到集群的节点上. 听起来非常简单, 但有很多要考虑的问题:
1. 公平: 如何保证每个节点都能被分配资源
2. 资源高效利用: 集群所有资源最大化被使用
3. 效率: 调度的性能要好, 能够尽快地对大批量的 pod 完成调度工作
4. 灵活: 允许用户根据自己的需求控制调度的逻辑
Sheduler 是作为单独的程序运行的, 启动之后会一直坚挺 API Server, 获取 PodSpec.NodeName 为空的 pod, 对每个 pod 都会创建一个 binding, 表明该 pod 应该放到哪个节点上
1.2 调度过程
调度分为几个部分: 首先是过滤掉不满足条件的节点, 这个过程称为 predicate ; 然后对通过的节点按照优先级排序, 这个是 priority; 最后从中选择优先级最高的节点. 如果中间任何一步骤有错误, 就直接返回错误
Predicate 有一系列的算法可以使用:
PodFitsResources : 节点上剩余的资源是否大于 pod 请求的资源
PodFitsHost : 如果 pod 指定了 NodeName, 检查节点名称是否和 NodeName 匹配
PodFitsHostPorts : 节点上已经使用的 port 是否和 pod 申请的 port 冲突
PodSelectorMatches : 过滤掉和 pod 指定的 label 不匹配的节点
NoDiskConflict : 已经 mount 的 volume 和 pod 指定的 volume 不冲突, 除非它们都是只读
如果在 predicate 过程中没有合适的节点, pod 会一直在 pending 状态, 不断重试调度, 直到有节点满足条件.
经过这个步骤, 如果有多个节点满足条件, 就继续 priorities 过程: 按照优先级大小对节点排序
优先级由一系列键值对组成, 键是该优先级项的名称, 值是它的权重(该项的重要性). 这些优先级选项包括:
LeastRequestedPriority : 通过计算 CPU 和 Memory 的使用率来决定权重, 使用率越低权重越高. 换句话说, 这个优先级指标倾向于资源使用比例更低的节点
BalancedResourceAllocation : 节点上 CPU 和 Memory 使用率越接近, 权重越高. 这个应该和上面的一起使用, 不应该单独使用
ImageLocalityPriority: 倾向于已经有要使用镜像的节点, 镜像总大小值越大, 权重越高通过算法对所有的优先级项目和权重进行计算, 得出最终的结果
二: kubernetes 调度的亲和性
键值运算关系
In:label 的值在某个列表中
NotIn:label 的值不在某个列表中
Gt:label 的值大于某个值
Lt:label 的值小于某个值
Exists: 某个 label 存在
DoesNotExist: 某个 label 不存在
2.1 节点亲和性
节点亲和性
pod.spec.nodeAffinity
preferredDuringSchedulingIgnoredDuringExecution: 软策略
requiredDuringSchedulingIgnoredDuringExecution: 硬策略
requiredDuringSchedulingIgnoredDuringExecution
VIM affit1.YAML
- ---
- apiVersion: v1
- kind: Pod
- metadata:
- name: affinity
- labels:
- App: node-affinity-pod
- spec:
- containers:
- - name: with-node-affinity
- image: wangyanglinux/myapp:v1
- affinity:
- nodeAffinity:
- requiredDuringSchedulingIgnoredDuringExecution:
- nodeSelectorTerms:
- - matchExpressions:
- - key: kubernetes.io/hostname
- operator: NotIn
- values:
- - node02.flyfish
- ---
- kubectl get pod --show-labes
排除 operator:NotIn 就是不在 node02.flyfish 运行
- kubectl apply -f affit1.YAML
- kubectl get pod
- kubernetes.io/hostname:
- kubectl get node --show-labels
VIM aifft2.YAML
- ----
- apiVersion: v1
- kind: Pod
- metadata:
- name: affinity1
- labels:
- App: node-affinity-pod1
- spec:
- containers:
- - name: with-node-affinity1
- image: wangyanglinux/myapp:v1
- affinity:
- nodeAffinity:
- requiredDuringSchedulingIgnoredDuringExecution:
- nodeSelectorTerms:
- - matchExpressions:
- - key: kubernetes.io/hostname
- operator: In
- values:
- - node03.flyfish
- ----
在 node03.flyfish 节点上 运行
kubectl apply -f aifft2.YAML
VIM aifft3.YAML
- -----
- apiVersion: v1
- kind: Pod
- metadata:
- name: affinity2
- labels:
- App: node-affinity-pod2
- spec:
- containers:
- - name: with-node-affinity2
- image: wangyanglinux/myapp:v1
- affinity:
- nodeAffinity:
- requiredDuringSchedulingIgnoredDuringExecution:
- nodeSelectorTerms:
- - matchExpressions:
- - key: kubernetes.io/hostname
- operator: NotIn
- values:
- - node03.flyfish
- -----
不在 node03.flyfish 运行
kubectl apply -f aifft3.YAML
preferredDuringSchedulingIgnoredDuringExecution: 软策略
VIM prefer.YAML
- ---
- apiVersion: v1
- kind: Pod
- metadata:
- name: affinity
- labels:
- App: node-affinity-pod
- spec:
- containers:
- - name: with-node-affinity
- image: wangyanglinux/myapp:v1
- affinity:
- nodeAffinity:
- preferredDuringSchedulingIgnoredDuringExecution:
- - weight: 1
- preference:
- matchExpressions:
- - key: source
- operator: In
- values:
- - node04.flyfish
- ---
软策略 如果节点 存在 者 在这个节点上面运行, 如果节点不存在者会随便选择一个节点去运行
- ---
- kubectl apply -f perfer.YAML
- kubectl get pod -o wide
VIM perfer1.YAML
- ---
- apiVersion: v1
- kind: Pod
- metadata:
- name: affinity1
- labels:
- App: node-affinity-pod1
- spec:
- containers:
- - name: with-node-affinity1
- image: wangyanglinux/myapp:v1
- affinity:
- nodeAffinity:
- preferredDuringSchedulingIgnoredDuringExecution:
- - weight: 1
- preference:
- matchExpressions:
- - key: source
- operator: In
- values:
- - node03.flyfish
- ---
因为 node3.flyfish 这个节点存在 所以 就会在 node03.flyfish 这个节点上面运行
- kubectl apply -f perfer1.YAML
- kubectl get pod -o wide
合体使用
---
VIM perfer3.YAML
- -----
- apiVersion: v1
- kind: Pod
- metadata:
- name: affinity2
- labels:
- App: node-affinity-pod2
- spec:
- containers:
- - name: with-node-affinity2
- image: wangyanglinux/myapp:v1
- affinity:
- nodeAffinity:
- requiredDuringSchedulingIgnoredDuringExecution:
- nodeSelectorTerms:
- - matchExpressions:
- - key: kubernetes.io/hostname
- operator: NotIn
- values:
- - node02.flyfish
- preferredDuringSchedulingIgnoredDuringExecution:
- - weight: 1
- preference:
- matchExpressions:
- - key: source
- operator: In
- values:
- - node03.flyfish
- ----
从上到下 进行亲和性 匹配 先满足硬 匹配在满足软匹配
- kubectl apply -f perfer3.YAML
- kubectl get pod -o wide
2.2 pod 亲和性
pod.spec.affinity.podAffinity/podAntiAffinity
preferredDuringSchedulingIgnoredDuringExecution: 软策略
requiredDuringSchedulingIgnoredDuringExecution: 硬策略
新建一个 node01 的 pod
-----
VIM node01.YAML
- ----
- apiVersion: v1
- kind: Pod
- metadata:
- name: node01
- labels:
- App: node01
- spec:
- containers:
- - name: with-node-affinity1
- image: wangyanglinux/myapp:v1
- ----
- kubectl apply -f node01.YAML
- kubectl get pod -o wide
在 node02.flyfish 上面运行
VIM pod1.YAML
- -----
- apiVersion: v1
- kind: Pod
- metadata:
- name: pod-3
- labels:
- App: pod-3
- spec:
- containers:
- - name: pod-3
- image: wangyanglinux/myapp:v1
- affinity:
- podAffinity:
- requiredDuringSchedulingIgnoredDuringExecution:
- - labelSelector:
- matchExpressions:
- - key: App
- operator: In
- values:
- - node01
- topologyKey: kubernetes.io/hostname
- ----
pod-3 这个 pod 必须和 node01 的 pod 在一个节点上面
- kubectl apply -f pod1.YAML
- kubectl get pod -o wide
VIM pod2.YAML
- -----
- apiVersion: v1
- kind: Pod
- metadata:
- name: pod-4
- labels:
- App: pod-4
- spec:
- containers:
- - name: pod-4
- image: wangyanglinux/myapp:v1
- affinity:
- podAntiAffinity:
- requiredDuringSchedulingIgnoredDuringExecution:
- - labelSelector:
- matchExpressions:
- - key: App
- operator: In
- values:
- - node01
- topologyKey: kubernetes.io/hostname
- ------
必须和 node01 的 pod 不在同一个节点
- kubectl apply -f pod2.YAML
- kubectl get pod -o wide
2.3 亲和性 / 反亲和性调度策略比较如下:
三: kubernetes 的污点与容忍
3.1 Taint 和 Toleration
节点亲和性, 是 pod 的一种属性(偏好或硬性要求), 它使 pod 被吸引到一类特定的节点. Taint 则相反, 它使
节点 能够 排斥 一类特定的 pod
Taint 和 toleration 相互配合, 可以用来避免 pod 被分配到不合适的节点上. 每个节点上都可以应用一个或多个
taint , 这表示对于那些不能容忍这些 taint 的 pod, 是不会被该节点接受的. 如果将 toleration 应用于 pod
上, 则表示这些 pod 可以 (但不要求) 被调度到具有匹配 taint 的节点上
3.2 污点(Taint)
Ⅰ, 污点 ( Taint ) 的组成
使用 kubectl taint 命令可以给某个 Node 节点设置污点, Node 被设置上污点之后就和 Pod 之间存在了一种相
斥的关系, 可以让 Node 拒绝 Pod 的调度执行, 甚至将 Node 已经存在的 Pod 驱逐出去
每个污点的组成如下:
每个污点有一个 key 和 value 作为污点的标签, 其中 value 可以为空, effect 描述污点的作用. 当前 taint
key=value:effect
effect 支持如下三个选项:
NoSchedule : 表示 k8s 将不会将 Pod 调度到具有该污点的 Node 上
PreferNoSchedule : 表示 k8s 将尽量避免将 Pod 调度到具有该污点的 Node 上
NoExecute : 表示 k8s 将不会将 Pod 调度到具有该污点的 Node 上, 同时会将 Node 上已经存在的 Pod 驱
逐出去
- kubectl get node
- kubectl describe node node01.flyfish |grep Taints
3.3 污点的设置, 查看和去除
- # 设置污点
- kubectl taint nodes node1.flyfish key1=value1:NoSchedule
- # 节点说明中, 查找 Taints 字段
- kubectl describe pod pod-name
- # 去除污点
- kubectl taint nodes node1 key1:NoSchedule-
驱逐 node03.flyfish 节点上面的 pod
kubectl taint nodes node03.flyfish check=flyfish:NoExecute
因为 此处的 pod 不是使用 deployment 创建的 自主式 pod 驱逐就没有了
3.4 容忍(Tolerations)
设置了污点的 Node 将根据 taint 的 effect:NoSchedule,PreferNoSchedule,NoExecute 和 Pod 之间产生
互斥的关系, Pod 将在一定程度上不会被调度到 Node 上. 但我们可以在 Pod 上设置容忍 ( Toleration ) , 意思
是设置了容忍的 Pod 将可以容忍污点的存在, 可以被调度到存在污点的 Node 上
- pod.spec.tolerations
- ------
- tolerations:
- - key: "key1"
- operator: "Equal"
- value: "value1"
- effect: "NoSchedule"
- tolerationSeconds: 3600
- - key: "key1"
- operator: "Equal"
- value: "value1"
- effect: "NoExecute"
- - key: "key2"
- operator: "Exists"
- effect: "NoSchedule"
- ------
VIM pod5.YAML
- ----
- apiVersion: v1
- kind: Pod
- metadata:
- name: pod-5
- labels:
- App: pod-5
- spec:
- containers:
- - name: pod-5
- image: wangyanglinux/myapp:v1
- tolerations:
- - key: "check"
- operator: "Equal"
- value: "flyfish"
- effect: "NoExecute"
- tolerationSeconds: 3600
容忍 3600s 在节点上面运行
-----
驱逐 node02.flyfish 上面的 pod
kubectl taint nodes node02.flyfish check=flyfish:NoExecute
kubectl apply -f pod5.YAML
其中 key, vaule, effect 要与 Node 上设置的 taint 保持一致
operator 的值为 Exists 将会忽略 value 值
tolerationSeconds 用于描述当 Pod 需要被驱逐时可以在 Pod 上继续保留运行的时间
1. 当不指定 key 值时, 表示容忍所有的污点 key:
- tolerations:
- - operator: "Exists"
2. 当不指定 effect 值时, 表示容忍所有的污点作用
- tolerations:
- - key: "key"
- operator: "Exists"
3. 有多个 Master 存在时, 防止资源浪费, 可以如下设置
kubectl taint nodes node01.flyfish node-role.kubernetes.io/master=:PreferNoSchedule
这样主节点上面 也可以 运行 pod 了
去除 node02.flyfish 与 node03.flyfish 的污点
- kubectl taint nodes node02.flyfish check=flyfish:NoExecute-
- kubectl taint nodes node03.flyfish check=flyfish:NoExecute-
四: kubernetes 的固定节点
1,Pod.spec.nodeName 将 Pod 直接调度到指定的 Node 节点上, 会跳过 Scheduler 的调度策略, 该匹配规则是强制匹配
----
VIM pod.YAML
- ----
- apiVersion: extensions/v1beta1
- kind: Deployment
- metadata:
- name: myweb
- spec:
- replicas: 3
- template:
- metadata:
- labels:
- App: myweb
- spec:
- nodeName: node02.flyfish
- containers:
- - name: myweb
- image: wangyanglinux/myapp:v1
- ports:
- - containerPort: 80
- ----
- kubectl apply -f pod.YAML
- kubectl get pod -o wide
强制 pod 运行在 node02.flyfish 上面
Pod.spec.nodeSelector: 通过 kubernetes 的 label-selector 机制选择节点, 由调度器调度策略匹配 label,
而后调度 Pod 到目标节点, 该匹配规则属于强制约束
----
VIM pod1.YAML
- -----
- apiVersion: extensions/v1beta1
- kind: Deployment
- metadata:
- name: myweb222
- spec:
- replicas: 2
- template:
- metadata:
- labels:
- App: myweb
- spec:
- nodeSelector:
- disk: ssd
- containers:
- - name: myweb
- image: wangyanglinux/myapp:v1
- ports:
- - containerPort: 80
- ----
- kubectl apply -f pod1.YAML
必须存在 labels disk:ssd 才能运行 pod
将 node03.flyfish 打上 disk:ssd 标签
- kubectl label node node03.flyfish disk=sshd
- kubectl get pod -o wide
来源: http://blog.51cto.com/flyfish225/2483131