2019-11-11 22:09:07 +00:00
|
|
|
# Restrict use of `NodePort` services
|
2019-10-23 21:06:03 +00: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-12 02:21:16 +00:00
|
|
|
[restrict_node_port.yaml](more/restrict_node_port.yaml)
|
2019-10-23 21:06:03 +00:00
|
|
|
|
|
|
|
````yaml
|
|
|
|
|
2019-11-13 21:56:20 +00:00
|
|
|
apiVersion: kyverno.io/v1
|
2019-10-23 21:06:03 +00:00
|
|
|
kind: ClusterPolicy
|
|
|
|
metadata:
|
2019-12-10 17:51:15 +00:00
|
|
|
name: restrict-nodeport
|
2019-10-23 21:06:03 +00:00
|
|
|
spec:
|
|
|
|
rules:
|
2019-12-10 17:51:15 +00:00
|
|
|
- name: validate-nodeport
|
2019-10-23 21:06:03 +00:00
|
|
|
match:
|
|
|
|
resources:
|
|
|
|
kinds:
|
|
|
|
- Service
|
|
|
|
validate:
|
2019-12-10 17:51:15 +00:00
|
|
|
message: "Services of type NodePort are not allowed"
|
2019-10-23 21:06:03 +00:00
|
|
|
pattern:
|
|
|
|
spec:
|
|
|
|
type: "!NodePort"
|
|
|
|
|
2019-12-10 17:51:15 +00:00
|
|
|
````
|