Skip to content

Commit b15fbf7

Browse files
committed
[pod] refactor: rewrite pod document.
1 parent 2592735 commit b15fbf7

1 file changed

Lines changed: 26 additions & 10 deletions

File tree

docs/pod.md

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
## Pod
22

3-
`pod` 是我们练习的第一个 k8s 资源,在了解 `pod``container` 的区别之前,我们可以先创建一个简单的 pod 试试,
3+
如果在生产环境中运行的都是独立的单体服务,那么 Container (容器) 也就够用了,但是在实际的生产环境中,维护着大规模的集群和各种不同的服务,服务之间往往存在着各种各样的关系。而这些关系的处理,才是手动管理最困难的地方。
44

5-
我们先创建 `nginx.yaml` 文件。
5+
**Pod** 是我们将要创建的第一个 k8s 资源,也是可以在 Kubernetes 中创建和管理的、最小的可部署的计算单元。在了解 `pod``container` 的区别之前,我们可以先创建一个简单的 pod 试试,
6+
7+
我们先创建 `nginx.yaml` 文件,编写一个可以创建 `nginx` 的 Pod。
68

79
```yaml
810
# nginx.yaml
@@ -18,15 +20,25 @@ spec:
1820
1921
其中 `kind` 表示我们要创建的资源是 `Pod` 类型, `metadata.name` 表示要创建的 pod 的名字,这个名字需要是唯一的。 `spec.containers` 表示要运行的容器的名称和镜像名称。镜像默认来源 `DockerHub`。
2022

21-
我们运行第一条 k8s 命令 `kubectl apply -f nginx.yaml` 命令启动 pod。
23+
我们运行第一条 k8s 命令 `kubectl apply -f nginx.yaml` 命令来创建 `nginx` Pod。
24+
25+
接着通过 `kubectl get pods` 来查看 pod 是否正常启动。
26+
27+
最后通过 `kubectl port-forward nginx-pod 4000:80` 命令将 `nginx` 默认的 `80` 端口映射到本机的 `4000` 端口,打开浏览器或者 `curl` 来访问 `http://127.0.0.1:4000` , 查看是否成功访问 `nginx` 默认页面!
2228

2329
``` shell
24-
kubectl apply -f nginx.yaml
25-
```
30+
kubectl apply -f nginx.yaml
31+
# pod/nginx-pod created
32+
33+
kubectl get pods
34+
# nginx-pod 1/1 Running 0 6s
2635
27-
我们可以通过 `kubectl get pods` 来查看 pod 是否正常启动,
36+
kubectl port-forward nginx-pod 4000:80
37+
# Forwarding from 127.0.0.1:4000 -> 80
38+
# Forwarding from [::1]:4000 -> 80
39+
```
2840

29-
通过命令下面的命令来配置 `nginx` 的首页内容
41+
`kubectl exec -it` 可以用来进入 Pod 内容器的 Shell。通过命令下面的命令来配置 `nginx` 的首页内容
3042

3143
```shell
3244
kubectl exec -it nginx-pod /bin/bash
@@ -40,11 +52,11 @@ kubectl port-forward nginx-pod 4000:80
4052

4153
### Pod 与 Container 的不同
4254

43-
回到 `pod` 和 `container` 的区别,我们会发现刚刚创建出来的资源如下图所示,在最内层是我们的服务 `nginx`,运行在 `container` , `container` (容器) 的本质是进程,而 `pod` 是管理这一组进程的资源。
55+
回到 `pod` 和 `container` 的区别,我们会发现刚刚创建出来的资源如下图所示,在最内层是我们的服务 `nginx`,运行在 `container` 容器当中, `container` (容器) 的本质是进程,而 `pod` 是管理这一组进程的资源。
4456

4557
![nginx_pod](https://cdn.jsdelivr.net/gh/guangzhengli/PicURL@master/uPic/nginx_pod.png)
4658

47-
所以自然 `pod` 可以管理多个 `container`,在某些场景例如 `container` 之间需要文件交换(日志收集),本地网络通信需求(使用 localhost 或者 Socket 文件进行本地通信),在这些场景中使用 `pod` 管理多个 `container` 就非常的推荐。如下图所示:
59+
所以自然 `pod` 可以管理多个 `container`,在某些场景例如服务之间需要文件交换(日志收集),本地网络通信需求(使用 localhost 或者 Socket 文件进行本地通信),在这些场景中使用 `pod` 管理多个 `container` 就非常的推荐。而这,也是 k8s 如何处理服务之间复杂关系的第一个例子,如下图所示:
4860

4961
![pod](https://cdn.jsdelivr.net/gh/guangzhengli/PicURL@master/uPic/pod.png)
5062

@@ -64,7 +76,9 @@ kubectl delete -f nginx.yaml
6476
# pod "nginx" deleted
6577
```
6678

67-
根据我们在 `container` 的那节构建的 `hellok8s:v1` 的镜像,同时参考 `nginx` pod 的资源定义,我们很容易的编写出 `hellok8s:v1` `pod` 的资源文件。并通过 `port-forward` 到本地的 `3000` 端口进行访问,最终得到字符串 `[v1] Hello, Kubernetes!`。
79+
最后,根据我们在 `container` 的那节构建的 `hellok8s:v1` 的镜像,同时参考 `nginx` pod 的资源定义,你能独自编写出 `hellok8s:v1` Pod 的资源文件吗。并通过 `port-forward` 到本地的 `3000` 端口进行访问,最终得到字符串 `[v1] Hello, Kubernetes!`。
80+
81+
`hellok8s:v1` Pod 资源定义和相应的命令如下所示:
6882

6983
```yaml
7084
# hellok8s.yaml
@@ -79,6 +93,8 @@ spec:
7993
```
8094

8195
```shell
96+
kubectl apply -f hellok8s.yaml
97+
8298
kubectl get pods
8399
84100
kubectl port-forward hellok8s 3000:3000

0 commit comments

Comments
 (0)