다른 네트워크 네임스페이스에서 실행 중인 KVM QEMU VM과의 핑

다른 네트워크 네임스페이스에서 실행 중인 KVM QEMU VM과의 핑

별도의 네트워크 네임스페이스에서 실행 중인 KVM QEMU VM에서 기본 네트워크 네임스페이스로 ping을 실행할 수 없습니다.

내 설정은 다음과 같습니다.

ip netns add test-ns

# Creating veth
ip link add if-in-ns type veth peer name if-notin-ns
# Default namespace
ip link addr add 10.21.0.10/24 dev if-notin-ns
ip link set dev if-notin-ns up
# Test-ns namespace
ip link set dev if-in-ns netns test-ns
ip netns exec test-ns ip addr add 10.21.0.20/24 dev if-in-ns
ip netns exec test-ns ip link set dev if-in-ns up

# Creating a tap device in the namespace
ip netns exec test-ns ip tuntap add tap0 mode tap
ip netns exec test-ns ip addr add 10.0.2.2/24 dev tap0
ip netns exec test-ns ip link set dev tap0 up

# Create route to 10.0.2.0/24 if default namespace:
ip route add 10.0.2.0/24 dev if-notin-ns

# Run VM
ip netns exec test-ns qemu-system-x86_64 -drive file=img.qcow2,format=qcow2,media=disk -accel kvm -cpu host -m 2G -netdev tap,id=vm0,ifname=tap0,script=no,downscript=no -device virtio-net-pci,netdev=vm0,mac=52:54:77:6a:cc:02

결과는 다음과 같습니다.

# In default namespace:
ping 10.21.0.20 # Works
ping 10.0.2.2 # Works
ping 10.0.2.3 # Doesn't work

# In test-ns namespace:
ping 10.21.0.10 # Works
ping 10.0.2.3 # Works

# Inside the image:
ping 10.0.2.2 # Works
ping 10.21.0.20 # Works
ping 10.21.0.10 # Doesn't work

따라서 VM은 개발자에게 ping을 보낼 수 있습니다.if-in-ns그리고 개발자if-in-ns피어에게 ping을 보낼 수 있음if-notin-ns. 나는 이것이 VM이 개발자에게 핑을 보낼 수 있어야 한다는 것을 의미한다고 생각했습니다.if-notin-ns그것도 그렇지만 그럴 수는 없어요. 내가 뭘 오해하고 있는 걸까?

편집: Salim Aljayousi 답변에 대한 의견:

test-ns 네임스페이스에는 이미 dev를 통해 기본 네임스페이스에 도달하는 경로가 있습니다.if-in-ns:

ip netns exec test-ns ip route
10.0.2.0/24 dev tap0 proto kernel scope link src 10.0.2.2 
10.21.0.0/24 dev if-in-ns proto kernel scope link src 10.21.0.20

해당 항목을 제거하고 제안한 항목을 추가하면 다음과 같습니다.

ip netns exec ns ip route del 10.21.0.0/24 dev if-in-ns
ip netns exec ns ip route add 10.21.0.0/24 dev tap0

그런 다음 VM 내에서 다음을 얻습니다.

ping 10.21.0.10
From 10.0.2.2 icmp_seq=1 Redirect Host(New nexthop: 10.21.0.10) ```

답변1

test-ns 네임스페이스에 경로를 추가하면 탭 인터페이스를 통해 기본 네임스페이스에 접근할 수 있습니다.

ip netns exec test-ns ip route add 10.21.0.0/24 dev tap0

이렇게 하면 VM이 기본 네임스페이스의 장치를 ping할 수 있습니다.

네임스페이스 간에 패킷이 전달되도록 하려면 호스트 시스템에서 IP 전달을 활성화해야 할 수도 있습니다.

sysctl -w net.ipv4.ip_forward=1

관련 정보