Configuring an egress IP address

    The OKD egress IP address functionality allows you to ensure that the traffic from one or more pods in one or more namespaces has a consistent source IP address for services outside the cluster network.

    For example, you might have a pod that periodically queries a database that is hosted on a server outside of your cluster. To enforce access requirements for the server, a packet filtering device is configured to allow traffic only from specific IP addresses. To ensure that you can reliably allow access to the server from only that specific pod, you can configure a specific egress IP address for the pod that makes the requests to the server.

    An egress IP address is implemented as an additional IP address on the primary network interface of a node and must be in the same subnet as the primary IP address of the node. The additional IP address must not be assigned to any other node in the cluster.

    Support for the egress IP address functionality on various platforms is summarized in the following table:

    PlatformSupported

    Bare metal

    Yes

    vSphere

    Yes

    Red Hat OpenStack Platform (RHOSP)

    No

    Public cloud

    No

    To assign one or more egress IPs to a namespace or specific pods in a namespace, the following conditions must be satisfied:

    • At least one node in your cluster must have the label.

    • An EgressIP object exists that defines one or more egress IP addresses to use as the source IP address for traffic leaving the cluster from pods in a namespace.

    When creating an EgressIP object, the following conditions apply to nodes that are labeled with the k8s.ovn.org/egress-assignable: "" label:

    • An egress IP address is never assigned to more than one node at a time.

    • An egress IP address is equally balanced between available nodes that can host the egress IP address.

    • If the spec.EgressIPs array in an EgressIP object specifies more than one IP address, no node will ever host more than one of the specified addresses.

    • If a node becomes unavailable, any egress IP addresses assigned to it are automatically reassigned, subject to the previously described conditions.

    When a pod matches the selector for multiple EgressIP objects, there is no guarantee which of the egress IP addresses that are specified in the EgressIP objects is assigned as the egress IP address for the pod.

    The following diagram depicts an egress IP address configuration. The diagram describes four pods in two different namespaces running on three nodes in a cluster. The nodes are assigned IP addresses from the 192.168.126.0/18 CIDR block on the host network.

    Both Node 1 and Node 3 are labeled with k8s.ovn.org/egress-assignable: "" and thus available for the assignment of egress IP addresses.

    The dashed lines in the diagram depict the traffic flow from pod1, pod2, and pod3 traveling through the pod network to egress the cluster from Node 1 and Node 3. When an external service receives traffic from any of the pods selected by the example EgressIP object, the source IP address is either 192.168.126.10 or 192.168.126.102.

    The following resources from the diagram are illustrated in detail:

    Namespace objects

    The namespaces are defined in the following manifest:

    Namespace objects

    EgressIP object

    The following EgressIP object describes a configuration that selects all pods in any namespace with the env label set to prod. The egress IP addresses for the selected pods are 192.168.126.10 and 192.168.126.102.

    EgressIP object

    1. apiVersion: k8s.ovn.org/v1
    2. kind: EgressIP
    3. metadata:
    4. name: egressips-prod
    5. spec:
    6. egressIPs:
    7. - 192.168.126.10
    8. - 192.168.126.102
    9. matchLabels:
    10. env: prod
    11. status:
    12. assignments:
    13. - node: node1
    14. egressIP: 192.168.126.10
    15. - node: node3
    16. egressIP: 192.168.126.102

    The following YAML describes the API for the EgressIP object. The scope of the object is cluster-wide; it is not created in a namespace.

    1. apiVersion: k8s.ovn.org/v1
    2. kind: EgressIP
    3. metadata:
    4. name: <name> (1)
    5. spec:
    6. egressIPs: (2)
    7. - <ip_address>
    8. namespaceSelector: (3)
    9. ...
    10. podSelector: (4)
    11. ...
    1The name for the EgressIPs object.
    2An array of one or more IP addresses.
    3One or more selectors for the namespaces to associate the egress IP addresses with.
    4Optional: One or more selectors for pods in the specified namespaces to associate egress IP addresses with. Applying these selectors allows for the selection of a subset of pods within a namespace.

    The following YAML describes the stanza for the namespace selector:

    Namespace selector stanza

    The following YAML describes the optional stanza for the pod selector:

    Pod selector stanza

    1. podSelector: (1)
    2. matchLabels:
    3. <label_name>: <label_value>
    1Optional: One or more matching rules for pods in the namespaces that match the specified namespaceSelector rules. If specified, only pods that match are selected. Others pods in the namespace are not selected.

    In the following example, the EgressIP object associates the 192.168.126.11 and 192.168.126.102 egress IP addresses with pods that have the app label set to web and are in the namespaces that have the label set to prod:

    Example EgressIP object

    1. apiVersion: k8s.ovn.org/v1
    2. metadata:
    3. name: egress-group1
    4. spec:
    5. egressIPs:
    6. - 192.168.126.11
    7. - 192.168.126.102
    8. podSelector:
    9. matchLabels:
    10. app: web
    11. namespaceSelector:
    12. matchLabels:
    13. env: prod

    In the following example, the EgressIP object associates the 192.168.127.30 and 192.168.127.40 egress IP addresses with any pods that do not have the environment label set to development:

    Example EgressIP object

    You can apply the k8s.ovn.org/egress-assignable="" label to a node in your cluster so that OKD can assign one or more egress IP addresses to the node.

    Prerequisites

    • Install the OpenShift CLI (oc).

    • Log in to the cluster as a cluster administrator.

    Procedure

    • To label a node so that it can host one or more egress IP addresses, enter the following command:

      1. $ oc label nodes <node_name> k8s.ovn.org/egress-assignable="" (1)

      You can alternatively apply the following YAML to add the label to a node:

      1. apiVersion: v1
      2. kind: Node
      3. metadata:
      4. labels:
      5. k8s.ovn.org/egress-assignable: “”
      6. name: <node_name>