네트워크 네임스페이스: VM 네트워킹 격리
Source: Dev.to
Introduction
이전 글에서는 Linux 가상화에 대한 다양한 네트워킹 접근 방식을 논의하고 qcontroller라는 도구를 소개했습니다. qcontroller는 QEMU VM 인스턴스의 전체 수명 주기를 관리하며, nftables를 사용해 방화벽 설정도 구성합니다. 기존 방식은 브리지, TAP 디바이스, 그리고 호스트 전체에 적용되는 nftables 규칙을 사용했으며, 이는 호스트 네트워킹 스택을 오염시키고 VM을 제거할 때 수동으로 정리해야 하는 번거로움이 있었습니다.
Linux 네트워크 네임스페이스는 모든 네트워킹 구성 요소를 격리하는 방법을 제공하며, 네임스페이스 하나만 삭제하면 그 안에 생성된 모든 것이 자동으로 제거됩니다.
What are Network Namespaces?
네트워크 네임스페이스는 각 네임스페이스마다 다음을 별도로 제공하는 Linux 커널 기능입니다.
- 네트워크 디바이스
- IPv4/IPv6 프로토콜 스택
- 라우팅 테이블
- 방화벽 규칙
/proc/net,/sys/class/net, 그리고 관련/proc/sys/net파일들- 포트 번호(소켓)
- UNIX‑domain 추상 소켓 네임스페이스
네임스페이스가 삭제되면, 해당 네임스페이스에 속한 모든 디바이스(예: veth 페어)도 자동으로 파괴됩니다.
veth 디바이스 페어는 두 네임스페이스(또는 네임스페이스와 호스트)를 연결할 수 있는 가상 이더넷 케이블과 같습니다. 이는 격리된 네임스페이스에 인터넷 연결을 제공하기 위한 기본 블록입니다.
Creating and Configuring a Network Namespace
# Create a new network namespace called 'example'
sudo ip netns add example
# Create a veth pair (virtual Ethernet cable)
sudo ip link add host-veth type veth peer name example-veth
# Move one end of the veth pair into the new namespace
sudo ip link set example-veth netns example
# Assign IP addresses
sudo ip addr add 192.168.26.1/24 dev host-veth # Host side
sudo ip netns exec example ip addr add 192.168.26.2/24 dev example-veth # Namespace side
# Bring both interfaces up
sudo ip link set dev host-veth up
sudo ip netns exec example ip link set dev example-veth up
연결 테스트:
sudo ip netns exec example ping -c 3 192.168.26.1
192.168.26.1로부터 응답이 표시되면, 두 네임스페이스가 서로 통신할 수 있으며 여전히 격리된 상태임을 확인할 수 있습니다.
Enabling Internet Access
-
네임스페이스 내부에 기본 라우트를 설정하여 트래픽이 veth 페어의 호스트 쪽으로 전송되도록 합니다.
sudo ip netns exec example ip route add default via 192.168.26.1 -
호스트에서 포워딩 및 NAT를 구성합니다(
enp0s1을 실제 외부 인터페이스 이름으로 교체하세요).# Enable IP forwarding sudo sysctl -w net.ipv4.ip_forward=1 # Allow established/related traffic back from the internet sudo iptables -A FORWARD -i enp0s1 -o host-veth -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT # Allow new outgoing connections from the namespace sudo iptables -A FORWARD -i host-veth -o enp0s1 -j ACCEPT # Masquerade (NAT) the namespace subnet sudo iptables -t nat -A POSTROUTING -s 192.168.26.0/24 -o enp0s1 -j MASQUERADE
네임스페이스에서 인터넷 연결 테스트:
sudo ip netns exec example ping -c 3 8.8.8.8
응답이 성공적으로 돌아오면 네임스페이스가 이제 인터넷에 접근할 수 있음을 의미합니다.
Adding a Bridge and TAP Devices for VMs
격리된 네임스페이스 안에서 VM을 실행하려면 브리지를 만들고, IP 주소를 브리지로 이동한 뒤, VM TAP 디바이스를 브리지에 연결합니다.
# 1. Create a bridge inside the namespace
sudo ip netns exec example ip link add name br0 type bridge
# 2. Move the IP address from the veth interface to the bridge
sudo ip netns exec example ip addr del 192.168.26.2/24 dev example-veth
sudo ip netns exec example ip addr add 192.168.26.2/24 dev br0
# 3. Attach the veth interface to the bridge
sudo ip netns exec example ip link set example-veth master br0
# 4. Bring the bridge up
sudo ip netns exec example ip link set br0 up
이제 br0에 연결된 모든 VM TAP 디바이스는 격리된 네트워크의 일부가 됩니다. example 네임스페이스를 삭제하면 브리지, veth 페어, 그리고 연결된 모든 TAP 디바이스가 자동으로 사라져 호스트 네트워킹이 깨끗하게 유지됩니다.