Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documented examples for NetworkPolicies #859

Merged
merged 3 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
23 changes: 23 additions & 0 deletions docs/examples/network-policies/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Network Policy Example

Kubernetes allows you to restrict the source/destination of traffic to & from your Pods at an IP / port level, by defining NetworkPolicies for your cluster, provided your
cluster has the [network plugin](https://kubernetes.io/docs/concepts/extend-kubernetes/compute-storage-net/network-plugins/) enabled. For more
information on NetworkPolicies, see the [Kubernetes documentation](https://kubernetes.io/docs/concepts/services-networking/network-policies/).

By defining NetworkPolicies, you can restrict the network entities with which your RabbitmqCluster can communicate, and prevent unrecognised traffic
from reaching the cluster. It is important to note that once a RabbitmqCluster Pod, or any other Pod for that matter, is the target of any
NetworkPolicy, it becomes isolated to all traffic except that permitted by a NetworkPolicy.

The first policy in this example, [allow-inter-node-traffic.yaml](./allow-inter-node-traffic.yaml) ensures that the only nodes in the RabbitmqCluster
send or receive traffic with each other on the ports used for inter-node communication.

The second policy, [allow-operator-traffic.yaml](./allow-operator-traffic.yaml), allows the cluster-operator and the messaging-topology-operator to
coro marked this conversation as resolved.
Show resolved Hide resolved
communicate with the cluster Pods over HTTP, which is necessary for some reconciliation operations.

The third policy, [allow-rabbitmq-traffic.yaml](./allow-rabbitmq-traffic.yaml), allows all ingress traffic to external-facing ports on the cluster,
such as for AMQP messaging, Prometheus scraping, etc. In practice you may wish to add a selector to this policy to only allow traffic to these
ports from your known client application Pods, or Prometheus servers, etc., depending on your network topology.

The ports exposed in these examples are taken from [the RabbitMQ documentation](https://www.rabbitmq.com/networking.html#ports), and represent
the default ports used by RabbitMQ. It is possible to configure different ports to be used; if you have applied such configuration in your cluster,
take care to ensure the ports in your NetworkPolicies match this configuration.
58 changes: 58 additions & 0 deletions docs/examples/network-policies/allow-inter-node-traffic.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-inter-node-traffic
spec:
podSelector:
matchLabels:
app.kubernetes.io/component: rabbitmq
coro marked this conversation as resolved.
Show resolved Hide resolved
app.kubernetes.io/name: network-policies
policyTypes:
- Ingress
- Egress
egress:
- to:
- podSelector:
matchLabels:
app.kubernetes.io/component: rabbitmq
app.kubernetes.io/name: network-policies
ports:
- port: 4369 # epmd
- port: 25672 # clustering
- port: 35672 # CLI tooling
- port: 35673 # CLI tooling
- port: 35674 # CLI tooling
- port: 35675 # CLI tooling
- port: 35676 # CLI tooling
- port: 35677 # CLI tooling
- port: 35678 # CLI tooling
- port: 35679 # CLI tooling
- port: 35680 # CLI tooling
- port: 35681 # CLI tooling
- port: 35682 # CLI tooling
# If using the k8s feature gate NetworkPolicyEndPort (enabled by default 1.22+), the last 11 entries can be simplified to:
# - port: 35672 # CLI tooling
# endPort: 35682
ingress:
- from:
- podSelector:
matchLabels:
app.kubernetes.io/component: rabbitmq
app.kubernetes.io/name: network-policies
ports:
- port: 4369 # epmd
- port: 25672 # clustering
- port: 35672 # CLI tooling
- port: 35673 # CLI tooling
- port: 35674 # CLI tooling
- port: 35675 # CLI tooling
- port: 35676 # CLI tooling
- port: 35677 # CLI tooling
- port: 35678 # CLI tooling
- port: 35679 # CLI tooling
- port: 35680 # CLI tooling
- port: 35681 # CLI tooling
- port: 35682 # CLI tooling
# If using the k8s feature gate NetworkPolicyEndPort (enabled by default 1.22+), the last 11 entries can be simplified to:
# - port: 35672 # CLI tooling
# endPort: 35682
19 changes: 19 additions & 0 deletions docs/examples/network-policies/allow-operator-traffic.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-operator-traffic
spec:
podSelector:
matchLabels:
app.kubernetes.io/component: rabbitmq
app.kubernetes.io/name: network-policies
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app.kubernetes.io/component: rabbitmq-operator
ports:
- port: 15672 # HTTP API
- port: 15671 # HTTP API + TLS
28 changes: 28 additions & 0 deletions docs/examples/network-policies/allow-rabbitmq-traffic.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-rabbitmq-traffic
spec:
podSelector:
matchLabels:
app.kubernetes.io/component: rabbitmq
app.kubernetes.io/name: network-policies
policyTypes:
- Ingress
ingress:
- ports:
- port: 5672 # AMQP
- port: 5671 # AMQP + TLS
- port: 5552 # Streams
- port: 5551 # Streams + TLS
- port: 15672 # HTTP API
- port: 15672 # HTTP API + TLS
coro marked this conversation as resolved.
Show resolved Hide resolved
- port: 61613 # STOMP
- port: 61614 # STOMP + TLS
- port: 1883 # MQTT
- port: 8883 # MQTT + TLS
- port: 15674 # STOMP-over-WebSockets
- port: 15675 # MQTT-over-WebSockets
- port: 15692 # Prometheus endpoint
- port: 15691 # Prometheus endpoint + TLS

14 changes: 14 additions & 0 deletions docs/examples/network-policies/rabbitmq.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: rabbitmq.com/v1beta1
kind: RabbitmqCluster
metadata:
name: network-policies
spec:
image: rabbitmq:3.9.7-management
replicas: 3
rabbitmq:
additionalPlugins:
- rabbitmq_mqtt
- rabbitmq_stomp
- rabbitmq_stream
- rabbitmq_web_mqtt
- rabbitmq_web_stomp