- Enable feature gates
- Node selectors
- Affinities and anti-affinities
- Taints and tolerations
- Topology spread constraints
- Resource labels and annotations
All
kubectl steps should be performed in the . By default, this is cockroach-operator-system.Enable feature gates
To enable the affinity, toleration, and topology spread constraint rules, download the Public operator manifest and add the following line to thespec.containers.args field:
Node selectors
A pod with a node selector will be scheduled onto a worker node that has matching labels, or key-value pairs. Specify the labels innodeSelector in the Public operator’s custom resource, which is used to . If you specify multiple nodeSelector labels, the node must match all of them.
The following configuration causes CockroachDB pods to be scheduled onto worker nodes that have both the labels worker-pool-name=crdb-workers and kubernetes.io/arch=amd64:
Affinities and anti-affinities
To use the affinity rules, first enable the feature gates.
operator fields to:
- Require CockroachDB pods to be scheduled onto a labeled worker node.
- Require CockroachDB pods to be co-located with labeled pods (e.g., on a node or region).
- Prevent CockroachDB pods from being scheduled onto a labeled worker node.
- Prevent CockroachDB pods from being co-located with labeled pods (e.g., on a node or region).
Add a node affinity
Specify node affinities inaffinity.nodeAffinity in the Public operator’s custom resource, which is used to . If you specify multiple matchExpressions labels, the node must match all of them. If you specify multiple values for a label, the node can match any of the values.
The following configuration requires that CockroachDB pods are scheduled onto worker nodes running either an intel or amd64 CPU, with a preference against worker nodes in the us-east4-b availability zone.
requiredDuringSchedulingIgnoredDuringExecution node affinity rule, using the In operator, requires CockroachDB pods to be scheduled onto nodes with either the matching label kubernetes.io/arch=intel or kubernetes.io/arch=amd64. It will not evict pods that are already running on nodes that do not match the affinity requirements.
The preferredDuringSchedulingIgnoredDuringExecution node affinity rule, using the NotIn operator and specified weight, discourages (but does not disallow) CockroachDB pods from being scheduled onto nodes with the label topology.kubernetes.io/zone=us-east4-b. This achieves a similar effect as a PreferNoSchedule taint.
For more context on how these rules work, see the Kubernetes documentation. The custom resource definition details the fields supported by the Public operator.
Add a pod affinity or anti-affinity
Specify pod affinities and anti-affinities inaffinity.podAffinity and affinity.podAntiAffinity in the Public operator’s custom resource, which is used to . If you specify multiple matchExpressions labels, the node must match all of them. If you specify multiple values for a label, the node can match any of the values.
The following configuration attempts to schedule CockroachDB pods in the same zones as the pods that run our example app. It disallows CockroachDB pods from being co-located on the same worker node.
preferredDuringSchedulingIgnoredDuringExecution pod affinity rule, using the In operator and specified weight, encourages (but does not require) CockroachDB pods to be co-located with pods labeled app=loadgen already running in the same zone, as specified with topologyKey.
The requiredDuringSchedulingIgnoredDuringExecution pod anti-affinity rule, using the In operator, requires CockroachDB pods not to be co-located on a worker node, as specified with topologyKey.
For more context on how these rules work, see the Kubernetes documentation. The custom resource definition details the fields supported by the Public operator.
Example: Scheduling CockroachDB onto labeled nodes
In this example, CockroachDB has not yet been deployed to a running Kubernetes cluster. We use a combination of node affinity and pod anti-affinity rules to schedule 3 CockroachDB pods onto 3 labeled worker nodes.-
List the worker nodes on the running Kubernetes cluster:
-
Add a
node=crdblabel to 3 of the running worker nodes.In this example, 6 GKE nodes are deployed in 3 node pools, and each node pool resides in a separate availability zone. To maintain an even distribution of CockroachDB pods as specified in our , each of the 3 labeled worker nodes must belong to a different node pool.
-
Add the following rules to the Public operator’s custom resource, which is used to :
The
nodeAffinityrule requires CockroachDB pods to be scheduled onto worker nodes with the labelnode=crdb. ThepodAntiAffinityrule requires CockroachDB pods not to be co-located on a worker node, as specified withtopologyKey. -
Apply the settings to the cluster:
-
The CockroachDB pods will be deployed to the 3 labeled nodes. To observe this:
Taints and tolerations
To use the toleration rules, first enable the feature gates.
- Prevent CockroachDB pods from being scheduled onto a labeled worker node.
- Evict CockroachDB pods from a labeled worker node on which they are currently running.
Add a toleration
Specify pod tolerations in thetolerations object of the Public operator’s custom resource, which is used to .
The following toleration matches a taint with the specified key, value, and NoSchedule effect, using the Equal operator. A toleration that uses the Equal operator must include a value field:
NoSchedule taint on a node prevents pods from being scheduled onto the node. The matching toleration allows a pod to be scheduled onto the node. A NoSchedule toleration is therefore best included before .
A
PreferNoSchedule taint discourages, but does not disallow, pods from being scheduled onto the node.NoExecute effect, using the Exists operator. A toleration that uses the Exists operator must exclude a value field:
NoExecute taint on a node prevents pods from being scheduled onto the node, and evicts pods from the node if they are already running on the node. The matching toleration allows a pod to be scheduled onto the node, and to continue running on the node if tolerationSeconds is not specified. If tolerationSeconds is specified, the pod is evicted after this number of seconds.
For more information on using taints and tolerations, see the Kubernetes documentation. The custom resource definition details the fields supported by the Public operator.
Example: Evicting CockroachDB from a running worker node
In this example, CockroachDB has already been deployed on a Kubernetes cluster. We use theNoExecute effect to evict one of the CockroachDB pods from its worker node.
-
List the worker nodes on the running Kubernetes cluster:
-
Add a taint to a running worker node:
-
Add a matching
tolerationsobject to the Public operator’s custom resource, which was used to :Because notolerationSecondsis specified, CockroachDB will be evicted immediately from the tainted worker node. -
Apply the new settings to the cluster:
-
The CockroachDB pod running on the tainted node (in this case,
cockroachdb-2) will be evicted and started on a different worker node. To observe this:cockroachdb-2is now scheduled onto thegke-cockroachdb-default-pool-4e5ce539-68p5node.
Topology spread constraints
To use the topology spread constraint rules, first enable the feature gates.
Add a topology spread constraint
Specify pod topology spread constraints in thetopologySpreadConstraints object of the Public operator’s custom resource, which is used to . If you specify multiple topologySpreadConstraints objects, the matching pods must satisfy all of the constraints.
The following topology spread constraint ensures that CockroachDB pods deployed with the label environment=production will not be unevenly distributed across zones by more than 1 pod:
DoNotSchedule condition prevents labeled pods from being scheduled onto Kubernetes worker nodes when doing so would fail to meet the spread and topology constraints specified with maxSkew and topologyKey, respectively.
For more context on how these rules work, see the Kubernetes documentation. The custom resource definition details the fields supported by the Public operator.
Resource labels and annotations
To assist in working with your cluster, you can add labels and annotations to your resources. Specify labels inadditionalLabels and annotations in additionalAnnotations in the Public operator’s custom resource, which is used to :
kubectl describe pod {pod-name}.
For more information about labels and annotations, see the Kubernetes documentation.
