k8s daemon node selector

Running Pods on Only Some Nodes

If you specify a .spec.template.spec.nodeSelector, then the DaemonSet controller will create Pods on nodes which match that node selector. Likewise if you specify a .spec.template.spec.affinity, then DaemonSet controller will create Pods on nodes which match that node affinity. If you do not specify either, then the DaemonSet controller will create Pods on all nodes.

场景:

内网K8S集群,应开发需求,每个应用环境,需要独立的consul,并且应用连接到node上consul-client。但是consul-client并不是所有node都要有,只是存在被选中的node上

从github consul-helm-chart,修改helm value.yaml 文件,(良好的习惯,将helm的value.yaml上传到仓库)

  • 修改image 版本对应版本,以及storage class 等等
  • 另一处,修改client的nodeSelector(只在特定的节点,启动consul client)
    1
    2
    3
    4
    5
    6
    7
    322   # nodeSelector labels for client pod assignment, formatted as a multi-line string.
    323 # ref: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#nodeselector
    324 # Example:
    325 # nodeSelector: |
    326 # beta.kubernetes.io/arch: amd64
    327 nodeSelector: |
    328 project: dop-test
  • 下一步,我们需要让节点自动注册到自己node上的consul client上。需要pods能够获取到本机consul client 的IP。于是通过valueFrom🤓.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    apiVersion: v1
    kind: Pod
    metadata:
    name: consul-example
    spec:
    containers:
    - name: example
    image: "consul:latest"
    env:
    - name: HOST_IP
    valueFrom:
    fieldRef:
    fieldPath: status.hostIP
    command:
    - "/bin/sh"
    - "-ec"
    - |
    export CONSUL_HTTP_ADDR="${HOST_IP}:8500"
    consul kv put hello world
    restartPolicy: Never
  • 最后便是应用程序,通过consul的headless svc 链接,注册进来啦。

Author: Chandler Kwok
Link: http://yoursite.com/2020/03/26/k8s-daemon-node-selector/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.