K8S进阶(Lifecycle......偏理论)
一、Pod进阶学习之路
1.1 Lifecycle
官网
:https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/
- 挂起(Pending): Pod 已被 Kubernetes 系统接受,但有一个或者多个容器镜像尚未创建。等待时间包括调度 Pod 的时间和通过网络下载镜像的时间,这可能需要花点时间。
- 运行中(Running): 该 Pod 已经绑定到了一个节点上,Pod 中所有的容器都已被创建。至少有一个容器正在运行,或者正处于启动或重启状态。
- 成功(Succeeded): Pod 中的所有容器都被成功终止,并且不会再重启。
- 失败(Failed): Pod 中的所有容器都已终止了,并且至少有一个容器是因为失败终止。也就是说,容器以非0状态退出或者被系统终止。
- 未知(Unknown): 因为某些原因无法取得 Pod 的状态,通常是因为与 Pod 所在主机通信失败。
1.2 重启策略
官网
:https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy
PodSpec有一个restartPolicy字段,可能值为Always、OnFailure、Never,缺省值为Always。restartPolicy应用于Pod中的所有容器。restartPolicy仅指kubelet在同一节点上重启容器。kubelet重新启动的退出容器以指数级的回退延迟(10s、20s、40s……)重新启动,上限为5分钟,并在成功执行10分钟后重新启动。正如Pods文档中所讨论的,一旦绑定到一个节点,一个Pod将永远不会被rebound(反弹)到另一个节点。
- Always:容器失效时,即重启
- OnFailure:容器终止运行且退出码不为0时重启
- Never:永远不重启
1.3 静态Pod
静态Pod是由kubelet进行管理的,并且存在于特定的Node上。
不能通过API Server进行管理,无法与ReplicationController,Ddeployment或者DaemonSet进行关联,也无法进行健康检查。
1.4 健康检查
The kubelet can optionally perform and react to three kinds of probes on running Containers:
kubelet可以在运行的容器上选择性地执行和响应三种探针:
livenessProbe
: Indicates whether the Container is running. If the liveness probe fails, the kubelet kills the Container, and the Container is subjected to its restart policy. If a Container does not provide a liveness probe, the default state isSuccess
.readinessProbe
: Indicates whether the Container is ready to service requests. If the readiness probe fails, the endpoints controller removes the Pod’s IP address from the endpoints of all Services that match the Pod. The default state of readiness before the initial delay isFailure
. If a Container does not provide a readiness probe, the default state isSuccess
.startupProbe
: Indicates whether the application within the Container is started. All other probes are disabled if a startup probe is provided, until it succeeds. If the startup probe fails, the kubelet kills the Container, and the Container is subjected to its restart policy. If a Container does not provide a startup probe, the default state isSuccess
.
LivenessProbe探针:判断容器是否存活
ReadinessProbe探针:判断容器是否启动完成
1.5 ConfigMap
官网
:https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/
ConfigMaps allow you to decouple configuration artifacts from image content to keep containerized applications portable. (ConfigMaps允许您将配置工件与图像内容解耦,以保持容器化的应用程序的可移植性。)
说白了就是用来保存配置数据的键值对,也可以保存单个属性,也可以保存配置文件。
所有的配置内容都存储在etcd中,创建的数据可以供Pod使用。
1.5.1 命令行创建
<pre data-index="0" class="set-code-show prettyprint"><code class="prism language-shell has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token comment"># 创建一个名称为my-config的ConfigMap,key值时db.port,value值是'3306'</span>
kubectl create configmap my-config --from-literal<span class="token operator">=</span>db.port<span class="token operator">=</span><span class="token string">'3306'</span>
kubectl get configmap
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li></ul></pre>
详情信息
apiVersion: v1 data: db.port: "3306" kind: ConfigMap metadata: creationTimestamp: "2019-11-22T09:50:17Z" name: my-config namespace: default resourceVersion: "691934" selfLink: /api/v1/namespaces/default/configmaps/my-config uid: 7d4f338b-0d0d-11ea-bb46-00163e0edcbd
查看命令:kubectl get configmap myconfig -o yaml
1.5.2 从配置文件中创建
创建一个文件,名称为app.properties
内容:
name=jack
age=17
命令:
kubectl create configmap app --from-file=./app.properties
kubectl get configmap
kubectl get configmap app -o yaml
1.5.3 从目录中创建
mkdir config
cd config
mkdir a
mkdir b
cd ..
命令:
kubectl create configmap config --from-file=config/
kubectl get configmap
1.5.4 通过yaml文件创建
创建configmaps.yaml:
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
special.how: very
---
apiVersion: v1
kind: ConfigMap
metadata:
name: env-config
namespace: default
data