Este conteúdo não está disponível no idioma selecionado.
Chapter 4. Network policies
4.1. About network policies
Learn how network policies work for MicroShift to restrict or allow network traffic to pods in your node.
4.1.1. How network policy works in MicroShift
					In a node that is using the default OVN-Kubernetes Container Network Interface (CNI) plugin for MicroShift, network isolation is controlled by both firewalld, which is configured on the host, and by NetworkPolicy objects created within MicroShift. Simultaneous use of firewalld and NetworkPolicy is supported.
				
- 
							Network policies work only within boundaries of OVN-Kubernetes-controlled traffic, so they can apply to every situation except for hostPort/hostNetworkenabled pods.
- 
							Firewalld settings also do not apply to hostPort/hostNetworkenabled pods.
- 
							Firewalld rules run before any NetworkPolicyis enforced.
Network policy does not apply to the host network namespace. Pods with host networking enabled are unaffected by network policy rules. However, pods connecting to the host-networked pods might be affected by the network policy rules.
Network policies cannot block traffic from localhost.
					By default, all pods in a MicroShift node are accessible from other pods and network endpoints. To isolate one or more pods in a node, you can create NetworkPolicy objects to indicate allowed incoming connections. You can create and delete NetworkPolicy objects.
				
					If a pod is matched by selectors in one or more NetworkPolicy objects, then the pod accepts only connections that are allowed by at least one of those NetworkPolicy objects. A pod that is not selected by any NetworkPolicy objects is fully accessible.
				
A network policy applies to only the TCP, UDP, ICMP, and SCTP protocols. Other protocols are not affected.
					The following example NetworkPolicy objects demonstrate supporting different scenarios:
				
- Deny all traffic: - To make a project deny by default, add a - NetworkPolicyobject that matches all pods but accepts no traffic:- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Allow connections from the default router, which is the ingress in MicroShift: - To allow connections from the MicroShift default router, add the following - NetworkPolicyobject:- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Only accept connections from pods within the same namespace: - To make pods accept connections from other pods in the same namespace, but reject all other connections from pods in other namespaces, add the following - NetworkPolicyobject:- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Only allow HTTP and HTTPS traffic based on pod labels: - To enable only HTTP and HTTPS access to the pods with a specific label ( - role=frontendin following example), add a- NetworkPolicyobject similar to the following:- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Accept connections by using both namespace and pod selectors: - To match network traffic by combining namespace and pod selectors, you can use a - NetworkPolicyobject similar to the following:- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
					NetworkPolicy objects are additive, which means you can combine multiple NetworkPolicy objects together to satisfy complex network requirements.
				
					For example, for the NetworkPolicy objects defined in previous examples, you can define both allow-same-namespace and allow-http-and-https policies. That configuration allows the pods with the label role=frontend to accept any connection allowed by each policy. That is, connections on any port from pods in the same namespace, and connections on ports 80 and 443 from pods in any namespace.
				
4.1.2. Optimizations for network policy with OVN-Kubernetes network plugin
When designing your network policy, refer to the following guidelines:
- 
							For network policies with the same spec.podSelectorspec, it is more efficient to use one network policy with multipleingressoregressrules, than multiple network policies with subsets ofingressoregressrules.
- Every - ingressor- egressrule based on the- podSelectoror- namespaceSelectorspec generates the number of OVS flows proportional to- number of pods selected by network policy + number of pods selected by ingress or egress rule. Therefore, it is preferable to use the- podSelectoror- namespaceSelectorspec that can select as many pods as you need in one rule, instead of creating individual rules for every pod.- For example, the following policy contains two rules: - Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - The following policy expresses those same two rules as one: - Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - The same guideline applies to the - spec.podSelectorspec. If you have the same- ingressor- egressrules for different network policies, it might be more efficient to create one network policy with a common- spec.podSelectorspec. For example, the following two policies have different rules:- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - The following network policy expresses those same two rules as one: - Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - You can apply this optimization when only multiple selectors are expressed as one. In cases where selectors are based on different labels, it may not be possible to apply this optimization. In those cases, consider applying some new labels for network policy optimization specifically. 
4.1.2.1. NetworkPolicy CR and external IPs in OVN-Kubernetes
						In OVN-Kubernetes, the NetworkPolicy custom resource (CR) enforces strict isolation rules. If a service is exposed using an external IP, a network policy can block access from other namespaces unless explicitly configured to allow traffic.
					
						To allow access to external IPs across namespaces, create a NetworkPolicy CR that explicitly permits ingress from the required namespaces and ensures traffic is allowed to the designated service ports. Without allowing traffic to the required ports, access might still be restricted.
					
Example output
where:
- <policy_name>
- Specifies your name for the policy.
- <my_namespace>
- Specifies the name of the namespace where the policy is deployed.
For more details, see "About network policy".
4.2. Creating network policies
You can create a network policy for a namespace.
4.2.1. Example NetworkPolicy object
The following annotates an example NetworkPolicy object:
- 1
- The name of the NetworkPolicy object.
- 2
- A selector that describes the pods to which the policy applies.
- 3
- A selector that matches the pods from which the policy object allows ingress traffic. The selector matches pods in the same namespace as the NetworkPolicy.
- 4
- A list of one or more destination ports on which to accept traffic.
4.2.2. Creating a network policy using the CLI
To define granular rules describing ingress or egress network traffic allowed for namespaces in your cluster, you can create a network policy.
Prerequisites
- 
							You installed the OpenShift CLI (oc).
- You are working in the namespace that the network policy applies to.
Procedure
- Create a policy rule: - Create a - <policy_name>.yamlfile:- touch <policy_name>.yaml - $ touch <policy_name>.yaml- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - where: - <policy_name>
- Specifies the network policy file name.
 
- Define a network policy in the file that you just created, such as in the following examples: - Deny ingress from all pods in all namespaces - This is a fundamental policy, blocking all cross-pod networking other than cross-pod traffic allowed by the configuration of other Network Policies. - Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - Allow ingress from all pods in the same namespace - Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - Allow ingress traffic to one pod from a particular namespace - This policy allows traffic to pods that have the - pod-alabel from pods running in- namespace-y.- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
 
- To create the network policy object, enter the following command. Successful output lists the name of the policy object and the - createdstatus.- oc apply -f <policy_name>.yaml -n <namespace> - $ oc apply -f <policy_name>.yaml -n <namespace>- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - where: - <policy_name>
- Specifies the network policy file name.
- <namespace>
- Optional parameter. If you defined the object in a different namespace than the current namespace, the parameter specifices the namespace.
 - Successful output lists the name of the policy object and the - createdstatus.
4.2.3. Creating a default deny all network policy
					This policy blocks all cross-pod networking other than network traffic allowed by the configuration of other deployed network policies and traffic between host-networked pods. This procedure enforces a strong deny policy by applying a deny-by-default policy in the my-project namespace.
				
						Without configuring a NetworkPolicy custom resource (CR) that allows traffic communication, the following policy might cause communication problems across your cluster.
					
Prerequisites
- 
							You installed the OpenShift CLI (oc).
- You are working in the namespace that the network policy applies to.
Procedure
- Create the following YAML that defines a - deny-by-defaultpolicy to deny ingress from all pods in all namespaces. Save the YAML in the- deny-by-default.yamlfile:- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - 1
- Specifies the namespace in which to deploy the policy. For example, the `my-projectnamespace.
- 2
- If this field is empty, the configuration matches all the pods. Therefore, the policy applies to all pods in themy-projectnamespace.
- 3
- There are noingressrules specified. This causes incoming traffic to be dropped to all pods.
 
- Apply the policy by entering the following command. Successful output lists the name of the policy object and the - createdstatus.- oc apply -f deny-by-default.yaml - $ oc apply -f deny-by-default.yaml- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - Successful output lists the name of the policy object and the - createdstatus.
4.2.4. Creating a network policy to allow traffic from external clients
					With the deny-by-default policy in place you can proceed to configure a policy that allows traffic from external clients to a pod with the label app=web.
				
						Firewalld rules run before any NetworkPolicy is enforced.
					
					Follow this procedure to configure a policy that allows external service from the public Internet directly or by using a Load Balancer to access the pod. Traffic is only allowed to a pod with the label app=web.
				
Prerequisites
- 
							You installed the OpenShift CLI (oc).
- You are working in the namespace that the network policy applies to.
Procedure
- Create a policy that allows traffic from the public Internet directly or by using a load balancer to access the pod. Save the YAML in the - web-allow-external.yamlfile:- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Apply the policy by entering the following command. Successful output lists the name of the policy object and the - createdstatus.- oc apply -f web-allow-external.yaml - $ oc apply -f web-allow-external.yaml- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - Successful output lists the name of the policy object and the - createdstatus.
4.2.5. Creating a network policy allowing traffic to an application from all namespaces
Follow this procedure to configure a policy that allows traffic from all pods in all namespaces to a particular application.
Prerequisites
- 
							You installed the OpenShift CLI (oc).
- You are working in the namespace that the network policy applies to.
Procedure
- Create a policy that allows traffic from all pods in all namespaces to a particular application. Save the YAML in the - web-allow-all-namespaces.yamlfile:- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow Note- By default, if you do not specify a - namespaceSelectorparameter in the policy object, no namespaces get selected. This means the policy allows traffic only from the namespace where the network policy deployes.
- Apply the policy by entering the following command. Successful output lists the name of the policy object and the - createdstatus.- oc apply -f web-allow-all-namespaces.yaml - $ oc apply -f web-allow-all-namespaces.yaml- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - Successful output lists the name of the policy object and the - createdstatus.
Verification
- Start a web service in the - defaultnamespace by entering the following command:- oc run web --namespace=default --image=nginx --labels="app=web" --expose --port=80 - $ oc run web --namespace=default --image=nginx --labels="app=web" --expose --port=80- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Run the following command to deploy an - alpineimage in the- secondarynamespace and to start a shell:- oc run test-$RANDOM --namespace=secondary --rm -i -t --image=alpine -- sh - $ oc run test-$RANDOM --namespace=secondary --rm -i -t --image=alpine -- sh- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Run the following command in the shell and observe that the service allows the request: - wget -qO- --timeout=2 http://web.default - # wget -qO- --timeout=2 http://web.default- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - Expected output - Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
4.2.6. Creating a network policy allowing traffic to an application from a namespace
					Follow this procedure to configure a policy that allows traffic to a pod with the label app=web from a particular namespace. You might want to do this to:
				
- Restrict traffic to a production database only to namespaces that have production workloads deployed.
- Enable monitoring tools deployed to a particular namespace to scrape metrics from the current namespace.
Prerequisites
- 
							You installed the OpenShift CLI (oc).
- You are working in the namespace that the network policy applies to.
Procedure
- Create a policy that allows traffic from all pods in a particular namespaces with a label - purpose=production. Save the YAML in the- web-allow-prod.yamlfile:- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Apply the policy by entering the following command. Successful output lists the name of the policy object and the - createdstatus.- oc apply -f web-allow-prod.yaml - $ oc apply -f web-allow-prod.yaml- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - Successful output lists the name of the policy object and the - createdstatus.
Verification
- Start a web service in the - defaultnamespace by entering the following command:- oc run web --namespace=default --image=nginx --labels="app=web" --expose --port=80 - $ oc run web --namespace=default --image=nginx --labels="app=web" --expose --port=80- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Run the following command to create the - prodnamespace:- oc create namespace prod - $ oc create namespace prod- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Run the following command to label the - prodnamespace:- oc label namespace/prod purpose=production - $ oc label namespace/prod purpose=production- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Run the following command to create the - devnamespace:- oc create namespace dev - $ oc create namespace dev- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Run the following command to label the - devnamespace:- oc label namespace/dev purpose=testing - $ oc label namespace/dev purpose=testing- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Run the following command to deploy an - alpineimage in the- devnamespace and to start a shell:- oc run test-$RANDOM --namespace=dev --rm -i -t --image=alpine -- sh - $ oc run test-$RANDOM --namespace=dev --rm -i -t --image=alpine -- sh- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Run the following command in the shell and observe the reason for the blocked request. For example, expected output states - wget: download timed out.- wget -qO- --timeout=2 http://web.default - # wget -qO- --timeout=2 http://web.default- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Run the following command to deploy an - alpineimage in the- prodnamespace and start a shell:- oc run test-$RANDOM --namespace=prod --rm -i -t --image=alpine -- sh - $ oc run test-$RANDOM --namespace=prod --rm -i -t --image=alpine -- sh- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Run the following command in the shell and observe that the request is allowed: - wget -qO- --timeout=2 http://web.default - # wget -qO- --timeout=2 http://web.default- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - Expected output - Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
4.3. Editing a network policy
				You can edit an existing network policy for a namespace. Typical edits might include changes to the pods to which the policy applies, allowed ingress traffic, and the destination ports on which to accept traffic. The apiVersion, kind, and name fields must not be changed when editing NetworkPolicy objects, as these define the resource itself.
			
4.3.1. Editing a network policy
You can edit a network policy in a namespace.
Prerequisites
- 
							You installed the OpenShift CLI (oc).
- You are working in the namespace where the network policy exists.
Procedure
- Optional: To list the network policy objects in a namespace, enter the following command: - oc get networkpolicy - $ oc get networkpolicy- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - where: - <namespace>
- Optional: Specifies the namespace if the object is defined in a different namespace than the current namespace.
 
- Edit the network policy object. - If you saved the network policy definition in a file, edit the file and make any necessary changes, and then enter the following command. - oc apply -n <namespace> -f <policy_file>.yaml - $ oc apply -n <namespace> -f <policy_file>.yaml- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - where: - <namespace>
- Optional: Specifies the namespace if the object is defined in a different namespace than the current namespace.
- <policy_file>
- Specifies the name of the file containing the network policy.
 
- If you need to update the network policy object directly, enter the following command: - oc edit networkpolicy <policy_name> -n <namespace> - $ oc edit networkpolicy <policy_name> -n <namespace>- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - where: - <policy_name>
- Specifies the name of the network policy.
- <namespace>
- Optional: Specifies the namespace if the object is defined in a different namespace than the current namespace.
 
 
- Confirm that the network policy object is updated. - oc describe networkpolicy <policy_name> -n <namespace> - $ oc describe networkpolicy <policy_name> -n <namespace>- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - where: - <policy_name>
- Specifies the name of the network policy.
- <namespace>
- Optional: Specifies the namespace if the object is defined in a different namespace than the current namespace.
 
4.3.2. Example NetworkPolicy object
The following annotates an example NetworkPolicy object:
- 1
- The name of the NetworkPolicy object.
- 2
- A selector that describes the pods to which the policy applies.
- 3
- A selector that matches the pods from which the policy object allows ingress traffic. The selector matches pods in the same namespace as the NetworkPolicy.
- 4
- A list of one or more destination ports on which to accept traffic.
4.4. Deleting a network policy
You can delete a network policy from a namespace.
4.4.1. Deleting a network policy using the CLI
You can delete a network policy in a namespace.
Prerequisites
- 
							You installed the OpenShift CLI (oc).
- You are working in the namespace where the network policy exists.
Procedure
- To delete a network policy object, enter the following command. Successful output lists the name of the policy object and the - deletedstatus.- oc delete networkpolicy <policy_name> -n <namespace> - $ oc delete networkpolicy <policy_name> -n <namespace>- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - where: - <policy_name>
- Specifies the name of the network policy.
- <namespace>
- Optional parameter. If you defined the object in a different namespace than the current namespace, the parameter specifices the namespace.
 - Successful output lists the name of the policy object and the - deletedstatus.
4.5. Viewing a network policy
Use the following procedure to view a network policy for a namespace.
4.5.1. Viewing network policies using the CLI
You can examine the network policies in a namespace.
Prerequisites
- 
							You installed the OpenShift CLI (oc).
- You are working in the namespace where the network policy exists.
Procedure
- List network policies in a namespace: - To view network policy objects defined in a namespace, enter the following command: - oc get networkpolicy - $ oc get networkpolicy- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Optional: To examine a specific network policy, enter the following command: - oc describe networkpolicy <policy_name> -n <namespace> - $ oc describe networkpolicy <policy_name> -n <namespace>- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - where: - <policy_name>
- Specifies the name of the network policy to inspect.
- <namespace>
- Optional: Specifies the namespace if the object is defined in a different namespace than the current namespace.
 - For example: - oc describe networkpolicy allow-same-namespace - $ oc describe networkpolicy allow-same-namespace- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - Output for - oc describecommand- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow