1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 07:57:07 +00:00
kyverno/samples/README.md

149 lines
8.5 KiB
Markdown
Raw Normal View History

2019-10-08 18:40:15 -07:00
# Best Practice Policies
2019-10-14 10:47:54 -07:00
Best practice policies are recommended policies that can be applied to your Kubernetes clusters with minimal changes. To import these policies [install Kyverno](../documentation/installation.md) and import the resources as follows:
2019-10-08 18:40:15 -07:00
2019-10-09 14:30:31 -07:00
````bash
kubectl create -f https://github.com/nirmata/kyverno/raw/master/samples/best_practices/
````
More information on each best-practice policy is provided below:
2019-10-09 19:06:38 -07:00
2019-10-09 14:30:31 -07:00
## Run as non-root user
2019-10-14 10:47:54 -07:00
By default, processes in a container run as a root user (uid 0). To prevent compromising the host, a best practice is to specify a least-privileged user ID when building the container image, and require that application containers run as non-root users.
2019-10-09 14:30:31 -07:00
2019-10-09 18:17:47 -07:00
***Policy YAML***: [deny_runasrootuser.yaml](best_practices/deny_runasrootuser.yaml)
2019-10-09 14:30:31 -07:00
2019-10-09 18:40:52 -07:00
**Additional Information**
2019-10-09 14:30:31 -07:00
* [Pod Security Context](https://kubernetes.io/docs/tasks/configure-pod-container/security-context/)
2019-10-08 18:40:15 -07:00
2019-10-10 11:53:51 -07:00
2019-10-10 12:29:48 -07:00
## Disallow automounte API credentials
2019-10-14 10:47:54 -07:00
One can access the API from inside a pod using automatically mounted service account credentials by default. To restrict access, opt-out of automounting API credentials for any pod by setting `automountServiceAccountToken` to `false`.
2019-10-10 12:29:48 -07:00
***Policy YAML***: [disallow_automountingapicred.yaml](best_practices/disallow_automountingapicred.yaml)
2019-10-10 11:53:51 -07:00
## Disallow use of default namespace
2019-10-14 10:47:54 -07:00
Namespaces are a way to divide cluster resources between multiple users. When multiple users or teams are sharing a single cluster, it is recommended to isolate different workloads and avoid using default namespace.
2019-10-10 11:53:51 -07:00
***Policy YAML***: [disallow_default_namespace.yaml](best_practices/disallow_default_namespace.yaml)
2019-10-10 18:42:54 -07:00
## Disallow use of host filesystem
2019-10-14 10:47:54 -07:00
The volume of type `hostpath` bounds the pods to host, and data persisted in the volume is based on the life of the node. It is suggested to disable the use of a volume of type hostpath.
2019-10-10 18:42:54 -07:00
***Policy YAML***: [disallow_host_filesystem.yaml](best_practices/disallow_host_filesystem.yaml)
2019-10-09 23:54:19 -07:00
## Disallow `hostNetwork` and `hostPort`
2019-10-09 18:17:47 -07:00
2019-10-14 10:47:54 -07:00
Using `hostPort` and `hostNetwork` limits the number of nodes the pod can be scheduled on, as the pod is bound to the host node.
2019-10-09 18:17:47 -07:00
To avoid this limitation, use a validate rule to make sure these attributes are set to null and false.
2019-10-09 18:21:04 -07:00
***Policy YAML***: [disallow_host_network_hostport.yaml](best_practices/disallow_host_network_hostport.yaml)
2019-10-09 18:17:47 -07:00
2019-10-09 18:40:52 -07:00
## Disallow `hostPID` and `hostIPC`
2019-10-10 11:53:51 -07:00
2019-10-14 10:47:54 -07:00
Sharing the host's PID namespace allows visibility of process on the host, potentially exposing process information.
Sharing the host's IPC namespace allows the container process to communicate with processes on the host.
To avoid pod container from having visibility to host process space, we can check `hostPID` and `hostIPC` are set as `false`.
2019-10-09 18:17:47 -07:00
2019-10-09 18:21:04 -07:00
***Policy YAML***: [disallow_hostpid_hostipc.yaml](best_practices/disallow_hostpid_hostipc.yaml)
2019-10-09 18:17:47 -07:00
## Disallow node port
2019-10-10 11:53:51 -07:00
2019-10-09 18:17:47 -07:00
Node port ranged service is advertised to the public and can be scanned and probed from others exposing all nodes.
2019-10-14 10:47:54 -07:00
NetworkPolicy resources can currently only control NodePorts by allowing or disallowing all traffic on them. Unless required, it is recommended to disable use to service type `NodePort`.
2019-10-09 18:17:47 -07:00
2019-10-09 18:21:04 -07:00
***Policy YAML***: [disallow_node_port.yaml](best_practices/disallow_node_port.yaml)
2019-10-09 18:17:47 -07:00
## Disable privileged containers
2019-10-10 11:53:51 -07:00
2019-10-14 10:47:54 -07:00
A process within privileged containers gets almost the same privileges that are available to processes outside a container providing almost unrestricted host access. With `securityContext.allowPrivilegeEscalation` enabled the process can gain ore privileges that its parent.
2019-10-09 18:17:47 -07:00
To restrcit the priveleges it is recommend to run pod containers with `securityContext.priveleged` as `false` and
`allowPrivilegeEscalation` as `false`
2019-10-09 18:21:04 -07:00
***Policy YAML***: [disallow_priviledged_priviligedescalation.yaml](best_practices/disallow_priviledged_priviligedescalation.yaml)
2019-10-09 18:40:52 -07:00
## Default deny all ingress traffic
2019-10-10 11:53:51 -07:00
2019-10-14 10:47:54 -07:00
When no policies exist in a namespace, Kubernetes allows all ingress and egress traffic to and from pods in that namespace. A "default" isolation policy for a namespace denies any ingress traffic to the pods in that namespace, this ensures that even pods that arent selected by any other NetworkPolicy will still be isolated.
2019-10-09 19:06:38 -07:00
2019-10-10 11:53:51 -07:00
***Policy YAML***: [require_default_network_policy.yaml](best_practices/require_default_network_policy.yaml)
2019-10-09 19:06:38 -07:00
## Disallow latest image tag
2019-10-10 11:53:51 -07:00
2019-10-14 10:47:54 -07:00
Using the `:latest` tag when deploying containers in production makes it harder to track which version of the image is running and more challenging to roll back properly. Specifying a none latest image tag prevents a lot of errors from occurring when versions are inconsistent.
2019-10-09 19:06:38 -07:00
2019-10-09 23:46:18 -07:00
***Policy YAML***: [require_image_tag_not_latest.yaml](best_practices/require_image_tag_not_latest.yaml)
2019-10-09 19:06:38 -07:00
2019-10-10 11:53:51 -07:00
## Default namesapce quotas
2019-10-14 10:47:54 -07:00
To limit the number of objects, as well as the total amount of compute resources that may be consumed by an application, it is essential to create one resource quota for each namespace by the cluster administrator.
2019-10-10 11:53:51 -07:00
**Additional Information**
* [Resource Quota](https://kubernetes.io/docs/concepts/policy/resource-quotas/)
***Policy YAML***: [require_namespace_quota.yaml](best_practices/require_namespace_quota.yaml)
2019-10-10 12:50:17 -07:00
## Require pod resource requests and limits
2019-10-10 11:53:51 -07:00
2019-10-14 10:47:54 -07:00
As workloads share the host cluster, it is essential to administer and limit resources requested and consumed by the pod. It is a good practice always to specify `resources.requests` and `resources.limits` per pod.
2019-10-09 19:06:38 -07:00
2019-10-09 23:46:18 -07:00
***Policy YAML***: [require_pod_requests_limits.yaml](best_practices/require_pod_requests_limits.yaml)
2019-10-09 23:54:19 -07:00
## Default health probe
2019-10-10 11:53:51 -07:00
2019-10-14 10:47:54 -07:00
Health checks mechanism available in kubernetes:
- `livenessProbe` is carried out by the kubelet to determine when to restart a container
- `readinessProbe` is used by services and deployments to determine if the pod should recieve traffic.
Its recommended to define them in pod manifests.
2019-10-09 23:46:18 -07:00
***Policy YAML***: [require_probes.yaml](best_practices/require_probes.yaml)
2019-10-09 19:06:38 -07:00
## Read-only root filesystem
2019-10-10 11:53:51 -07:00
2019-10-14 10:47:54 -07:00
A read-only root file system helps to enforce an immutable infrastructure strategy; the container only needs to write on the mounted volume that persists the state. An immutable root filesystem can also prevent malicious binaries from writing to the host system.
2019-10-09 19:06:38 -07:00
***Policy YAML***: [require_readonly_rootfilesystem.yaml](best_practices/require_readonly_rootfilesystem.yaml)
2019-10-10 11:53:51 -07:00
## Trusted image registries
2019-10-14 10:47:54 -07:00
Images from the unrecognized registry can introduce complexity to maintain the application. By specifying trusted registries help to reduce such complexity. Follow instructions [here](https://github.com/nirmata/kyverno/blob/master/documentation/writing-policies-validate.md#operators) to add allowed registries using `OR` operator.
2019-10-10 11:53:51 -07:00
***Policy YAML***: [trusted_image_registries.yaml](best_practices/trusted_image_registries.yaml)
2019-10-08 18:40:15 -07:00
# Additional Policies
2019-10-09 21:07:15 -07:00
Additional policies list some policies that can also assist in maintaing kubernetes clusters.
2019-10-08 18:40:15 -07:00
2019-10-09 18:40:52 -07:00
## Assign Linux capabilities inside Pod
2019-10-14 10:47:54 -07:00
Linux divides the privileges traditionally associated with superuser into distinct units, known as capabilities, which can be independently enabled or disabled by listing them in `securityContext.capabilites`.
2019-10-09 18:40:52 -07:00
2019-10-14 10:47:54 -07:00
***Policy YAML***: [policy_validate_container_capabilities.yaml](additional/policy_validate_container_capabilities.yaml)
2019-10-09 18:40:52 -07:00
**Additional Information**
* [List of linux capabilities](https://github.com/torvalds/linux/blob/master/include/uapi/linux/capability.h)
## Check userID, groupIP & fsgroup used inside a Pod
2019-10-14 10:47:54 -07:00
All processes inside the pod can be made to run with specific user and groupID by setting `runAsUser` and `runAsGroup` respectively. `fsGroup` can be specified to make sure any file created in the volume with have the specified groupID. These options can be used to validate the IDs used for user and group.
2019-10-09 18:40:52 -07:00
2019-10-14 10:47:54 -07:00
***Policy YAML***: [policy_validate_container_capabilities.yaml](additional/policy_validate_user_group_fsgroup_id.yaml)
2019-10-09 18:40:52 -07:00
## Configure kernel parameters inside pod
2019-10-14 10:47:54 -07:00
The Sysctl interface allows to modify kernel parameters at runtime and in the pod can be specified under `securityContext.sysctls`. If kernel parameters in the pod are to be modified, should be handled cautiously, and policy with rules restricting these options will be helpful. We can control minimum and maximum port that a network connection can use as its source(local) port by checking net.ipv4.ip_local_port_range
2019-10-09 18:40:52 -07:00
2019-10-14 10:47:54 -07:00
***Policy YAML***: [policy_validate_container_capabilities.yaml](additional/policy_validate_user_group_fsgroup_id.yaml)
2019-10-09 18:40:52 -07:00
**Additional Information**
* [List of supported namespaced sysctl interfaces](https://kubernetes.io/docs/tasks/administer-cluster/sysctl-cluster/)