前言
创建编排文件,是一件复杂的事情,很多时候可能没有头绪该如何开始。所以此篇文章提供一些创建的思路。
在定义资源时,将包含以下字段:
1 | apiVersion: apps/v1 |
以下操作均在1.20.0版本,其他版本命令或结果有所不同。
字段详解
apiVersion
该字段指用于创建资源的API组和药使用的API版本。Kubernetes API被聚合到API组中,v1是要使用的apps API版本。如果想列出可用的API组及其版本,可以使用以下命令:
1 | ~ kubectl api-versions |
kind
指定要创建的资源类型,比如Deployment、Pod和ReplicaSet等,可以使用以下命令查看可用的资源类型以及关联的API组:
1 | ~ kubectl api-resources | more |
使用1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
如果想了解某种资源类型的用途,可以使用```kubectl explain```命令:
```bash
~ kubectl explain --api-version=apps/v1 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 metadata.
spec <Object>
Specification of the desired behavior of the Deployment.
status <Object>
Most recently observed status of the Deployment.
metadata
用于唯一标识Kubernetes集群中的资源,可以为资源命名、分配标签、注解和指定命名空间等。
1 | $ kubectl explain deployment.metadata | more |
spec
可以定义要使用的容器镜像、副本数量、selector条件、存活或就绪探针的定义等。查看具体信息可以使用以下命令:
1 | ~ kubectl explain deployment.spec | more |
获取模板
1 | ~ kubectl create deployment nginx --image=nginx -o yaml --dry-run=client |
或者ingress
1 | ~ kubectl create ingress my-ingress --rule=host/path=app1:80 -o yaml --dry-run=client |
还可以使用 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
```bash
~ kubectl explain deployment.spec.template.spec.containers.livenessProbe --recursive | more
KIND: Deployment
VERSION: apps/v1
RESOURCE: livenessProbe <Object>
DESCRIPTION:
Periodic probe of container liveness. Container will be restarted if the
probe fails. Cannot be updated. More info:
https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
Probe describes a health check to be performed against a container to
determine whether it is alive or ready to receive traffic.
FIELDS:
exec <Object>
command <[]string>
failureThreshold <integer>
httpGet <Object>
host <string>
httpHeaders <[]Object>
name <string>
value <string>
path <string>
port <string>
scheme <string>
initialDelaySeconds <integer>
periodSeconds <integer>
successThreshold <integer>
tcpSocket <Object>
host <string>
port <string>
timeoutSeconds <integer>
如果想进一步了解更详细的信息,可以继续拼接:
1 | [root@k8s-node-217 ~]# kubectl explain deployment.spec.template.spec.containers.lifecycle |
总结
组成
整个编排文件分为四个字段或者四部分:
- apiVersion API组及版本
- kind 资源类型
- metadata 资源注解
- spec 定义和管理资源
常用命令
1 | # 获取API 版本 |