Pod Affinity in Skuber

12/19/2019

When creating Kubernetes Pods with Skuber, how do you define a Pod that has affinity with another Pod?

Eg:

val podASpec = Pod.Spec( ..., affinity = podB )
val podBSpec = Pod.Spec( ..., affinity = podA )
-- Jethro
kubernetes
scala

1 Answer

12/19/2019

First, decide on a topologyKey to use. If you want affinity to work per distinct node, you can use kubernetes.io/hostname.

You can create a Skuber Pod Affinity like so:

import skuber.Pod.Affinity
import skuber.Pod.Affinity.{PodAffinity, PodAffinityTerm}
import skuber.LabelSelector.dsl._

val podAffinty = PodAffinity( List(PodAffinityTerm(
  Some(LabelSelector("myLabel" is "something")),
  List("my-namespace"),
  topologyKey
)))
val affinity = Some( Affinity(None, Some( podAffinty ) ) )

and pass in affinity=affinity when creating your Pod.Spec.

-- Jethro
Source: StackOverflow