【云原生 | Kubernetes 实战】03、手把手教你基于YAML文件运行pod应用
admin
2024-02-19 22:32:49
0

目录

一、通过资源清单文件创建第一个Pod

二、Pod 资源清单字段解读

常见字段详情可参考官网帮助命令文档:

三、Pod 常用命令

3.1 通过 kubectl run 命令行创建pod(不使用yaml文件)

四、Pod资源清单编写技巧 

4.1 通过 kubectl explain 查看定义Pod资源包含哪些字段。

4.2 查看 pod.metadata 字段如何定义

4.3 查看pod.spec字段如何定义

4.4 查看 pod.spec.containers 字段如何定义

4.5 查看 pod.spec.container.ports 字段如何定义


一、通过资源清单文件创建第一个Pod

[root@k8s-master01 pod-yaml]# vi pod-first.yaml 
apiVersion: v1
kind: Pod
metadata:name: tomcat-testnamespace: defaultlabels:app:  tomcat
spec:containers:- name:  tomcat-javaimage: tomcat:latestimagePullPolicy: IfNotPresentports:- containerPort: 8080

二、Pod 资源清单字段解读

#1. 创建pod 的资源清单参数详细帮助命令
[root@k8s-master01 ~]# kubectl explain pod#2. pod 文件解读
# 注意:每个英文冒号后面必须有一个空格
[root@k8s-master01 pod-yaml]# vim pod-tomcat.yaml
apiVersion: v1  # api版本为V1。通过 kubectl explain pod 命令查看
kind: Pod       # 创建的资源类型。第一个 P 必须大写
metadata:       # 创建对象。通过 kubectl explain pod.metadata 查看包含有哪些字段name: tomcat-test    # Pod的名字namespace: default   # Pod所在的名称空间(default 是默认名称空间)labels:        app: tomcat     # Pod具有的标签
spec:               # 通过 kubectl explain pod.spec 查看包含的字段containers:       # 通过 kubectl explain pod.spec.containers 查看包含的字段;containers	<[]Object> -required- 是对象列表,下面包含的第一个字段需要加 '-'- name:  tomcat-java   # Pod里容器的名字    image: tomcat:latest  # 容器使用的镜像名称imagePullPolicy: IfNotPresent    # 镜像拉取策略(使用本地的镜像)ports:      # ports	<[]Object> 对象列表中的第一个字段要加 '-'- containerPort: 8080  # 容器暴露的端口#3. 创建 pod
[root@k8s-master01 pod-yaml]# kubectl apply -f pod-first.yaml 
pod/tomcat-test created
[root@k8s-master01 pod-yaml]# kubectl get pods
NAME                         READY   STATUS    RESTARTS        AGE
nginx-test-7767bdd4d-d5pqv   1/1     Running   1 (6h19m ago)   2d19h
nginx-test-7767bdd4d-rqrm5   1/1     Running   1 (6h19m ago)   2d19h
tomcat-test                  1/1     Running   0               2s

常见字段详情可参考官网帮助命令文档:

  • metadata.annotations:Annotations | Kubernetes
  • metadata.labels:Labels and Selectors | Kubernetes
  • metadata.name:Object Names and IDs | Kubernetes
  • metadata.namespace:Namespaces | Kubernetes
  • spec.containers.image:Images | Kubernetes
  • spec.containers.imagePullPolicy:Images | Kubernetes

三、Pod 常用命令

# 更新资源清单文件
[root@k8s-master01 pod-yaml]# kubectl apply -f pod-first.yaml# 查看pod是否创建成功
[root@k8s-master01 pod-yaml]# kubectl get pods -l app=tomcat
或者
[root@k8s-master01 pod-yaml]# kubectl get pods -A# 查看pod的ip和pod调度到哪个节点上
[root@k8s-master01 pod-yaml]# kubectl get pods -owide# 查看pod日志
[root@k8s-master01 pod-yaml]# kubectl logs tomcat-test# 进入到创建的pod
[root@k8s-master01 pod-yaml]# kubectl exec -it tomcat-test -- /bin/bash# 假如pod里有多个容器,进入到pod里的指定容器,-c 指定容器
[root@k8s-master01 pod-yaml]# kubectl exec -it tomcat-test -c tomcat-java -- /bin/bash# 查看k8s集群中的名称空间
[root@k8s-master01 pod-yaml]# kubectl get namespaces# 查看指定名称空间下的pod
[root@k8s-master01 pod-yaml]# kubectl get pods -n default# 查看pod详细信息
[root@k8s-master01 pod-yaml]# kubectl describe pods tomcat-test# 查看pod具有哪些标签:
[root@k8s-master01 pod-yaml]# kubectl get pods --show-labels# 删除pod
[root@k8s-master01 pod-yaml]# kubectl delete pods tomcat-test
或者
[root@k8s-master01 pod-yaml]# kubectl delete -f pod-first.yaml

3.1 通过 kubectl run 命令行创建pod(不使用yaml文件)

[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	Standard object's metadata. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata# spec制定了定义Pod的规格,里面包含容器的信息spec	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表示状态,这个不可以修改,定义pod的时候也不需要定义这个字段status	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 

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	Annotations is an unstructured key value map stored with a resource thatmay be set by external tools to store and retrieve arbitrary metadata. Theyare not queryable and should be preserved when modifying objects. Moreinfo: http://kubernetes.io/docs/user-guide/annotations······# 创建的资源具有的标签。labels是标签,labels是map类型,map类型表示对应的值是key-value键值对,表示 key和value都是String类型的labels	Map of string keys and values that can be used to organize and categorize(scope and select) objects. May match selectors of replication controllersand services. More info: http://kubernetes.io/docs/user-guide/labelsmanagedFields	<[]Object>name	    # 创建pod资源的名字# 创建的资源所属的名称空间。namespaces划分了一个空间,在同一个namesace下的资源名字是唯一的,默认的名称空间是default。namespace	Namespace defines the space within which each name must be unique. An emptynamespace is equivalent to the "default" namespace, but "default" is thecanonical representation. Not all objects are required to be scoped to anamespace - 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······ 

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博客

相关内容

热门资讯

原创 谈... 一家澳洲矿业巨头,突然被宣告——你在中国港口堆积的七百多万吨货,别想动了。这不是关税战,也不是外交场...
2026年成都起泡果酒市场:大... 起泡果酒是低酒精度果汁发酵饮品中,以二次发酵或充气工艺保留碳酸口感的一类产品。2026年,起泡果酒行...
输球又输人!巴拉圭将足球场变拳... 北京时间7月5日,在美加墨世界杯1/8决赛法国1-0小胜巴拉圭的比赛中,费城体育场见证了一场粗野球风...
脑瘤手术后,放疗是“必选项”吗... 大家好,我是纪跃廷主任。 放疗不是所有脑瘤术后的“必选项”,但对很多患者来说,它确实是决定预后的关键...
有人喊话奈雪的茶董事长赵林拿1... 来源:运营商财经网 运营商财经网 食品酒业频道 周颖/文 近日,奈雪的茶召开股东周年大会,有投资者描...
原创 财... 随着英国财政危机持续发酵,工党内部的财政政策裂痕已彻底浮出水面。英国首相热门人选、候任工党领袖安迪·...
涉及680万股东!A股下周一正... 文/帅可聪 下周一(7月6日)起,沪深交易所主板风险警示股票(ST股)涨跌幅限制将上调至10%。 今...
2025-2000上市公司医药... 本人将中国上市公司医药数据精心整理为面板数据的形式,医药企业具有471家,5972多个样本,无论是做...
汇川技术:PLC已形成完整系列... 来源:问董秘 投资者提问: 董秘,您好:想问一下贵司在大中型PLC的研发投入和技术发展现在处于什么阶...
原创 董... 昨晚天津卫视黄金档突然上线的《花开如梦》,像是从时间缝隙里掉出来的一部剧,让不少守在电视机前的观众一...
和讯高璐明:突发!券商爆利好!... 券商爆利好,业绩大幅飙升,创历史新高,那么到底对于下周市场会产生多大影响?券商板块未来还有没有上攻的...
国足短期内不会与佛得角交手 近期,关于佛得角可能与国足来一场友谊赛的消息备受关注。 在与阿根廷队赛前,佛得角队首发阵容合影。图...
BC技术接棒、钙钛矿叠层技术蓄... 本报记者 殷高峰 张晓玉 “现阶段光伏项目招标,不配备BC(背接触)组件,基本很难进入业主采购短名单...
原创 海... 新华社德黑兰7月4日电,伊朗伊斯兰革命卫队迎来关键人事变动,阿里·阿兹玛伊少将出任革命卫队海军司令。...
金属3D打印火箭厂商Rocke... 长三角G60激光联盟导读:国外的金属3D打印火箭厂商Rocket Lab,发展速度之快就像坐上了火箭...
嘴歪眼斜是什么病前兆 嘴歪眼斜一般情况下是面瘫、脑卒中、脑梗死等疾病的前兆。 1、面瘫:通常是因为感染因素,也有可能是因为...
上海建工:获政府补助3.48亿... 根据《企业会计准则第16号——政府补助》相关规定,公司将上述与收益相关的政府补助确定为“其他收益”并...
李彦宏最大IPO来了 窗口期。 作者/吴琼 报道/投资界PEdaily “份额抢不到。” 这一幕正在出现在昆仑芯身上。自年...
万全区召开传统制造业数字化升级... 来源:厚德万全 7月3日,万全区召开传统制造业数字化升级与电子商务发展座谈会。区政协、区商务局、...
原创 德... 在全球经济的复杂棋局中,近期德国总理默茨对人民币汇率的言论引发了不小的波澜。他声称人民币“低估了30...