Skip to content

Commit

Permalink
[pod] refactor: modify README.md and remove apache pod.
Browse files Browse the repository at this point in the history
  • Loading branch information
guangzhengli committed Aug 24, 2022
1 parent aa95043 commit 0409813
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 40 deletions.
72 changes: 44 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,59 +178,73 @@ docker push guangzhengli/hellok8s:v1

## Pod

关于 `pod` 的练习,我们先来练习下[官网的例子](https://kubernetes.io/docs/concepts/workloads/pods/)
`pod` 是我们练习的第一个 k8s 资源,在了解 `pod``container` 的区别之前,我们可以先创建一个简单的 pod 试试,

我们先创建 `nginx.yaml` 文件。

```yaml
# nginx.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
name: nginx-pod
spec:
containers:
- name: nginx-container
image: nginx
```
其中 `kind` 表示我们要创建的资源是 `Pod` 类型, `metadata.name` 表示要创建的 pod 的名字,这个名字需要是唯一的。 `spec.containers` 表示要运行的容器的名称和镜像名称。镜像默认来源 `DockerHub`。

我们运行第一条 k8s 命令 `kubectl apply -f nginx.yaml` 命令启动 pod。

``` shell
kubectl apply -f nginx.yaml
```

我们可以通过 `kubectl get pods` 来查看 pod 是否正常启动,

通过命令下面的命令来配置 `nginx` 的首页内容

```shell
kubectl apply -f nginx.yaml
kubectl exec -it nginx-pod /bin/bash
echo "hello kubenates by nginx!" > /usr/share/nginx/html/index.html
kubectl port-forward nginx-pod 4000:80
```

最后可以通过浏览器或者 `curl` 来访问 `http://127.0.0.1:4000` , 查看是否成功启动 `nginx` 和返回字符串 `hello kubenates by nginx!`。

### Pod 与 Container 的不同

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

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

kubectl port-forward nginx 3001:80
所以自然 `pod` 可以管理多个 `container`,在某些场景例如 `container` 之间需要文件交换(日志收集),本地网络通信需求(使用 localhost 或者 Socket 文件进行本地通信),在这些场景中使用 `pod` 管理多个 `container` 就非常的推荐。如下图所示:

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

### Pod 其它命令

我们可以通过 `logs` 或者 `logs -f` 命令查看 pod 日志,可以通过 `exec -it` 进入 pod 或者调用容器命令,通过 `delete pod` 或者 `delete -f nginx.yaml` 的方式删除 pod 资源。这里可以看到 [kubectl 所有命令](https://kubernetes.io/docs/reference/kubectl/cheatsheet/)。

```shell
kubectl logs --follow nginx
               ^        ^
               |        |
                ------------- tells kubectl to keep
                        |     streaming the logs live,
                        |     instead of just
                        |     printing and exiting
                        |
                         ---- the name of the pod we want
                              to see the logs
                              
kubectl exec nginx -- ls
root@nginx:/ echo "it works!" > /usr/share/nginx/html/index.html

kubectl delete pod nginx
# pod "nginx" deleted
kubectl delete -f nginx.yaml
# pod "nginx" deleted
```

```yaml
apiVersion: v1
kind: Pod
metadata:
name: httpd
spec:
containers:
- name: httpd-container
image: registry.hub.docker.com/library/httpd
### 作业一:Hellok8s Pod

# default page in: /usr/local/apache2/htdocs/index.html
```
根据我们在 `container` 的那节构建的 `hellok8s:v1` 的镜像,编写出 `pod` 的资源文件。并通过 `port-forward` 到本地的 `3000` 端口进行访问,最终得到字符串 `[v1] Hello, Kubernetes!`。

```yaml
# hellok8s.yaml
Expand All @@ -244,8 +258,10 @@ spec:
image: guangzhengli/hellok8s:v1
```

```
kubectl port-forward hellok8s 4567:4567
```shell
kubectl get pods
kubectl port-forward hellok8s 3000:3000
```

## Deployment
Expand Down
10 changes: 0 additions & 10 deletions pod/apache.yaml

This file was deleted.

6 changes: 4 additions & 2 deletions pod/nginx.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
apiVersion: v1
kind: Pod
metadata:
name: nginx
name: nginx-pod
spec:
containers:
- name: nginx-container
image: registry.hub.docker.com/library/nginx
image: nginx

# default page in: /usr/share/nginx/html/index.html

0 comments on commit 0409813

Please sign in to comment.