动态更新POD的节点选择器字段 [英] Update Node Selector field for PODs on the fly
问题描述
这些天来,我一直在尝试围绕k8s做不同的事情.我想知道POD规范中的字段nodeSelector. 据我了解,我们必须为节点分配一些标签,并且这些标签可以在POD规范的nodeSelector字段部分中进一步使用.
I have been trying different things around k8s these days. I am wondering about the field nodeSelector in the POD specification. As I understand we have to assign some labels to the nodes and these labels can further be used in the nodeSelector field part of the POD specification.
基于nodeSelector将节点分配给Pod可以正常工作.但是,在创建Pod之后,现在我想更新/覆盖nodeSelector字段,该字段将根据更新的新nodeSelector标签将我的Pod部署到新节点.
Assignment of the node to pods based on nodeSelector works fine. But, after I create the pod, now I want to update/overwrite the nodeSelector field which would deploy my pod to new node based on new nodeSelector label updated.
我正在以与使用有没有骇客来实现这种情况?
Are there any hacks to achieve such a case ?
如果当前最新版本的kubernetes无法做到这一点,为什么我们不考虑呢?
If this is not possible for the current latest versions of the kubernetes, why should not we consider it ?
谢谢.
推荐答案
虽然按照cookiedough的建议手动编辑部署是一种选择,但我相信使用kubctl patch
是更好的解决方案.
While editing deployment manually as cookiedough suggested is one of the options, I believe using kubctl patch
would be a better solution.
您可以使用 yaml
You can either patch by using yaml file or JSON string, which makes it easier to integrate thing into scripts. Here is a complete reference.
这是我使用的nginx的简单部署,它将在node-1
上创建:
Here's a simple deployment of nginx I used, which will be created on 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"}}}}}'
通过运行kubectl patch deployment nginx-deployment --patch "$(cat patch.yaml)"
,其中patch.yaml的准备如下:
By running kubectl patch deployment nginx-deployment --patch "$(cat patch.yaml)"
, where patch.yaml is prepared as follows:
spec:
template:
spec:
nodeSelector:
kubernetes.io/hostname: node-2
这两种方法都会导致调度程序在请求的节点上调度新的pod,并在新的pod准备好后立即终止旧的pod.
Both will result in scheduler scheduling new pod on requested node, and terminating the old one as soon as the new one is ready.
这篇关于动态更新POD的节点选择器字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!