Set Priority for Pods in K8s

Posted May 6, 2023 by Rohith ‐ 2 min read

In Kubernetes, you can set priorities for pods using the PriorityClass resource. PriorityClass is a resource that defines a mapping between a priority value and a set of pod specifications.

Pods that are associated with a higher priority value will be scheduled before pods with a lower priority value. Here’s how you can set the priority for pods:

  1. Define a PriorityClass resource: First, you need to define a PriorityClass resource that specifies the priority value and any other attributes you want to associate with the priority class. For example, you can define a PriorityClass resource with a priority value of 1000 as follows:
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
  name: high-priority
value: 1000
  1. Associate pods with the PriorityClass: Next, you need to associate pods with the PriorityClass you defined in step 1. You can do this by adding a priorityClassName field to the pod specification, as follows:
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: nginx
  priorityClassName: high-priority

In this example, the pod “my-pod” is associated with the PriorityClass “high-priority” with a priority value of 1000.

  1. Verify the PriorityClass: Once you have defined the PriorityClass and associated it with pods, you can verify that the priority is being applied correctly by using the kubectl describe pod command. For example:
$ kubectl describe pod my-pod
...
Priority:     1000
...

This shows that the pod “my-pod” has a priority value of 1000, which corresponds to the “high-priority” PriorityClass.

By setting priorities for pods, you can ensure that critical workloads are scheduled before less important workloads, which can help you optimize resource utilization and improve the overall performance of your Kubernetes cluster.

quick-references blog k8s

Subscribe For More Content