Linux
Introducción a los Namespaces en Linux
Los Namespaces de Linux es una característica del kernel de linux que nos permiten aislar procesos del sistema principal. Los Namespaces son un aspecto fundamental de los contenedores (dockers) . El conocimiento de el funcionamiento de los Namespaces nos va a permitir conocer el funcionamiento interno de los dockers. Existen varios Namespaces(Process,Network,IPC,etc ) y algunos mas que se iran añadiendo al kernel. Aquí vamos a ver una introducción al Network Namespace. Cada namespace va a tener :
- Tabla de enrutamiento
- Tabla ARP
- Dispositivos
- Iptables
1 |
ip netns add prueba |
1 |
ip netns exec prueba ip link |
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00[/su_note]
Vamos a ver un ejemplo de configuración creando un interface ethernet vitual. Los veth siempre van en pares . Seria como las dos terminaciones de un cable de red. Creamos un par veth:
1 |
ip link add veth-1 type veth peer name veth-2 |
25: veth-2@veth-1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether 7e:b8:84:78:13:5c brd ff:ff:ff:ff:ff:ff 26: veth-1@veth-2: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether 76:97:9b:0b:02:5a brd ff:ff:ff:ff:ff:ff[/su_note]
Movemos el veth-2 al ns prueba:
1 |
ip link set veth-2 netns prueba |
1 2 3 4 5 |
ip add add 10.10.10.2/24 dev veth-1 ip netns exec prueba ip add add 10.10.10.3/24 dev veth-2 ip link set veth-1 up ip netns exec prueba ip link set veth-2 up ping 10.10.10.3 # Comprobamos conectividad |
1 2 3 4 |
ip netns add servidor ip netns add cliente ip netns add gateway ip netns list |
gateway cliente servidor
En el NS gateway tenemos que activar le forward de paquetes:
1 |
ip netns exec gateway sysctl net.ipv4.ip_forward=1 |
1 2 3 4 5 6 7 |
ip link add svr-veth type veth peer name svrgw-veth ip link add cli-veth type veth peer name cligw-veth ip link set svr-veth netns servidor ip link set svrgw-veth netns gateway ip link set cligw-veth netns gateway ip link set cli-veth netns cliente ip netns exec gateway ip link show |
ip netns exec gateway ip link show 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 12: svrgw-veth@if13: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 32:37:51:0b:e5:50 brd ff:ff:ff:ff:ff:ff link-netnsid 0 14: cligw-veth@if15: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether e2:9a:3a:df:28:48 brd ff:ff:ff:ff:ff:ff link-netnsid 2
ip netns exec cliente ip link show 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 15: cli-veth@if14: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 76:e5:4a:65:b8:3c brd ff:ff:ff:ff:ff:ff link-netnsid 0
ip netns exec servidor ip link show 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 13: svr-veth@if12: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether f2:fa:c4:09:42:8b brd ff:ff:ff:ff:ff:ff link-netnsid 1[/su_note]
Añadimos las IP’s de los interfaces virtuales
1 2 3 4 |
ip netns exec servidor ifconfig svr-veth 192.168.100.1 ip netns exec gateway ifconfig svrgw-veth 192.168.100.254 ip netns exec gateway ifconfig cligw-veth 10.0.100.254 ip netns exec cliente ifconfig cli-veth 10.0.100.1 |
1 2 3 4 5 6 |
ip netns exec cliente route add default gw 10.0.100.254 ip netns exec servidor route add default gw 192.168.100.254 Comprobamos la conectividad: ip netns exec cliente ping 192.168.100.1 -I 10.0.100.1 |