在Kubernetes中所有操作的内容,我们都称为“资源对象”,是由API Server基于HTTP/HTTPS接收并响应客户端的操作请求,是一种Restful风格的接口,将各种组件及操作内容都抽象成为标准的REST资源,如Namespace、Pod等,其中操作内容以JSON或yml格式数据进行操作。本文讲解的是Kubernetes中的最为重要的一节——资源清单,我们想要在Kubernetes中部署Pod、Service等资源对象,都需要通过资源清单的方式来部署,无论是通过命令kubectl,还是可视化控制台,都是离不开资源清单的定义,本文重点讲述资源清单如何定义、如何创建及使用。
根据资源的功能进行资源分类,Kubernetes资源对象可分为:
一个应用通常需要多个资源的支撑,例如,使用Deployment资源管理应用实例(Pod)、使用ConfigMap资源保存应用配置、使用Service或Ingress资源暴露服务、使用Volume资源提供外部存储等。
资源清单,等同于一个剧本,能够告诉我们每一步应该怎么去做,Kubernetes接收到这么一个剧本,就能够按照这个剧本去执行,以达到我们的预期。在Kubernetes中,一般都是通过定义资源清单的方式去创建资源。一般使用yaml格式的文件来创建符合我们预期期望的资源,这样的yaml文件我们称为资源清单。(也可以定义为json格式)如,创建一个Pod资源:
- apiVersion: v1
- kind: Pod
- metadata:
- name: vue-frontend
- namespace: test
- labels:
- app: vue-frontend
- spec:
- containers:
- - name: vue-frontend
- image: xcbeyond/vue-frontend:latest
- ports:
- - name: port
- containerPort: 80
- hostPort: 8080
接下来,以Pod资源定义为例展开对资源清单的详细说明。
2.1 资源清单定义
yaml格式的Pod资源清单定义文件的完整内容如下:
- apiVersion: v1
- kind: Pod # 资源类别
- metadata: # 资源元数据
- name: string
- namespace: string
- labels:
- - name: string
- annotations:
- - name: string
- spec: # 资源期望的状态
- containers: # 容器列表
- - name: string # 容器名称,下面的属性均属于对该容器的定义或约束
- image: string
- imagePullPolicy: [Always|Never|IfNotPresent]
- command: [string]
- args: [string]
- workingDir: string
- volumeMounts:
- - name: string
- mountPath: string
- readOnly: boolean
- ports:
- - name: string
- containerPort: int
- hostPort: int
- protocol: string
- env:
- - name: string
- value: string
- resources:
- limits:
- cpu: string
- memory: string
- requests:
- cpu: string
- memory: string
- livenssProbe:
- exec:
- command: [string]
- httpGet:
- path: string
- port: number
- host: string
- scheme: string
- httpHeaders:
- - name: string
- value: string
- tcpSocket:
- port: number
- initialDelaySeconds: 0
- timeoutSeconds: 0
- periodSeconds: 0
- successThreshold: 0
- failureThreshold: 0
- ……
对各属性的详细说明如下表所示:(必选属性,是必须存在的,否则创建失败。)
上述列举的是常用的属性,如果想查看全部属性,可以使用命令kubectl explain pod:
- [xcbeyond@bogon ~]$ 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
- 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 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