Deploying an egress router pod in redirect mode

As a cluster administrator, you can deploy an egress router pod to redirect traffic to specified destination IP addresses from a reserved source IP address.

The egress router implementation uses the egress router Container Network Interface (CNI) plug-in.

Egress router custom resource

Define the configuration for an egress router pod in an egress router custom resource. The following YAML describes the fields for the configuration of an egress router in redirect mode:

  1. apiVersion: network.operator.openshift.io/v1
  2. kind: EgressRouter
  3. metadata:
  4. name: <egress_router_name>
  5. namespace: <namespace> (1)
  6. spec:
  7. addresses: [ (2)
  8. {
  9. ip: "<egress_router>", (3)
  10. gateway: "<egress_gateway>" (4)
  11. }
  12. ]
  13. mode: Redirect
  14. redirect: {
  15. redirectRules: [ (5)
  16. {
  17. destinationIP: "<egress_destination>",
  18. port: <egress_router_port>,
  19. targetPort: <target_port>, (6)
  20. protocol: <network_protocol> (7)
  21. },
  22. ...
  23. ],
  24. fallbackIP: "<egress_destination>" (8)
  25. }
1Optional: The namespace field specifies the namespace to create the egress router in. If you do not specify a value in the file or on the command line, the default namespace is used.
2The addresses field specifies the IP addresses to configure on the secondary network interface.
3The ip field specifies the reserved source IP address and netmask from the physical network that the node is on to use with egress router pod. Use CIDR notation to specify the IP address and netmask.
4The gateway field specifies the IP address of the network gateway.
5Optional: The redirectRules field specifies a combination of egress destination IP address, egress router port, and protocol. Incoming connections to the egress router on the specified port and protocol are routed to the destination IP address.
6Optional: The targetPort field specifies the network port on the destination IP address. If this field is not specified, traffic is routed to the same network port that it arrived on.
7The protocol field supports TCP, UDP, or SCTP.
8Optional: The fallbackIP field specifies a destination IP address. If you do not specify any redirect rules, the egress router sends all traffic to this fallback IP address. If you specify redirect rules, any connections to network ports that are not defined in the rules are sent by the egress router to this fallback IP address. If you do not specify this field, the egress router rejects connections to network ports that are not defined in the rules.

Example egress router specification

  1. apiVersion: network.operator.openshift.io/v1
  2. kind: EgressRouter
  3. metadata:
  4. name: egress-router-redirect
  5. spec:
  6. networkInterface: {
  7. macvlan: {
  8. mode: "Bridge"
  9. }
  10. }
  11. addresses: [
  12. {
  13. ip: "192.168.12.99/24",
  14. gateway: "192.168.12.1"
  15. }
  16. ]
  17. mode: Redirect
  18. redirect: {
  19. redirectRules: [
  20. {
  21. destinationIP: "10.0.0.99",
  22. port: 80,
  23. protocol: UDP
  24. },
  25. {
  26. destinationIP: "203.0.113.26",
  27. port: 8080,
  28. targetPort: 80,
  29. protocol: TCP
  30. },
  31. {
  32. destinationIP: "203.0.113.27",
  33. port: 8443,
  34. targetPort: 443,
  35. protocol: TCP
  36. }
  37. ]
  38. }

Deploying an egress router in redirect mode

You can deploy an egress router to redirect traffic from its own reserved source IP address to one or more destination IP addresses.

After you add an egress router, the client pods that need to use the reserved source IP address must be modified to connect to the egress router rather than connecting directly to the destination IP.

Prerequisites

  • Install the OpenShift CLI (oc).

  • Log in as a user with cluster-admin privileges.

Procedure

  1. Create an egress router definition.

  2. To ensure that other pods can find the IP address of the egress router pod, create a service that uses the egress router, as in the following example:

    1. apiVersion: v1
    2. kind: Service
    3. metadata:
    4. name: egress-1
    5. spec:
    6. ports:
    7. - name: web-app
    8. protocol: TCP
    9. port: 8080
    10. type: ClusterIP
    11. selector:
    12. app: egress-router-cni (1)
    1Specify the label for the egress router. The value shown is added by the Cluster Network Operator and is not configurable.

    After you create the service, your pods can connect to the service. The egress router pod redirects traffic to the corresponding port on the destination IP address. The connections originate from the reserved source IP address.

Verification

To verify that the Cluster Network Operator started the egress router, complete the following procedure:

  1. View the network attachment definition that the Operator created for the egress router:

    1. $ oc get network-attachment-definition egress-router-cni-nad

    The name of the network attachment definition is not configurable.

    Example output

    1. NAME AGE
    2. egress-router-cni-nad 18m
  2. View the deployment for the egress router pod:

    1. $ oc get deployment egress-router-cni-deployment

    The name of the deployment is not configurable.

    Example output

    1. NAME READY UP-TO-DATE AVAILABLE AGE
    2. egress-router-cni-deployment 1/1 1 1 18m
  3. View the status of the egress router pod:

    1. $ oc get pods -l app=egress-router-cni

    Example output

    1. NAME READY STATUS RESTARTS AGE
    2. egress-router-cni-deployment-575465c75c-qkq6m 1/1 Running 0 18m
  4. View the logs and the routing table for the egress router pod.

  5. Get the node name for the egress router pod:

    1. $ POD_NODENAME=$(oc get pod -l app=egress-router-cni -o jsonpath="{.items[0].spec.nodeName}")
  6. Enter into a debug session on the target node. This step instantiates a debug pod called <node_name>-debug:

    1. $ oc debug node/$POD_NODENAME
  7. Set /host as the root directory within the debug shell. The debug pod mounts the root file system of the host in /host within the pod. By changing the root directory to /host, you can run binaries from the executable paths of the host:

    1. # chroot /host
  8. From within the chroot environment console, display the egress router logs:

    1. # cat /tmp/egress-router-log

    Example output

    1. 2021-04-26T12:27:20Z [debug] Called CNI ADD
    2. 2021-04-26T12:27:20Z [debug] Gateway: 192.168.12.1
    3. 2021-04-26T12:27:20Z [debug] IP Source Addresses: [192.168.12.99/24]
    4. 2021-04-26T12:27:20Z [debug] IP Destinations: [80 UDP 10.0.0.99/30 8080 TCP 203.0.113.26/30 80 8443 TCP 203.0.113.27/30 443]
    5. 2021-04-26T12:27:20Z [debug] Created macvlan interface
    6. 2021-04-26T12:27:20Z [debug] Renamed macvlan to "net1"
    7. 2021-04-26T12:27:20Z [debug] Adding route to gateway 192.168.12.1 on macvlan interface
    8. 2021-04-26T12:27:20Z [debug] deleted default route {Ifindex: 3 Dst: <nil> Src: <nil> Gw: 10.128.10.1 Flags: [] Table: 254}
    9. 2021-04-26T12:27:20Z [debug] Added new default route with gateway 192.168.12.1
    10. 2021-04-26T12:27:20Z [debug] Added iptables rule: iptables -t nat PREROUTING -i eth0 -p UDP --dport 80 -j DNAT --to-destination 10.0.0.99
    11. 2021-04-26T12:27:20Z [debug] Added iptables rule: iptables -t nat PREROUTING -i eth0 -p TCP --dport 8080 -j DNAT --to-destination 203.0.113.26:80
    12. 2021-04-26T12:27:20Z [debug] Added iptables rule: iptables -t nat PREROUTING -i eth0 -p TCP --dport 8443 -j DNAT --to-destination 203.0.113.27:443
    13. 2021-04-26T12:27:20Z [debug] Added iptables rule: iptables -t nat -o net1 -j SNAT --to-source 192.168.12.99

    The logging file location and logging level are not configurable when you start the egress router by creating an EgressRouter object as described in this procedure.

  9. From within the chroot environment console, get the container ID:

    1. # crictl ps --name egress-router-cni-pod | awk '{print $1}'

    Example output

    1. CONTAINER
    2. bac9fae69ddb6
  10. Determine the process ID of the container. In this example, the container ID is bac9fae69ddb6:

    1. # crictl inspect -o yaml bac9fae69ddb6 | grep 'pid:' | awk '{print $2}'

    Example output

    1. 68857
  11. Enter the network namespace of the container:

    1. # nsenter -n -t 68857
  12. Display the routing table:

    1. # ip route

    In the following example output, the net1 network interface is the default route. Traffic for the cluster network uses the eth0 network interface. Traffic for the 192.168.12.0/24 network uses the net1 network interface and originates from the reserved source IP address 192.168.12.99. The pod routes all other traffic to the gateway at IP address 192.168.12.1. Routing for the service network is not shown.

    Example output

    1. default via 192.168.12.1 dev net1
    2. 10.128.10.0/23 dev eth0 proto kernel scope link src 10.128.10.18
    3. 192.168.12.0/24 dev net1 proto kernel scope link src 192.168.12.99
    4. 192.168.12.1 dev net1