Configuring an egress firewall for a project

    As a cluster administrator, you can use an egress firewall to limit the external hosts that some or all pods can access from within the cluster. An egress firewall supports the following scenarios:

    • A pod can only connect to internal hosts and cannot initiate connections to the public internet.

    • A pod can only connect to the public internet and cannot initiate connections to internal hosts that are outside the OKD cluster.

    • A pod cannot reach specified internal subnets or hosts outside the OKD cluster.

    • A pod can connect to only specific external hosts.

    For example, you can allow one project access to a specified IP range but deny the same access to a different project. Or you can restrict application developers from updating from Python pip mirrors, and force updates to come only from approved sources.

    You configure an egress firewall policy by creating an EgressFirewall custom resource (CR) object. The egress firewall matches network traffic that meets any of the following criteria:

    • An IP address range in CIDR format

    • A DNS name that resolves to an IP address

    • A port number

    • A protocol that is one of the following protocols: TCP, UDP, and SCTP

    Egress firewall rules do not apply to traffic that goes through routers. Any user with permission to create a Route CR object can bypass egress firewall policy rules by creating a route that points to a forbidden destination.

    • No project can have more than one EgressFirewall object.

    • A maximum of one EgressFirewall object with a maximum of 8,000 rules can be defined per project.

    Violating any of these restrictions results in a broken egress firewall for the project, and might cause all external network traffic to be dropped.

    The egress firewall policy rules are evaluated in the order that they are defined, from first to last. The first rule that matches an egress connection from a pod applies. Any subsequent rules are ignored for that connection.

    If you use DNS names in any of your egress firewall policy rules, proper resolution of the domain names is subject to the following restrictions:

    • Domain name updates are polled based on a time-to-live (TTL) duration. By default, the duration is 30 minutes. When the egress firewall controller queries the local name servers for a domain name, if the response includes a TTL and the TTL is less than 30 minutes, the controller sets the duration for that DNS name to the returned value. Each DNS name is queried after the TTL for the DNS record expires.

    • The pod must resolve the domain from the same local name servers when necessary. Otherwise the IP addresses for the domain known by the egress firewall controller and the pod can be different. If the IP addresses for a hostname differ, the egress firewall might not be enforced consistently.

    • Because the egress firewall controller and pods asynchronously poll the same local name server, the pod might obtain the updated IP address before the egress controller does, which causes a race condition. Due to this current limitation, domain name usage in EgressFirewall objects is only recommended for domains with infrequent IP address changes.

    You can define one or more rules for an egress firewall. A rule is either an Allow rule or a Deny rule, with a specification for the traffic that the rule applies to.

    The following YAML describes an EgressFirewall CR object:

    EgressFirewall object

    1. apiVersion: k8s.ovn.org/v1
    2. metadata:
    3. name: <name> (1)
    4. spec:
    5. egress: (2)
    6. ...
    1The name for the object must be default.
    2A collection of one or more egress network policy rules as described in the following section.

    The following YAML describes an egress firewall rule object. The egress stanza expects an array of one or more objects.

    Egress policy rule stanza

    1. egress:
    2. - type: <type> (1)
    3. to: (2)
    4. cidrSelector: <cidr> (3)
    5. dnsName: <dns_name> (4)
    6. ports: (5)
    7. ...
    1The type of rule. The value must be either Allow or Deny.
    2A stanza describing an egress traffic match rule that specifies the field or the dnsName field. You cannot use both fields in the same rule.
    3An IP address range in CIDR format.
    4A DNS domain name.
    5Optional: A stanza describing a collection of network ports and protocols for the rule.

    The following example defines several egress firewall policy rules:

    1. apiVersion: k8s.ovn.org/v1
    2. kind: EgressFirewall
    3. metadata:
    4. name: default
    5. spec:
    6. egress: (1)
    7. - type: Allow
    8. to:
    9. cidrSelector: 1.2.3.0/24
    10. - type: Deny
    11. cidrSelector: 0.0.0.0/0
    1A collection of egress firewall policy rule objects.

    The following example defines a policy rule that denies traffic to the host at the 172.16.1.1 IP address, if the traffic is using either the TCP protocol and destination port 80 or any protocol and destination port .

    1. apiVersion: k8s.ovn.org/v1
    2. kind: EgressFirewall
    3. metadata:
    4. name: default
    5. spec:
    6. egress:
    7. - type: Deny
    8. to:
    9. cidrSelector: 172.16.1.1
    10. ports:
    11. - port: 80
    12. protocol: TCP
    13. - port: 443

    As a cluster administrator, you can create an egress firewall policy object for a project.

    If the project already has an EgressFirewall object defined, you must edit the existing policy to make changes to the egress firewall rules.

    Prerequisites

    • A cluster that uses the OVN-Kubernetes default Container Network Interface (CNI) network provider plug-in.

    • Install the OpenShift CLI (oc).

    • You must log in to the cluster as a cluster administrator.

    Procedure

    1. Create a policy rule:

      1. Create a <policy_name>.yaml file where <policy_name> describes the egress policy rules.

      2. In the file you created, define an egress policy object.

    2. Enter the following command to create the policy object. Replace <policy_name> with the name of the policy and <project> with the project that the rule applies to.

      In the following example, a new EgressFirewall object is created in a project named project1:

      1. $ oc create -f default.yaml -n project1

      Example output