Fix format issue

This commit is contained in:
Baohua Yang
2026-02-21 10:19:28 -08:00
parent 063c3f1381
commit 47cfc173a6
45 changed files with 596 additions and 604 deletions

View File

@@ -4,15 +4,24 @@
> **Namespace Linux 内核提供的资源隔离机制它让容器内的进程仿佛运行在独立的操作系统中**Namespace 是容器技术的核心基础之一它回答了一个关键问题**如何让一个进程"以为"自己独占整个系统**
```
宿主机视角: 容器内视角:
┌─────────────────────────┐ ┌─────────────────────────┐
PID 1: systemd │ │ PID 1: nginx │ ← 容器认为自己是 PID 1
PID 2: sshd │ │ PID 2: nginx worker │
│ PID 3: dockerd │ │ │
PID 1234: nginx ←──────│─────│ (实际是宿主机的 1234
PID 1235: nginx worker │ │ │
└─────────────────────────┘ └─────────────────────────┘
```mermaid
flowchart LR
subgraph Host ["宿主机视角"]
direction TB
H1["PID 1: systemd"]
H2["PID 2: sshd"]
H3["PID 3: dockerd"]
H4["PID 1234: nginx"]
H5["PID 1235: nginx worker"]
end
subgraph Container ["容器内视角"]
direction TB
C1["PID 1: nginx<br/>← 容器认为自己是 PID 1"]
C2["PID 2: nginx worker"]
end
H4 -. "(实际是宿主机的 1234" .- C1
```
### Namespace 的类型
@@ -76,13 +85,21 @@ NET Namespace 负责网络栈的隔离,包括网卡、路由表和 iptables
#### NET 隔离效果
```
宿主机 容器
┌─────────────────────┐ ┌─────────────────────┐
eth0: 192.168.1.10 │ │ eth0: 172.17.0.2 │ ← 不同的 IP
│ docker0: 172.17.0.1│◄───────►│ (veth pair 连接) │
端口 80 可用 │ │ 端口 80 可用 │ ← 可以使用相同端口
└─────────────────────┘ └─────────────────────┘
```mermaid
flowchart LR
subgraph Host ["宿主机"]
direction TB
H1["eth0: 192.168.1.10<br/>端口 80 可用"]
H2["docker0: 172.17.0.1"]
end
subgraph Container ["容器"]
direction TB
C1["eth0: 172.17.0.2<br/>端口 80 可用"]
C2["(veth pair 连接)"]
end
H2 <--> C2
```
#### NET 关键点
@@ -185,12 +202,22 @@ USER Namespace 允许将容器内的用户 ID 映射到宿主机的不同用户
#### USER 隔离效果
```
容器内 宿主机
┌─────────────────┐ ┌─────────────────┐
UID 0 (root) │───映射────►│ UID 100000 │ ← 非特权用户
UID 1 (daemon) │───映射────►│ UID 100001 │
└─────────────────┘ └─────────────────┘
```mermaid
flowchart LR
subgraph Container ["容器内"]
direction TB
C1["UID 0 (root)"]
C2["UID 1 (daemon)"]
end
subgraph Host ["宿主机"]
direction TB
H1["UID 100000<br/>← 非特权用户"]
H2["UID 100001"]
end
C1 -- 映射 --> H1
C2 -- 映射 --> H2
```
#### 安全意义