Kubenetes 资源清单定义入门

Kubernetes 常用资源:

我将它们简单的分类为以下几种资源对象:

类别 名称
工作负载型资源对象 Pod、ReplicaSet、Replication Controller、Deployment、StatefulSet、DaemonSet、Job、CronJob
服务发现及负载均衡 Service、Ingress
配置与存储 Volume、Persistent Volume、CSl 、 configmap、secret 、DownwardAPI
集群资源 Namespace、Node、Role、ClusterRole、RoleBinding、ClusterRoleBinding
元数据资源 HPA、PodTemplate、LimitRange

利用命令获取一个Pod的资源清单内容并输出为yaml格式:

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
[root@master app]# kubectl get pods nginx-deployment-67594d6bf6-8w947 -n kube-system -o yaml

apiVersion: v1 # K8S API版本,应该由两部分组成:group/version,group省略表示默认为core
kind: Pod # 资源类别: Pod、Deployment、Service等等
metadata: # 资源元数据
creationTimestamp: 2018-09-27T06:29:25Z
generateName: nginx-deployment-67594d6bf6-
labels:
app: nginx
pod-template-hash: "2315082692"
name: nginx-deployment-67594d6bf6-8w947
namespace: kube-system
ownerReferences:
- apiVersion: apps/v1
blockOwnerDeletion: true
controller: true
kind: ReplicaSet
name: nginx-deployment-67594d6bf6
uid: ceebf775-acef-11e8-b183-d8490b8af3ae
resourceVersion: "5070139"
selfLink: /api/v1/namespaces/kube-system/pods/nginx-deployment-67594d6bf6-8w947
uid: adca27df-c21e-11e8-8dd6-d8490b8af3ae
spec: # specifications, 资源规格。(定义资源对象期望的状态),这个是最重要的字段,用于规定接下来要创建的资源对象应该拥有的特性。然后依靠控制器确保这些特性能够被满足。
containers:
- image: nginx:1.7.9
imagePullPolicy: IfNotPresent
name: nginx
ports:
- containerPort: 80
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
nodeName: node1
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
status: # 用于显示这个资源对象当前的状态,这个字段是只读的。
conditions:
- lastProbeTime: null
lastTransitionTime: 2018-09-27T06:30:35Z
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: 2018-09-27T06:30:37Z
status: "True"
type: Ready
- lastProbeTime: null
lastTransitionTime: null
status: "True"
type: ContainersReady
- lastProbeTime: null
lastTransitionTime: 2018-09-27T06:29:25Z
status: "True"
type: PodScheduled
containerStatuses:
- containerID: docker://3e1b5e5f2590bf50d04c98540c937b3d3bf9ea4ffb6a5a08979ac0914f161e49
image: nginx:1.7.9
imageID: docker-pullable://nginx@sha256:e3456c851a152494c3e4ff5fcc26f240206abac0c9d794affb40e0714846c451
lastState: {}
name: nginx
ready: true
restartCount: 0
state:
running:
startedAt: 2018-09-27T06:30:36Z
hostIP: 172.19.0.204
phase: Running
podIP: 10.1.35.2
qosClass: BestEffort
startTime: 2018-09-27T06:30:35Z

缺少部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
tolerations:        # 容忍度,能够容忍哪些污点
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300
- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 300
volumes:
- name: default-token-rqmtb
secret:
defaultMode: 420
secretName: default-token-rqmtb

创建资源清单的方法:

1
2
apiserver 仅接受JOSN格式的资源定义
yaml格式提供配置清单,apiserver自动转成json格式,然后在提交

大部分资源的配置清单:

1
2
3
4
5
apiVersion: 格式:group/version 查看 kubectl apiversion
kind: Pod Replicaset Deployments ...
metadata: name namespace labels annotations
spec: 定义用户期望状态 disired state
status: 定义当前状态 current state 该字段由kubenetes集群维护;

配置清单格式:

1
2
3
4
5
6
7
8
9
10
11
12
apiVersion: v1     # kubectl api-versions(查看命令)

kind: 资源类别(Pod Replicaset Deployments ...)

metadata: 元数据
name: 资源名称
namespace: 名称空间
labels: 标签,键值数据。数据大小有限制。
annotations: 注解,也是键值数据,但是它的数据没有大小限制。

spec: 期望的状态,disired state,由用户定义,最重要。每种资源支持的字段不一样。
status: 当前状态,current state, 本字段由K8S集群维护。

通过命令查看如何定义资源 (标有 required 意味是必填项)

kubectl explain 命令语法:

1
kubectl explain RESOURCE [options]

示例:

1
2
3
4
5
#获取pods资源及其字段的文档
kubectl explain pods

#获取pods资源的特定字段的文档(只要字段后面有<Object>关键字就表示有二级字段)
kubectl explain pods.spec.containers
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
[root@master app]# kubectl explain pod             查看如何定义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/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/api-conventions.md#types-kinds

metadata <Object>
Standard object's metadata. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata

spec <Object>
Specification of the desired behavior of the pod. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status

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/api-conventions.md#spec-and-status

注释:map类型的不需要在前面加“-”线,list类型的需要在前面加“-”线

定义一个简单的资源清单:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-ye
namespace: kube-system
labels:
app: myapp #deployment的labels
spec:
replicas: 5
selector:
matchLabels:
app: frontend
template:
metadata:
labels:
app: frontend #pods的labels
spec:
containers:
- name: nginx
image: nginx:1.7.9

kubectl create 命令:

描述:

通过配置文件名或stdin创建一个集群资源对象。支持JSON和YAML格式的文件。

语法:

1
kubectl create -f FILENAME [options]

示例:

1
2
3
4
5
通过pod.json文件创建资源。
kubectl create -f pod.json

根据docker-registry.yaml文件创建资源。
kubectl create -f docker-registry.yaml

Kubenetes 资源清单定义入门
https://system51.github.io/2019/08/23/Kubenetes-Resource/
作者
Mr.Ye
发布于
2019年8月23日
许可协议