资源清单
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
资源清单
资源清单的含义
在 k8s 中,一般使用 yaml 格式的文件来创建符合我们预期期望的 pod ,这样的 yaml 文件我们一般称为资源清单
资源清单 yaml > kubectl > json > apiserver
资源清单格式
apiVersion: group/apiversion # 如果没有给定 group 名称,那么默认为 core,可以使用 kubectl api-versions # 获取当前 k8s 版本上所有的 apiVersion 版本信息( 每个版本可能不同 )不同的资源有不同的接口
kind: #资源类别
metadata: #资源元数据
name
namespace
lables
annotations # 主要目的是方便用户阅读查找
spec: # 期望的状态(disired state)
status:# 当前状态,本字段有 Kubernetes 自身维护,用户不能去定义
资源清单的常用命令
获取 api-version 版本信息
[root@k8s-master01 ~]# kubectl api-versions
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1
......(以下省略)
获取资源的 api-Version 版本信息
[root@k8s-master01 ~]# kubectl explain pod
KIND: Pod
VERSION: v1
.....(以下省略)
[root@k8s-master01 ~]# kubectl explain Ingress
KIND: Ingress
VERSION: extensions/v1beta1
获取字段设置帮助文档
[root@k8s-master01 ~]# 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>
........
........
kubectl explain pod.spec # pod。spec字段下有哪些设置方式
字段配置格式
apiVersion <string> #表示字符串类型
metadata <Object> #表示需要嵌套多层字段
labels <map[string]string> #表示由k:v组成的映射
finalizers <[]string> #表示字串列表
ownerReferences <[]Object> #表示对象列表
hostPID <boolean> #布尔类型
priority <integer> #整型
name <string> -required- #如果类型后面接 -required-,表示为必填字段
通过定义清单文件创建 Pod
$ mkdir 1-pod
$ cd 1-pod
$ vim 1-pod.yaml
apiVersion: v1 # 定义组
kind: Pod # 资源类型
metadata: # 元数据信息
name: pod-demo # 名
namespace: default # 放在default空间下
labels: # 标签
app: myapp
version: v1
spec: # 期望
containers: # 容器
- name: myapp-1 # 容器名
image: hub.wyy.com/library/myapp:v1 # 容器地址
- name: busybox-1
image: hub.wyy.com/library/busybox:v1
command: # 替换原本镜像的启动命令
- "/bin/sh"
- "-c"
- "sleep 3600"
$ kubectl create -f 1-pod.yaml
# 若有干扰,先删除
$ kubectl delete deployment --all
$ kubectl get svc
$ kubectl delete service myapp
$ kubectl get pod
kubectl get pod xx.xx.xx -o yaml
<!--使用 -o 参数 加 yaml,可以将资源的配置以 yaml的格式输出出来,也可以使用json,输出为json格式-->
<!--资源被创建的流程:Kubectl(Yaml) >>> KubeapiServer(Json)-->
常用字段解释说明
必须存在的属性
主要对象
Pod生命周期
1.pod创建第一个步骤创建pause容器(作用:创建网络栈、挂载存储卷)
2.初始化容器initC(初始化环境,下载代码至pause的共享存储里,检查网络状态等)可按序创建多个
3.MainC容器创建(主容器,可有多个)
4.启动前操作(钩子)(初始化环境、检测环境等)
5.生存探测:判断主容器是否运行,不成功,则重新建立
6.就绪探测:判断当前进程是否满足对外服务的标准,未就绪,就不会暴露。
7.退出前操作(在镜像将关闭时的操作)
init容器介绍及作用
# 介绍
Pod 能够具有多个容器,应用运行在容器里面,但是它也可能有一个或多个先于应用容器启动的 Init 容器
# Init 容器与普通的容器非常像,除了如下两点:
Init 容器总是运行到成功完成为止
每个 Init 容器都必须在下一个 Init 容器启动之前成功完成
如果 Pod 的 Init 容器失败,Kubernetes 会不断地重启该 Pod,直到 Init 容器成功为止。然而,如果 Pod 对应的 restartPolicy 为 Never,它不会重新启动
# 作用
因为 Init 容器'具有与应用程序容器分离的单独镜像',所以它们的启动相关代码具有如下优势:
它们(initC)可以包含并运行实用工具,但是出于安全考虑,是不建议在应用程序容器镜像中包含这些实用工具的
它们可以包含使用工具和定制化代码来安装,但是不能出现在应用程序镜像中。例如,创建镜像没必要 FROM 另一个镜像,只需要在安装过程中使用类似 sed、 awk、 python 或 dig 这样的工具。
应用程序镜像可以分离出创建和部署的角色,而没有必要联合它们构建一个单独的镜像。
Linux Namespace,所以相对应用程序容器来说具有不同的文件系统视图。因此,它们能够具有访问 Secret 的权限,而应用程序容器则不能。
它们必须在应用程序容器启动之前运行完成,而应用程序容器是并行运行的,所以 Init 容器能够提供了一种简单的阻塞或延迟应用容器的启动的方法,直到满足了一组先决条件。
init 容器模板
$ mkdir 1-initC
$ vim pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: hub.wyy.com/library/busybox:v1
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
initContainers:
- name: init-myservice
image: hub.wyy.com/library/busybox:v1
command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2; done;']
- name: init-mydb
image: hub.wyy.com/library/busybox:v1
command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done;']
$ kubectl create -f pod.yaml # 创建
$ kubectl get pod # 查看
# 创建svc
$ vim myservice.yaml
kind: Service
apiVersion: v1
metadata:
name: myservice
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9376
$ vim mydb.yaml
kind: Service
apiVersion: v1
metadata:
name: mydb
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9377
$ kubectl create -f mydb.yaml
$ kubectl create -f myservice.yaml # 运行
$ kubectl get svc # 获取当前的状态
特殊说明
在 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 地址执行 HTTP Get 请求。如果响应的'状态码大于等于200 且小于 400',则诊断被认为是成功的
每次探测都将获得以下三种结果之一:
成功:容器通过了诊断。
失败:容器未通过诊断。
未知:诊断失败,因此不会采取任何行动
# 探测方式
'livenessProbe':指示容器是否正在运行。如果存活探测失败,则 kubelet 会杀死容器,并且容器将受到其 重启策略 的影响。如果容器不提供存活探针,则默认状态为 Success
'readinessProbe':指示容器是否准备好服务请求。如果就绪探测失败,端点控制器将从与 Pod 匹配的所有 Service 的端点中删除该 Pod 的 IP 地址。初始延迟之前的就绪状态默认为 Failure。如果容器不提供就绪探针,则默认状态为 Success
# 钩子
Pod hook(钩子)是由 Kubernetes 管理的 kubelet 发起的,当容器中的进程启动前或者容器中的进程终止之前运行,这是包含在容器的生命周期之中。可以同时为 Pod 中的所有容器都配置 hook
Hook 的类型包括两种:
exec:执行一段命令
HTTP:发送HTTP请求
检测探针 - 就绪检测
readinessProbe-httpget
$ kubectl get pod
$ kubectl delete pod --all
$ mkdir read
$ cd read
$ vim 1.pod
apiVersion: v1
kind: Pod
metadata:
name: readiness-httpget-pod
namespace: default
labels:
app: nginx
spec:
containers:
- name: readiness-httpget-container
image: hub.wyy.com/myapp:v1
imagePullPolicy: IfNotPresent
readinessProbe:
httpGet:
port: 80
path: /index1.html
initialDelaySeconds: 1
periodSeconds: 3
$ kubectl create -f 1.pod
$ kubectl get pod --show-labels -o wide
$ ipvsadm -Ln
$ kubectl exec -it readiness-httpget-pod -- /bin/bash
$ cd /usr/share/nginx/html
$ touch index1.html
$ kubectl get pod
$ ipvsadm -Ln
检测探针 - 存活检测
livenessProbe-exec
$ kubectl get svc # 查看svc类型的资源
$ kubectl delete svc mydb # 删除资源
$ kubectl get pod # 查看pod类型资源
$ kubectl delete pod --all # 删除所有pod资源
$ mkdir live
$ cd live
$ vim 1.pod
apiVersion: v1
kind: Pod
metadata:
name: liveness-exec-pod
namespace: default
spec:
containers:
- name: liveness-exec-container
image: hub.wyy.com/library/myapp:v2
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
$ kubectl create -f 1.pod
$ kubectl get pod -o wide # 显示更详细的信息 -w:先空
livenessProbe-httpget
$ vim 2.pod
apiVersion: v1
kind: Pod
metadata:
name: liveness-httpget-pod
namespace: default
spec:
containers:
- name: liveness-httpget-container
image: hub.wyy.com/library/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
livenessProbe:
httpGet:
port: 80
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
timeoutSeconds: 3
$ kubectl create -f 2.pod
$ kubectl get pod
$ kubectl exec -it liveness-httpget-pod -- /bin/bash
$ cd /usr/share/nginx/html
$ mv index1.html index1.html.back
$ kubectl get pod
livenessProbe-tcp
apiVersion: v1
kind: Pod
metadata:
name: probe-tcp
spec:
containers:
- name: nginx
image: hub.wyy.com/library/myapp:v1
livenessProbe:
initialDelaySeconds: 5
timeoutSeconds: 1
tcpSocket:
port: 80
启动、退出动作
$ mkdir hook
$ cd hook
$ vim pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: lifecycle-demo
spec:
containers:
- name: lifecycle-demo-container
image: hub.wyy.com/library/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 poststop handler > /usr/share/message"]
$ kubectl create -f pod.yaml
$ kubectl get pod
$ kubectl exec lifecycle-demo -- cat /usr/share/message
# 抓取stop(可能抓取不到)
$ while 2>1;
$ do
$ kubectl exec lifecycle-demo -- cat /usr/share/message
$ done
# 新建窗口
$ kubectl delete pod --all
检测叠加(生命周期)
$ mkdir all
$ cd all
$ vim pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: hub.wyy.com/library/myapp:v1
readinessProbe:
httpGet:
port: 80
path: /index1.html
initialDelaySeconds: 1
periodSeconds: 3
livenessProbe:
httpGet:
port: 80
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
timeoutSeconds: 3
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"]
- name: busybox-1
image: hub.wyy.com/library/busybox:v1
command: ["/bin/sh","-c","touch /tmp/live ; sleep 36000000"]
livenessProbe:
exec:
command: ["test","-e","/tmp/live"]
initialDelaySeconds: 1
periodSeconds: 3
initContainers:
- name: init-myservice
image: hub.wyy.com/library/busybox:v1
command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2; done;']
- name: init-mydb
image: hub.wyy.com/library/busybox:v1
command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done;']
$ kubectl create -f pod.yaml
$ kubectl create -f myservice.yaml
$ kubectl get pod
$ kubectl create -f mydb.yaml
$ kubectl get pod
$ kubectl exec -it myapp-pod -c myapp-container -- /bin/sh
$ cd /usr/share/nginx/html
$ touch index1.html
$ kubectl get pod