Home Explore Blog CI



kubernetes

3rd chunk of `content/en/blog/_posts/2017-03-00-Advanced-Scheduling-In-Kubernetes.md`
a4bd0b1c9392bafe2d63218e1109d0d747a7c63e556a50e60000000100000b90
 ```



In addition to moving taints and tolerations to _beta_ in Kubernetes 1.6, we have introduced an _alpha_ feature that uses taints and tolerations to allow you to customize how long a pod stays bound to a node when the node experiences a problem like a network partition instead of using the default five minutes. See [this section](/docs/user-guide/node-selection/#per-pod-configurable-eviction-behavior-when-there-are-node-problems-alpha-feature) of the documentation for more details.



**Pod Affinity/Anti-Affinity**



Node affinity/anti-affinity allows you to constrain which nodes a pod can run on based on the nodes’ labels. But what if you want to specify rules about how pods should be placed relative to one another, for example to spread or pack pods within a service or relative to pods in other services? For that you can use [pod affinity/anti-affinity](/docs/user-guide/node-selection/#inter-pod-affinity-and-anti-affinity-beta-feature), which is also _beta_ in Kubernetes 1.6.



Let’s look at an example. Say you have front-ends in service S1, and they communicate frequently with back-ends that are in service S2 (a “north-south” communication pattern). So you want these two services to be co-located in the same cloud provider zone, but you don’t want to have to choose the zone manually--if the zone fails, you want the pods to be rescheduled to another (single) zone. You can specify this with a pod affinity rule that looks like this (assuming you give the pods of this service a label “service=S2” and the pods of the other service a label “service=S1”):



```
affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: service
            operator: In
            values: [“S1”]
        topologyKey: failure-domain.beta.kubernetes.io/zone
 ```


As with node affinity/anti-affinity, there is also a preferredDuringSchedulingIgnoredDuringExecution variant.  

Pod affinity/anti-affinity is very flexible. Imagine you have profiled the performance of your services and found that containers from service S1 interfere with containers from service S2 when they share the same node, perhaps due to cache interference effects or saturating the network link. Or maybe due to security concerns you never want containers of S1 and S2 to share a node. To implement these rules, just make two changes to the snippet above -- change podAffinity to podAntiAffinity and change topologyKey to kubernetes.io/hostname.  

**Custom Schedulers**  

If the Kubernetes scheduler’s various features don’t give you enough control over the scheduling of your workloads, you can delegate responsibility for scheduling arbitrary subsets of pods to your own custom scheduler(s) that run(s) alongside, or instead of, the default Kubernetes scheduler. [Multiple schedulers](/docs/admin/multiple-schedulers/) is _beta_ in Kubernetes 1.6.  

Title: Pod Affinity/Anti-Affinity and Custom Schedulers
Summary
Pod affinity/anti-affinity allows specifying rules for pod placement relative to each other, exemplified by co-locating front-ends (S1) and back-ends (S2) in the same cloud provider zone using label selectors and topology keys. `preferredDuringSchedulingIgnoredDuringExecution` is also available. Pod anti-affinity prevents services from interfering by avoiding co-location on the same node. For more control, custom schedulers can be used alongside or instead of the default Kubernetes scheduler.