
目录
Pod 是可以在 Kubernetes 中创建和管理的、最小的可部署的计算单元。
Pod (就像在鲸鱼荚或者豌豆荚中)是一组(一个或多个) 容器; 这些容器共享存储、网络、以及怎样运行这些容器的声明。
Pod 特点
• 一个Pod可以理解为是一个应用实例,提供服务
• Pod中容器始终部署在一个Node上
• Pod中容器共享网络、存储资源
• Kubernetes直接管理Pod,而不是容器

Pod 的共享上下文包括一组 Linux 名字空间、控制组(cgroup)和可能一些其他的隔离 ,即用来隔离 Docker 容器的技术。 在 Pod 的上下文中,每个独立的应用可能会进一步实施隔离。
就 Docker 概念的术语而言,Pod 类似于共享命令空间和文件系统卷的一组 Docker 容器。
- 创建Pod:
- kubectl apply -f pod.yaml
-
- 或者使用命令 kubectl run nginx --image=nginx
-
- 查看Pod:
- kubectl get pods
- kubectl describe pod <Pod名称>
-
- 查看日志:
- kubectl logs <Pod名称> [-c CONTAINER]
- kubectl logs <Pod名称> [-c CONTAINER] -f
-
- 进入容器终端:
- kubectl exec <Pod名称> [-c CONTAINER] -- bash
-
- 删除Pod:
- kubectl delete <Pod名称>
将业务容器网络加入到“负责网络的容器”实现网络共享

在一个pod 中运行两个容器,两个容器之间网络是互通的。
- apiVersion: v1
- kind: Pod
- metadata:
- labels:
- app: test
- name: pod-net-test
- namespace: default
- spec:
- containers:
- - image: busybox
- name: test01
- command: ["/bin/sh","-c","sleep 360000"]
- - image: nginx:1.17
- name: web
进入 web容器中在 index.html 页面中输入 “hello world” ,我们再从 test01 容器中下载 访问

一个pod中的容器通过数据卷共享数据。

通过在 web容器 /data 文件夹下创建一个文件 可以在容器 test 中查看 ,从而可以得出 pod 中容器中的部分数据是共享的。
- apiVersion: v1
- kind: Pod
- metadata:
- labels:
- app: test
- name: pod-volume-test
- namespace: default
- spec:
- containers:
- - image: busybox
- name: test
- command: ["/bin/sh","-c","sleep 360000"]
- volumeMounts:
- - name: log
- mountPath: /data
- - image: nginx
- name: web
- volumeMounts:
- - name: log
- mountPath: /data
-
- volumes:
- - name: log
- emptyDir: {}
在web容器中的 /data 文件夹下创建 index.html 文件,我们可以看到 在test容器中可以 查看到 index.html文件。

Pod 一共有 5 种状态,这个状态反映在 Pod 的 status 属性中
• Always:当容器终止退出后,总是重启容器,默认策略。
• OnFailure:当容器异常退出(退出状态码非0)时,才重启容器。
• Never:当容器终止退出,从不重启容器。
• livenessProbe(存活检查):如果检查失败,将杀死容器,根据Pod 的restartPolicy来操作。
• readinessProbe(就绪检查):如果检查失败,Kubernetes会把Pod从service endpoints中剔除。
• startupProbe(启动检查):指示容器中的应用是否已经启动。如果提供了启动探针(startup probe),则禁用所有其他探针,直到它成功为止。如果启动探针失败,kubelet 将杀死容器,容器服从其重启策略进行重启。如果容器没有提供启动探针,则默认状态为成功Success。
• httpGet:发送HTTP请求,返回200-400范围状态码为成功。
• exec:执行Shell命令返回状态码是0为成功。
• tcpSocket:发起TCP Socket建立成功。

官网示例:
Configure Liveness, Readiness and Startup Probes | Kubernetes
运行一个 pod 在容器中 /tmp/healthy 目录下创建文件并 删除 ,存活检查 检查 此路径是否存在。
- apiVersion: v1
- kind: Pod
- metadata:
- labels:
- test: liveness
- name: pod-check
- spec:
- containers:
- - name: liveness
- image: busybox
- args:
- - /bin/sh
- - -c
- - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
- livenessProbe: # 存活检查
- exec:
- command:
- - cat
- - /tmp/healthy
- initialDelaySeconds: 5
- periodSeconds: 5
- readinessProbe: # 就绪检查
- exec:
- command:
- - cat
- - /tmp/healthy
- initialDelaySeconds: 5
- periodSeconds: 5
-
- ### Pod 健康检查示例
- kubectl apply -f pod-healthy-check.yaml
- ### 查看pod的详细信息
- kubectl describe pod pod-check
- ### 暴露服务端口
- kubectl expose pod pod-check --port=80 --target-port=80
存活检查生效,可以看到 pod 的restarts 次数已经增加了 一次。

就绪检查失败, K8s会把Pod从service endpoints中剔除, 从而失去此 service 。

官方示例地址:通过环境变量将 Pod 信息呈现给容器 | Kubernetes
- apiVersion: v1
- kind: Pod
- metadata:
- name: pod-env-vars
- spec:
- containers:
- - name: test
- image: busybox
- command: [ "sh", "-c", "sleep 36000"]
- env:
- - name: MY_NODE_NAME
- valueFrom:
- fieldRef:
- fieldPath: spec.nodeName
- - name: MY_POD_NAME
- valueFrom:
- fieldRef:
- fieldPath: metadata.name
- - name: MY_POD_NAMESPACE
- valueFrom:
- fieldRef:
- fieldPath: metadata.namespace
- - name: MY_POD_IP
- valueFrom:
- fieldRef:
- fieldPath: status.podIP
- - name: MY_POD_SERVICE_ACCOUNT
- valueFrom:
- fieldRef:
- fieldPath: spec.serviceAccountName
- - name: Hello
- value: "helle Kubernetes!"
- restartPolicy: Never
进入容器 查看Pod 信息,我们使用echo $可以输出结果

官网示例地址: ConfigMap | Kubernetes
ConfigMap 是一种 API 对象,用来将非机密性的数据保存到键值对中。使用时, Pods 可以将其用作环境变量、命令行参数或者存储卷中的配置文件。
使用 ConfigMap 来将你的配置数据和应用程序代码分开。ConfigMap 在设计上不是用来保存大量数据的。在 ConfigMap 中保存的数据不可超过 1 MiB。如果你需要保存超出此尺寸限制的数据,你可能希望考虑挂载存储卷 或者使用独立的数据库或者文件服务。
congfigMap.yaml
- apiVersion: v1
- kind: ConfigMap
- metadata:
- name: game-demo
- data:
- # 类属性键:每一个键都映射到一个值
- player_initial_lives: "3"
- ui_properties_file_name: "user-interface.properties"
-
- game.properties: |
- enemy.types=aliens,monsters
- player.maximum-lives=5
- user-interface.properties: |
- color.good=purple
- color.bad=yellow
- allow.textmode=true
pod-configMap.yaml
- apiVersion: v1
- kind: Pod
- metadata:
- name: configmap-demo-pod
- spec:
- containers:
- - name: demo
- image: alpine
- command: ["sleep", "3600"]
- env:
- - name: PLAYER_INITIAL_LIVES # 请注意这里和 ConfigMap 中的键名是不一样的
- valueFrom:
- configMapKeyRef:
- name: game-demo # 这个值来自 ConfigMap
- key: player_initial_lives # 需要取值的键
- - name: UI_PROPERTIES_FILE_NAME
- valueFrom:
- configMapKeyRef:
- name: game-demo
- key: ui_properties_file_name
- volumeMounts:
- - name: config
- mountPath: "/config"
- readOnly: true
- volumes:
- - name: config
- configMap:
- name: game-demo
- items:
- - key: "game.properties"
- path: "game.properties"
- - key: "user-interface.properties"
- path: "user-interface.properties"
创建Pod
- kubectl apply -f configMap.yaml
- kubectl apply -f pod-configMap.yaml
configmap-demo-pod运行, 进入容器可以看到 挂载目录成功。

Init Container:顾名思义,用于初始化工作,执行完就结束,可以理解为一次性任务。
• 支持大部分应用容器配置,但不支持健康检查
• 优先应用容器执行
• 环境检查:例如确保应用容器依赖的服务启动后再启动应用容器
• 初始化配置:例如给应用容器准备配置文件,工具安装和安装脚本运行。
部署一个web网站,网站程序没有打到镜像中,而是希望从代码仓库中动态拉取放到应用容器中。下面的例子定义了一个具有 一 个 Init 容器的简单 Pod。 等待 index下载成功 。 一旦这 Init容器 启动完成,Pod 将启动 spec 节中的应用容器。
- apiVersion: v1
- kind: Pod
- metadata:
- name: init-demo
- spec:
- initContainers:
- - name: download
- image: busybox
- command: ['wget','-O', '/opt/index.html','http://www.ctnrs.com']
- volumeMounts:
- - name: wwwroot
- mountPath: "/opt"
- containers:
- - name: nginx
- image: nginx
- ports:
- - containerPort: 80
- volumeMounts:
- - name: wwwroot
- mountPath: /usr/share/nginx/html
- volumes:
- - name: wwwroot
- emptyDir: {}
从pod 的状态中我们可以看到 InitContainer 中的执行状态处于 Init

init-demo 已处于运行之中

因此,Pod中会有这几种类型的容器:
• Infrastructure Container:基础容器,维护整个Pod网络空间
• InitContainers:初始化容器,先于业务容器开始执行
• Containers:业务容器,并行启动
