什么是资源 掌握资源清单的语法 编写Pod 掌握Pod的生命周期
集群资源分类
名称空间级别:kubeadm kube-system
集群级别: role
元数据型: HPA
常用字段的解释
必须存在的属性

主要对象

Pod容器生命周期

Readness:就绪检测
liveness:生存检测
Init C
Init 容器:Pod能够具有多个容器,应用运行在容器里面,但是它也可能有一个或多个先于应用容器启动的Init容器
init容器与普通的容器非常像,除了如下两点:
Init 容器总是运行到成功完成为止
每个init容器都必须在下一个Init容器启动之前成功完成
如果Pod的Init容器失败,K8S会不断地重启该Pod,直到Init容器成功为止。
然而,如果Pod对应的restartPolicy为Never,它不会重新启动
init容器的作用
因为Init容器具有与应用程序容器分离的单独镜像,所以它们的启动相关代码具有如下优势:
- 它们可以包含并运行实用工具,但是出于安全考虑,是不建议在应用程序容器镜像中包含这些实用工具的
- 它们可以包含使用工具和定制化代码来安装,但不能出现在应用程序镜像中。例如,创建镜像没必要FROM另一个镜像,
只需要在安装过程中使用类似sed、awk、python或dig这样的工具
- 应用程序镜像可以分离出创建和部署的角色,而没有必要联合它们构建一个单独的镜像。
- Init容器使用Linux Namespace,所以相对应用程序容器来说具有不同的文件系统视图。因此,
它们能够具有访问Secret的权限,而应用程序容器则不能
- 它们必须在应用程序容器启动之前运行完成,而应用程序容器时并行运行的,所以Init容器能够提供了一种简单的阻塞或延迟应用容器
的启动的方法,直到满足一组先决条件。
情景说明:
现让initcontainer在循环当中:cat init_c.yaml
---
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;']
kubectl create -f init_c.yaml
pod/myapp-pod created
[root@k8s-master01 pod]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Init:0/2 0 7s
kubectl logs myapp-pod -c init-myservice
报错为解析出问题,无法解析主机名
创建svc
[root@k8s-master01 pod]# cat init_svc.yaml
---
apiVersion: v1
kind: Service
metadata:
name: myservice
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9376
---
apiVersion: v1
kind: Service
metadata:
name: mydb
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9377
创建后即可
Init C 特殊说明:
- 在Pod启动过程中,Init容器会按顺序在网络和数据卷初始化之后启动。每个容器必须在下一个容器
启动之前成功退出
- 如果由于运行时或失败退出,将导致容器启动失败,它会根据Pod的restartPolicy指定的策略进行重试。
然而,如果Pod的restartPolicy设置为Always,Init容器失败时会使用RestartPolicy策略
- 在所有的Init容器没有成功之前,Pod将不会变成Ready状态。Init容器的端口将不会在Service中进行聚集。
正在初始化的Pod处于Pending状态,但应该会将Initializing状态设置为true
- 如果Pod重启,所有Init容器必须重新执行
- 对Init容器spec的修改被限制在容器image字段,修改其他字段都不会生效。更改Init容器的image字段,
等价于重启该Pod
- Init容器具有应用容器的所有字段。除了readinessProbe,因为Init容器无法定义不同于完成(completion)
的就绪(readiness)之外的其他状态。这会在验证过程中强制执行
- 在Pod中的每个app和Init容器的名称必须唯一;与任何其他容器共享同一个名称,会在验证时抛出错误
探针
由kubelet对容器执行的定期诊断。要执行诊断,kubelet调用由容器实现的Handler。有三种类型的处理程序:
ExecAction:在容器内执行指定命令。如果命令退出时返回码为0则认为诊断成功
TCPSocketAction:对指定端口上的容器的IP地址进行TCP检查。如果端口打开,则诊断被认为是成功的。
HTTPGetAction:对指定的端口和路径上的容器的IP地址执行HTTPGet请求。如果响应的状态码大于等于200
且小于400,则诊断被认为是成功的
每次探测都将获得以下三种结果之一:
成功
失败
未知
livenessProbe:指示容器是否正在运行。如果存活探测失败,则kubelet会杀死容器,并且容器将受到其重启策略
的影响。如果容器不提供存活探针,则默认状态为Success
readinessProbe:指示容器是否准备好服务请求。如果就绪探测失败,端点控制器将从与Pod匹配的所有Service的
端点中删除该Pod的IP地址。初始延迟之前的就绪状态默认为Failure。如果容器不提供就绪探针,
则默认状态为Success
例子(就绪检测):
[root@k8s-master01 probe]# cat readinessProbe-httpget.yaml
---
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
periodSeconds: 3
由于目录下无此文件所以:kubectl describe pod readiness-httpget-pod
Warning Unhealthy 1s (x9 over 25s) kubelet, k8s-node02 Readiness probe failed: HTTP probe failed with statuscode: 404
进入容器编辑index1.html即可通过readinessProbe探测
存活检测:
livenessProbe-exec:
[root@k8s-master01 probe]# cat livenessProbe-exec.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: liveness-exec-pod
namespace: default
spec:
containers:
- name: liveness-exec-container
image: 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
在/tmp目录下创建文件,livenessProbe检测此文件是否存在,延时1S,3S一周期,60S后删除,liveness检测
不存在文件,则删除containers,pod重启,重新执行
livenessProbe-httpget
---
apiVersion: v1
kind: Pod
metadata:
name: liveness-httpget-pod
namespace: default
spec:
containers:
- name: liveness-httpget-container
image: wangyanglinux/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
livenessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
timeoutSeconds: 10
如果index.html文件不存在则重启
livenessProbe-tcp
[root@k8s-master01 probe]# cat livenessProbe-tcp.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: probe-tcp
spec:
containers:
- name: nginx
image: wangyanglinux/myapp:v1
livenessProbe:
initialDelaySeconds: 5
timeoutSeconds: 1
tcpSocket:
port: 8080
periodSeconds: 3
检测端口存活,干掉container,pod重启
start、stop、 (Status状态)相位
[root@k8s-master01 start_stop]# cat start_stop.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: lifecycle-demo
spec:
containers:
- name: lifecycle-demo-container
image: wangyanglinux/myapp:v1
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 preStop handler > /usr/share/message"]
容器启动前,容器结束后的动作
Status状态
pending
Pod已被K8S系统接受,但有一个或多个容器镜像尚未创建。等待时间包括调度Pod的时间和下载时间
Running
该Pod已经绑定到了一个节点上,Pod中所有的容器都创建。至少有一个容器正在运行,或者正处于启动或
重启状态
Succeeded
Pod中所有的容器都被成功终止,并且不会再重启
Failed
Pod中的所有容器都被成功终止,并且至少有一个容器是因为失败终止。也就是,容器非0状态退出
或被系统终止
unknown
因为某些原因无法取得Pod的状态,通常是因为与Pod所在主机通信失败
Comments NOTHING