即时更新 PODs 的节点选择器字段

Update Node Selector field for PODs on the fly

这些天我一直在围绕 k8s 尝试不同的东西。我想知道 POD 规范中的字段 nodeSelector。 据我了解,我们必须为节点分配一些标签,这些标签可以进一步用于 POD 规范的 nodeSelector 字段部分。

根据 nodeSelector 将节点分配给 pods 工作正常。但是,在我创建 pod 之后,现在我想 update/overwrite nodeSelector 字段,它会根据更新的新 nodeSelector 标签将我的 pod 部署到新节点。

我认为这与使用 kubectl label 命令对普通标签所做的方式相同。

是否有任何技巧可以实现这种情况?

如果当前最新版本的 kubernetes 不可能做到这一点,我们为什么不考虑呢?

谢谢。

虽然按照 cookiedough 的建议手动编辑部署是一种选择,但我相信使用 kubctl patch 会是更好的解决方案。

您可以使用 yaml file or JSON string, which makes it easier to integrate thing into scripts. Here is a complete reference 进行修补。


例子

下面是我使用的nginx的简单部署,将在node-1:

创建
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
      nodeSelector:
        kubernetes.io/hostname: node-1

JSON补丁

您可以修补部署以更改所需的节点,如下所示:
kubectl patch deployments nginx-deployment -p '{"spec": {"template": {"spec": {"nodeSelector": {"kubernetes.io/hostname": "node-2"}}}}}'

YAML 补丁

由运行kubectl patch deployment nginx-deployment --patch "$(cat patch.yaml)",其中patch.yaml准备如下:

spec:
  template:
    spec:
      nodeSelector:
        kubernetes.io/hostname: node-2

两者都会导致调度程序在请求的节点上调度新 pod,并在新 pod 准备好后立即终止旧 pod。