首页 > 解决方案 > Hybrid between replicaset and daemonset

问题描述

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?

标签: kubernetes

解决方案


在部署或副本集中,您可以使用 podAffinity 和 podAntiaffinity。

pod 间亲和性和反亲和性允许您基于节点上已经运行的 pod 上的标签而不是基于节点上的标签来限制您的 pod 有资格调度的节点。

规则的形式是“如果 X 已经在运行一个或多个满足规则 Y 的 Pod,则该 Pod 应该(或者,在反亲和的情况下,不应该)在 X 中运行”。Y 表示为具有可选关联命名空间列表的 LabelSelector。

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" 

上面的示例 nginx pod1 和 pod2 永远不会被安排在同一个节点上。

在官方文档中查找更多详细信息。


推荐阅读