文章目录
Pod
https://www.cnblogs.com/linuxk/p/9569618.html
https://kubernetes.io/zh/docs/concepts/workloads/pods/pod-lifecycle/
主容器启动前可以完成Init Container(初始化容器),
Init Container可以有多个,他们是串行执行的,执行完成后就退出了,
在主容器刚刚启动的时候可以指定一个 post start 主容器启动开始后执行一些操作,
在主容器结束前可以指定一个 pre stop 表示主容器结束前执行的一些操作。
在主容器启动后可以做两类检测 liveness probe(存活性探测) 和 readiness probe(就绪性探测)
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
version: v1
# kubectl explain pod.spec
# spec: 详情
spec:
# 主容器们
containers:
- name: app
image: docker.io/library/busybox:latest
imagePullPolicy: IfNotPresent
command: ["ping", "www.baidu.com"]
- name: test
image: docker.io/library/busybox:latest
imagePullPolicy: IfNotPresent
command: ["ping", "www.baidu.com"]
kubectl get pod -w
# NAME READY STATUS RESTARTS AGE
# myapp-pod 0/2 ContainerCreating 0 0s
# myapp-pod 2/2 Running 0 1s
Init Container
- 初始化容器总是运行到成功完成为止
- 每个初始化容器都必须在下一个初始化容器启动之前成功完成
如果Pod的初始化容器运行失败,将会一直重启该Pod,直到初始化容器成功
但如果Pod对应的restartPolicy为Never,初始化容器运行失败Pod将不再重启
初始化容器使用Linux Namespace,所以可以访问Secret的权限,而主容器不能
apiVersion: v1
kind: Pod
metadata:
name: init-myapp-pod
labels:
app: init-myapp-pod
spec:
# 初始化容器们
initContainers:
- name: init-container1
image: busybox
imagePullPolicy: IfNotPresent
command: ["sh", "-c", "echo Init container 1 is running! && sleep 3"]
- name: init-container2
image: busybox
imagePullPolicy: IfNotPresent
command: ["sh", "-c", "echo Init container 2 is running! && sleep 3"]
# 主容器
containers:
- name: app
image: docker.io/library/busybox:latest
imagePullPolicy: IfNotPresent
command: ["ping", "www.baidu.com"]
kubectl get pod -w
# NAME READY STATUS RESTARTS AGE
# init-myapp-pod 0/1 Init:0/2 0 1s
# init-myapp-pod 0/1 Init:0/2 0 2s
# init-myapp-pod 0/1 Init:1/2 0 5s
# init-myapp-pod 0/1 Init:1/2 0 6s
# init-myapp-pod 0/1 PodInitializing 0 9s
# init-myapp-pod 1/1 Running 0 10s
liveness probe && readiness probe
探测(探针)是由kubelet对容器执行的定期诊断
要执行诊断,kubelet调用由容器实现的Handler
有三种类型的Handler:
- ExecAction:在容器内执行命令。命令退出时返回码为0则诊断成功
- TCPSocketAction:四层检测,端口打开则诊断成功
- HTTPGetAction:七层Get检测,Get请求成功则诊断成功
两种探针可以同时存在
livenessProbe:存活探测
指示容器是否正在运行。
如果探测失败,则kubelet杀死主容器,Pod根据restartPolicy继续操作。
如果没有livenessProbe,默认为Success
在主容器的整个生命周期中,该探针都存在
exec探测示例:
apiVersion: v1
kind: Pod
metadata:
name: liveness-pod
spec:
# 初始化容器
initContainers:
- name: init-container1
image: busybox
imagePullPolicy: IfNotPresent
command: ["sh", "-c", "echo Init container 1 is running! && sleep 3"]
# 主容器
containers:
- name: app
image: busybox
imagePullPolicy: IfNotPresent
# 新建被探测文件,等三秒删除该文件
command: ["sh", "-c", "touch /live; sleep 3; rm -rf /live"]
# 探针
livenessProbe:
exec:
# 探测文件是否存在,否则重启该主容器
command: ["test", "-e", "/live"]
# 延迟1秒开始探测
initialDelaySeconds: 1
# 每3秒探测一次
periodSeconds: 3
kubectl get pod -w
# NAME READY STATUS RESTARTS AGE
# liveness-pod 0/1 Init:0/1 0 0s
# liveness-pod 0/1 Init:0/1 0 1s
# liveness-pod 0/1 PodInitializing 0 4s
# liveness-pod 1/1 Running 0 5s
# liveness-pod 0/1 Completed 0 8s
# liveness-pod 1/1 Running 1 9s
# liveness-pod 0/1 Completed 1 13s
# 先运行了初始化容器
# 然后主容器开始运行
# 三秒后主容器删除被探测文件
# 探测检测失败
# 重启主容器(因为初始化容器没有重启,所以不是重启整个Pod)
httpGet探测示例:
apiVersion: v1
kind: Pod
metadata:
name: liveness-pod
spec:
# 主容器
containers:
- name: app
image: nginx:1.8
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
# 探针
livenessProbe:
httpGet:
port: 80
path: /
initialDelaySeconds: 1
periodSeconds: 3
# 每次探测最大超时时间为10秒
timeoutSeconds: 10
tcp探测示例:
apiVersion: v1
kind: Pod
metadata:
name: liveness-pod
spec:
# 主容器
containers:
- name: app
image: nginx:1.8
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
# 探针
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 1
periodSeconds: 3
timeoutSeconds: 10
readinessProbe:就绪探测
指示容器是否准备好服务请求。
如果探测失败,端点控制器将从与Pod匹配的所有Service的端点中删除该Pod的IP地址。
初始延迟之前的就绪状态默认为Failure。
如果没有readinessProbe,默认为Success
httpGet示例:
apiVersion: v1
kind: Pod
metadata:
name: readiness-pod
spec:
# 主容器
containers:
- name: app
image: nginx:1.8
imagePullPolicy: IfNotPresent
# 探针
readinessProbe:
httpGet:
port: 80
path: /
initialDelaySeconds: 1
periodSeconds: 3
kubectl get pod -w
# NAME READY STATUS RESTARTS AGE
# readiness-pod 0/1 ContainerCreating 0 0s
# readiness-pod 0/1 Running 0 1s
# readiness-pod 1/1 Running 0 2s
# 如果探测不成功,会一直卡在第二行的位置
post start && pre stop
- 主容器启动后执行操作
- 主容器停止前执行操作
apiVersion: v1
kind: Pod
metadata:
name: lifecycle-demo
spec:
containers:
- name: app
image: busybox
imagePullPolicy: IfNotPresent
command: ["/bin/sh", "-c", "sleep 5 && echo app >> /message.txt && ping www.baidu.com"]
# 生命周期执行操作
lifecycle:
# 启动后执行操作
postStart:
exec:
command: ["/bin/sh", "-c", "echo postStart >> /message.txt"]
# 停止前执行操作
preStop:
exec:
command: ["/bin/sh", "-c", "echo preStop >> /message.txt"]
kubectl get pod -w
# pod/lifecycle-demo created
# NAME READY STATUS RESTARTS AGE
# lifecycle-demo 0/1 ContainerCreating 0 0s
# lifecycle-demo 1/1 Running 0 2s
kubectl exec lifecycle-demo -it -- cat /message.txt
# postStart
# app
# 实际上postStart和主容器的命令是同时执行的
# 因为主容器一运行就先sleep 5,所以postStart先被写入文件