How to Secure Kubernetes with KubeLinter
The open source tool analyzes Kubernetes YAML files and Helm charts to ensure they adhere to best practices, focusing on production readiness and security. Here's how to set it up and use it.
Jul 10th, 2023 6:57am by
Image by Reto Scheiwiller from Pixabay.
Why KubeLinter?
KubeLinter comes with sensible default checks, but it is also configurable. You have the flexibility to enable or disable specific checks according to your organization’s policies. Additionally, you can create your own custom checks to enforce specific requirements. When a lint check fails, KubeLinter provides recommendations on how to resolve the identified issues. It also returns a non-zero exit code to indicate the presence of potential problems.Installation, Setup and Getting Started
To get started with KubeLinter, you can refer to the official documentation. The documentation provides detailed information on installing, using and configuring KubeLinter. Here are a few installation options for KubeLinter.Using Go
Install KubeLinter using Go by running the following command:
go install golang.stackrox.io/kube-linter/cmd/kube-linter@latest
Using Homebrew (macOS) or LinuxBrew (Linux)
Install KubeLinter using Homebrew or LinuxBrew by running the following command:
brew install kube-linter
Building from Source
If you prefer to build KubeLinter from source, follow these steps:- Clone the KubeLinter repository:
git clone git@github.com:stackrox/kube-linter.git
- Compile the source code to create the kube-linter binary files:
make build
- Verify the installation by checking the version:
.gobin/kube-linter version
How to Use KubeLinter
To use KubeLinter, you can start by running it against your local YAML files. Simply specify the path to the YAML file you want to test, and KubeLinter will perform the linting checks. For example.
kube-linter lint /path/to/your/yaml.yaml
kube-linter lint /path/to/yaml-file.yaml
kube-linter lint /path/to/directory/containing/yaml-files/
- Locate the YAML file that you want to test for security and production readiness best practices.
- Run the following command, replacing `/path/to/your/yaml.yaml` with the actual path to your YAML file. Here’s the format:
kube-linter lint /path/to/your/yaml.yaml
apiVersion: v1
kind: Pod
metadata:
name: security-context-demo
spec:
securityContext:
runAsUser: 1000
runAsGroup: 3000
fsGroup: 2000
volumes:
- name: sec-ctx-vol
emptyDir: {}
containers:
- name: sec-ctx-demo
image: busybox
resources:
requests:
memory: "64Mi"
cpu: "250m"
command: [ "sh", "-c", "sleep 1h" ]
volumeMounts:
- name: sec-ctx-vol
mountPath: /data/demo
securityContext:
allowPrivilegeEscalation: false
kube-linter lint lint-pod.yaml
pod.yaml: (object: <no namespace>/security-context-demo /v1, Kind=Pod)
container "sec-ctx-demo" does not have a read-only root file system (check:
no-read-only-root-fs, remediation: Set readOnlyRootFilesystem to true in your
container's securityContext.)
pod.yaml: (object: <no namespace>/security-context-demo /v1, Kind=Pod)
container "sec-ctx-demo" has cpu limit 0 (check: unset-cpu-requirements,
remediation: Set your container's CPU requests and limits depending on its
requirements. See
https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#requests-and-limits for more details.)
pod.yaml: (object: <no namespace>/security-context-demo /v1, Kind=Pod)
container "sec-ctx-demo" has memory limit 0 (check: unset-memory-requirements,
remediation: Set your container's memory requests and limits depending on its requirements.
See
https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#requests-and-limits for more details.)
Error: found 3 lint errors
kube-linter lint /path/to/directory/containing/chart.yaml-file/
- repo: https://github.com/stackrox/kube-linter
rev: 0.6.0 # kube-linter version
hooks:
- id: kube-linter
- `resource`. specifies the resources on which you want to perform operations, such as checks or templates
- `command`. specifies the operation you want to perform, such as lint or checks list
- `options`. specifies additional options for each command. For example, you can use the `-c` or `–config` option to specify a configuration file.
kube-linter --help
kube-linter checks --help
kube-linter lint --help
.kube-linter.yaml
.kube-linter.yml
- customChecks for configuring custom checks.
- checks for configuring default checks.
checks:
doNotAutoAddDefaults: true
checks:
addAllBuiltIn: true
customChecks:
- name: required-annotation-responsible
template: required-annotation
params:
key: company.io/responsible
Conclusion
KubeLinter is an alpha release, which means it is still in the early stages of development. As a result, there may be breaking changes in the future regarding command usage, flags and configuration file formats. However, you are encouraged to use KubeLinter to test your environment YAML files, identify issues — and contribute to its development.
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.