[root@k8s-master01 pod-yaml]# kubectl run tomcat --image=tomcat:latest --image-pull-policy='IfNotPresent' --port=8080
pod/tomcat created
您在 /var/spool/mail/root 中有新邮件
[root@k8s-master01 pod-yaml]# kubectl get pods -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
tomcat 1/1 Running 0 15s 10.244.169.136 k8s-node2
四、Pod资源清单编写技巧
通过 kubectl explain 帮助指令查看每一个字段的含义,一步一步慢慢摸索下去。
4.1 通过 kubectl explain 查看定义Pod资源包含哪些字段。
[root@k8s-master01 pod-yaml]# kubectl explain pod
KIND: Pod
VERSION: v1# Pod是可以在主机上运行的容器的集合。这个资源是由客户端创建并调度到主机上。
DESCRIPTION:Pod is a collection of containers that can run on a host. This resource iscreated by clients and scheduled onto hosts.FIELDS:# APIVersion定义了对象,代表了一个版本为V1。apiVersion APIVersion defines the versioned schema of this representation of anobject. Servers should convert recognized schemas to the latest internalvalue, and may reject unrecognized values. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources# Kind是字符串类型的值,代表了要创建的资源。服务器可以从客户端提交的请求推断出这个资源kind Kind is a string value representing the REST resource this objectrepresents. Servers may infer this from the endpoint the client submitsrequests to. Cannot be updated. In CamelCase. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds# metadata是对象,定义元数据属性信息的metadata
4.2 查看 pod.metadata 字段如何定义
[root@k8s-master01 pod-yaml]# kubectl explain pod.metadata
KIND: Pod
VERSION: v1# metadata是对象,下面可以有多个字段
RESOURCE: metadata DESCRIPTION:Standard object's metadata. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadataObjectMeta is metadata that all persisted resources must have, whichincludes all objects users must create.FIELDS:# annotations是注解,map类型表示对应的值是key-value键值对,表示 key 和value都是String类型的annotations
4.3 查看pod.spec字段如何定义
[root@k8s-master01 pod-yaml]# kubectl explain pod.spec
KIND: Pod
VERSION: v1RESOURCE: spec # Pod的spec字段是用来描述Pod的
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-statusPodSpec is a description of a pod.FIELDS:# 表示Pod可以运行的最长时间,达到设置的值后,Pod会自动停止。activeDeadlineSeconds Optional duration in seconds the pod may be active on the node relative toStartTime before the system will actively try to mark it failed and killassociated containers. Value must be a positive integer.affinity # 定义亲和性的automountServiceAccountToken AutomountServiceAccountToken indicates whether a service account tokenshould be automatically mounted.# containers是对象列表,用来定义容器的,是必须字段。对象列表,表示下面有很多对象,对象列表下面的内容用 - 连接。containers <[]Object> -required-List of containers belonging to the pod. Containers cannot currently beadded or removed. There must be at least one container in a Pod. Cannot beupdated.······
4.4 查看 pod.spec.containers 字段如何定义
[root@k8s-master01 pod-yaml]# kubectl explain pod.spec.containers
KIND: Pod
VERSION: v1RESOURCE: containers <[]Object># container是定义在pod里面的,一个pod至少要有一个容器。
DESCRIPTION:List of containers belonging to the pod. Containers cannot currently beadded or removed. There must be at least one container in a Pod. Cannot beupdated.A single application container that you want to run within a pod.FIELDS:
······ # image是用来指定容器需要的镜像的 image Container image name. More info:https://kubernetes.io/docs/concepts/containers/images This field isoptional to allow higher level config management to default or overridecontainer images in workload controllers like Deployments and StatefulSets.# 镜像拉取策略,pod是要调度到node节点的,那pod启动需要镜像,可以根据这个字段设置镜像拉取策略,支持如下三种:
Always:不管本地是否存在镜像,都要重新拉取镜像
Never: 从不拉取镜像
IfNotPresent:如果本地存在,使用本地的镜像,本地不存在,从官方拉取镜像imagePullPolicy Image pull policy. One of Always, Never, IfNotPresent. Defaults to Alwaysif :latest tag is specified, or IfNotPresent otherwise. Cannot be updated.More info:https://kubernetes.io/docs/concepts/containers/images#updating-imagesPossible enum values:- `"Always"` means that kubelet always attempts to pull the latest image.Container will fail If the pull fails.- `"IfNotPresent"` means that kubelet pulls if the image isn't present ondisk. Container will fail if the image isn't present and the pull fails.- `"Never"` means that kubelet never pulls an image, but only uses a localimage. Container will fail if the image isn't present······# name是必须字段,用来指定容器名字的name -required-Name of the container specified as a DNS_LABEL. Each container in a podmust have a unique name (DNS_LABEL). Cannot be updated.# port是端口,属于对象列表ports <[]Object>List of ports to expose from the container. Not specifying a port here DOESNOT prevent that port from being exposed. Any port which is listening onthe default "0.0.0.0" address inside a container will be accessible fromthe network. Modifying this array with strategic merge patch may corruptthe data. For more information Seehttps://github.com/kubernetes/kubernetes/issues/108255. Cannot be updated.······
4.5 查看 pod.spec.container.ports 字段如何定义
[root@k8s-master01 pod-yaml]# kubectl explain pod.spec.containers.ports
KIND: Pod
VERSION: v1RESOURCE: ports <[]Object>DESCRIPTION:List of ports to expose from the container. Not specifying a port here DOESNOT prevent that port from being exposed. Any port which is listening onthe default "0.0.0.0" address inside a container will be accessible fromthe network. Modifying this array with strategic merge patch may corruptthe data. For more information Seehttps://github.com/kubernetes/kubernetes/issues/108255. Cannot be updated.ContainerPort represents a network port in a single container.FIELDS:# containerPort是必须字段, pod中的容器需要暴露的端口。containerPort -required-Number of port to expose on the pod's IP address. This must be a valid portnumber, 0 < x < 65536.# 将容器中的服务暴露到宿主机的端口上时,可以指定绑定的宿主机 IP。hostIP What host IP to bind the external port to.# 容器中的服务在宿主机上映射的端口hostPort Number of port to expose on the host. If specified, this must be a validport number, 0 < x < 65536. If HostNetwork is specified, this must matchContainerPort. Most containers do not need this.# 端口的名字name If specified, this must be an IANA_SVC_NAME and unique within the pod. Eachnamed port in a pod must have a unique name. Name for the port that can bereferred to by services.
······
上一篇文章:【云原生 | Kubernetes 实战】02、k8s 核心资源 Pod 介绍_Stars.Sky的博客-CSDN博客