k8s-pod的健康检查

k8s-pod的健康检查

1 最基本的yaml文件

下面这个是一个最基本的资源清单,各个字段必须存在

metadata下面的name是pod的名字,labels是标签

spec,下面的name是容器的名字,image是下载的镜像地址

2 initc的作用

  • init容器总是运行到成功完成为止

  • 每一个init容器都必须在下一个init容器启动之前成功完成

2.1 init使用案例

 

apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: busybox
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
initContainers:
- name: init-myservice
image: busybox
command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2;
done;']
- name: init-mydb
image: busybox
command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done;']

如图所示

 

创建两个service,让上面的initc检测

kind: Service
apiVersion: v1
metadata:
name: myservice
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9376


---
kind: Service
apiVersion: v1
metadata:
name: mydb
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9377

如图所示,

3 探针检测

3.1 两种检测类型

  • LivenessProbe:用于判断容器是否存活(running状态),如果LivenessProbe探针探测到容器不健康,则kubelet杀掉该容器,并根据容器的重启策略做相应的处理。如果一个容器不包含LivenessProbe探针,则kubelet认为该容器的LivenessProbe探针返回的值永远是“Success”。

 

  • ReadinessProbe:用于判断容器是否启动完成(ready状态),可以接收请求。如果ReadinessProbe探针检测到失败,则Pod的状态被修改。Endpoint Controller将从Service的Endpoint中删除包含该容器所在Pod的Endpoint。

3.2 三种检测方式

  • 1.HTTP GET探针对容器的ip地址(指定端口和路径)执行HTTP GET请求。响应状态码是2xx或3xx则探测成功。

  • 2.TCP套接字探针尝试建立TCP连接,成功建立则成功。

  • 3.Exec探针,在容器内执行任意命令,检测命令的退出状态码,是0则成功,其他失败

 

3.3 检测探针 - 就绪检测

readinessProbe-httpget

 

apiVersion: v1
kind: Pod
metadata:
  name: readiness-httpget-pod
  namespace: default
spec:
  containers:
  - name: readiness-httpget-container
    image: wangyanglinux/myapp:v1
    imagePullPolicy: IfNotPresent
    readinessProbe:
      httpGet:
        port: 80
        path: /index1.html
      initialDelaySeconds: 1 #延迟1秒,容器启动1秒之后,检测
      periodSeconds: 3 #周期,每隔3秒重新检测

 

3.4 检测探针 - 存活检测

livenessProbe-exec

 

apiVersion: v1
kind: Pod
metadata:
  name: liveness-exec-pod
  namespace: default
spec:
  containers:
  - name: liveness-exec-container
    image: hub.atguigu.com/library/busybox
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh","-c","touch /tmp/live ; sleep 60; rm -rf /tmp/live; sleep
3600"]
    livenessProbe:
      exec:
        command: ["test","-e","/tmp/live"]
      initialDelaySeconds: 1
      periodSeconds: 3

livenessProbe-httpget

 

apiVersion: v1
kind: Pod
metadata:
  name: liveness-httpget-pod
  namespace: default
spec:
  containers:
  - name: liveness-httpget-container
    image: hub.atguigu.com/library/myapp:v1
    imagePullPolicy: IfNotPresent
    ports:
    - name: http
      containerPort: 80
    livenessProbe:
      httpGet:
        port: http
        path: /index.html
      initialDelaySeconds: 1
      periodSeconds: 3
      timeoutSeconds: 10 #超时时间

livenessProbe-tcp

 

apiVersion: v1
kind: Pod
metadata:
  name: probe-tcp
spec:
  containers:
  - name: nginx
    image: hub.atguigu.com/library/myapp:v1
    livenessProbe:
      initialDelaySeconds: 5
      timeoutSeconds: 1
      tcpSocket:
        port: 80

 

3.5 启动和退出之后的操作

 

apiVersion: v1
kind: Pod
metadata:
  name: lifecycle-demo
spec:
  containers:
  - name: lifecycle-demo-container
  image: nginx
  lifecycle: #生命周期
    postStart:
      exec:
        command: ["/bin/sh", "-c", "echo Hello from the postStart handler >
/usr/share/message"]
    preStop:
      exec:
        command: ["/bin/sh", "-c", "echo Hello from the postStart handler >

  

 

posted @ 2020-04-15 14:25  huningfei  阅读(292)  评论(0编辑  收藏  举报
levels of contents