<p>Is there any shortcut or kubectl command or REST API call to get a list of worker nodes only. ( not including the master nodes )</p>

<p><strong>Update</strong>:
For the masters we can do like this:</p>

<pre><code>kubectl get nodes --selector=node-role.kubernetes.io/master
</code></pre>

<p>for the workers I dont see any such label created by default. Can we do get by reversing or do != kind of thing on selector.</p>

<p>We can't grep it either:</p>

<pre><code>C02W84XMHTD5:ucp iahmad$ kubectl get nodes | grep worker
C02W84XMHTD5:ucp iahmad$ 
C02W84XMHTD5:ucp iahmad$ kubectl get nodes -o wide| grep worker
C02W84XMHTD5:ucp iahmad$ 
C02W84XMHTD5:ucp iahmad$ kubectl get nodes -o yaml | grep worker
C02W84XMHTD5:ucp iahmad$ 
C02W84XMHTD5:ucp iahmad$ kubectl get nodes -o json | grep worker
C02W84XMHTD5:ucp iahmad$ 
</code></pre>

<p>My use case is that want to get this list every minute to update the external load balancer pools, in case new nodes are added, removed from the cluster. Indeed I can label them myself but if there is some default built in way of doing this would be useful</p>

Ijaz Ahmad
<p>You can get roles/labels of your nodes by </p>

<pre><code>kubectl get nodes --show-labels
</code></pre>

<p>in my case, I do have three nodes each having the given roles and labels:</p>

<pre><code>NAME        STATUS    ROLES                      AGE       VERSION   LABELS
host01      Ready     controlplane,etcd,worker   61d       v1.10.5   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=host01,node-role.kubernetes.io/controlplane=true,node-role.kubernetes.io/etcd=true,node-role.kubernetes.io/worker=true
host02      Ready     etcd,worker                61d       v1.10.5   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=host02,node-role.kubernetes.io/etcd=true,node-role.kubernetes.io/worker=true
host03      Ready     etcd,worker                61d       v1.10.5   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=host03,node-role.kubernetes.io/etcd=true,node-role.kubernetes.io/worker=true
</code></pre>

<p>Only host01 has the label <code>controlplane, worker</code> and <code>etcd</code>. The other two have <code>etcd</code> and <code>worker</code> (Scroll right to see the labels as well).</p>

<p>So I can get all worker nodes by </p>

<pre><code>kubectl get nodes -l node-role.kubernetes.io/worker=true

NAME        STATUS    ROLES                      AGE       VERSION
host01      Ready     controlplane,etcd,worker   61d       v1.10.5
host02      Ready     etcd,worker                61d       v1.10.5
host03      Ready     etcd,worker                61d       v1.10.5
</code></pre>

<p>To exclude <code>controlplane</code>s, you can exclude them with a second label by <code>!=true</code></p>

<pre><code>kubectl get nodes -l node-role.kubernetes.io/worker=true,node-role.kubernetes.io/controlplane!=true

NAME        STATUS    ROLES         AGE       VERSION
host02      Ready     etcd,worker   61d       v1.10.5
host03      Ready     etcd,worker   61d       v1.10.5
</code></pre>

<p>Please adapt that to your labels or set labels accordingly to your cluster. In my case it is a <a href="https://rancher.com/" rel="noreferrer">Rancher 2.0</a> cluster. The labels are automatically created by Rancher when added a node.</p>

<p>The API for that is in Rancher at (with the filter already appended):</p>

<pre><code>/v3/clusters/c-xxxxx/nodes?worker=true&amp;controlPlane_ne=true
</code></pre>
