Kubernetes之资源清单
1、k8s中的资源
- K8s中所有的内容都抽象为资源,资源实例化之后,叫做对象。
名称空间级别
- 工作负载型资源(workload):Pod、ReplicaSet、Deployment、StatefulSet、DaemonSet、Job、CronJob(ReplicationController在v1.11版本被废弃) 
- 服务发现及负载均衡型资源(ServiceDiscovery LoadBalance):Service、Ingress、… 
- 配置与存储型资源:Volume(存储卷)、CSI(容器存储接口,可以扩展各种各样的第三方存储卷) 
- 特殊类型的存储卷:ConfigMap(当配置中心来使用的资源类型)、Secret(保存敏感数据)、DownwardAPI(把外部环境中的信息输出给容器) - 集群级资源 
- Namespace、Node、Role、ClusterRole、RoleBinding、ClusterRoleBinding 
元数据型资源
- HPA、PodTemplate、LimitRange
2、资源清单
- 在K8S中,一般使用yaml格式的文件来创建符合我们预期期望的pod,这样的yaml文件我们一般称为资源清单。yaml是一个可读性高,用来表达数据序列的格式。YAML 的意思其实是:仍是一种标记语言,但为了强调这种语言以数据做为中心,而不是以标记语言为重点。yaml语法如下: - 缩进时不允许使用Tab键,只允许使用空格。
- 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可。
- 用#标识注释,从这个字符一直到行尾,都会被解释器忽略。
 
- YAML支持的数据结构: - 对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary) - 1 
 2
 3
 4
 5
 6- # 对象的一组键值对,使用冒号结构表示 
 name: Steve
 age: 18
 # Yaml也允许另一种写法,将所有键值对写成一个行内对象
 hash: { name: Steve, age: 18 }
- 数组:一组按次序排列的值,又称为序列(sequence) / 列表 (list) - 1 
 2
 3
 4
 5
 6
 7- # 一组连词线开头的行,构成一个数组 
 animal:
 - Cat
 - Dog
 # 数组也可以采用行内表示法
 animal: [Cat, Dog]
- 纯量(scalars):单个的、不可再分的值。 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10- # 对象和数组可以结合使用,形成复合结构 
 languages:
 - Ruby
 - Perl
 - Python
 websites:
 YAML: yaml.org
 Ruby: ruby-lang.org
 Python: python.org
 Perl: use.perl.org- 纯量是最基本的、不可再分的值。以下数据类型都属于纯量: - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20- # 字符串 布尔值 整数 浮点数 Null 时间 日期 
 # 数值直接以字面量的形式表示
 number: 12.30
 # 布尔值用true和false表示
 isSet: true
 # null用 ~ 表示
 parent: ~
 # 时间采用 ISO8601 格式
 iso8601: 2001-12-14t21:59:43.10-05:00
 # 日期采用复合 iso8601 格式的年、月、日表示
 date: 1976-07-31
 # YAML 允许使用两个感叹号,强制转换数据类型
 e: !!str 123
 f: !!str true
 
 
- 字符串: - 字符串默认不使用引号表示。 - 1 - str: 这是一行字符串 
- 如果字符串之中包含空格或特殊字符,需要放在引号之中。 - 1 - str: '内容: 字符串' 
- 单引号和双引号都可以使用,双引号不会对特殊字符转义,默认是单引号。 - 1 
 2- s1: "内容\n字符串" # 输出"内容 换行 字符串" 
 s2: '内容\n字符串' # 输出"内容\n字符串"
- 单引号之中如果还有单引号,必须连续使用两个单引号转义。 - 1 - str: 'labor''s day' 
- 字符串可以写成多行,从第二行开始,必须有一个单空格缩进。换行符会被转为空格。 - 1 
 2
 3- str: 这是一段 
 多行
 字符串
- 多行字符串可以使用|保留换行符,也可以使用>折叠换行。 - 1 
 2
 3
 4
 5
 6- this: | 
 Foo
 Bar
 that: >
 Foo
 Bar
- +表示保留文字块末尾的换行,-表示删除字符串末尾的换行。 - 1 
 2
 3
 4
 5
 6- s1: | 
 Foo
 s2: |+
 Foo
 s3: |-
 Foo
 
2.1 常用字段的解释
| 参数名 | 字段类型 | 说明 | 
|---|---|---|
| version | String | 这里是指的是K8S API的版本,目前基本上是v1,可以用”kubectl api-versions“命令查询 | 
| kind | string | 这是指的是yaml文件定义的资源类型和角色,比如:pod | 
| metadata | Object | 元数据对象,固定值就写metedata | 
| metadata.name | string | 元数据对象的名字,这里由我们编写,比如命名Pod的名字 | 
| metadata.namespace | string | 元数据对象的命名空间,由我们自身定义 | 
| Spec | Object | 详细定义对象,固定值就写Spec | 
| spec.containers[] | list | 这里是Spec对象的容器列表定义,是个列表 | 
| spec.containers[].name | string | 这里定义容器的名字 | 
| spec.containers[].image | string | 这里定义要用到的镜像名称 | 
| spec.containers[].imagePullPolicy | string | 定义镜像拉取策略,有Always、Never、IfNotPresent三个值可选(1)Always:意思是每次都尝试重新拉取镜像(2)Never:表示仅适用本地镜像(3)IfNotPresent:如果本地有镜像就使用本地镜像,没有就拉取在线镜像。上面三个值都没设置的话,默认是Always。 | 
| 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。 1.Always:pod一旦终止运行,则无论容器是如何终止的,kubelet服务都将重启它。2.OnFailure:只有pod以非零退出码终止时,kubelet才会重启该容器。如果容器正常结束(退出码为0),则kubectl将不会重启它。3.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将无法在同一台宿主机上启动第二个副本 | 
2.2 资源清单的常用命令
- 获取apiVersion版本信息。 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33- [root@master ~]# kubectl api-versions 
 admissionregistration.k8s.io/v1beta1
 apiextensions.k8s.io/v1beta1
 apiregistration.k8s.io/v1
 apiregistration.k8s.io/v1beta1
 apps/v1
 apps/v1beta1
 apps/v1beta2
 authentication.k8s.io/v1
 authentication.k8s.io/v1beta1
 authorization.k8s.io/v1
 authorization.k8s.io/v1beta1
 autoscaling/v1
 autoscaling/v2beta1
 autoscaling/v2beta2
 batch/v1
 batch/v1beta1
 certificates.k8s.io/v1beta1
 coordination.k8s.io/v1
 coordination.k8s.io/v1beta1
 events.k8s.io/v1beta1
 extensions/v1beta1
 networking.k8s.io/v1
 networking.k8s.io/v1beta1
 node.k8s.io/v1beta1
 policy/v1beta1
 rbac.authorization.k8s.io/v1
 rbac.authorization.k8s.io/v1beta1
 scheduling.k8s.io/v1
 scheduling.k8s.io/v1beta1
 storage.k8s.io/v1
 storage.k8s.io/v1beta1
 v1
- 查看pod帮助命令。 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33- [root@master ~]# kubectl explain pod 
 KIND: Pod
 VERSION: v1
 DESCRIPTION:
 Pod is a collection of containers that can run on a host. This resource is
 created by clients and scheduled onto hosts.
 FIELDS:
 apiVersion <string>
 APIVersion defines the versioned schema of this representation of an
 object. Servers should convert recognized schemas to the latest internal
 value, and may reject unrecognized values. More info:
 https://git.k8s.io/community/contributors/devel/api-conventions.md#resources
 kind <string>
 Kind is a string value representing the REST resource this object
 represents. Servers may infer this from the endpoint the client submits
 requests to. Cannot be updated. In CamelCase. More info:
 https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds
 metadata <Object>
 Standard object's metadata. More info:
 https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
 spec <Object>
 Specification of the desired behavior of the pod. More info:
 https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
 status <Object>
 Most recently observed status of the pod. This data may not be up to date.
 Populated by the system. Read-only. More info:
 https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-statu
- 查看pod的apiVersion详细信息。 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11- [root@master ~]# kubectl explain pod.apiVersion 
 KIND: Pod
 VERSION: v1
 FIELD: apiVersion <string>
 DESCRIPTION:
 APIVersion defines the versioned schema of this representation of an
 object. Servers should convert recognized schemas to the latest internal
 value, and may reject unrecognized values. More info:
 https://git.k8s.io/community/contributors/devel/api-conventions.md#resources
2.3 通过资源清单创建pod
- 模板如下: - 1 
 2
 3
 4
 5
 6
 7
 8
 9- apiVersion: group/apiversion # 如果有没给定 group名称,那么默认为core,可以使用 kubectl api-versions 获取当前 k8s版本上所有的apiVersion 版本信息(每个版本可能不同) 
 kind: #资源类别
 metadata: #资源元数据
 name:
 namespace: # 默认是default
 labels:
 annotations: #主要目的是方便用户阅读查找
 spec: #期望的状态(disired state)
 status: #当前状态,文本段有 Kubernetes 自身维护,用户不能去定义
- 实际案例: - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70- [root@master ~]# vim pod.yaml 
 [root@master ~]# cat pod.yaml
 apiVersion: v1
 kind: Pod
 metadata:
 name: myapp-pod
 labels:
 app: myapp
 version: v1
 spec:
 containers:
 - name: app
 image: nginx:latest
 [root@master ~]# kubectl create -f pod.yaml
 pod/myapp-pod created
 [root@master ~]# kubectl get pod
 NAME READY STATUS RESTARTS AGE
 myapp-pod 1/1 Running 0 10s
 nginx-deployment-5d98ff5667-45pz5 1/1 Running 3 2d23h
 nginx-deployment-5d98ff5667-fpm5z 1/1 Running 3 2d23h
 nginx-deployment-5d98ff5667-kzpfj 1/1 Running 3 2d23h
 [root@master ~]# kubectl get pod -o wide
 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
 myapp-pod 1/1 Running 0 60s 10.244.1.7 node1 <none> <none>
 nginx-deployment-5d98ff5667-45pz5 1/1 Running 3 2d23h 10.244.2.10 node2 <none> <none>
 nginx-deployment-5d98ff5667-fpm5z 1/1 Running 3 2d23h 10.244.1.6 node1 <none> <none>
 nginx-deployment-5d98ff5667-kzpfj 1/1 Running 3 3d 10.244.2.9 node2 <none> <none>
 [root@master ~]# curl 10.244.1.7
 <!DOCTYPE html>
 <html>
 <head>
 <title>Welcome to nginx!</title>
 <style>
 html { color-scheme: light dark; }
 body { width: 35em; margin: 0 auto;
 font-family: Tahoma, Verdana, Arial, sans-serif; }
 </style>
 </head>
 <body>
 <h1>Welcome to nginx!</h1>
 <p>If you see this page, the nginx web server is successfully installed and
 working. Further configuration is required.</p>
 <p>For online documentation and support please refer to
 <a href="http://nginx.org/">nginx.org</a>.<br/>
 Commercial support is available at
 <a href="http://nginx.com/">nginx.com</a>.</p>
 <p><em>Thank you for using nginx.</em></p>
 </body>
 </html>
 [root@master ~]# kubectl log myapp-pod -c app # 查看运行日志,如果pod中只有一个容器则也可不需要明确使用-c指定查看哪个容器
 log is DEPRECATED and will be removed in a future version. Use logs instead.
 /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
 /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
 /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
 /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
 /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
 /docker-entrypoint.sh: Configuration complete; ready for start up
 2021/10/09 08:04:41 [notice] 1#1: using the "epoll" event method
 2021/10/09 08:04:41 [notice] 1#1: nginx/1.21.3
 2021/10/09 08:04:41 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6)
 2021/10/09 08:04:41 [notice] 1#1: OS: Linux 5.4.150-1.el7.elrepo.x86_64
 2021/10/09 08:04:41 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
 2021/10/09 08:04:41 [notice] 1#1: start worker processes
 2021/10/09 08:04:41 [notice] 1#1: start worker process 32
 2021/10/09 08:04:41 [notice] 1#1: start worker process 33
 10.244.0.0 - - [09/Oct/2021:08:05:51 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-
3、容器的生命周期
- 生命周期图示如下: ![]() 
3.1 Init Container
- Pod能够具有多个容器,应用运行在容器里面,但是它也可能有一个或多个先于容器启动的Init容器。
- Init容器与普通容器非常像,除了如下两点: - Init容器总是运行到成功完成为止。
- 每个Init容器都必须在下一个Init容器启动之前成功完成。
 
- 如果Pod的Init容器失败,Kubernetes会不断重启该Pod,直到Init容器成功为止。然而,如果Pod对应的restartPolicy为Never,它不会重新启动。
- 因为Init容器具有与应用程序容器分离的单独镜像,所有它们的启动相关代码具有如下优势:- 它们可以包含并运行实用工具,但是出于安全考虑,是不建议在应用程序容器镜像中包含这些实用工具的。
- 它们可以包含实用工具和定制化代码来安装,但是不能出现在应用程序镜像中。例如,创建镜像没必要FROM另一个镜像,只需要在安装过程中使用类似sed、awk、python或dig这样的工具。
- 应用程序镜像可以分离出创建和部署的角色,而没有必要联合它们构建一个单独的镜像。
- Init容器使用Linux Namespace,所以相对应用程序容器来说具有不同的文件系统视图。因此,它们能够具有访问Secret的权限,而应用程序容器则不能。
- 它们必须在应用程序启动之前完成,而应用程序容器是并行运行的,所以Init容器能够提供了一种简单的阻塞或延迟应用容器的启动的方法,直接满足了一组先决条件。
 
3.2 通过资源清单创建init容器
| 1 | # 为了避免命名冲突,先清除之前创建的pod的相关信息 | 
- 在pod启动过程中,Init容器会按顺序在网络和数据卷初始化(这两者在pause容器中完成)之后启动。每个容器必须在下一个容器自动启动之前成功退出。 
- 如果由于运行时或失败退出,将导致容器启动失败,它会根据pod的restartPolicy指定的策略进行重试。然而,如果pod的restartPolicy设置为Always,Init容器失败时会使用RestartPolicy策略。 
- 在所有的Init容器没有成功之前,pod将不会变成Ready状态。Init容器的端口将不会在Service中进行聚集。正在初始化中的pod处于Pending状态,但应该会将Initializing状态设置为True。 
- 如果pod重启,所有Init容器必须重新执行。 
- 对Init容器spec的修改被限制在容器image字段,修改其他字段都不会生效。更改Init容器的image字段,等价于重启该pod。 - 1 
 2
 3
 4
 5- [root@master ~]# kubectl get pod 
 NAME READY STATUS RESTARTS AGE
 myapp-pod 1/1 Running 0 24m
 [root@master ~]# kubectl edit pod myapp-pod
 ………………
- Init容器具有应用容器的所有字段。除了readinessProbe,因为Init容器无法定义不同于完成(completion)的就绪(readiness)之外的其他状态。这会在验证过程中强制执行。 
- 在pod中的每个app和init容器的名称必须唯一;与任何其他容器共享同一个名称,会在验证时抛出错误。 
3.3 容器探针
- 探针是由Kubelet对容器执行的定期诊断。要执行诊断,kubelet调用由容器实现的Handler。有三种类型的处理程序: - ExecAction:在容器内执行指定命令。如果命令退出时返回码为0则认为诊断成功。
- TCPSocketAction:对指定端口上的容器的IP地址进行TCP检查。如果端口打开,则诊断被认为是成功的。
- HTTPGetAction:对指定的端口和路径上的容器的IP地址指定HTTP Get请求。如果响应的状态码大于等于200且小于400,则诊断被认为是成功的。
 
- 每次探测将获得以下三种结果之一: - 成功:容器通过了诊断。
- 失败:容器未通过诊断。
- 未知:诊断失败,因此不会采取任何行动。
 
- 探测方式一般有如下两种: - livenessProbe:指示容器是否正在运行。如果存活探测失败,则kubelet会杀死容器,并且容器将受到其重启策略的影响。如果容器不提供存活探针,则默认状态为Success。 - ①以命令的方式检测资源清单: - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26- [root@master ~]# vim livenessProbe-exec.yaml 
 [root@master ~]# cat livenessProbe-exec.yaml
 apiVersion: v1
 kind: Pod
 metadata:
 name: liveness-exec-pod
 namespace: default
 spec:
 containers:
 - name: liveness-exec-container
 image: 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
 # 因为检测方式特殊,所以前60s状态为正常
 [root@master ~]# kubectl get pod
 NAME READY STATUS RESTARTS AGE
 liveness-exec-pod 1/1 Running 0 3s
 # 60s过后容器会重启一次
 [root@master ~]# kubectl get pod
 NAME READY STATUS RESTARTS AGE
 liveness-exec-pod 1/1 Running 1 1m45s
- ②以httpget方式的存活检测资源清单: - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33- [root@master ~]# vim livenessProbe-httpget.yaml 
 [root@master ~]# cat livenessProbe-httpget.yaml
 apiVersion: v1
 kind: Pod
 metadata:
 name: liveness-httpget-pod
 namespace: default
 spec:
 containers:
 - name: liveness-httpget-container
 image: nginx:latest
 imagePullPolicy: IfNotPresent
 ports:
 - name: http
 containerPort: 80
 livenessProbe:
 httpGet:
 port: http
 path: /index.html
 initialDelaySeconds: 1
 periodSeconds: 3
 timeoutSeconds: 10
 [root@master ~]# kubectl create -f livenessProbe-httpget.yaml
 pod/liveness-httpget-pod created
 [root@master ~]# kubectl get pod
 NAME READY STATUS RESTARTS AGE
 liveness-httpget-pod 1/1 Running 0 5s
 [root@master ~]# kubectl exec liveness-httpget-pod -it -- /bin/sh
 # rm -rf /usr/share/nginx/html/index.html
 # exit
 [root@master ~]# kubectl get pod # 由于有restartPolicy,所以会自动重启pod
 NAME READY STATUS RESTARTS AGE
 liveness-httpget-pod 1/1 Running 1 2m17s
- ③创建TCPsock方式的存活检测资源清单: - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23- [root@master ~]# vim livenessProbe-tcp.yaml 
 [root@master ~]# cat livenessProbe-tcp.yaml
 apiVersion: v1
 kind: Pod
 metadata:
 name: liveness-tcp-pod
 namespace: default
 spec:
 containers:
 - name: liveness-tcp-container
 image: nginx:latest
 livenessProbe:
 tcpSocket:
 port: 8080
 initialDelaySeconds: 5
 periodSeconds: 3
 timeoutSeconds: 1
 # 由于上方检测8080端口失败导致一直重启
 [root@master ~]# kubectl get pod -w
 NAME READY STATUS RESTARTS AGE
 liveness-tcp-pod 1/1 Running 0 33s
 liveness-tcp-pod 1/1 Running 1 33s
 liveness-tcp-pod 1/1 Running 2 47s
 
- readinessProbe:指示容器是否准备好服务请求。如果就绪探测失败,端点控制器将从与Pod匹配的所有Service的端点中删除该Pod的IP地址。初始延迟之前的就绪状态默认为Failure。如果容器不提供就绪探针,则默认状态为Success。 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78- [root@master ~]# vim readinessProbe-httpget.yaml 
 [root@master ~]# cat readinessProbe-httpget.yaml
 apiVersion: v1
 kind: Pod
 metadata:
 name: readiness-httpget-pod
 namespace: default
 spec:
 containers:
 - name: readiness-httpget-container
 image: nginx:latest
 imagePullPolicy: IfNotPresent
 readinessProbe:
 httpGet:
 port: 80
 path: /index1.html
 initialDelaySeconds: 1
 periodSeconds: 3
 [root@master ~]# kubectl create -f readinessProbe-httpget.yaml
 pod/readiness-httpget-pod created
 [root@master ~]# kubectl get pod # # 因为没有检测到index1.html,所以不会ready
 NAME READY STATUS RESTARTS AGE
 readiness-httpget-pod 0/1 Running 0 8s
 [root@master ~]# kubectl describe pod readiness-httpget-pod
 Name: readiness-httpget-pod
 Namespace: default
 Priority: 0
 Node: node1/192.168.200.30
 Start Time: Sat, 09 Oct 2021 21:01:22 +0800
 Labels: <none>
 Annotations: <none>
 Status: Running
 IP: 10.244.1.8
 Containers:
 readiness-httpget-container:
 Container ID: docker://d6d675c42ea5fc8dca46baaf181c2bb8a8d641d5584dd085db748c187a436b17
 Image: nginx:latest
 Image ID: docker-pullable://nginx@sha256:06e4235e95299b1d6d595c5ef4c41a9b12641f6683136c18394b858967cd1506
 Port: <none>
 Host Port: <none>
 State: Running
 Started: Sat, 09 Oct 2021 21:01:23 +0800
 Ready: False
 Restart Count: 0
 Readiness: http-get http://:80/index1.html delay=1s timeout=1s period=3s #success=1 #failure=3
 Environment: <none>
 Mounts:
 /var/run/secrets/kubernetes.io/serviceaccount from default-token-4q5xv (ro)
 Conditions:
 Type Status
 Initialized True
 Ready False
 ContainersReady False
 PodScheduled True
 Volumes:
 default-token-4q5xv:
 Type: Secret (a volume populated by a Secret)
 SecretName: default-token-4q5xv
 Optional: false
 QoS Class: BestEffort
 Node-Selectors: <none>
 Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
 node.kubernetes.io/unreachable:NoExecute for 300s
 Events:
 Type Reason Age From Message
 ---- ------ ---- ---- -------
 Normal Scheduled 71s default-scheduler Successfully assigned default/readiness-httpget-pod to node1
 Normal Pulled 70s kubelet, node1 Container image "nginx:latest" already present on machine
 Normal Created 70s kubelet, node1 Created container readiness-httpget-container
 Normal Started 70s kubelet, node1 Started container readiness-httpget-container
 Warning Unhealthy 6s (x22 over 69s) kubelet, node1 Readiness probe failed: HTTP probe failed with statuscode: 404
 # 进入容器内部增加一个index1.html文件
 [root@master ~]# kubectl exec readiness-httpget-pod -it -- /bin/sh
 # echo "hello k8s" >> /usr/share/nginx/html/index1.html
 # exit
 [root@master ~]# kubectl get pod # 此时该节点已经处于READY状态
 NAME READY STATUS RESTARTS AGE
 readiness-httpget-pod 1/1 Running 0 4m57
 
3.4 Pod Hook
- Pod Hook(钩子)是由Kubernetes管理的kubelet发起的,当容器中的进程启动前或者容器中的进程终止之前运行,这是包含在容器的生命周期之中。可以同时为pod中的所有容器都配置Hook。 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24- [root@master ~]# vim hook-pod.yaml 
 [root@master ~]# cat hook-pod.yaml
 apiVersion: v1
 kind: Pod
 metadata:
 name: lifecycle-demo
 spec:
 containers:
 - name: lifecycle-demo-container
 image: nginx:latest
 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 poststop handler > /usr/share/message"]
 [root@master ~]# kubectl create -f hook-pod.yaml
 pod/lifecycle-demo created
 [root@master ~]# kubectl get pod
 NAME READY STATUS RESTARTS AGE
 lifecycle-demo 1/1 Running 0 15s
 [root@master ~]# kubectl exec lifecycle-demo -it -- /bin/cat /usr/share/message
 Hello from the poststart handle
3.5 Pod phase
- pod的status字段是一个podstatus对象,podstatus中有一个phase字段。 pod的相位(phase)是pod在其生命周期中的简单宏观概述。该阶段并不是对容器或pod的综合汇总,也不是为了作为综合状态机pod相位的数量和含义是严格指定的。除了本文档中列举的状态外,不应该再假定pod有其他的phase。 - 挂起(Pending):Pod已被Kubernetes系统接受,但有一个或者多个容器镜像尚未创建。等待时间包括调度Pod的时间和通过网络下载镜像的时间,这可能需要花点时间。
- 运行中(Running):该Pod已经绑定到了一个节点上,Pod中所有的容器都已被创建。至少有一个容器正在运行,或者处于启动或重启状态。
- 成功(Succeeded):Pod中的所有容器都被成功终止,并且不会再重启。
- 失败(Faild):Pod中的所有容器都已终止了,并且至少有一个容器是因为失败终止。也就是说,容器以非0状态退出或者被系统终止。
- 未知(Unknown):因为某些原因无法取得Pod的状态,通常是因为与Pod所在主机通信失败。
 
- 重启策略: - PodSpec中有一个restartPolicy字段,可能的值为Always、OnFailure和Never。默认为Always。restartPolicy适用于Pod中的所有容器。restartPolicy仅指通过同一节点上的kubelet重新启动容器。失败的容器由kubelet 以五分钟为上限的指数退避延迟(10秒,20秒,40秒….)重新启动,并在成功执行十分钟后重置。如Pod文档中所述,一旦绑定到一个节点,Pod将永远不会重新绑定到另一个节点。
 
