2019-11-11 14:09:07 -08:00
|
|
|
# Restrict use of `NodePort` services
|
2019-10-23 14:06:03 -07:00
|
|
|
|
|
|
|
A Kubernetes service of type `NodePort` uses a host port (on every node in the cluster) to receive traffic from any source.
|
|
|
|
|
|
|
|
Kubernetes Network Policies cannot be used to control traffic to host ports.
|
|
|
|
|
|
|
|
Although NodePort services can be useful, their use should be limited to services with additional upstream security checks.
|
|
|
|
|
|
|
|
## Policy YAML
|
|
|
|
|
2019-11-11 14:09:07 -08:00
|
|
|
[restrict_node_port.yaml](best_practices/restrict_node_port.yaml)
|
2019-10-23 14:06:03 -07:00
|
|
|
|
|
|
|
````yaml
|
|
|
|
|
|
|
|
apiVersion: kyverno.io/v1alpha1
|
|
|
|
kind: ClusterPolicy
|
|
|
|
metadata:
|
2019-11-11 14:09:07 -08:00
|
|
|
name: restrict-node-port
|
2019-10-23 14:06:03 -07:00
|
|
|
spec:
|
|
|
|
rules:
|
2019-11-10 21:34:22 -08:00
|
|
|
- name: validate-node-port
|
2019-10-23 14:06:03 -07:00
|
|
|
match:
|
|
|
|
resources:
|
|
|
|
kinds:
|
|
|
|
- Service
|
|
|
|
validate:
|
2019-11-11 14:09:07 -08:00
|
|
|
message: "Service of type NodePort is not allowed"
|
2019-10-23 14:06:03 -07:00
|
|
|
pattern:
|
|
|
|
spec:
|
|
|
|
type: "!NodePort"
|
|
|
|
|
|
|
|
````
|
|
|
|
|