Kubernetes Pod发行 [英] Kubernetes pod distribution

查看:93
本文介绍了Kubernetes Pod发行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几年中,我在Docker上做了大量的工作,但是当谈到Kubernetes时,我是一个新手.我从今天开始,与过去使用Docker swarm进行思考的方式相比,我在Pod概念的实用性方面苦苦挣扎.

I've worked quite a lot with Docker in the past years, but I'm a newbie when it comes to Kubernetes. I'm starting today and I am struggling with the usefulness of the Pod concept in comparison with the way I used to do thinks with Docker swarm.

比方说,我有一个包含7台强大机器的集群,并且我具有以下堆栈:

Let's say that I have a cluster with 7 powerful machines and I have the following stack:

  • 我想要三个Cassandra副本,每个副本都在专用计算机(3/7)中运行
  • 我想要两个分别在专用计算机(5/7)中运行的Kafka副本
  • 我想要一个MyProducer副本在自己的计算机上运行,​​从网络上接收消息并将其推送到Kafka(6/7)
  • 我想要3个MyConsumer副本全部在最后一台计算机(7/7)中运行,它们从Kafka中拉出并插入Cassandra中.

我使用docker swarm来处理带有节点标签的容器分发,例如我将三台机器和Cassandra容器配置标记为C_HOST,将两台机器和Kafka配置标记为K_HOST,...群部署将正确放置每个容器.

With docker swarm I used to handle container distribution with node labels, e.g. I would label three machines and Cassandra container configuration as C_HOST, 2 machines and Kafka configuration as K_HOST,... The swarm deployment would place each container correctly.

我有以下问题:

  • 与我以前的方法相比(例如简单性),Kubernetes Pod是否带来任何优势?我了解我仍然需要配置标签,如果这样,我看不到吸引力.

  • Does Kubernetes pods bring any advantage comparing to my previous approach (e.g. simplicity)? I understood that I am still required to configure labels, if so, I don't see the appeal.

配置这些容器的正确方法是什么?是Cassandra副本的一个容器,Kafka副本的一个容器,MyConsumer副本的一个容器和MyProducer的一个容器吗?

What would be the correct way to configure these pods? Would it be one pod for Cassandra replicas, one pod for Kafka replicas, one pod for MyConsumer replicas and one pod for MyProducer?

推荐答案

使用容器反亲和力,可以确保一个容器与其他带有特定标签的容器不在同一位置.

Using pod anti-affinity, you can ensure that a pod is not co-located with other pods with specific labels.

所以说您有一个标签"app",其值为"cassandra","kafka","my-producer"和"my-consumer".

So say your have a label "app" with values "cassandra", "kafka", "my-producer" and "my-consumer".

由于您希望自己在专用节点上都拥有cassandra,kafka和my-producer,因此您只需为所有现有标签配置反亲和力即可:

Since you want to have cassandra, kafka and my-producer on dedicated nodes all by themselves, you simply configure an anti-affinity to ALL the existing labels:

(请参阅 https://kubernetes.io/docs/concepts/配置/assign-pod-node/(用于完整模式)

(see https://kubernetes.io/docs/concepts/configuration/assign-pod-node/ for full schema)

  podAntiAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
    - labelSelector:
      matchExpressions:
      - key: app
        operator: In
        values:
        - cassandra
        - kafka
        - my-producer
        - my-consumer

这是针对"Pod"资源的,因此您需要在Pod模板的部署中(也定义了多少个副本)进行定义.

This is for a "Pod" resource, so you'd define this in a deployment (where you also define how many replicas) in the pod template.

由于您希望在同一节点上运行三个my-consumer实例(或者实际上,您不在乎它们在哪里运行,因为到目前为止只剩下一个节点),因此您无需定义任何有关亲和力或反亲和力:

Since you want three instances of my-consumer running on the same node (or really, you don't care where they run, since by now only one node is left), you do not need to define anything about affinity or anti-affinity:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-consumer
  namespace: default
  labels:
    app: my-consumer
spec:
  selector:
    matchLabels:
      app: my-consumer
  replicas: 3 # here you set the number of replicas that should run
  template:   # this is the pod template
    metadata:
      labels:
        app: my-consumer # now this is the label you can set an anti-affinity to
    spec:
      containers:
      - image: ${IMAGE}
        name: my-consumer
#       affinity:
# now here below this you'd put the affinity-settings from above
# for the other deployments

这篇关于Kubernetes Pod发行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆