虚拟linux网络,linux虚拟网络

linux虚拟网络

neutron的使命 ‘实现服务和相关库以提供按需、可伸缩和技术无关的网络抽象’

tap

操作系统内核中的的虚拟网络设备 位于数据链路层

tap和数据链路层主要协议中的以太网协议Ethernet对应,所以也被成为 虚拟以太网设备

modinfo tun #检查内核模块

lsmod | grep tun #检查模块是否加载

modprobe tun #加载模块

tunctl -t tap-test #创建虚拟网卡

ip link show #查看二层设备

ifconfig -a #查看所有网卡

ip addr add local 192.168.150.1/24 dev tap-test

ifconfig tap-test 10.10.10.10/24 #绑定ip 上同

namespace

linux许多资源是全局的,namespace首先就是要让这些资源做资源隔离

linux可以在一个host中做多个namespace,而且不同的namespace的资源相互不可见相互透明

namespace隔离的资源

uts_ns Unix分时系统 各种底层的信息

ipc_ns 进程间通信

mnt_ns 当前装载的文件系统

pid_ns 进程ID的相关信息

user_ns 资源配额

net_ns 网络信息

从网络视角看namespace提供了一份独立的网络协议栈网络设备接口、IPv4、IPv6、路由、防火墙

一个设备只能位于一个namespace中,不同的设备可以通过veth pair进行桥接

ip netns list #查看命名空间列表

ip netns add ns_test #创建一个新的命名空间

ip link set tap-test netns ns_test #将现有的虚拟网卡转移到namespace

ip netns exec ns_test ip link show #查看namespace内的网卡信息

ip netns exec ns_test ifconfig tun-test 192.168.150.1/24 up #绑定ip

ip netns exec ns_test ifconfig -a #查看ip

veth pair

veth pair 不是一个设备,而是一对设备,以连接两个虚拟以太网端口

操作veth pair 需要和 namespace 一起使用

ip link add tap1 type veth peer name tap2 #创建一对veth pair设备

#不指定name会从veth0开始

ip link show #查看链路层

ip link set tap1 netns ns1 #将设备转移到命名空间

ip link set tap2 netns ns2 #转移到另一个命名空间

ip netns exec ns1 ifconfig tap1 10.10.10.10/24 up #添加ip

ip netns exec ns1 ifconfig -a

...... #重复操作

ip netns exec ns2 ping 10.10.10.10 #在相互隔离的命名空间相互可访

以上就是两个namespace之间的连接方法

假设连接两个以上的命名空间呢 这时候最好的办法就是bridge

Bridge

linux现实bridge功能的是brctl命令,如果没有用以下方法安装

yum install bridge-utils

现在假设有四个namespace,需要互相通信

1、创建四个 veth pair

ip link add tap0 type veth peer

ip link add tap1 type veth peer

ip link add tap2 type veth peer

ip link add tap3 type veth peer

ip link show

2、创建四个namespace

ip netns add ns0

ip netns add ns1

ip netns add ns2

ip netns add ns3

ip netns list

3、迁移veth设备并添加ip

ip link set veth0 netns ns0

ip link set veth1 netns ns1

ip link set veth2 netns ns2

ip link set veth3 netns ns3

ip netns exec ns0 ifconfig veth0 10.10.10.10/24 up

ip netns exec ns1 ifconfig veth1 10.10.10.11/24 up

ip netns exec ns2 ifconfig veth2 10.10.10.12/24 up

ip netns exec ns3 ifconfig veth3 10.10.10.13/24 up

4、创建bridge

brctl addbr br0

brctl show

5、将tap添加到bridge并启用tap设备

brctl addif br0 tap0

brctl addif br0 tap1

brctl addif br0 tap2

brctl addif br0 tap3

brctl show

ip link set br0 up

ip link set tap0 up

ip link set tap1 up

ip link set tap2 up

ip link set tap3 up

6、测试

ip netns exec ns[0-3] ping 10.10.10.1[0-3]

为什么本地的namespace中的ip不可达

Router

1、查看内核ip转发状态

cat /proc/sys/net/ipv4/ip_forward

sysctl -a |grep "ip_forward"

grep -R net.ip /usr/lib/sysctl.d/

2、开启转发

net.ipv4.ip_forward = 1 #开启路由转发

/etc/sysctl.conf #写入配置文件

sysctl -p #生效配置

例:

1、创建两个namespace,并创建两对veth pair设备迁移到namespace

ip netns add ns0

ip netns add ns1

ip netns list

ip link add tap0 type veth peer name tap_peer0

ip link add tap1 type veth peer name tap_peer1

ip link show

ip link set tap0 netns ns0

ip link set tap1 netns ns1

ip link show

2、给各个设备配置ip地址、启用设备

ip addr add local 10.10.10.10/24 dev tap_peer0

ip addr add local 10.10.11.10/24 dev tap_peer1

ip netns exec ip addr add local 10.10.10.20/24 dev tap0

ip netns exec ns0 ip addr add local 10.10.10.20/24 dev tap0

ip netns exec ns1 ip addr add local 10.10.11.20/24 dev tap1

ip link set tap_peer0 up

ip link set tap_peer1 up

ip netns exec ns0 ip link set tap0 up

ip netns exec ns1 ip link set tap1 up

3、在namespace中添加路由

ip netns exec ns0 route add -net 10.10.11.0/24 gw 10.10.10.10

ip netns exec ns1 route add -net 10.10.10.0/24 gw 10.10.11.10

ip netns exec ns0 ping 10.10.11.20

ip netns exec ns1 ping 10.10.10.20

ns0中的网卡10.20通过veth ns0通过自身路由表发现到达11.20需要通过10.10,ns0通过pair设备可以直接访问到10.10,数据包通过内核转发到veth1设备,最终到达ns1的11.20,ns1接受到ICMP协议通过路由表发现回复数据要通过11.10,于是就讲数据直接丢给11.10,内核接受到数据将数据发往veth0设备,完成访问过程。

tun

tun是网络层的点对点设备,它启用了IP层隧道功能。

linux原生支持三层隧道

ipip

IP IN IP在IPv4报文的基础上再封装一个IPv4bao wen

gre 通用路由封装

在任意一种网络层协议上封装任意一个其他网络层的协议

sit

类似IPIP,IPv4的头封装一个IPv6的报文

isatap

站内自动隧道寻址协议

vti

IPsec隧道提供一个可路由的接口类型

modprobe ipip

lsmod | grep ipip

iptables

iptables 是一个运行在用户空间的命令行工具

netfilter 内核空间的模块iptables所有功能的实现

四张表

mangle 修改数据包的内容,用来做流量整形 包标记表

filter 数据过滤表

nat 地址装换表

raw 状态跟踪表

五个链条

INPUT 入站

OUTPUT 出站

FORWARD 转发

PRERROUTING 路由前

POSTROUTING 路由后

从报文进入host的那一刻起,到报文离开本机的那一刻止,中间这段时间,或者是发自本机的报文,从报文准备发送的那一刻止,中间的这段时间,netfilter会在某些时刻点插入处理模块,这些模块根据相应的策略和规则对报文进行处理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值