第四步: apiserver 又通过 watch 机制,调用 kubelet,指定 pod 信息,调用 Docker API 创建并启动 pod 内的容 器。
第五步: 创建完成之后反馈给 kubelet, kubelet 又将 pod 的状态信息给 apiserver, apiserver 又将 pod 的状态信息写入 etcd。
2.1 资源清单 YAML 文件书写技巧
Pod 资源清单编写技巧
通过 kubectl explain 查看定义 Pod 资源包含哪些字段。
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.
# [Pod 是可以在主机上运行的容器的集合。此资源是由客户端创建并安排到主机上。]
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/apiconventions.md#resources
# [APIVersion 定义了对象,代表了一个版本。]
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/apiconventions.md#types-kinds
# [Kind 是字符串类型的值,代表了要创建的资源。服务器可以从客户端提交的请求推断出这个资源。]
metadata <Object>
Standard object's metadata. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/apiconventions.md#metadata
# [metadata 是对象,定义元数据属性信息的]
spec <Object>
Specification of the desired behavior of the pod. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/apiconventions.md#spec-and-status
# [spec 制定了定义 Pod 的规格,里面包含容器的信息]
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/apiconventions.md#spec-and-status
# [status 表示状态,这个不可以修改,定义 pod 的时候也不需要定义这个字段]
查看 pod.metadata 字段如何定义
kubectl explain pod.metadata
KIND: Pod
VERSION: v1
RESOURCE: metadata <Object>
# metadata 是对象<Object>,下面可以有多个字段
DESCRIPTION:
Standard object's metadata. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/apiconventions.md#metadata
ObjectMeta is metadata that all persisted resources must have, which
includes all objects users must create.
FIELDS:
annotations <map[string]string>
Annotations is an unstructured key value map stored with a resource that
may be set by external tools to store and retrieve arbitrary metadata. They
are not queryable and should be preserved when modifying objects. More
info: http://kubernetes.io/docs/user-guide/annotations
# annotations 是注解,map 类型表示对应的值是 key-value 键值对<string,string>表示 key 和value 都是 String 类型的用 Annotation 来记录的信息包括:
# build 信息、release 信息、Docker 镜像信息等,例如时间戳、release id 号、镜像 hash 值、docker
# registry 地址等;日志库、监控库、分析库等资源库的地址信息;程序调试工具信息,例如工具名称、版本号等;团队的联系信息,例如电话号码、负责人名称、网址等。
clusterName <string>
The name of the cluster which the object belongs to. This is used to
distinguish resources with same name and namespace in different clusters.
This field is not set anywhere right now and apiserver is going to ignore
it if set in create or update request.
#对象所属群集的名称。这是用来区分不同集群中具有相同名称和命名空间的资源。此字段现在未设置在任何位置,apiserver 将忽略它,如果设置了就使用设置的值
creationTimestamp <string>
deletionGracePeriodSeconds <integer>
deletionTimestamp <string>
finalizers <[]string>
generateName <string>
generation <integer>
labels <map[string]string> #创建的资源具有的标签
Map of string keys and values that can be used to organize and categorize
(scope and select) objects. May match selectors of replication controllers
and services. More info: http://kubernetes.io/docs/user-guide/labels
#labels 是标签,labels 是 map 类型,map 类型表示对应的值是 key-value 键值对,<string,string>表示 key 和 value 都是 String 类型的
managedFields <[]Object>
name <string> #创建的资源的名字
namespace <string> #创建的资源所属的名称空间
Namespace defines the space within which each name must be unique. An empty
namespace is equivalent to the "default" namespace, but "default" is the
canonical representation. Not all objects are required to be scoped to a
namespace - the value of this field for those objects will be empty.
Must be a DNS_LABEL. Cannot be updated. More info:
http://kubernetes.io/docs/user-guide/namespaces
# namespaces 划分了一个空间,在同一个 namesace 下的资源名字是唯一的,默认的名称空间是default。
ownerReferences <[]Object>
resourceVersion <string>
selfLink <string>
uid <string>
查看 pod.spec 字段如何定义
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/apiconventions.md#spec-and-status
PodSpec is a description of a pod.
#Pod 的 spec 字段是用来描述 Pod 的
FIELDS:
activeDeadlineSeconds <integer>
#表示 Pod 可以运行的最长时间,达到设置的值后,Pod 会自动停止。
affinity <Object>
#定义亲和性的
automountServiceAccountToken <boolean>
containers <[]Object> -required-
#containers 是对象列表,用来定义容器的,是必须字段。对象列表 表示下面有很多对象,对象列
表下面的内容用 - 连接。
dnsConfig <Object>
dnsPolicy <string>
enableServiceLinks <boolean>
ephemeralContainers <[]Object>
hostAliases <[]Object>
hostIPC <boolean>
hostNetwork <boolean>
hostPID <boolean>
hostname <string>
imagePullSecrets <[]Object>
initContainers <[]Object>
nodeName <string>
nodeSelector <map[string]string>
overhead <map[string]string>
preemptionPolicy <string>
priority <integer>
priorityClassName <string>
readinessGates <[]Object>
restartPolicy <string>
runtimeClassName <string>
schedulerName <string>
securityContext <Object>
serviceAccount <string>
serviceAccountName <string>
setHostnameAsFQDN <boolean>
shareProcessNamespace <boolean>
subdomain <string>
terminationGracePeriodSeconds <integer>
tolerations <[]Object>
topologySpreadConstraints <[]Object>
volumes <[]Object>
查看 pod.spec.containers 字段如何定义
kubectl explain pod.spec.containers
KIND: Pod
VERSION: v1
RESOURCE: containers <[]Object>
DESCRIPTION:
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.A single application container that you want to run within a pod.
#container 是定义在 pod 里面的,一个 pod 至少要有一个容器。
FIELDS:
args <[]string>
command <[]string>
env <[]Object>
envFrom <[]Object>
image <string>
韩老师微信: luckylucky421302
版权声明,本文档全部内容及版权归韩先超所有,只可用于自己学习使用,禁止私自传阅,违者依法
追责。
#image 是用来指定容器需要的镜像的
imagePullPolicy <string>
#镜像拉取策略,pod 是要调度到 node 节点的,那 pod 启动需要镜像,可以根据这个字段设置镜像拉
取策略,支持如下三种:
Always:不管本地是否存在镜像,都要重新拉取镜像
Never: 从不拉取镜像
IfNotPresent:如果本地存在,使用本地的镜像,本地不存在,从官方拉取镜像
lifecycle <Object>
livenessProbe <Object>
name <string> -required-
#name 是必须字段,用来指定容器名字的
ports <[]Object>
#port 是端口,属于对象列表
readinessProbe <Object>
resources <Object>
securityContext <Object>
startupProbe <Object>
stdin <boolean>
stdinOnce <boolean>
terminationMessagePath <string>
terminationMessagePolicy <string>
tty <boolean>
volumeDevices <[]Object>
volumeMounts <[]Object>
workingDir <string>
查看 pod.spec.container.ports 字段如何定义
kubectl explain pod.spec.containers.ports
KIND: Pod
VERSION: v1
RESOURCE: ports <[]Object>
DESCRIPTION:
List of ports to expose from the container. Exposing a port here gives the system additional information about the network connections a container uses, but is primarily informational. Not specifying a port here DOES NOT prevent that port from being exposed. Any port which is listening on the
default "0.0.0.0" address inside a container will be accessible from the network. Cannot be updated. ContainerPort represents a network port in a single container.
FIELDS:
containerPort <integer> -required-
Number of port to expose on the pod's IP address. This must be a valid port
number, 0 < x < 65536.
#containerPort 是必须字段, pod 中的容器需要暴露的端口。
hostIP <string>
What host IP to bind the external port to.
#将容器中的服务暴露到宿主机的端口上时,可以指定绑定的宿主机 IP。
hostPort <integer>
Number of port to expose on the host. If specified, this must be a valid port number, 0 < x < 65536. If HostNetwork is specified, this must match ContainerPort. Most containers do not need this.
#容器中的服务在宿主机上映射的端口
name <string>
If specified, this must be an IANA_SVC_NAME and unique within the pod. Each named port in a pod must have a unique name. Name for the port that can be referred to by services.
#端口的名字
protocol <string>
Protocol for port. Must be UDP, TCP, or SCTP. Defaults to "TCP".