1 yaml文件基础组成

k8s中yaml文件结尾需以.yml或.yaml结尾。文件放置位置不做限定。

常见的的yaml文件中一般都包含这四个元素:apiVersion、kind、metadata、spec,这四个元素均处于最顶层层级就是说书写时这四个元素前不需要用空格。

书写yaml文件时可首先把四个元素写入到yaml文件中。

apiVersion: ******
kind: ******
metadata:
  ******
  ******
spec:
  ******
  ******

参数解释

apiVersion: 置于首行,标注该yaml文件使用的版本。不同资源版本中资源使用方式和配置参数存在一定的差异。

kind: 一般置于第二行,紧跟apiVersion行后。标注该yaml文件中使用的资源类型。

metadata: 一般处于第三行yaml文件的元数据,如名称、标签、属于哪个命名空间、文件作者信息等。

spec:  定义具体的资源配置参数信息。如pod、pv、svc、deployments等资源的具体参数配置。

(apiVersion、kind、metadata这行的配置方法对于k8s中所有类型资源参数基本通用,所以需要付出一点点汗水把常用的项记下来,如metadata中的name、namespace等)。

对于spec中的内容有一点映像就行不用死记硬背,不太清楚的可以上官网查看,也可以通过kubectl explain命令查看具体配置。

1.1 选项前是否需要加-横线

当你的父级参数是列表格式时,该父级参数下的第一行子参数前需要加上横线。metadata、spec这两个参数下的第一级子参数首行都不用加横线。

以pod的spec下的containers参数为例,执行kubectl explain  pod.spec.containers.

2 通过yaml文件创建自主式pod

可通过帮助命令写yaml文件

[root@master1 ~]# 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>  --yaml文件这个不需要写--
     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

通过yaml文件创建自主式Pod:

[root@master1 ~]# vim pod-first.yaml
您在 /var/spool/mail/root 中有新邮件
[root@master1 ~]# cat pod-first.yaml 
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: tomcat
  name: tomcat-test
  namespace: default
spec:
  containers:
  - name: tomcat-java
    image: docker.io/xianchao/tomcat-8.5-jre8:v1
    imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 8080
[root@master1 ~]# kubectl apply -f pod-first.yaml 
pod/tomcat-test created
[root@master1 ~]# kubectl get pods
NAME          READY   STATUS    RESTARTS   AGE
tomcat-test   1/1     Running   0          4s

3 通过yaml文件创建控制器管理Pod

[root@master1 ~]# vim deployment-first.yaml
[root@master1 ~]# cat deployment-first.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-test
  labels:
    app: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: my-nginx
        image: xianchao/nginx:v1
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
[root@master1 ~]# kubectl explain deployment
KIND:     Deployment
VERSION:  apps/v1

DESCRIPTION:
     Deployment enables declarative updates for Pods and ReplicaSets.

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 Deployment.

   status       <Object>
     Most recently observed status of the Deployment.
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐