7 Best Practices in Kubernetes

The growing use of containers has led to a subsequent rise in the demand for Container Orchestration Platforms like Kubernetes. Using containers to package applications has dramatically streamlined the process of shipping applications from Development to Production. No doubt, Kubernetes as a platform orchestrator by introducing its profound ways has further simplified the process.

Today, I want to talk about some of the good practices that I learned while using Kubernetes that can enable secure deployments and drive the efficiency of clusters.

So, let’s begin.

Kubernetes provides an efficient resource for large teams to isolate their work called a Namespace. You should leverage the ability of namespaces to separate different teams’ work on a single Kubernetes cluster. Namespaces also help in isolating individual applications so that managing them becomes easier.

One can have proper standards implemented indicating which namespace should be used for what kind of work, simplifying management. It also avoids clutter by not having to keep everything under one roof.

Most applications require configuration details including some confidential data like secret keys, passwords, or userids. A quick solution is to pass these details via environment variables.

However, this practice is a lot less secure and you should use secrets and configmaps. This not only secures applications but also provides a way to edit these details individually in respective YAML files without having to change the main-application deployment file.

Kubernetes has implemented a rather rich feature of RBAC to define individuals, teams, or applications’ access to the cluster. Kubernetes Roles is a resource to specify the allowed actions in the cluster. These roles are bound to Users or Applications (Service Accounts) using RoleBindings. This way of defining restricted access to clusters implements a layer of access management.

Having said this, one must keep in mind to use these features wisely. An application requesting a namespace-scoped pod view only permission must not be given access to cluster-wide pod listing permission. And a user/teammate should not be given access to secrets he/she is not concerned with. A strict access management layer keeps the cluster away from unexpected mishaps.

It is a good practice to set resource usage limits for applications. It allows keeping the resources of the cluster under check and eliminates opportunities for excessive drainage of resources by a single application, which can seriously impact the performance of other applications. These limits of CPU or Memory usage can be defined for each container in the spec of a Kubernetes Pod/Deployment/StatefulSet.

An initContainer exhibits an attractive feature. It runs to completion before the main application container is started. For this reason, people see it as a potential helper that can run custom utilities and setup code that is not a part of the main image.

It is a good practice to use initContainers in cases of performing tasks like pulling code from repositories or using commands like ‘sed’, ‘awk’ for setup. It allows eliminating extra layers in the image by not having to include these steps in the image build process.

Kubernetes Probes are defined for Pods. Readiness Probe checks whether a pod is ready to accept traffic or not. A Pod is considered ready when all of its containers are ready. This information is important as it is used to decide whether or not to add a Pod from the list of load-balanced servers behind a Kubernetes Service.

Liveness Probes helps restart a Pod in case it does not give a healthy response. Sometimes applications break and besides restarting, there is no other option. Liveness Probe is useful in that case.

It should also be acknowledged that the default value of periodSeconds(how often to perform the probe) is 10 seconds and the initialDelaySeconds(number of seconds to wait after the container has started to perform the probe) is 0.

These values are not suitable for every situation. Heavy applications that you suspect will take time to start should not undergo unnecessary restarts. Thus, each of the values along with the type of liveness Probe(liveness command, liveness HTTP request, TCP liveness probe) should be custom set.

Keeping an eye on the Kubernetes Cluster produces better management and quicker debugging in times of a breakdown. Hence, one should have a reliable monitoring and logging mechanism in place for a transparent view of the entire cluster.

That’s a wrap.

Every tool serves a purpose and Kubernetes is a game-changer in the world of containerization. Adhering to some specific practices ensures that we leverage the best capabilities in the best possible way.



Software Engineer | DevOps Enthusiast | Writer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store