Switching Routing
ip link
์๋ก ๋ค๋ฅธ Host ๊ฐ ํต์ ํ๊ธฐ ์ํด์ ๋ Host ๋ฅผ ์ด์ด์ฃผ๋ Switch ๊ฐ ํ์ํ๋ค. Host ๋ฅผ Switch ์ ์ฐ๊ฒฐํ๋ ค๋ฉด Network Interface ๊ฐ ํ์ํ๋ฐ, ์ ๋ช
๋ น์ด๋ฅผ ํตํด Host ์ Network Interface ๋ฅผ ํ์ธํ ์ ์๋ค. Linux ์ ๊ฒฝ์ฐ eth0
๋ผ๋ Network Interface ๋ฅผ ํ์ธํ ์ ์๋ค.
ip addr
ip addr add 192.168.1.10/24 dev eth0
Host ๋ค์ด Switch ์ ์ฐ๊ฒฐ๋ ์ดํ ์ ๋ช ๋ น์ด๋ฅผ ํตํด Host ์ IP ์ฃผ์๋ฅผ ํ ๋นํด์ค ์ ์๋ค.
ip route
ip route add 192.168.2.0/24 via 192.168.1.1
ip route add default via 192.168.2.1
ip route add 0.0.0.0 via 192.168.2.1
์๋ก ๋ค๋ฅธ Switch ๊ฐ ํต์ ํ๊ธฐ ์ํด์ ๋ Switch ๋ฅผ ์ด์ด์ฃผ๋ Router ๊ฐ ํ์ํ๋ค. 192.168.2.0/24
์ ์ฃผ์๋ฅผ ๊ฐ์ง๋ Switch ์ ์ ๊ทผํ๊ธฐ ์ํด์ Router ์ 192.168.1.1
์ฃผ์๋ฅผ ๊ฑฐ์ณ๊ฐ๋ฉด ๋๋ค๋ ์๋ฏธ๋ค. default
๋๋ 0.0.0.0
๋ ์ด์ธ ๋ชจ๋ ์ฃผ์๋ฅผ ์๋ฏธํ๋ฉฐ ์ฆ, ์ธํฐ๋ท์ ์ ๊ทผํ๊ธฐ ์ํด์ 192.168.2.1
์ฃผ์๋ฅผ ๊ฑฐ์ณ๊ฐ์ผ ๋๋ค๋ ์๋ฏธ๋ค.
cat /proc/sys/net/ipv4/ip_forward
echo 1 > /proc/sys/net/ipv4/ip_forward
Router ๊ฐ 192.168.1.1
์ 192.168.2.1
์ ์ฃผ์๋ฅผ ๊ฐ์ง๊ณ ์๋ค๋ ๊ฒ์ eth0
๊ณผ eth1
์ด ํ ๋น๋๋ค๋ ์๋ฏธ์ด๊ณ , ๊ธฐ๋ณธ์ ์ผ๋ก ์๋ก ๋ค๋ฅธ Network Interface ๊ฐ์ Packet ์ ๋ฌ์ ๋งํ์๋ค. ์ ๋ช
๋ น์ด๋ฅผ ํตํด ip_forward
๊ฐ์ 1 ๋ก ๋ฐ๊ฟ์ฃผ๋ฉด Router ๋ด๋ถ์์ Packet ๋ฅผ Network Interface ๊ฐ์ ์ ๋ฌํ ์ ์๋๋ก ํด์ค ์ ์๋ค. ์ฌ๋ถํ
ํ ๊ฒฝ์ฐ ์ค์ ๊ฐ์ด ์ด๊ธฐํ ๋๊ธฐ ๋๋ฌธ์ /etc/sysctl.conf
์์ net.ipv4.ip_forward
๊ฐ์ 1 ๋ก ์ค์ ํด์ฃผ๋ฉด ์๊ตฌ์ ์ผ๋ก ์ค์ ํ ์๋ ์๋ค.
DNS
cat /etc/hosts
IP ์ฃผ์ ๋์ Name ์ผ๋ก Host ๋ฅผ ์๋ณํ๊ธฐ ์ํด ์ ๊ฒฝ๋ก์ IP ์ฃผ์์ ํด๋นํ๋ Host Name ์ ์ง์ ํด ์ค ์ ์๋ค. ๋์ ๋ชจ๋ Host ์ ํ์ํ๊ธฐ ๋๋ฌธ์ ๊ด๋ฆฌํ๊ธฐ๊ฐ ์ด๋ ค์์ง๋ค. ๋๋ฌธ์ DNS ์๋ฒ๋ฅผ ํ์ฉํด ํ๋์ ๊ด๋ฆฌํฌ์ธํธ๋ก ํตํฉํ๋ค.
cat /etc/resolv.conf
DNS ์๋ฒ์ ์ฃผ์๋ ์ ๊ฒฝ๋ก์ ์ ์ฅํ์ฌ ์ฌ์ฉํ ์ ์๋ค. 8.8.8.8
์ Google ์ด ์ ๊ณตํ๋ DNS ์๋ฒ๋ค.
cat /etc/nsswitch.conf
...
hosts: files dns
...
๊ธฐ๋ณธ์ ์ผ๋ก /etc/hosts
์์ ์ฃผ์๋ฅผ ์ฐพ๊ณ ์์ผ๋ฉด /etc/resolv.conf
์์ ์ฃผ์๋ฅผ ์ฐพ๋๋ฐ, ์ ์ค์ ํ์ผ์์ ์์๋ฅผ ๋ณ๊ฒฝํ ์๋ ์๋ค.
CoreDNS
wget https://github.com/coredns/coredns/releases/download/v1.7.0/coredns_1.7.0_linux_amd64.tgz
cat > /etc/hosts
192.168.1.10 web
192.168.1.11 db
192.168.1.15 web-1
192.168.1.16 db-1
192.168.1.21 web-2
192.168.1.22 db-2
DNS ์๋ฒ๋ฅผ ๊ตฌ์ถํ๋ ค๋ฉด DNS ์ํํธ์จ์ด๊ฐ ์ค์น๋ Host ๊ฐ ํ์ํ๋ค. CoreDNS ๋ผ๋ DNS ์ํํธ์จ์ด๋ฅผ ์ค์นํ์ฌ DNS ์๋ฒ๋ฅผ ๊ตฌ์ถํ ์ ์๋ค.
Network Namespaces
ip netns add red
ip netns add blue
Container ๊ฐ namespace ๋ฅผ ํตํด ํ๋ก์ธ์ค๋ฅผ ๊ฒฉ๋ฆฌํ๋ฏ Network ์ญ์ ๊ฒฉ๋ฆฌํ ์ ์๋ค. ์ ๋ช
๋ น์ด๋ red
์ blue
๋ผ๋ ์ด๋ฆ์ ๊ฐ์ง Network Namespace ๋ฅผ ์์ฑํ๋ค๋ ์๋ฏธ๋ค.
ip netns exec red ip link
ip -n red link
ip link
๋ช
๋ น์ด๋ฅผ ํตํด Network Interface ๋ฅผ ํ์ธํ๋ฏ Network Namespace ์ Network Interface ๋ฅผ ํ์ธํ๊ธฐ ์ํด์ ์ ๋ช
๋ น์ด๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
ip -n red arp
ip -n red route
arp ์ route ์ญ์ ๋์ผํ ๋ฐฉ๋ฒ์ผ๋ก ํ์ธํ ์ ์๋ค.
Virtual Cable
ip link add veth-red type veth peer name veth-blue
์ ๋ช ๋ น์ด๋ก ๊ฐ์ ์ผ์ด๋ธ์ ์์ฑํ๊ณ ,
ip link set veth-red netns red
ip link set veth-blue netns blue
ip -n red addr add 192.168.15.1/24 dev veth-red
ip -n blue addr add 192.168.15.2/24 dev veth-blue
ip -n red link set veth-red up
ip -n blue link set veth-blue up
์ ๋ช ๋ น์ด๋ก Network Namespace ๋ผ๋ฆฌ ์ฐ๊ฒฐํด์ค ์ ์๋ค.
Linux Bridge
ip link add v-net-0 type bridge
ip link set dev v-net-0 up
Network Namespace ๊ฐ ๋ง์์ง ๊ฒฝ์ฐ Switch ์ญํ ์ ํ๋ Linux Bridge ๋ฅผ ํ์ฉํ ์ ์๋ค.
ip link add veth-red type veth peer name veth-red-br
ip link set veth-red netns red
ip link set veth-red-br master v-net-0
ip -n red addr add 192.168.15.1 dev veth-red
ip -n red link set veth-red up
์ดํ Network Namespace ์ Bridge ๋ฅผ ์ฐ๊ฒฐํ๊ธฐ ์ํ Virtual Cable ์ ์์ฑ ๋ฐ ํ ๋นํด์ฃผ๊ณ , IP ์ฃผ์ ๋ ํ ๋นํด ์ค ๋ค ํ์ฑํ ํด์ฃผ์.
ip addr add 192.168.15.5/24 dev v-net-0
์ ๋ช ๋ น์ด๋ฅผ ํตํด Host ์ Bridge ๋ฅผ ์ฐ๊ฒฐํด์ฃผ์.
ip netns exec blue ip route add 192.168.1.0/24 via 192.168.15.5
Namespace ๊ฐ ์ธ๋ถ Host ์ ํต์ ํ๊ธฐ ์ํด์ ์ธ๋ถ์ 192.168.1.0/24
์ ์ฃผ์๋ฅผ ๊ฐ์ง๋ Switch ์ routing ์ค์ ์ ํด์ค์ผํ๋ค. ์ ๋ช
๋ น์ด๋ฅผ ์ฌ์ฉํ๋ฉด 192.168.15.5
์ ์ฃผ์๋ฅผ ๊ฐ์ง๋ v-net-0
์ ํตํด ํธ์คํธ์ eth0
๋ฅผ ๊ฑฐ์ณ ์ธ๋ถ๋ก ํฅํ๊ฒ ๋๋ค.
iptables -t nat -A PREROUTING --dport 80 --to-destination 192.168.15.2:80 -j DNAT
์ธ๋ถ Host ๊ฐ ๋ค๋ฅธ Host ์ Network Namespace ์ ์ ๊ทผํ๊ธฐ ์ํด์ ์ ๋ช ๋ น์ด๋ก ํฌํธํฌ์๋ฉ ํด์ค์ผ ํ๋ค.
Docker Networking
ip link
08. Docker Networking ์์ ๋ฐฐ์ด bridge ๊ฐ ๋ฐ๋ก Linux Bridge ๋ค. ip link
๋ก ํ์ธํด๋ณด๋ฉด docker0
๋ผ๋ ์ด๋ฆ์ ๊ฐ์ง Network Interface ๋ฅผ ํ์ธํ ์ ์๋ค.
ip netns
Docker Container ๋ฅผ ์คํํ๋ฉด Network Namespace ๋ ๋ง์ฐฌ๊ฐ์ง๋ก ์๋์ผ๋ก ์์ฑ๋๋ค.
iptables -nvL -t nat
ํฌํธํฌ์๋ฉ๋ ๋ง์ฐฌ๊ฐ์ง. Docker Container ๋ฅผ ์คํํ ๋ iptable
์ค์ ๊น์ง ์๋์ผ๋ก ์์ฑ๋๋ค.
์ฆ, Docker Container ๋ฅผ ์์ฑํ ๋, ๋คํธ์ํฌ ์ธก๋ฉด์์ ๋ด๋ถ์ ์ผ๋ก ์์์ ์ธ๊ธํ ๋ชจ๋ ์์ ๋ค์ด ์๋์ผ๋ก ์ํ๋๋ ๊ฒ์ด๋คโฆ