1
0
mirror of https://github.com/openshift/openshift-docs.git synced 2026-02-05 12:46:18 +01:00
Files
openshift-docs/modules/nodes-scheduler-node-selectors-cluster.adoc

203 lines
5.1 KiB
Plaintext

// Module included in the following assemblies:
//
// * nodes/nodes-scheduler-node-selector.adoc
:_mod-docs-content-type: PROCEDURE
[id="nodes-scheduler-node-selectors-cluster_{context}"]
= Creating default cluster-wide node selectors
You can use default cluster-wide node selectors on pods together with labels on nodes to constrain all pods created in a cluster to specific nodes.
With cluster-wide node selectors, when you create a pod in that cluster, {product-title} adds the default node selectors to the pod and schedules
the pod on nodes with matching labels.
You configure cluster-wide node selectors by editing the Scheduler Operator custom resource (CR). You add labels to a node, a compute machine set, or a machine config. Adding the label to the compute machine set ensures that if the node or machine goes down, new nodes have the label. Labels added to a node or machine config do not persist if the node or machine goes down.
[NOTE]
====
You can add additional key/value pairs to a pod. But you cannot add a different value for a default key.
====
.Procedure
To add a default cluster-wide node selector:
. Edit the Scheduler Operator CR to add the default cluster-wide node selectors:
+
[source,terminal]
----
$ oc edit scheduler cluster
----
+
.Example Scheduler Operator CR with a node selector
[source,yaml]
----
apiVersion: config.openshift.io/v1
kind: Scheduler
metadata:
name: cluster
...
spec:
defaultNodeSelector: type=user-node,region=east <1>
mastersSchedulable: false
----
<1> Add a node selector with the appropriate `<key>:<value>` pairs.
+
After making this change, wait for the pods in the `openshift-kube-apiserver` project to redeploy. This can take several minutes. The default cluster-wide node selector does not take effect until the pods redeploy.
. Add labels to a node by using a compute machine set or editing the node directly:
* Use a compute machine set to add labels to nodes managed by the compute machine set when a node is created:
.. Run the following command to add labels to a `MachineSet` object:
+
[source,terminal]
----
$ oc patch MachineSet <name> --type='json' -p='[{"op":"add","path":"/spec/template/spec/metadata/labels", "value":{"<key>"="<value>","<key>"="<value>"}}]' -n openshift-machine-api <1>
----
<1> Add a `<key>/<value>` pair for each label.
+
For example:
+
[source,terminal]
----
$ oc patch MachineSet ci-ln-l8nry52-f76d1-hl7m7-worker-c --type='json' -p='[{"op":"add","path":"/spec/template/spec/metadata/labels", "value":{"type":"user-node","region":"east"}}]' -n openshift-machine-api
----
+
[TIP]
====
You can alternatively apply the following YAML to add labels to a compute machine set:
[source,yaml]
----
apiVersion: machine.openshift.io/v1beta1
kind: MachineSet
metadata:
name: <machineset>
namespace: openshift-machine-api
spec:
template:
spec:
metadata:
labels:
region: "east"
type: "user-node"
----
====
.. Verify that the labels are added to the `MachineSet` object by using the `oc edit` command:
+
For example:
+
[source,terminal]
----
$ oc edit MachineSet abc612-msrtw-worker-us-east-1c -n openshift-machine-api
----
+
.Example `MachineSet` object
[source,yaml]
----
apiVersion: machine.openshift.io/v1beta1
kind: MachineSet
...
spec:
...
template:
metadata:
...
spec:
metadata:
labels:
region: east
type: user-node
...
----
.. Redeploy the nodes associated with that compute machine set by scaling down to `0` and scaling up the nodes:
+
For example:
+
[source,terminal]
----
$ oc scale --replicas=0 MachineSet ci-ln-l8nry52-f76d1-hl7m7-worker-c -n openshift-machine-api
----
+
[source,terminal]
----
$ oc scale --replicas=1 MachineSet ci-ln-l8nry52-f76d1-hl7m7-worker-c -n openshift-machine-api
----
.. When the nodes are ready and available, verify that the label is added to the nodes by using the `oc get` command:
+
[source,terminal]
----
$ oc get nodes -l <key>=<value>
----
+
For example:
+
[source,terminal]
----
$ oc get nodes -l type=user-node
----
+
.Example output
[source,terminal]
----
NAME STATUS ROLES AGE VERSION
ci-ln-l8nry52-f76d1-hl7m7-worker-c-vmqzp Ready worker 61s v1.33.4
----
* Add labels directly to a node:
.. Edit the `Node` object for the node:
+
[source,terminal]
----
$ oc label nodes <name> <key>=<value>
----
+
For example, to label a node:
+
[source,terminal]
----
$ oc label nodes ci-ln-l8nry52-f76d1-hl7m7-worker-b-tgq49 type=user-node region=east
----
+
[TIP]
====
You can alternatively apply the following YAML to add labels to a node:
[source,yaml]
----
kind: Node
apiVersion: v1
metadata:
name: <node_name>
labels:
type: "user-node"
region: "east"
----
====
.. Verify that the labels are added to the node using the `oc get` command:
+
[source,terminal]
----
$ oc get nodes -l <key>=<value>,<key>=<value>
----
+
For example:
+
[source,terminal]
----
$ oc get nodes -l type=user-node,region=east
----
+
.Example output
[source,terminal]
----
NAME STATUS ROLES AGE VERSION
ci-ln-l8nry52-f76d1-hl7m7-worker-b-tgq49 Ready worker 17m v1.33.4
----