Kubernetes pod distribution

为君一笑 提交于 2019-12-06 07:56:40

You can still use node labels and use nodeSelector parameter.

You can add node labels by using kubectl...

kubectl label nodes <node-name> <label-key>=<label-value> to add a label to the node you’ve chosen.

But more advanced way is use affinity for pod distribution...

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

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

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:

(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

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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!