개인 네트워크(lxdbr0)에서 나가는 트래픽 라우팅

개인 네트워크(lxdbr0)에서 나가는 트래픽 라우팅

내 호스트 시스템에 lxd를 설정하고 lxdbr0 브리지를 네트워크 카드로 사용하고 있습니다. 이제 내 컨테이너는 dhcp를 통해 lxdbr0에서 IP 주소(10.204.xx 범위)를 가져옵니다.

또한 2개의 공용 IP 주소가 있습니다. 하나는 호스트용(xxxx)이고 다른 하나는 컨테이너용(bbbb)입니다. 컨테이너는 나가고 들어오는 트래픽에 두 번째 공용 IP를 사용해야 합니다. 두 공용 IP 주소 모두 호스트 시스템으로 이동하므로 호스트 시스템이 모든 트래픽을 먼저 가져옵니다.

공용 IP에서 들어오는 모든 트래픽이 특정 컨테이너로 이동하도록 공용 IP에서 개인 IP로의 사전 경로(호스트에서)를 설정했습니다.

하지만 컨테이너에서 공용 IP로 나가는 트래픽을 라우팅하는 방법을 모르겠습니다. 들어오는 트래픽에 대해 했던 것처럼 사전 라우팅을 설정해 보았지만 소용이 없었습니다.

iptables -L 쇼

Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:domain /* managed by lxd-bridge */
ACCEPT     udp  --  anywhere             anywhere             udp dpt:domain /* managed by lxd-bridge */
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:bootps /* managed by lxd-bridge */
ACCEPT     udp  --  anywhere             anywhere             udp dpt:bootps /* managed by lxd-bridge */

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere             /* managed by lxd-bridge */
ACCEPT     all  --  anywhere             anywhere             /* managed by lxd-bridge */

iptables -t nat -L 표시

Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination
DNAT       all  --  anywhere             ip-b.ip-b-b-b.eu  to:10.204.119.5
DNAT       all  --  anywhere             10.204.119.5         to:b.b.b.b

b.b.b.b --> second public ip (for the container)
10.204.119.5 --> containers (private) ip in the lxdbr0 bridge

공용 IP에서 들어오는 트래픽은 컨테이너로 라우팅되지만 컨테이너에서 나가는 트래픽은 그렇지 않습니다.

또한 LXD_IPV4_NAT="false"컨테이너가 나가는 트래픽에 내 호스트 IP 주소를 사용할 수 있도록 lxd 브리지 구성에서 이를 설정했습니다(원하지 않음).

편집 #1: 경로 -n 표시

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         x.x.x.1         0.0.0.0         UG    0      0        0 ens3
10.204.119.0    0.0.0.0         255.255.255.0   U     0      0        0 lxdbr0
x.x.x.1         0.0.0.0         255.255.255.255 UH    0      0        0 ens3

x.x.x.1 --> gateway of my hosts ip (x.x.x.x)

편집 #2: 예

- pIP1 = public ip 1, should be used for host
- pIP2 = "      "  2, should be used for the container

the container runs on the host system.

container = 10.204.119.5 (device lxdbr0)
host      = pIP1 (device ens3) and pIP2 (device ens3:0)

Outgoing packets from the container come with the source ip 10.204.119.5. 
Now these packets should change the source ip to pIP2 and then sent to the 
gateway (so it appears to the router, that the packet from the container 
comes from the pIP2)

답변1

컨테이너의 프라이빗 IP에서 컨테이너의 퍼블릭 IP($publicIP2)의 호스트 인터페이스로 NAT 트래픽을 전송하기만 하면 됩니다.

iptables -t nat -A POSTROUTING -s 10.204.119.5/32 -j SNAT --to-source $publicIP2

관련 정보