K8s新手系列之资源清单(Manifests)

Manifests的基本概念

在 Kubernetes 中,Manifests(清单)是用于定义集群中资源对象的声明式配置文件(通常以 YAML 或 JSON 格式编写,生产环境中通常以YAML编写)。它们描述了 期望的状态(Desired State),由 Kubernetes 控制器负责将集群的实际状态逐步调整为该状态。

简单点说就是:用于描述定义K8S集群资源的配置文件。

Manifests的核心作用

  • 声明式配置:通过文件定义资源(如 Pod、Deployment、Service),而非命令式操作(如 kubectl run)。

  • 版本控制:文件可纳入 Git 等版本控制系统,便于追踪变更和协作。

  • 可重复部署:确保不同环境(开发、测试、生产)的配置一致性。

  • 自动化基础:与 CI/CD 工具集成,实现自动化部署和更新。

Manifests的文件组成部分

Manifests通常是以下几个部分组成

apiVersion

指定api的版本号,基本上每个K8S的型号固定,不能乱写,apiVersion通过kubectl api-resources指令获取

kind

资源的类型,每个资源的类型也是固定的,也是通过kubectl api-resources指令获取

metadata:

资源的元数据,比如资源的名称,资源的标签,所属于的名称空间以及资源注解等信息。

通常由下面部分组成:

复制代码
metadata:         #必选,元数据
  name: string     #必选,Pod名称
  namespace: string  #Pod所属的命名空间,默认为"default"
  labels:           #自定义标签列表
    - name: string 

spec:

用户期望资源,定义,镜像名称,容器的名称,调度策略,镜像拉取策略,重启策略,环境变量等相关的配置。

说白了就是用户期望容器如何运行。

spec具体的值可以通过kubectl explain 资源类型.spec来获取。

例如:

复制代码
[root@master01 ~/pod]# kubectl explain pod.spec
KIND:     Pod
VERSION:  v1

RESOURCE: spec <Object>

DESCRIPTION:
     Specification of the desired behavior of the pod. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

     PodSpec is a description of a pod.

FIELDS:
   activeDeadlineSeconds        <integer>
     Optional duration in seconds the pod may be active on the node relative to
     StartTime before the system will actively try to mark it failed and kill
     associated containers. Value must be a positive integer.

   affinity     <Object>
     If specified, the pod's scheduling constraints

   automountServiceAccountToken <boolean>
     AutomountServiceAccountToken indicates whether a service account token
     should be automatically mounted.

   containers   <[]Object> -required-
     List of containers belonging to the pod. Containers cannot currently be
     added or removed. There must be at least one container in a Pod. Cannot be
     updated.
... 省略万字内容

status

在k8s集群中资源实际的运行状态,由k8s组件维护,自动更新,在Manifests通常不写

Manifests的管理操作

创建/更新资源

复制代码
# 应用 Manifest 文件(创建或更新资源)
kubectl apply -f manifests.yaml

# 应用目录下所有 Manifest 文件
kubectl apply -f manifests/

删除资源

复制代码
kubectl delete -f manifests.yaml

查看生成的资源配置

复制代码
# 查看实际生效的配置(含集群填充的默认值)
kubectl get pod nginx-pod -o yaml

Pod资源完整的资源清单参考

可以使用下面的命令来获取资源清单

  • kubectl explain 资源类型:查看某种资源可以配置的一级属性

  • kubectl explain 资源类型.属性:查看属性的子属性

    apiVersion: v1 #必选,版本号,例如v1
    kind: Pod   #必选,资源类型,例如 Pod
    metadata:   #必选,元数据
    name: string #必选,Pod名称
    namespace: string #Pod所属的命名空间,默认为"default"
    labels:    #自定义标签列表
    - name: string
    spec: #必选,Pod中容器的详细定义
    containers: #必选,Pod中容器列表
    - name: string #必选,容器名称
    image: string #必选,容器的镜像名称
    imagePullPolicy: [ Always|Never|IfNotPresent ] #获取镜像的策略
    command: [string] #容器的启动命令列表,如不指定,使用打包时使用的启动命令
    args: [string] #容器的启动命令参数列表
    workingDir: string #容器的工作目录
    volumeMounts: #挂载到容器内部的存储卷配置
    - name: string #引用pod定义的共享存储卷的名称,需用volumes[]部分定义的的卷名
    mountPath: string #存储卷在容器内mount的绝对路径,应少于512字符
    readOnly: boolean #是否为只读模式
    ports: #需要暴露的端口库号列表
    - name: string #端口的名称
    containerPort: int #容器需要监听的端口号
    hostPort: int #容器所在主机需要监听的端口号,默认与Container相同
    protocol: string #端口协议,支持TCP和UDP,默认TCP
    env: #容器运行前需设置的环境变量列表
    - name: string #环境变量名称
    value: string #环境变量的值
    resources: #资源限制和请求的设置
    limits: #资源限制的设置
    cpu: string #Cpu的限制,单位为core数,将用于docker run --cpu-shares参数
    memory: string #内存限制,单位可以为Mib/Gib,将用于docker run --memory参数
    requests: #资源请求的设置
    cpu: string #Cpu请求,容器启动的初始可用数量
    memory: string #内存请求,容器启动的初始可用数量
    lifecycle: #生命周期钩子
    postStart: #容器启动后立即执行此钩子,如果执行失败,会根据重启策略进行重启
    preStop: #容器终止前执行此钩子,无论结果如何,容器都会终止
    livenessProbe: #对Pod内各容器健康检查的设置,当探测无响应几次后将自动重启该容器
    exec:   #对Pod容器内检查方式设置为exec方式
    command: [string] #exec方式需要制定的命令或脚本
    httpGet: #对Pod内个容器健康检查方法设置为HttpGet,需要制定Path、port
    path: string
    port: number
    host: string
    scheme: string
    HttpHeaders:
    - name: string
    value: string
    tcpSocket: #对Pod内个容器健康检查方式设置为tcpSocket方式
    port: number
    initialDelaySeconds: 0 #容器启动完成后首次探测的时间,单位为秒
    timeoutSeconds: 0    #对容器健康检查探测等待响应的超时时间,单位秒,默认1秒
    periodSeconds: 0    #对容器监控检查的定期探测时间设置,单位秒,默认10秒一次
    successThreshold: 0
    failureThreshold: 0
    securityContext:
    privileged: false
    restartPolicy: [Always | Never | OnFailure] #Pod的重启策略
    nodeName: <string> #设置NodeName表示将该Pod调度到指定到名称的node节点上
    nodeSelector: obeject #设置NodeSelector表示将该Pod调度到包含这个label的node上
    imagePullSecrets: #Pull镜像时使用的secret名称,以key:secretkey格式指定
    - name: string
    hostNetwork: false #是否使用主机网络模式,默认为false,如果设置为true,表示使用宿主机网络
    volumes: #在该pod上定义共享存储卷列表
    - name: string #共享存储卷名称 (volumes类型有很多种)
    emptyDir: {} #类型为emtyDir的存储卷,与Pod同生命周期的一个临时目录。为空值
    hostPath: string #类型为hostPath的存储卷,表示挂载Pod所在宿主机的目录
    path: string    #Pod所在宿主机的目录,将被用于同期中mount的目录
    secret:    #类型为secret的存储卷,挂载集群与定义的secret对象到容器内部
    scretname: string
    items:
    - key: string
    path: string
    configMap: #类型为configMap的存储卷,挂载预定义的configMap对象到容器内部
    name: string
    items:
    - key: string
    path: string