<p>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.</p>

<p>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.</p>

<p>I am thinking this in the same way it is done for the normal labels using <a href="https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#label" rel="noreferrer">kubectl label</a> command.</p>

<p>Are there any hacks to achieve such a case ?</p>

<p>If this is not possible for the current latest versions of the kubernetes, why should not we consider it ?</p>

<p>Thanks.</p>

Pert8S
<p>While editing deployment manually as cookiedough suggested is one of the options, I believe using <code>kubctl patch</code> would be a better solution. </p>

<p>You can either patch by using <a href="https://kubernetes.io/docs/tasks/run-application/update-api-object-kubectl-patch/" rel="noreferrer">yaml</a> file or <a href="https://kubernetes.io/docs/tasks/run-application/update-api-object-kubectl-patch/#use-a-json-merge-patch-to-update-a-deployment" rel="noreferrer">JSON</a> string, which makes it easier to integrate thing into scripts. Here is a complete <a href="https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#patch" rel="noreferrer">reference</a>.</p>

<hr>

<h1>Example</h1>

<p>Here's a simple deployment of nginx I used, which will be created on <code>node-1</code>:</p>

<pre><code>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
</code></pre>

<h3>JSON patch</h3>

<p>You can patch the deployment to change the desired node as follows:<br/><code>kubectl patch deployments nginx-deployment -p '{"spec": {"template": {"spec": {"nodeSelector": {"kubernetes.io/hostname": "node-2"}}}}}'</code></p>

<h3>YAML patch</h3>

<p>By running <code>kubectl patch deployment nginx-deployment --patch "$(cat patch.yaml)"</code>, where patch.yaml is prepared as follows:</p>

<pre><code>spec:
  template:
    spec:
      nodeSelector:
        kubernetes.io/hostname: node-2
</code></pre>

<p>Both will result in scheduler scheduling new pod on requested node, and terminating the old one as soon as the new one is ready.</p>
