45

Linux Namespace

 5 years ago
source link: http://ericcenblog.com/2018/08/24/linux-namespace/?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Namespace是Linux虚拟网络的一个重要概念,其实不光是虚拟网络,像现在最火的容器技术也是以Namespace为基石(另外一个是cgroups),所以了解Namespace是很有必要的。

传统的Linux的许多资源是全局的,比如进程ID资源,而Namespace的 目的首先就是把这些资源做资源隔离。Linux可以在一个Host内创建许多namespace,于是那些原本是Linux全局的资源,就变成了namespace范围内的“全局”资源,而且不同namespace的资源互相不可见,彼此透明。

Linux具体将哪些全局资源做了隔离呢?看Linux相应的代码最直观:

//nsproxy.h
struct nsproxy{ 

     atomic_t count;
     struct uts_namespace *uts_ns;
     struct ipc_namespace *ipc_ns;
     struct mnt_namespace *mnt_ns;
     struct pid_namespace *pid_ns;
     struct user_namespace *user_ns;
     struct net *net_ns;
};

以上6个资源,就是Linux namespace所隔离的资源,其基本含义如下:

uts_ ns: UTS为Unix Timesharing System的简称,包含内存名称,版本 ,底层体系结构等信息。

ipc_ ns: 所有与进程间通信(IPC)有关的信息。

mnt_ ns: 当前装载的 文件系统。

pid_ ns: 有关进程ID的信息。

user_ ns: 资源配额的信息。

net_ ns: 网络信息。

Nz6vErj.jpg!web 上图表明,在每个namespace里面将原本是全局资源的进行了隔离,彼此互相不可见。同时在Linux的Host或者VM中,当然也会有一套相关的资源。

单纯从网络的视角来看,一个namespace提供了一份独立的网络协议栈(网络设备接口,IPv4,IPv6,IP路由,防火墙规则,socket等)。一个设备(Linux Device)只能在位于一个namespace钟,不同namespace中的设备可以利用veth_pair进行桥接。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK