Kubernetes-集群资源对象描述文件YAML

本文详细解释了在Kubernetes中如何使用yaml格式的资源清单文件创建Pod,包括元数据、容器配置、常用字段如imagePullPolicy和restartPolicy,以及如何声明式地创建namespace和Pod实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在kubernetes中,一般使用ymal格式的文件来创建符合我们预期期望的pod,这样的yaml文件称为资源清单文件。

一、常用字段

参数名字段类型说明
versionString这里是指的是K8S API的版本,目前基本上是v1,可以用 kubectl api-versions命令查询
kindString这里指的是yam文件定义的资源类型和角色,比如:Pod
metadataObject元数据对象,固定值就写 metadata
metadata.nameString元数据对象的名字,这里由我们编写,比如命名Pod的名字
metadata.namespaceString元数据对象的命名空间,由我们自身定义
SpecObject详细定义对象,固定值就写Spec
spec. containers[]list这里是Spec对象的容器列表定义,是个列表
spec containers [].nameString这里定义容器的名字
spec.containers [].imageString这里定义要用到的镜像名称
spec.containers [].imagePullPolicyString定义镜像拉取策路,有 Always、 Never、Ifnotpresent三个值可选:(1) Always:意思是每次都尝试重新拉取镜像;(2) Never:表示仅使用本地镜像;(3) IfNotPresent:如果本地有镜像就使用本地镜像,没有就拉取在线镜像。上面三个值都没设置的话,默认是 Always。
spec containers [].command[]List指定容器启动命令,因为是数组可以指定多个。不指定则使用镜像打包时使用的启动命令。
spec.containers [].argsList指定容器启动命令参数,因为是数组可以指定多个.
spec.containers [].workDirString指定容器的工作目录
spec.containers[]. volumeMounts[]List指定容器内部的存储卷配置
spec.containers[]. volumeMounts[].nameString指定可以被容器挂载的存储卷的名称
spec.containers[]. volumeMounts[].mountPathString指定可以被容器挂载的存储卷的路径
spec.containers[]. volumeMounts[].readOnlyString设置存储卷路径的读写模式,ture或者 false,默认为读写模式
spec.containers [].ports[]String指容器需要用到的端口列表
spec.containers [].ports[].nameString指定端口名称
spec.containers [].ports[].containerPortString指定容器需要监听的端口号
spec.containers [].ports[].hostPortString指定容器所在主机需要监听的端口号,默认跟上面 containerPort相同,注意设置了 hostPort同一台主机无法启动该容器的相同副本(因为主机的端口号不能相同,这样会冲突)
spec.containers [].ports[].protocolString指定端口协议,支持TCP和UDP,默认值为TCP
spec.containers [].env[]List指定容器运行前需设的环境变量列表
spec.containers [].env[].nameString指定环境变量名称
spec.containers [].env[].valueString指定环境变量值
spec.containers[].resourcesObject指定资源 限制和资源请求的值(这里开始就是设置容器的资源上限)
spec.containers[].resources.limitsObject指定设置容器运行时资源的运行上限
spec.containers[].resources.limits.cpuString指定CPU限制,单位为core数,将用于docker run -- cpu-shares参数
spec.containers[].resources.limits.memoryString指定MEM内存的限制,单位为MiB、GiB
spec.containers[].resources.requestsObject指定容器启动和调度时的限制设置
spec.containers[].resources.requests.cpuStringCPU请求,单位为core数,容器启动时初始化可用数量
spec.containers[].resources.requests.memoryString内存请求,单位为MiB、GiB,容器启动时初始化可用数量
sepc.restartPolicyString定义Pod的重启策略,可选值为Always、OnFailure,默认值为Always。1.Always:Pod一旦终止运行,则无论容器时如何终止的,kubelet服务都将重启它。2.OnFailure:只有Pod以非零退出码终止时,kubelet才会重启该容器。如果容器正常结束(退出码为0),则kubelet将不会重启它。3.Never:Pod终止后,kubelet将退出码报告给Master,不会重启该Pod。
spec.nodeSelectorObject定义Node的Label过滤标签,以key:value格式指定。
spec.imagePullSecretsObject定义pull镜像时使用secret名称,以name:secretkey格式指定。
spec.hostNetworkBoolean定义是否使用主机网络模式,默认值为false。设置true表示使用宿主机网络,不使用docker网桥,同时设置了true将无法在同一台宿主机上启动第二个副本。

二、实战

2.1 使用声明式文件YAML创建namespace

apiVersion: v1
kind: Namespace
metadata:
  name: test

 2. 使用声明式文件YMAL创建pod

apiVersion: v1
kind: Pod
metadata:
  name: pod1
spec:
  containers:
  - name: k8sonline1
    image: nginx:latest
    imagePullPolicy: IfNotPresent

三、使用explain 查看文档

[root@master01 deploy]# 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/sig-architecture/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/sig-architecture/api-conventions.md#types-kinds

   metadata    <Object>
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

   spec    <Object>
     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

   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/sig-architecture/api-conventions.md#spec-and-status

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值