kubernetes-security-best-practices-cheat-sheet
kubernetes-security-best-practices-cheat-sheet
Kubernetes Security
Best Practices
Since we’ve already covered the essential
Kubernetes security best practices in the
CloudSec Academy, we’ve gone a step
further and put together a cheat sheet
of some advanced steps you can take
to safeguard your Kubernetes clusters.
2 Securing kubelets
A kubelet runs on each node and takes care of actions like creating a new pod, performing
health checks on containers, and, if the containers are down, trying to spawn them again. It is
also responsible for gathering and reporting metrics for running pods.
Kubelets expose some APIs that can be used to start and stop pods, and you will not want
these APIs to be accessible from the outside. To lock them down, follow these steps:
Restrict the kubelet: Make sure that a kubelet in one node cannot access or make
changes in other nodes. You can enable this setting in the admission controller’s
NodeRestriction configuration
Disable anonymous access: Use –anonymous-auth=false while starting the kubelet
process so that you can block anonymous access
Verify the authorization mode: Never run –authorization with AlwaysAllow.
Shut down the read-only port: The –read-only-port=0 will not allow anyone to see
what workload is running on the Kubernetes server; you can implement this in the
reconnaissance phase.
Tools like Dex, InfraHQ, Azure AD, and AWS IAM authenticator can be great alternatives
to this authentication process. Make sure that your team refrains from sharing kubeconfig files
amongst themselves since that will make it complicated to identify who made which changes;
each kubeconfig file is marked with a user identifier, and sharing the files will create confusion.
1 Network policies
Adding networking policies can go a long way. Initially, you generally don’t implement them and
just allow all the services to talk to each other. But as you grow, your number of services can also
grow exponentially—and not having proper networking policies will create chaos. Adding policies
like which services can access what resources or other services will help you maintain control
over your infrastructure.
You may have to use a third-party CNI for this purpose. Calico, for starters, can be a great solution.
Most service meshes also have this functionality, like Istio and Linkerd.
Monitoring tools add a lot of value here. Solutions like Cilium’s Hubble give you a lot of visibility
into your Kubernetes networking infrastructure.
One very interesting controller is the validating admission policy controller. It offers a declarative
way of creating a policy using the Common Expression Language (CEL). With this, you can apply
your rules to all the resources created in Kubernetes clusters.
Below, we cover a few of the important rules you should apply in your cluster with
a validating admission policy:
Allowing containers to run as root privileges can allow hackers to access your containers and
exploit the node and hence the cluster. You can stop this with the following policy:
apiVersion: admissionregistration.k8s.io/v1alpha1
kind: ValidatingAdmissionPolicyBinding
metadata:
name: c0016-binding
spec:
policyName: kubescape-c-0016-allow-privilege-escalation
paramRef:
name: basic-control-configuration
matchResources:
namespaceSelector:
matchLabels:
policy: enforced
EOT
You don’t want your images for containers to be coming from untrusted or forbidden sources. This
policy can help you make sure that you deny such repos:
apiVersion: admissionregistration.k8s.io/v1alpha1
kind: ValidatingAdmissionPolicyBinding
metadata:
name: c0001-binding
spec:
policyName: kubescape-c-0001-deny-forbidden-container-registries
paramRef:
name: basic-control-configuration
matchResources:
namespaceSelector:
matchLabels:
policy: enforced
EOT
An attacker who has access to a container can download scripts in the filesystem and execute
them. With an immutable filesystem, you can stop such malicious actors. Follow this binding to
do so:
apiVersion: admissionregistration.k8s.io/v1alpha1
kind: ValidatingAdmissionPolicyBinding
metadata:
name: c0017-binding
spec:
policyName: kubescape-c-0017-deny-resources-with-mutable-
container-filesystem
paramRef:
name: basic-control-configuration
matchResources:
namespaceSelector:
matchLabels:
policy: enforced
EOT
Additional policies can be found here, including some very key ones you should consider applying:
2 Process whitelisting
As the name suggests, this means you only allow certain processes to run in your Kubernetes
nodes instead of allowing just anything to run. This can be a very tricky task, as every time a team
has to deploy a new application, they have to get it whitelisted.
Also for your service accounts, make sure that no other entity apart from the allowed pods have
access to the resources.
Conclusion
Security is a layering concept. More layers of security will make it more difficult for attackers to
reach a targeted area. With the above list of actions, you’ll add a few extra layers of security to
achieve a more secure Kubernetes infrastructure.
Wiz can help boost the security of your Kubernetes environment, as well as greatly improve your
auditing capabilities. Get your demo today.