Hybrid between replicaset and daemonset

2/19/2020

Is there such a thing as a hybrid between a replicaset and a daemonset. I want to specify that I always want to have 2 pods up. But those pods must never be on the same node. (and I have like 10 nodes) Is there a way I can achieve this?

-- user2997204
kubernetes

1 Answer

2/19/2020

In a deployment or replicaSet you can use podAffinity and podAntiaffinity.

Inter-pod affinity and anti-affinity allow you to constrain which nodes your pod is eligible to be scheduled, based on labels on pods that are already running on the node rather than based on labels on nodes.

The rules are of the form “this pod should (or, in the case of anti-affinity, shouldn't) run in an X if that X is already running one or more pods that meet rule Y”. Y is expressed as a LabelSelector with an optional associated list of namespaces.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: k8s.gcr.io/nginx-slim:0.8
        ports:
        - containerPort: 80
          name: web
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - nginx
            topologyKey: "kubernetes.io/hostname" 

Above example nginx pod1 and pod2 will never be scheduled on same node.

Find more details in the official docs.

-- Arghya Sadhu
Source: StackOverflow