三种调度粘性,主要根据官方文档说明:

NodeSelector(定向调度)、NodeAffinity(Node亲和性)、PodAffinity(Pod亲和性)。

1.      nodeSelector

提供简单的pod部署限制,pod选择一个或多个node的label部署。

①   给node添加label

kubectl label nodes <node-name> <label-key>=<label-value>

②   为pod添加nodeSelector机制

apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
env: test
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
nodeSelector:
disktype: ssd

③   部署pod

2.      nodeAffinity

该功能是nodeSelector的改进,现在处于beta阶段。

主要的改进有以下几点:

-       语法更多样(不仅支持“AND”,)

-       不仅可以指定硬条件,还支持软条件

-       支持pod亲和性

当nodeAffinity成熟的时候,nodeSelector会被废弃。

requiredDuringSchedulingIgnoredDuringExecution   #硬性强制

preferredDuringSchedulingIgnoredDuringExecution  #软性配置

IgnoredDuringExecution  表示 ,如果一个pod所在的节点 在Pod运行期间其标签发生了改变,不再符合该Pod的节点亲和性需求,则系统将忽略Node上Label的变化,该pod继续在该节点上运行。

如果同时设置了nodeSelector和nodeAffinity,则需要同时满足才能成为候选者node。

下面看一个例子:

①     该pod只部署在具有label kubernetes.io/e2e-az-name=e2e-az1,kubernetes.io/e2e-az-name=e2e-az2的node上;且会优先选择具有label another-node-label-key= another-node-label-value的node,当然如果没有满足该条件的node,该pod也会部署在其它node上。

②     operator支持In, NotIn, Exists, DoesNotExist, Gt, Lt。可以使用NotIn和DoesNotExist实现node的反亲和性,或者使用pod taints与tolerations实现。

③     如果设置了多个nodeSelectorTerms,则只需要匹配其中一个就可以成为候选者node。

④     如果设置了多个matchExpressions,则需要全部匹配才能成为候选者node。

⑤     weight取值范围是1-100,对于有多个软条件的情况时,将匹配了改条件的weight相加,取最大的值为最优先候选者node。

# cat pods/pod-with-node-affinity.yaml

pods/pod-with-node-affinity.yaml

apiVersion: v1
kind: Pod
metadata:
name: with-node-affinity
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution: #hard条件必须匹配
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/e2e-az-name
operator: In #支持In, NotIn, Exists, DoesNotExist, Gt, Lt
values:
- e2e-az1
- e2e-az2
preferredDuringSchedulingIgnoredDuringExecution: #soft条件优先匹配
- weight: 1 #取值范围1-100
preference:
matchExpressions:
- key: another-node-label-key
operator: In
values:
- another-node-label-value
containers:
- name: with-node-affinity
image: k8s.gcr.io/pause:2.0

3.      Inter-pod affinity and anti-affinity (beta feature)

pod亲和性与反亲和性是根据pod的label挑选scheduler的候选者node,而不是根据node的label。

pod亲和性只在一个namespace生效,因为pod具有namespace,所以pod亲和性设置隐含了namespace。

topologyKey指示作用域,使用node的label的一个key值表示。

还可以使用一个namespaces列表限定schedulerr调度时查找的pod限定,namespaces放在labelSelector和topologyKey同一层,如:

        podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: appname
operator: In
values:
- dbpool-server
topologyKey: kubernetes.io/hostname
namespaces: #这样只会查找poa-ea和pletest下面的pod,而不是全部
- poa-ea
- pletest

注意:Inter-pod affinity and anti-affinity需要消耗大量计算资源,会增加调度时间。如果node数量超过几百台的时候不建议使用。

注意:Pod反亲和性需要制定topologyKey

下面看一个例子:

①   出于安全考虑,requiredDuringSchedulingIgnoredDuringExecution的anti-affinity,topologyKey不允许为空;

②   For requiredDuringSchedulingIgnoredDuringExecution pod anti-affinity, the admission controller LimitPodHardAntiAffinityTopology was introduced to limit topologyKey to kubernetes.io/hostname. If you want to make it available for custom topologies, you may modify the admission controller, or simply disable it.

③   For preferredDuringSchedulingIgnoredDuringExecution pod anti-affinity, empty topologyKey is interpreted as “all topologies” (“all topologies” here is now limited to the combination of kubernetes.io/hostnamefailure-domain.beta.kubernetes.io/zone and failure-domain.beta.kubernetes.io/region).

pods/pod-with-pod-affinity.yaml

apiVersion: v1
kind: Pod
metadata:
name: with-pod-affinity
spec:
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: security
operator: In
values:
- S1
topologyKey: failure-domain.beta.kubernetes.io/zone
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: security
operator: In
values:
- S2
topologyKey: kubernetes.io/hostname
containers:
- name: with-pod-affinity
image: k8s.gcr.io/pause:2.0

4.      使用案例

需求:有一个web-server有3个实例,该web-server会使用到redis做为缓存。先需要将redis调度到和web-server同一个node。

①   部署redis,label app=store保证redis和web-server部署到相同的node

apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-cache
spec:
selector:
matchLabels:
app: store
replicas: 3
template:
metadata:
labels:
app: store
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- store
topologyKey: "kubernetes.io/hostname"
containers:
- name: redis-server
image: redis:3.2-alpine

②   部署web-server,与redis部署到一起,但是web-server之间不部署到一起。

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:1.12-alpine

5.      参考资料

http://blog.51cto.com/newfly/2066630

https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#affinity-and-anti-affinity

kubernetes Pod亲和性的更多相关文章

  1. 六、Kubernetes节点与 Pod 亲和性

    Kubernetes节点与 Pod 亲和性 一.节点亲和性策略介绍 ​pod.spec.nodeAffinity preferredDuringSchedulingIgnoredDuringExecu ...

  2. K8S调度之pod亲和性

    目录 Pod Affinity Pod亲和性调度 pod互斥性调度 Pod Affinity 通过<K8S调度之节点亲和性>,我们知道怎么在调度的时候让pod灵活的选择node,但有些时候 ...

  3. Kubernetes Pod 驱逐详解

    原文链接:Kubernetes Pod 驱逐详解 在 Kubernetes 中,Pod 使用的资源最重要的是 CPU.内存和磁盘 IO,这些资源可以被分为可压缩资源(CPU)和不可压缩资源(内存,磁盘 ...

  4. Kubernetes Pod 镜像拉取策略

    Kubernetes Pod 镜像拉取策略 官方文档:https://kubernetes.io/docs/concepts/containers/images/ • IfNotPresent:默认值 ...

  5. Kubernetes Pod 资源限制

    Kubernetes Pod 资源限制 官方文档:https://kubernetes.io/docs/concepts/configuration/manage-compute-resources- ...

  6. Kubernetes Pod 调度约束

    Kubernetes Pod 调度约束 可以将pod调度到指定的节点Node内 默认:根据节点资源利用率等分配Node节点. nodeName用于将Pod调度到指定的Node名称上 nodeSelec ...

  7. Kubernetes Pod故障归类与排查方法

    Pod概念 Pod是kubernetes集群中最小的部署和管理的基本单元,协同寻址,协同调度. Pod是一个或多个容器的集合,是一个或一组服务(进程)的抽象集合. Pod中可以共享网络和存储(可以简单 ...

  8. Python Django撸个WebSSH操作Kubernetes Pod(下)- 终端窗口自适应Resize

    追求完美不服输的我,一直在与各种问题斗争的路上痛并快乐着 上一篇文章Django实现WebSSH操作Kubernetes Pod最后留了个问题没有解决,那就是terminal内容窗口的大小没有办法调整 ...

  9. Kubernetes Pod 全面知识

    Pod 是在 Kubernetes 中创建和管理的.最小的可部署的计算单元,是最重要的对象之一.一个 Pod 中包含一个或多个容器,这些容器在 Pod 中能够共享网络.存储等环境. 学习 Kubern ...

随机推荐

  1. zjnu1726 STOGOVI (lca)

    Description Mirko is playing with stacks. In the beginning of the game, he has an empty stack denote ...

  2. HDU - 4221 贪心

    题意: 你有n个任务,每一个任务有一个完成所需时间AI,和一个截止时间BI.时间从0开始,如果完成任务的时间(设这个时间为ans)大于BI那么就会收到ans-BI的惩罚,问你完成所有这些任务你会收到的 ...

  3. CGI & FastCGI 协议

    目录 CGI 是什么 CGI 特点 CGI 的流程 FastCGI 是什么 CGI & FastCGI(转载) 推荐Blog: CGI是什么,FastCGI是什么 CGI 是什么 公共网关接口 ...

  4. VRRP(Virtual Router Redundancy Protocol) 虚拟路由器冗余协议简介

    因工作中使用Keepalived配置Nginx代理和MySQL代理的高可用,而Keepalived是VRRP协议在linux上的软件实现.因此了解了下VRRP的基础. 1. VRRP技术的引入 随着I ...

  5. haut-1282 ykc想吃好吃的

    1282: ykc想吃好吃的 时间限制: 1 秒  内存限制: 128 MB提交: 706  解决: 89提交 状态 题目描述 一天,ykc在学校闲的无聊,于是决定上街买点吃的,ykc很懒,本来就不是 ...

  6. 2017CCCC决赛 L1-3. 阅览室

    L1-3 阅览室(20 分) 天梯图书阅览室请你编写一个简单的图书借阅统计程序.当读者借书时,管理员输入书号并按下S键,程序开始计时:当读者还书时,管理员输入书号并按下E键,程序结束计时.书号为不超过 ...

  7. Leetcode(1)-两数之和

    给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], target ...

  8. 操作系统 part2

    一.程序的内存结构 references: newcoder 运行时,程序分为:text段.data段.BSS段(2个合称数据段).堆.栈. text段:代码段,静态分配内存,只读. data段:初始 ...

  9. mybatis(十一)mybatis常见问题

    用注解还是用 xml 配置? 常用注解:@Insert.@Select.@Update.@Delete.@Param.@Results. @Result 在 MyBatis 的工程中,我们有两种配置 ...

  10. docker-swarm----多机容器管理

    Docker Swarm: 准备三台机器,都装上 Docker docker swarm是docker官方提供的一套容器编排系统.它的架构如下: swarm是一系列节点的集合,而节点可以是一台裸机或者 ...