As the service logic becomes increasingly complex, many applications require network calls between modules. Traditional external firewalls or application-based firewalls cannot meet the requirements. Network policies are urgently needed between modules, service logic layers, or functional teams in a large cluster.
CCE has enhanced the Kubernetes-based network policy feature, allowing network isolation in a cluster by configuring network policies. This means that a firewall can be set between pods.
For example, to make a payment system accessible only to specified components for security purposes, you can configure network policies.
Egress rules are supported only in the following operating systems:
If no network policies have been configured for a workload, such as workload-1, other workloads in the same cluster can access workload-1.
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:
Click Select Workload. In the dialog box displayed, select a workload for which the network policy is to be created, for example, workload-1. Then, click OK.
Parameter |
Description |
---|---|
Direction |
Only Inbound is supported, indicating that the whitelisted workloads access the current workload (workload-1 in this example). |
Protocol |
Select a protocol. Currently, the TCP and UDP protocols are supported. The ICMP protocol is not supported. |
Destination Container Port |
Specify a port on which the workload in the container image listens. The Nginx application listens on port 80. If no container port is specified, all ports can be accessed by default. |
Whitelisted Workloads |
Select other workloads that can access the current workload. These workloads will access the current workload at the destination container port.
|
After the network policies are created, only the specified workloads or workloads in the specified namespaces can access the current workload.