『K8S 入门』二:深入 Pod
一、基础命令
- 获取所有 Pod
shell
复制代码
kubectl get pods
- 获取 deploy
shell
复制代码
kubectl get deploy
- 删除 deploy,这时候相应的 pod 就没了
shell
复制代码
kubectl delete deploy nginx
- 虽然删掉了 Pod,但是这是时候还有 service,我们可以也删掉
shell
复制代码
kubectl get services
kubectl delete svc nginx
- 自己创建配置文件,构建 Pod
shell
复制代码
mkdir pods
touch nginx-demo.yaml
参数名 |
类型 |
字段说明 |
apiVersion |
String |
K8S APl 的版本,可以用 kubectl api versions 命令查询 |
kind |
String |
yam 文件定义的资源类型和角色 |
metadata |
Object |
元数据对象,下面是它的属性 |
metadata.name |
String |
元数据对象的名字,比如 pod 的名字 |
metadata.namespace |
String |
元数据对象的命名空间 |
Spec |
Object |
详细定义对象 |
spec.containers[] |
list |
定义 Spec 对象的容器列表 |
spec.containers[].name |
String |
为列表中的某个容器定义名称 |
spec.containers[].image |
String |
为列表中的某个容器定义需要的镜像名称 |
spec.containers[].imagePullPolicy |
string |
定义镜像拉取策略,有 Always、Never、IfNotPresent 三个值可选 - Always(默认):意思是每次都尝试重新拉取镜像 - Never:表示仅适用本地镜像 - IfNotPresent:如果本地有镜像就使用本地镜像,没有就拉取在线镜像。 |
spec.containers[].command[] |
list |
指定容器启动命令,因为是数组可以指定多个,不指定则使用镜像打包时使用的启动命令。 |
spec.containers[].args[] |
list |
指定容器启动命令参数,因为是数组可以指定多个。 |
spec.containers[].workingDir |
string |
指定容器的工作目录 |
spec.containers[].volumeMounts[] |
list |
指定容器内部的存储卷配置 |
spec.containers[].volumeMounts[].name |
string |
指定可以被容器挂载的存储卷的名称 |
spec.containers[].volumeMounts[].mountPath |
string |
指定可以被容器挂载的存储卷的路径 |
spec.containers[].volumeMounts[].readOnly |
string |
设置存储卷路径的读写模式,ture 或者 false,默认是读写模式 |
spec.containers[].ports[] |
list |
指定容器需要用到的端口列表 |
spec.containers[].ports[].name |
string |
指定端口的名称 |
spec.containers[].ports[].containerPort |
string |
指定容器需要监听的端口号 |
spec.containers[].ports[].hostPort |
string |
指定容器所在主机需要监听的端口号,默认跟上面 containerPort 相同,注意设置了 hostPort 同一台主机无法启动该容器的相同副本(因为主机的端口号不能相同,这样会冲突) |
spec.containers[].ports[].protocol |
string |
指定端口协议,支持 TCP 和 UDP,默认值为 TCP |
spec.containers[].env[] |
list |
指定容器运行前需设置的环境变量列表 |
spec.containers[].env[].name |
string |
指定环境变量名称 |
spec.containers[].env[].value |
string |
指定环境变量值 |
spec.containers[].resources |
Object |
指定资源限制和资源请求的值(这里开始就是设置容器的资源上限) |
spec.containers[].resources.limits |
Object |
指定设置容器运行时资源的运行上限 |
spec.containers[].resources.limits.cpu |
string |
指定 CPU 的限制,单位为 Core 数,将用于 docker run --cpu-shares 参数 |
spec.containers[].resources.limits.memory |
string |
指定 mem 内存的限制,单位为 MIB、GiB |
spec.containers[].resources.requests |
Object |
指定容器启动和调度时的限制设置 |
spec.containers[].resources.requests.cpu |
string |
CPU请求,单位为core数,容器启动时初始化可用数量 |
spec.containers[].resources.requests.memory |
string |
内存请求,单位为MIB、GiB,容器启动的初始化可用数量 |
spec.restartPolicy |
string |
定义 pod 的重启策略,可选值为 Always、OnFailure、Never,默认值为 Always。 - Always:pod 一旦终止运行,则无论容器是如何终止的,kubelet 服务都将重启它。 - OnFailure:只有 pod 以非零退出码终止时,kubelet 才会重启该容器。如果容器正常结束(退出码为0),则 kubectl 将不会重启它。 - Never:Pod 终止后,kubelet 将退出码报告给 master,不会重启该 pod |
spec.nodeSelector |
Object |
定义 Node 的 label 过滤标签,以 key:value 格式指定 |
spec.imagePullSecrets |
Object |
定义 pull 镜像时使用 secret 名称,以 name:secretkey 格式指定 |
spec.hostNetwork |
Boolean |
定义是否使用主机网络模式,默认值为 false。设置 true 表示使用宿主机网络,不使用 docker 网桥,同时设置了 true将无法在同一台宿主机上启动第二个副本 |
yaml
复制代码
apiVersion: v1 # api 文档版本
kind: Pod # 资源对象类型,可以配置为像Deployment StatefulSet这一类的对象
metadata: # Pod相关的元数据,用于描述Pod的数据
name: nginx-demo #Pod的名称
labels: # 定义Pod的标签,这个标签可以自己任意指定,是无所谓的
type: app # 自定义label标签,名字为type,值为app
version: 1.0.0 # 自定义label标签,描述版本号
namespace: 'default' # 命名空间的配置
spec: # 期望Pod按照这里面的描述进行创建
containers: # 对于Pod中的容器描述
- name: nginx # 容器的名称,这个是可以乱取的
image: nginx:1.7.9 # 指定容器的镜像,docker会去search
imagePullPolicy: IfNotPresent # 镜像拉去策略, 如果本地有就用本地的,如果本地没有就拉去远程的
command: # 指定容器启动时执行的命令
- nginx
- -g
- 'daemon off;' # nginx -g 'daemon off;'
workingDir: /usr/share/nginx/html/ # 定义容器启动后的工作目录
ports:
- name: http # 端口名称,随便起
containerPort: 80 # 描述容器要暴露什么端口
protocol: TCP # 描述端口是用那种通信协议
env: # 环境变量
- name: JVM_OPTS # 环境变量的名称
value: '-Xms128m -Xmx128m'
resources:
requests: # 最少需要多少资源
cpu: 100m # 限制cpu最少使用0.1个核心,一个核心用满是1000m
memory: 128Mi # 限制内存最少使用128兆
limits: # 最多可以使用多少资源
cpu: 200m # 限制cpu最多使用0.2个核心
memory: 256Mi # 限制最多使用256兆
restartPolicy: OnFailure #Secrets 重启策略,只有失败的情况才会重启
shell
复制代码
kubectl create -f nginx-demo.yaml
kubectl get po
- 描述当前这个 Pod,这里可以看到 Pod 执行过程
shell
复制代码
kubectl describe po nginx-demo
shell
复制代码
kubectl get po -o wide
- 执行下面命令可以查看到 nginx 可以访问,自动路由到 node2 节点(因为我们一开始装了 calico,网关路由的东西)
shell
复制代码
curl 10.244.169.135
- 通过下面命令还可以查看 calico 已经给我们配置好网关
shell
复制代码
route -n
- 通过下面命令,还能发现 k8s 给我们创建了很多容器
shell
复制代码
docker ps
二、探针
类型
- StartupProbe:用于判断应用程序是否已经启动了
- 当配置了 startupProbe 后,会先禁用其他探针,直到 startupProbe 成功后,其他探针才会继续
yml
复制代码
startupProbe:
httpGet:
path: /api/startup
port: 80
- LivenessProbe:用于探测容器中的应用是否运行
- 如果探测失败,kubelet 会根据配置的重启策略进行重启,若没有配置,默认就认为容器启动成功,不会执行重启策略
yml
复制代码
livenessProbe:
failureThreshold: 5
httpGet:
path: /health
port: 8080
scheme: HTTP
initialDelaySeconds: 60
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 5
- ReadinessProbe:用于探测容器内的程序是否健康
- 它的返回值如果返回 success,那么就认为该容器已经完全启动,并且该容器是可以接收外部流量的
yml
复制代码
readinessProbe:
failureThreshold: 3 # 错误次数
httpGet:
path: /ready
port: 8181
scheme: HTTP
periodSeconds: 10 # 间隔时间
successThreshold: 1
timeoutSeconds: 1
探测方式
- ExecAction:在容器内部执行一个命令,如果返回值为 0,则任务容器时健康的
yml
复制代码
livenessProbe:
exec:
command:
- cat
- /health
- TCPSocketAction:通过 tcp 连接监测容器内端口是否开放,如果开放则证明该容器健康
yml
复制代码
livenessProbe:
tcpSocket:
port: 80
- HTTPGetAction:生产环境用的较多的方式,发送 HTTP 请求到容器内的应用程序,如果接口返回的状态码在 200~400 之间,则认为容器健康
yml
复制代码
livenessProbe:
failureThreshold: 5
httpGet:
path: /health
port: 8080
scheme: HTTP
httpHeaders:
- name: xxx
value: xxx
测试探针
- 每次执行前,先删除测试 pod。修改内容后,重新启动配置,并观察 pod 的启动
shell
复制代码
# 删除 po
kubectl delete po nginx-demo
# 重新启动容器
kubectl create -f nginx-demo.yaml
# 观察 pod
kubectl describe po nginx-demo
kubectl get po
- 测试 StartupProbe
- HTTPGetAction :往 nginx-demo.xml 添加下面内容
yml
复制代码
...
spec: # 期望Pod按照这里面的描述进行创建
containers: # 对于Pod中的容器描述
- name: nginx # 容器的名称,这个是可以乱取的
image: nginx:1.7.9 # 指定容器的镜像,docker会去search
imagePullPolicy: IfNotPresent # 镜像拉去策略, 如果本地有就用本地的,如果本地没有就拉去远程的
# ===== 这里才是新添加 =====
startupProbe:
httpGet:
path: /index.html # http 请求路径
port: 80 # 请求端口
failureThreshold: 3 # 失败多少次才算失败
periodSeconds: 10 # 间隔时间
successThreshold: 1 # 多少次检测成功算成功
timeoutSeconds: 5 # 请求的超时时间
# =====
command: # 指定容器启动时执行的命令
- nginx
- -g
- 'daemon off;' # nginx -g 'daemon off;'
workingDir: /usr/share/nginx/html/ # 定义容器启动后的工作目录
...
- TCPSocketAction :往 nginx-demo.xml 添加下面内容
yml
复制代码
...
spec: # 期望Pod按照这里面的描述进行创建
containers: # 对于Pod中的容器描述
- name: nginx # 容器的名称,这个是可以乱取的
image: nginx:1.7.9 # 指定容器的镜像,docker会去search
imagePullPolicy: IfNotPresent # 镜像拉去策略, 如果本地有就用本地的,如果本地没有就拉去远程的
# ===== 这里才是新添加 =====
startupProbe:
#httpGet:
# path: /index.html # http 请求路径
tcpSocket:
port: 80 # 请求端口
failureThreshold: 3 # 失败多少次才算失败
periodSeconds: 10 # 间隔时间
successThreshold: 1 # 多少次检测成功算成功
timeoutSeconds: 5 # 请求的超时时间
# =====
command: # 指定容器启动时执行的命令
- nginx
- -g
- 'daemon off;' # nginx -g 'daemon off;'
...
- ExecAction :往 nginx-demo.xml 添加下面内容
yml
复制代码
...
spec: # 期望Pod按照这里面的描述进行创建
containers: # 对于Pod中的容器描述
- name: nginx # 容器的名称,这个是可以乱取的
image: nginx:1.7.9 # 指定容器的镜像,docker会去search
imagePullPolicy: IfNotPresent # 镜像拉去策略, 如果本地有就用本地的,如果本地没有就拉去远程的
# ===== 这里才是新添加 =====
startupProbe:
#httpGet:
# path: /index.html # http 请求路径
#tcpSocket:
# port: 80 # 请求端口
exec:
command:
- sh
- -c
- "sleep 3; echo successaaa > /inited"
failureThreshold: 3 # 失败多少次才算失败
periodSeconds: 10 # 间隔时间
successThreshold: 1 # 多少次检测成功算成功
timeoutSeconds: 5 # 请求的超时时间
# =====
command: # 指定容器启动时执行的命令
- nginx
- -g
- 'daemon off;' # nginx -g 'daemon off;'
...
shell
复制代码
kubectl exec -it nginx-demo -c nginx -- cat /inited
- 测试 LivenessProbe
yml
复制代码
...
spec: # 期望Pod按照这里面的描述进行创建
containers: # 对于Pod中的容器描述
- name: nginx # 容器的名称,这个是可以乱取的
image: nginx:1.7.9 # 指定容器的镜像,docker会去search
imagePullPolicy: IfNotPresent # 镜像拉去策略, 如果本地有就用本地的,如果本地没有就拉去远程的
startupProbe:
exec:
command:
- sh
- -c
- "sleep 3; echo successaaa > /inited"
failureThreshold: 3 # 失败多少次才算失败
periodSeconds: 10 # 间隔时间
successThreshold: 1 # 多少次检测成功算成功
timeoutSeconds: 5 # 请求的超时时间
# ===== 这里才是新添加 =====
livenessProbe:
httpGet:
path: /started.html # http 请求路径,这个路径肯定不存在
port: 80
failureThreshold: 3 # 失败多少次才算失败
periodSeconds: 10 # 间隔时间
successThreshold: 1 # 多少次检测成功算成功
timeoutSeconds: 5 # 请求的超时时间
# =====
command: # 指定容器启动时执行的命令
- nginx
- -g
- 'daemon off;' # nginx -g 'daemon off;'
...
- 重新启动,一定会失败,这时候要添加下面命令才会启动成功
shell
复制代码
echo 'started' > started.html
kubectl cp started.html nginx-demo:/usr/share/nginx/html/
- 测试 ReadinessProbe
shell
复制代码
...
spec: # 期望Pod按照这里面的描述进行创建
containers: # 对于Pod中的容器描述
- name: nginx # 容器的名称,这个是可以乱取的
image: nginx:1.7.9 # 指定容器的镜像,docker会去search
imagePullPolicy: IfNotPresent # 镜像拉去策略, 如果本地有就用本地的,如果本地没有就拉去远程的
startupProbe:
#httpGet:
# path: /index.html # http 请求路径
#tcpSocket:
# port: 80 # 请求端口
exec:
command:
- sh
- -c
- "sleep 3; echo successaaa > /inited"
failureThreshold: 3 # 失败多少次才算失败
periodSeconds: 10 # 间隔时间
successThreshold: 1 # 多少次检测成功算成功
timeoutSeconds: 5 # 请求的超时时间
# ===== 这里才是新添加 =====
readinessProbe:
httpGet:
path: /started.html # http 请求路径,这个路径肯定不存在
port: 80
failureThreshold: 5 # 失败多少次才算失败
periodSeconds: 10 # 间隔时间
successThreshold: 1 # 多少次检测成功算成功
timeoutSeconds: 3 # 请求的超时时间
# =====
command: # 指定容器启动时执行的命令
- nginx
- -g
- 'daemon off;' # nginx -g 'daemon off;'
...
- 重新启动,一定会失败,这时候要添加下面命令才会启动成功
shell
复制代码
echo 'started' > started.html
kubectl cp started.html nginx-demo:/usr/share/nginx/html/
三、Pod 的生命周期
- 概述
- 初始化阶段,一定会在容器启动前执行
- 启动后,会先执行 postStart 钩子函数,但是这个函数可能和容器启动的 command 并行执行,有可能发生冲突,所以很少用
- postStart 启动后启动容器应用,先执行 StartupProbe 探针,执行后启动 LivenessProbe 与 ReadinessProbe 探针
- 最后容器住准备结束的时候,执行 preStop 钩子函数(容器不在,钩子函数肯定会失败)
- Pod 推出流程(删除操作)
- Endpoint 删除 pod 的 ip 地址
- pod 变成 Terminating 状态:变为删除中的状态后,会给 pod 一个宽限期,让 pod 去执行一些清理或销毁操作
- 执行 preStop 的指令
yml
复制代码
# 配置参数:
# 作用于 pod 中的所有容器(默认)
terminationGracePeriodSeconds: 30
containers:
- xxx
- 测试生命周期
yml
复制代码
...
spec: # 期望Pod按照这里面的描述进行创建
# ===== 这里才是新添加 =====
# 一定要注意,如果 preStop 的任务执行超过 30s,一定要修改这个配置同时间,否则会直接结束容器。同时,如果 preStop 任务提前结束,容器也会提前结束,而不是限定 30s
terminationGracePeriodSeconds: 30
# =====
containers: # 对于Pod中的容器描述
- name: nginx # 容器的名称,这个是可以乱取的
image: nginx:1.7.9 # 指定容器的镜像,docker会去search
imagePullPolicy: IfNotPresent # 镜像拉去策略, 如果本地有就用本地的,如果本地没有就拉去远程的
# ===== 这里才是新添加 =====
lifecycle: # 生命周期配置
postStart: # 生命周期启动阶段做的事情,不一定在容器的 command 之前运行
exec:
command:
- sh
- -c
- "echo '<h1>pre stop</h1>' > /usr/share/nginx/html/prestop.html"
preStop:
exec:
command:
- sh
- -c
- "echo 'sleep finished...' >> /usr/share/nginx/html/prestop.html; sleep 50"
# =====
command: # 指定容器启动时执行的命令
- nginx
- -g
- 'daemon off;' # nginx -g 'daemon off;'
...
- 启动容器后,查看是否执行 preStart 脚本。可以看到脚本内容真实写进去

- 多开一个窗口,执行下面命令,执行监控容器退出
shell
复制代码
kubectl get po -w
shell
复制代码
time kubectl delete po nginx-demo