Network Policies
NetworkPolicy is a Kubernetes object used to restrict pod access. By setting a NetworkPolicy, you can define ingress rules specifying what addresses can be allowed to the selected pods or egress rules specifying what addresses can be allowed from the selected pods. This is equivalent to setting up a firewall at the application layer to further ensure network security.
NetworkPolicy is implemented by the network plugin of the cluster to which NetworkPolicy will apply. Therefore, the type of rules supported by NetworkPolicy depends on the network plugin. For example, CCE clusters support only ingress rules of pods.
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.
NetworkPolicy rules are classified into the following types:
- namespaceSelector: This selects particular namespaces for which all pods should be allowed as ingress sources or egress destinations.
- podSelector: This selects particular pods in the same namespace as the NetworkPolicy which should be allowed as ingress sources or egress destinations.
- ipBlock: This selects particular IP CIDR ranges to allow as ingress sources or egress destinations. Currently, CCE does not support ipBlock.
Notes and Constraints
- Only clusters that use the tunnel network model support network policies.
- Egresses are not supported for network policies.
- Network isolation is not supported for IPv6 addresses.
Using podSelector to Select Ingress Sources and Egress Destinations
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: test-network-policy
namespace: default
spec:
podSelector:
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 Figure 1 shows how podSelector selects ingress sources.
Using namespaceSelector to Select Ingress Sources
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: test-network-policy
spec:
podSelector:
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.
Creating a Network Policy on the Console
- Log in to the CCE console and access the cluster details page.
- Choose Networking in the navigation pane, click the Network Policies tab, and click Create Network Policy in the upper right corner.
- Policy Name: Specify a network policy name.
- Namespace: Select a namespace in which the network policy is applied.
- Selector: Enter a label, select the pod to be associated, and click Add. You can also click Reference Workload Label to reference the label of an existing workload.
- Inbound Rule: Click
to add an inbound rule. For details about parameter settings, see Table 1.
- Click OK.
Last Article: Accessing Public Networks from a Container
Next Article: SecurityGroups



Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.