Kubernetes is a complex tool. As is the case with most complex tools, getting optimal performance out of Kubernetes can be tricky. Most Kubernetes distributions don’t come fine-tuned to maximize performance (and even if they did, it’s likely that they would not be tweaked in a way that is optimal for your environment).
With that reality in mind, keep reading for tips on Kubernetes performance optimization. We’ll focus on simple things you can do to improve Kubernetes performance whether you are just starting out with building your cluster, or already have a production environment up and running.
1Add Resources to Existing Worker Nodes Before You Create New Worker Nodes
Probably the most obvious way to improve Kubernetes performance is to add more worker nodes to your cluster. The more workers you have, the more resources are available to power your workloads. You also get an availability boost, because having more nodes reduces the chances of having so many of them fail that your workloads start failing.
If you want to get the very most out of your worker nodes, however, you’ll get more bang for your buck by adding memory and CPU resources to existing worker nodes rather than creating new nodes. In other words, you’re better off having 20 nodes with 16 gigabytes of memory each, rather than 40 nodes with 8 gigabytes each.
This is true for two reasons. First, there is a certain amount of overhead for each node due to the host operating system. Having fewer nodes means fewer resources wasted in that way. Second, the more nodes you have, the harder the scheduler, kube-proxy and other components have to work to keep track of everything.
Obviously, you need to take availability into account, and ensure that you have a minimum number of worker nodes to meet your availability goals. But once you cross that threshold, you’ll achieve better overall performance efficiency by making sure that each node has as many resources allocated to it as you can afford, rather than trying to maximize the number of overall nodes. Just don’t go to extremes (you probably don’t want 24 terabytes of memory on a single node, for instance) because you risk losing those resources if a node fails.
Of course, you may or may not have a lot of flexibility in determining the resource allocations of your nodes. If they’re virtual machines running in the cloud, you can assign as many resources as you want. If they’re on-premise virtual machines or (gasp) physical servers, this is trickier.
2Use Multiple Master Nodes
The main reason to use multiple masters in a Kubernetes cluster is to achieve high availability. The more masters you have, the less likely it is that they will all fail and bring your cluster down with them.
However, adding more masters also offers performance benefits because it provides more hosting resources to the essential Kubernetes components (like the scheduler, API server and Etcd) that are hosted on masters. Kubernetes will use the collective resources of all master nodes to power these components.
Thus, adding a master (or two or four) is a good and easy way to give your Kubernetes cluster a performance boost.
3Set Worker Node Scoring Limits
Part of the job that the Kubernetes scheduler does is to “score” the worker nodes, which means it determines which worker nodes are fit to handle a workload. In clusters with several dozen or more worker nodes, the scheduler can end up wasting time checking every worker node.
To avoid this inefficiency, you can set the percentageOfNodesToScore parameter to a percentage lower than 100. The scheduler will then check only the percentage of nodes that you specify.
4Set Resource Quotas
A simple but very effective way to improve Kubernetes performance, especially in large clusters shared by multiple teams, is to set resource quotas. Resource quotas set limits on the amount of CPU, memory and storage resources that can be used by a given namespace.
Thus, if you divide your cluster into namespaces, give each team a different namespace and set a resource quota for each namespace, you’ll help ensure that all workloads get a fair share of resources.
Resource quotas aren’t a performance optimization per se; they are more of a solution to the noisy-neighbor issue. But they do help ensure that each namespace has the resources it needs to perform its job adequately.
5Set Limit Ranges
What if you want to limit the resources consumed by a workload, but that workload runs in the same namespace as other workloads? This is what limit ranges do.
Whereas resource quotas set limits on how many resources each namespace can consume, limit ranges do the same thing on a per-pod or per-container basis.
For the sake of simplicity, a best practice in most cases is to segment workloads using namespaces and resource quotas. But if you are in a situation where that approach is not practical, limit ranges will allow you to guarantee that individual pods or containers have the resources they need to perform as desired.
6Set up Endpoint Slices
Endpoint Slices are a little-discussed Kubernetes feature that lets you group network endpoints together based on service and port combinations. When they are set up, kube-proxy refers to them when deciding how to route traffic.
In environments with large numbers of endpoints, Endpoint Slices can improve performance by reducing the amount of work that kube-proxy needs to perform in order to route traffic within the cluster.
7Use a Minimalist Host OS
Last but not least is a basic but effective tip: Make sure that the operating system hosting your Kubernetes clusters is as minimal as possible. Any extra components that aren’t strictly necessary for running Kubernetes lead to wasted resources, which in turn degrades the performance of your cluster.
Depending on which Kubernetes distribution you use, you may or may not have the luxury of choosing the host OS. But if you do, choose a Linux distribution with a minimal footprint.
Kubernetes is designed to do many things automatically. But it doesn’t automate its own performance management. Getting the most performance out of the infrastructure that you dedicate to Kubernetes requires being smart about how you design the infrastructure and how you configure certain Kubernetes components.
You can also make your job easier by utilizing a Kubernetes service like Platform9 Managed Kubernetes, which is already optimized, scales well, and offers solutions for every workload. If you want to try out the platform, you can sign up for the sandbox here.
- Why and How to Run Machine Learning Workloads on Kubernetes - July 23, 2021
- Learning About Kubernetes Admission Controllers and OPA Gatekeeper - July 20, 2021
- Fargate vs. Kubernetes: Which Container Orchestration Solution Do You Need? - July 19, 2021