// Module included in the following assemblies:
//
// * networking/metallb/metallb-operator-install.adoc

:_mod-docs-content-type: PROCEDURE
[id="nw-metallb-operator-setting-pod-priority-affinity_{context}"]
= Configuring pod priority and pod affinity in a MetalLB deployment

[role="_abstract"]
To control scheduling of MetalLB controller and `speaker` pods in {product-title}, you can assign pod priority and pod affinity in the `MetalLB` custom resource. You create a `PriorityClass` and set `priorityClassName` and affinity in the `MetalLB` spec, then apply the configuration.

The pod priority indicates the relative importance of a pod on a node and schedules the pod based on this priority. Set a high priority on your `controller` or `speaker` pod to ensure scheduling priority over other pods on the node.

Pod affinity manages relationships among pods. Assign pod affinity to the `controller` or `speaker` pods to control on what node the scheduler places the pod in the context of pod relationships. For example, you can use pod affinity rules to ensure that certain pods are located on the same node or nodes, which can help improve network communication and reduce latency between those components.

.Prerequisites

* You are logged in as a user with `cluster-admin` privileges.

* You have installed the MetalLB Operator.

* You have started the MetalLB Operator on your cluster.

.Procedure

. Create a `PriorityClass` custom resource, such as `myPriorityClass.yaml`, to configure the priority level. This example defines a `PriorityClass` named `high-priority` with a value of `1000000`. Pods that are assigned this priority class are considered higher priority during scheduling compared to pods with lower priority classes:
+
[source,yaml]
----
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
  name: high-priority
value: 1000000
----

. Apply the `PriorityClass` custom resource configuration:
+
[source,bash]
----
$ oc apply -f myPriorityClass.yaml
----

. Create a `MetalLB` custom resource, such as `MetalLBPodConfig.yaml`, to specify the `priorityClassName` and `podAffinity` values:
+
[source,yaml]
----
apiVersion: metallb.io/v1beta1
kind: MetalLB
metadata:
  name: metallb
  namespace: metallb-system
spec:
  logLevel: debug
  controllerConfig:
    priorityClassName: high-priority
    affinity:
      podAffinity:
        requiredDuringSchedulingIgnoredDuringExecution:
        - labelSelector:
            matchLabels:
             app: metallb
          topologyKey: kubernetes.io/hostname
  speakerConfig:
    priorityClassName: high-priority
    affinity:
      podAffinity:
        requiredDuringSchedulingIgnoredDuringExecution:
        - labelSelector:
            matchLabels:
             app: metallb
          topologyKey: kubernetes.io/hostname
----
+
where:
+
--
`spec.controllerConfig.priorityClassName`:: Specifies the priority class for the MetalLB controller pods. In this case, it is set to `high-priority`.

`spec.controllerConfig.affinity.podAffinity`:: Specifies that you are configuring pod affinity rules. These rules dictate how pods are scheduled in relation to other pods or nodes. This configuration instructs the scheduler to schedule pods that have the label `app: metallb` onto nodes that share the same hostname. This helps to co-locate MetalLB-related pods on the same nodes, potentially optimizing network communication, latency, and resource usage between these pods.
--

. Apply the `MetalLB` custom resource configuration by running the following command:
+
[source,terminal]
----
$ oc apply -f MetalLBPodConfig.yaml
----

.Verification

* To view the priority class that you assigned to pods in the `metallb-system` namespace, run the following command:
+
[source,terminal]
----
$ oc get pods -n metallb-system -o custom-columns=NAME:.metadata.name,PRIORITY:.spec.priorityClassName
----
+
.Example output
[source,terminal]
----
NAME                                                 PRIORITY
controller-584f5c8cd8-5zbvg                          high-priority
metallb-operator-controller-manager-9c8d9985-szkqg   <none>
metallb-operator-webhook-server-c895594d4-shjgx      <none>
speaker-dddf7                                        high-priority
----

* Verify that the scheduler placed pods according to pod affinity rules by viewing the metadata for the node of the pod. For example:
+
[source,terminal]
----
$ oc get pod -o=custom-columns=NODE:.spec.nodeName,NAME:.metadata.name -n metallb-system
----
