【云原生 | Kubernetes 实战】03、手把手教你基于YAML文件运行pod应用

目录

一、通过资源清单文件创建第一个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-test
  namespace: default
  labels:
    app:  tomcat
spec:
  containers:
  - name:  tomcat-java
    image: tomcat:latest
    imagePullPolicy: IfNotPresent
    ports:
    - 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

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

三、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   <none>           <none>

四、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 is
     created by clients and scheduled onto hosts.

FIELDS:
   # APIVersion定义了对象,代表了一个版本为V1。
   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是字符串类型的值,代表了要创建的资源。服务器可以从客户端提交的请求推断出这个资源
   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是对象,定义元数据属性信息的
   metadata	<Object>
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

   # spec制定了定义Pod的规格,里面包含容器的信息
   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表示状态,这个不可以修改,定义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/api-conventions.md#spec-and-status

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

[root@k8s-master01 pod-yaml]# kubectl explain pod.metadata
KIND:     Pod
VERSION:  v1

# metadata是对象<Object>,下面可以有多个字段
RESOURCE: metadata <Object>

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

     ObjectMeta is metadata that all persisted resources must have, which
     includes all objects users must create.

FIELDS:
   # annotations是注解,map类型表示对应的值是key-value键值对,<string,string>表示 key 和value都是String类型的
   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

······

   # 创建的资源具有的标签。labels是标签,labels是map类型,map类型表示对应的值是key-value键值对,<string,string>表示 key和value都是String类型的
   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

   managedFields	<[]Object>

   name	<string>    # 创建pod资源的名字

   # 创建的资源所属的名称空间。namespaces划分了一个空间,在同一个namesace下的资源名字是唯一的,默认的名称空间是default。
   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

······

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

[root@k8s-master01 pod-yaml]# kubectl explain pod.spec
KIND:     Pod
VERSION:  v1

RESOURCE: spec <Object>

# 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-status

     PodSpec is a description of a pod.

FIELDS:
   # 表示Pod可以运行的最长时间,达到设置的值后,Pod会自动停止。
   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>

   # 定义亲和性的
   automountServiceAccountToken	<boolean>
     AutomountServiceAccountToken indicates whether a service account token
     should be automatically mounted.

   # containers是对象列表,用来定义容器的,是必须字段。对象列表,表示下面有很多对象,对象列表下面的内容用 - 连接。
   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.

······

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

[root@k8s-master01 pod-yaml]# kubectl explain pod.spec.containers
KIND:     Pod
VERSION:  v1

RESOURCE: containers <[]Object>

# container是定义在pod里面的,一个pod至少要有一个容器。
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.

FIELDS:
······   
   # image是用来指定容器需要的镜像的 
   image	<string>
     Container image name. More info:
     https://kubernetes.io/docs/concepts/containers/images This field is
     optional to allow higher level config management to default or override
     container images in workload controllers like Deployments and StatefulSets.

   # 镜像拉取策略,pod是要调度到node节点的,那pod启动需要镜像,可以根据这个字段设置镜像拉取策略,支持如下三种:
Always:不管本地是否存在镜像,都要重新拉取镜像
Never: 从不拉取镜像
IfNotPresent:如果本地存在,使用本地的镜像,本地不存在,从官方拉取镜像
   imagePullPolicy	<string>
     Image pull policy. One of Always, Never, IfNotPresent. Defaults to Always
     if :latest tag is specified, or IfNotPresent otherwise. Cannot be updated.
     More info:
     https://kubernetes.io/docs/concepts/containers/images#updating-images

     Possible 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 on
     disk. 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 local
     image. Container will fail if the image isn't present

······
   # name是必须字段,用来指定容器名字的
   name	<string> -required-
     Name of the container specified as a DNS_LABEL. Each container in a pod
     must 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 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. Modifying this array with strategic merge patch may corrupt
     the data. For more information See
     https://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:  v1

RESOURCE: ports <[]Object>

DESCRIPTION:
     List of ports to expose from the container. 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. Modifying this array with strategic merge patch may corrupt
     the data. For more information See
     https://github.com/kubernetes/kubernetes/issues/108255. Cannot be updated.

     ContainerPort represents a network port in a single container.

FIELDS:
   # containerPort是必须字段, pod中的容器需要暴露的端口。
   containerPort	<integer> -required-
     Number of port to expose on the pod's IP address. This must be a valid port
     number, 0 < x < 65536.

   # 将容器中的服务暴露到宿主机的端口上时,可以指定绑定的宿主机 IP。
   hostIP	<string>
     What host IP to bind the external port to.

   # 容器中的服务在宿主机上映射的端口
   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.
······

 上一篇文章:【云原生 | Kubernetes 实战】02、k8s 核心资源 Pod 介绍_Stars.Sky的博客-CSDN博客

下一篇文章:【云原生 | Kubernetes 实战】04、k8s 名称空间和资源配额_Stars.Sky的博客-CSDN博客

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 这是一个示例yaml文件:apiVersion: v1 kind: Pod metadata: name: my-pod spec: containers: - name: nginx image: nginx:1.7.9 - name: redis image: redis:2.8 - name: mysql image: mysql:5.6我可以提供这样的yaml文件,它可以用来在Kubernetes运行三个Pod:apiVersion: v1 kind: Pod metadata: name: my-pod spec: containers: - name: nginx image: nginx:1.7.9 - name: redis image: redis:2.8 - name: mysql image: mysql:5.6 ### 回答2: 在Kubernetes运行3个PodYAML文件需要以编排的方式定义Pod副本的数量。下面是一个示例的YAML文件: ```yaml apiVersion: v1 kind: Pod metadata: name: my-pod spec: replicas: 3 selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: containers: - name: my-container image: my-image ``` 在这个YAML文件中,我们使用了`replicas: 3`来定义了3个Pod的副本数量。另外,`selector`部分用于标识这些Pod副本的标签,这样Kubernetes可以正确地识别和管理它们。 在`template`部分,我们定义了每个Pod副本的配置。在这个示例中,我们仅定义了一个名为"my-container"的容器,并指定了要使用的镜像"my-image"。 使用这个YAML文件,我们可以通过以下命令将其部署到Kubernetes集群中: ```bash kubectl apply -f filename.yaml ``` 其中,`filename.yaml`是你保存该YAML文件的位置。 这样,Kubernetes将会根据YAML文件中定义的配置,在集群中创建3个Pod的副本,并自动管理它们的状态和健康状况。 ### 回答3: 要在 Kubernetes运行三个 pod,可以创建一个包含三个 podyaml 文件。下面是一个示例: ```yaml apiVersion: v1 kind: Pod metadata: name: pod1 spec: containers: - name: container1 image: <image_name> # 其他容器配置选项 --- apiVersion: v1 kind: Pod metadata: name: pod2 spec: containers: - name: container2 image: <image_name> # 其他容器配置选项 --- apiVersion: v1 kind: Pod metadata: name: pod3 spec: containers: - name: container3 image: <image_name> # 其他容器配置选项 ``` 通过这个 yaml 文件,我们定义了三个 Pod,分别是 `pod1`、`pod2` 和 `pod3`,每个 Pod 都包含一个容器。你需要替换 `<image_name>` 为你想要运行的容器镜像的名称。 要通过该 yaml 文件Kubernetes 中创建这三个 Pod,可以使用 `kubectl apply` 命令: ``` kubectl apply -f <yaml_file_name>.yaml ``` 这将使用该 yaml 文件中的定义自动创建三个 Pod。完成后,你可以使用 `kubectl get pods` 命令来查看已创建的 Pod 的状态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Stars.Sky

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值