NetworkPolicy is a Kubernetes object used to restrict pod access. In CCE, by setting network policies, you can define ingress rules specifying the addresses to access pods or egress rules specifying the addresses pods can access. This is equivalent to setting up a firewall at the application layer to further ensure network security.
Network policies depend on the networking add-on of the cluster to which the policies apply.
By default, if a namespace does not have any policy, pods in the namespace accept traffic from any source and send traffic to any destination.
Network policy rules are classified into the following types:
Egress rules are supported only in the following operating systems:
apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: test-network-policy namespace: default spec: podSelector: # The rule takes effect for pods with the role=db label. matchLabels: role: db ingress: #This is an ingress rule. - from: - podSelector: #Only traffic from the pods with the role=frontend label is allowed. matchLabels: role: frontend ports: #Only TCP can be used to access port 6379. - protocol: TCP port: 6379
Diagram:
apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: test-network-policy spec: podSelector: # The rule takes effect for pods with the role=db label. matchLabels: role: db ingress: #This is an ingress rule. - from: - namespaceSelector: # Only traffic from the pods in the namespace with the "project=myproject" label is allowed. matchLabels: project: myproject ports: #Only TCP can be used to access port 6379. - protocol: TCP port: 6379
Figure 2 shows how namespaceSelector selects ingress sources.
Egress supports not only podSelector and namespaceSelector, but also ipBlock.
Only clusters of version 1.23 or later support egress rules. Currently, only EulerOS 2.5, EulerOS 2.9, and CentOS 7.7 nodes are supported.
apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: deny-client-a-via-except-cidr-egress-rule namespace: default spec: policyTypes: # Must be specified for an egress rule. - Egress podSelector: # The rule takes effect for pods with the role=db label. matchLabels: role: db egress: # Egress rule - to: - ipBlock: cidr: 172.16.0.16/16 # Allow access to this CIDR block. except: - 172.16.0.40/32 # This CIDR block cannot be accessed. This value must fall within the range specified by cidr.
Diagram:
You can define ingress and egress in the same rule.
apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: test-network-policy namespace: default spec: policyTypes: - Ingress - Egress podSelector: # The rule takes effect for pods with the role=db label. matchLabels: role: db ingress: # Ingress rule - from: - podSelector: #Only traffic from the pods with the "role=frontend" label is allowed. matchLabels: role: frontend ports: #Only TCP can be used to access port 6379. - protocol: TCP port: 6379 egress: # Egress rule - to: - podSelector: # Only pods with the role=web label can be accessed. matchLabels: role: web
Diagram:
Parameter |
Description |
---|---|
Protocol & Port |
Select the protocol type and port. Currently, TCP and UDP are supported. |
Source Namespace |
Select a namespace whose objects can be accessed. If this parameter is not specified, the source object belongs to the same namespace as the current policy. |
Source Pod Label |
Allow access to the pods with this label, if not specified, all pods in the namespace can be accessed. |
Parameter |
Description |
---|---|
Protocol & Port |
Select the protocol type and port. Currently, TCP and UDP are supported. If this parameter is not specified, the protocol type is not limited. |
Destination CIDR Block |
Allows requests to be routed to a specified CIDR block (and not to the exception CIDR blocks). Separate the destination and exception CIDR blocks by vertical bars (|), and separate multiple exception CIDR blocks by commas (,). For example, 172.17.0.0/16|172.17.1.0/24,172.17.2.0/24 indicates that 172.17.0.0/16 is accessible, but not for 172.17.1.0/24 or 172.17.2.0/24. |
Destination Namespace |
Select a namespace whose objects can be accessed. If this parameter is not specified, the source object belongs to the same namespace as the current policy. |
Destination Pod Label |
Allow access to the pods with this label, if not specified, all pods in the namespace can be accessed. |