﻿# Load balancing rules for nodes

> [HTML Version](affinity-antiaffinity.html)

To improve fault tolerance and system performance, you can define flexible load balancing rules for nodes using the tools:

- [Affinity](#affinity). Set flexible rules for placing pods in relation to each other or on specific nodes.

- [Anti-affinity](#antiaffinity). Prevent pods of the same type from being placed on the same node or in the same topology.

Before configuring Affinity or Anti-affinity, please read the [BRIX On-Premises advanced settings](advanced-kubernetes-settings.md) article to learn about all the pod placement tools and recommendations for using them together.

## Affinity tool

You can configure flexible rules to distribute pods across nodes or zones. For example, prioritize nodes or set complex conditions for pod placement. To do this, use the Affinity tool. You can configure it using the \[OBJECT\] parameter.

### How to configure the nodeAffinity parameter

Let’s see how to configure the \[OBJECT\] parameter by setting pod running only on nodes with the \[OBJECT\] label. To do this, in the \[OBJECT\] file, go to the \[OBJECT\] field and specify the values:

````
global:  
  affinity:  
    nodeAffinity:  
      requiredDuringSchedulingIgnoredDuringExecution:  
        nodeSelectorTerms:  
        - matchExpressions:  
          - key: role  
            operator: In  
            values:  
            - worker

````
Where:

- \[OBJECT\]. A rule for placing a pod on a node.

- \[OBJECT\]. A list of conditions that must be met for nodes.

- \[OBJECT\]. Logical expressions for filtering labels:

	- \[OBJECT\]. Label name.

	- \[OBJECT\]. Comparison operator:

		- \[OBJECT\]. A label value must be included in the list of values.

		- \[OBJECT\]. A label value must not be included in the list of values.

		- \[OBJECT\]. A pod must have a \[OBJECT\] with the given name.

		- \[OBJECT\]. A pod must not have a \[OBJECT\] with the given name.

- \[OBJECT\]. Label values that are used with the \[OBJECT\] and \[OBJECT\] operators. You can specify several values.

Once the parameter is configured, apply it as described in the [Modify BRIX parameters](change-settings-enterprise.md#apply-new-parameters) article.

## Anti-affinity tool

To improve system fault tolerance, you can distribute pods of the same type to different nodes. For example, set rules for the pods of the **mailer** service. If one node is not enough to perform computations, the service will continue to run on other nodes.

To set such rules, use the Anti-affinity tool. You can configure it using the \[OBJECT\] parameter.

### How to configure the podAntiAffinity parameter

Let's see how to configure the \[OBJECT\] parameter and prohibit the placement of pods with the \[OBJECT\] label on one node. To do this, in the \[OBJECT\] file, go to the \[OBJECT\] field and set the settings:

````
global:  
  affinity:  
    podAntiAffinity:  
      requiredDuringSchedulingIgnoredDuringExecution:  
      - labelSelector:  
          matchExpressions:  
          - key: app  
            operator: In  
            values:  
            - mailer  
        topologyKey: kubernetes.io/hostname

````
Where:

- \[OBJECT\]. A rule for placing a pod on a node.

- \[OBJECT\]. Labels of pods to which the rule applies.

- \[OBJECT\]. Logical expressions for filtering labels:

	- \[OBJECT\]. Label name.

	- \[OBJECT\]. Comparison operator:

		- \[OBJECT\]. The label value must be included in the list of \[OBJECT\].

		- \[OBJECT\]. The label value must not be included in the list of \[OBJECT\].

		- \[OBJECT\]. A pod must have a \[OBJECT\] with the given name.

		- \[OBJECT\]. A pod must not have a \[OBJECT\] with the given name.

- \[OBJECT\]. Label values that are used with the \[OBJECT\] and \[OBJECT\] operators. You can specify multiple values.

- \[OBJECT\]. Metadata key that is used to define the topology of nodes, for example \[OBJECT\].

Once the parameter is configured, apply it as described in the [Modify BRIX parameters](change-settings-enterprise.md#apply-new-parameters) article.

## Default values in the .Values.global.affinity field

If you have not set values in the \[OBJECT\] field, the default settings apply:

````
affinity:  
  podAntiAffinity:  
    preferredDuringSchedulingIgnoredDuringExecution:  
      - podAffinityTerm:  
          labelSelector:  
            matchExpressions:  
              - key: app  
                operator: In  
                values:  
                  - <service name>  
              - key: release  
                operator: In  
                values:  
                  - "<release name>"  
          topologyKey: kubernetes.io/hostname  
        weight: 10

````
In this configuration:

- Multiple pods of the same app (\[OBJECT\]) or release (\[OBJECT\]) cannot be placed on the same node.

- The \[OBJECT\] key is used to define the topology. It distributes pods to different nodes.

- The \[OBJECT\] parameter specifies the priority of the rule that the Kubernetes scheduler uses when selecting a node to place pods on.